Skip to content

Instantly share code, notes, and snippets.

@mcasimir
Created December 11, 2019 16:43
Show Gist options
  • Save mcasimir/1afff786a1276879bd7a253f85e862e9 to your computer and use it in GitHub Desktop.
Save mcasimir/1afff786a1276879bd7a253f85e862e9 to your computer and use it in GitHub Desktop.
compile-and-copy-to-compass
nodemon -e js,jsx,css,less,scss,sass --watch ./src --delay 2 --exec npm run compile &
sync-module-dir ../compass --watch
wait
#!/usr/bin/env node
const yargsParser = require('yargs-parser');
const cpx = require('cpx');
const path = require('path');
const fs = require('fs');
function main() {
const args = parseArgs();
ensureTargetPathIsAModule(args.targetPackagePath);
const source = path.join(args.syncDir, args.glob);
const dest = path.join(args.targetPackagePath, 'node_modules', getCurrentPackageName(), args.syncDir);
watchAndCopy(source, dest, args.watch);
}
main();
function watchAndCopy(source, dest, watch) {
cpx.watch(source, dest)
.on('copy', (e) => { console.log('+', e.dstPath); })
.on('remove', (e) => { console.log('-', e.path); })
.on('watch-ready', () => {
if (!watch) {
process.exit(0);
}
console.info('Watching', source, '...');
})
.on('watch-error', (err) => {
console.error(err);
process.exit(1);
});
}
function getCurrentPackageName() {
return require(path.join(process.cwd(), 'package.json')).name;
}
function ensureTargetPathIsAModule(targetPackagePath) {
if (!fs.existsSync(path.join(targetPackagePath, 'package.json'))) {
throw new Error('TARGET_MODULE_PATH does not appear to be a node module');
}
}
function parseArgs() {
const argv = yargsParser(process.argv.slice(2));
const targetPackagePath = argv._[0];
if (!targetPackagePath) {
const programName = path.basename(process.argv[1]);
throw new Error(`USAGE: ${programName} TARGET_MODULE_PATH [--watch] [--sync-dir=lib] [--glob='**']`);
}
const syncDir = argv.dir || 'lib';
const glob = argv.glob || '**';
const watch = argv.watch;
return {
targetPackagePath,
syncDir,
glob,
watch
};
}
{
"name": "sync-module-dir",
"version": "1.0.0",
"description": "",
"main": "index.js",
"dependencies": {
"cpx": "1.5.0",
"yargs-parser": "16.1.0"
},
"devDependencies": {},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"bin": {
"sync-module-dir": "index.js"
},
"author": "",
"license": "ISC"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment