UNPKG

@olegjs/on-change

Version:

Run a command when a file changes since last time the command was executed

47 lines (38 loc) 1.29 kB
#!/usr/bin/env node const { yellow, magenta } = require('chalk') const { spawnSync } = require('child_process') const { existsSync, readFileSync, writeFileSync } = require('fs') const { checksum, checksumFilePath: getChecksumFilePath, hashFromFileContent, } = require('./checksum') const argv = require('yargs') .scriptName('on-file-change') .usage('Usage: $0 --file [file] [command]') .example( '$0 --file package-lock.json npm ci', 'Reinstall dependencies on changed package-lock.json', ) .option('file', { alias: 'f', demandOption: true, describe: 'Path to file to check for changes', type: 'string', }) .demandCommand(1).argv const UTF = 'utf8' const getPastCheckSum = path => existsSync(path) ? hashFromFileContent(readFileSync(path, UTF)) : null const checkSum = checksum(readFileSync(argv.file, UTF)) const checksumFilePath = getChecksumFilePath(argv.file) const pastCheckSum = getPastCheckSum(checksumFilePath) if (checkSum !== pastCheckSum) { console.log( `File "${magenta(argv.file)}" has changed.`, `Running "${yellow(argv._.join(' '))}"...`, ) const [command, ...args] = argv._ spawnSync(command, args, { encoding: UTF, stdio: 'inherit' }) writeFileSync(checksumFilePath, `${checkSum} ${argv.file}`) }