feat: run command on changes

based on dependencies, devDependencies, and hashes of other included files
This commit is contained in:
2019-04-04 15:50:15 -07:00
parent ca2fc85135
commit 5d05775560
4 changed files with 42 additions and 9 deletions

View File

@@ -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"

View File

@@ -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,
}
);

View File

@@ -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));
};

View File

@@ -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);
};