diff --git a/package.json b/package.json index 42dcf78..c332ed7 100644 --- a/package.json +++ b/package.json @@ -54,6 +54,7 @@ "trailingComma": "es5" }, "dependencies": { + "fast-deep-equal": "^2.0.1", "joycon": "^2.2.4", "md5-file": "^4.0.0", "mkdirp": "^0.5.1" diff --git a/src/cli.js b/src/cli.js index 155272b..03a80b0 100755 --- a/src/cli.js +++ b/src/cli.js @@ -9,11 +9,13 @@ function parseArgs() { return args.reduce( (acc, arg) => { if (arg === '--verbose') acc.verbose = true; + if (arg === '--force') acc.force = true; if (arg === '--use-yarn') acc.useYarn = true; return acc; }, { verbose: false, + force: false, useYarn: false, } ); diff --git a/src/data.js b/src/data.js index 0b4e420..3821c6a 100644 --- a/src/data.js +++ b/src/data.js @@ -29,6 +29,11 @@ exports.read = (fileRoot, ident) => { }; exports.write = (fileRoot, ident, payload) => { - console.log('WRITE', { fileRoot, ident, payload }); - throw new Error('Data write not implemented'); + const filePath = path.join(fileRoot, CONFIG_FILENAME); + const existingCache = JSON.parse(fs.readFileSync(filePath)); + + // update the cache + existingCache[ident] = payload; + + fs.writeFileSync(filePath, JSON.stringify(existingCache)); }; diff --git a/src/index.js b/src/index.js index 75a2bdf..019032a 100644 --- a/src/index.js +++ b/src/index.js @@ -1,6 +1,8 @@ +const path = require('path'); const md5File = require('md5-file'); +const deepEqual = require('fast-deep-equal'); const data = require('./data'); -// const utils = require('./utils'); +const utils = require('./utils'); function getFileHash(file, verbose = false) { try { @@ -12,9 +14,22 @@ function getFileHash(file, verbose = false) { } } -module.exports = function mod(config, ident, opts = {}) { - const packageData = data.read(config.dataDir, ident); - const firstRun = Object.keys(packageData).length === 0; +function execCommand(cmd) { + console.log('RUN:', cmd); +} + +const defaultOptions = { + verbose: false, + force: false, +}; + +module.exports = function pkgcomp(config, ident, opts = defaultOptions) { + const packageHistory = data.read(config.dataDir, ident); + const firstRun = Object.keys(packageHistory).length === 0; + const doExec = !firstRun || opts.force; + + // eslint-disable-next-line no-console + if (firstRun && !opts.force && opts.verbose) console.info('First run, command skipped'); // create file hash object const hashes = config.checkFiles.reduce((acc, file) => { @@ -22,9 +37,19 @@ module.exports = function mod(config, ident, opts = {}) { return acc; }, {}); - console.log('First run?', firstRun); + // read dependencies from package.json + const pkg = utils.getFileContents(path.join(config.rootDir, 'package.json'), { format: 'json' }); - data.write(config.dataDir, ident, { + const payload = { hashes, - }); + dependencies: pkg.dependencies, + devDependencies: pkg.devDependencies, + }; + + // check if the command should be run + const runCmd = Boolean(opts.force || (doExec && !deepEqual(packageHistory, payload))); + + if (runCmd) execCommand(config.cmd); + + data.write(config.dataDir, ident, payload); };