zotero/scripts/watch.js
Tom Najdek b53fabbb58 Better build process (#1248)
* Remove gulp, replace with custom scripts
* Symlink entire dirs where possible (fixes #1232)
* Significantly speed up subsequent builds (fixes #1238)
* Watch process now observes new/removed files, not only changed
* Add ignoreMask, exclude all files with names starting with a #
* Better logging during builds
* Update travis.yml to use new, non-gulp-based build
2017-06-20 19:18:46 -04:00

81 lines
No EOL
2 KiB
JavaScript

const path = require('path');
const chokidar = require('chokidar');
const multimatch = require('multimatch');
const { dirs, jsGlob, jsGlobIgnore, copyDirs, symlinkFiles } = require('./config');
const { onSuccess, onError, getSignatures, writeSignatures, cleanUp, formatDirsForMatcher } = require('./utils');
const getJS = require('./js');
const getSass = require('./sass');
const getCopy = require('./copy');
const getSymlinks = require('./symlinks');
const ROOT = path.resolve(__dirname, '..');
const source = [
'chrome',
'components',
'defaults',
'resource',
'scss',
'test',
'styles',
'translators',
'scss',
'chrome/**',
'components/**',
'defaults/**',
'resource/**',
'scss/**',
'test/**',
'styles/**',
'translators/**',
'scss/**'
];
const symlinks = symlinkFiles
.concat(dirs.map(d => `${d}/**`))
.concat([`!${formatDirsForMatcher(dirs)}/**/*.js`])
.concat([`!${formatDirsForMatcher(copyDirs)}/**`]);
var signatures;
process.on('SIGINT', () => {
writeSignatures(signatures);
process.exit();
});
function getWatch() {
let watcher = chokidar.watch(source, { cwd: ROOT })
.on('change', async (path) => {
try {
if (multimatch(path, jsGlob).length && !multimatch(path, jsGlobIgnore).length) {
onSuccess(await getJS(path, { ignore: jsGlobIgnore }, signatures));
} else if (multimatch(path, 'scss/*.scss').length) {
onSuccess(await getSass(path, {}, signatures));
} else if (multimatch(path, copyDirs.map(d => `${d}/**`)).length) {
onSuccess(await getCopy(path, {}, signatures));
} else if (multimatch(path, symlinks).length) {
onSuccess(await getSymlinks(path, { nodir: true }, signatures));
}
onSuccess(await cleanUp(signatures));
} catch (err) {
onError(err);
}
})
.on('unlink', async () => {
const signatures = await getSignatures();
onSuccess(await cleanUp(signatures));
});
watcher.add(source);
console.log('Watching files for changes...');
}
module.exports = getWatch;
if (require.main === module) {
(async () => {
signatures = await getSignatures();
getWatch();
})();
}