const path = require('path'); const fs = require('fs'); const os = require('os'); const JoyCon = require('joycon'); exports.getRootPath = () => process.cwd(); exports.getHomeDir = () => os.homedir(); exports.getDataDir = () => path.join(exports.getHomeDir(), '.local/share'); exports.canAccessFile = (p, flag) => { try { fs.accessSync(p, flag || fs.constants.R_OK); return true; } catch (e) { return false; } }; exports.getFileContents = (filePath, opts = {}) => { if (!exports.canAccessFile(filePath)) throw new Error(`File not found: ${filePath}`); const content = fs.readFileSync(filePath); if (opts.format === 'json') return JSON.parse(content); if (opts.format === 'text') return content.toString(); return content; }; exports.getRootFileContents = (filename, opts = {}) => { return exports.getFileContents(path.join(exports.getRootPath(), filename), opts); }; exports.getIdent = () => { const { name } = exports.getRootFileContents('package.json', { format: 'json' }); return name; }; exports.getConfig = (overrides = {}) => { const joycon = new JoyCon(); const defaults = { checkFiles: ['package-lock.json', 'yarn.lock'], rootDir: process.cwd(), dataDir: exports.getDataDir(), cmd: false, }; const result = joycon.loadSync({ packageKey: 'pkgcomp', files: ['.config/pkgcomp.json', '.pkgcomp.json', 'pkgcomp.json', 'package.json'], }); return Object.assign(defaults, result.data, overrides); };