2017-06-20 23:18:46 +00:00
|
|
|
const path = require('path');
|
2022-05-25 17:56:23 +00:00
|
|
|
const fs = require('fs-extra');
|
2017-06-20 23:18:46 +00:00
|
|
|
const chokidar = require('chokidar');
|
|
|
|
const multimatch = require('multimatch');
|
2022-05-25 17:56:23 +00:00
|
|
|
const { exec } = require('child_process');
|
2017-05-22 23:10:03 +00:00
|
|
|
const { dirs, jsFiles, scssFiles, ignoreMask, copyDirs, symlinkFiles } = require('./config');
|
2022-05-25 17:56:23 +00:00
|
|
|
const { envCheckTrue, onSuccess, onError, getSignatures, writeSignatures, cleanUp, formatDirsForMatcher } = require('./utils');
|
2017-06-20 23:18:46 +00:00
|
|
|
const getJS = require('./js');
|
|
|
|
const getSass = require('./sass');
|
|
|
|
const getCopy = require('./copy');
|
|
|
|
const getSymlinks = require('./symlinks');
|
|
|
|
|
|
|
|
|
|
|
|
const ROOT = path.resolve(__dirname, '..');
|
2022-05-25 17:56:23 +00:00
|
|
|
const addOmniExecPath = path.join(ROOT, '..', 'zotero-standalone-build', 'scripts', 'add_omni_file');
|
|
|
|
let shouldAddOmni = false;
|
|
|
|
|
2017-06-20 23:18:46 +00:00
|
|
|
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`])
|
2018-12-12 10:34:39 +00:00
|
|
|
.concat([`!${formatDirsForMatcher(dirs)}/**/*.jsx`])
|
2021-02-24 15:36:27 +00:00
|
|
|
.concat([`!${formatDirsForMatcher(dirs)}/**/*.scss`])
|
2017-06-20 23:18:46 +00:00
|
|
|
.concat([`!${formatDirsForMatcher(copyDirs)}/**`]);
|
|
|
|
|
|
|
|
var signatures;
|
|
|
|
|
|
|
|
process.on('SIGINT', () => {
|
|
|
|
writeSignatures(signatures);
|
|
|
|
process.exit();
|
|
|
|
});
|
|
|
|
|
2022-05-25 17:56:23 +00:00
|
|
|
async function addOmniFiles(relPaths) {
|
|
|
|
const t1 = Date.now();
|
|
|
|
const buildDirPath = path.join(ROOT, 'build');
|
|
|
|
const wrappedPaths = relPaths.map(relPath => `"${path.relative(buildDirPath, relPath)}"`);
|
|
|
|
|
|
|
|
await new Promise((resolve, reject) => {
|
|
|
|
const cmd = `"${addOmniExecPath}" ${wrappedPaths.join(' ')}`;
|
|
|
|
exec(cmd, { cwd: buildDirPath }, (error, output) => {
|
|
|
|
if (error) {
|
|
|
|
reject(error);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
process.env.NODE_ENV === 'debug' && console.log(`Executed:\n${cmd};\nOutput:\n${output}\n`);
|
|
|
|
resolve(output);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
const t2 = Date.now();
|
|
|
|
|
|
|
|
return {
|
|
|
|
action: 'add-omni-files',
|
|
|
|
count: relPaths.length,
|
|
|
|
totalCount: relPaths.length,
|
|
|
|
processingTime: t2 - t1
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
async function getWatch() {
|
|
|
|
try {
|
|
|
|
await fs.access(addOmniExecPath, fs.constants.F_OK);
|
|
|
|
shouldAddOmni = !envCheckTrue(process.env.SKIP_OMNI);
|
|
|
|
}
|
|
|
|
catch (_) {}
|
|
|
|
|
2017-06-20 23:18:46 +00:00
|
|
|
let watcher = chokidar.watch(source, { cwd: ROOT })
|
|
|
|
.on('change', async (path) => {
|
|
|
|
try {
|
2022-05-25 17:56:23 +00:00
|
|
|
var result = false;
|
2017-06-24 23:24:52 +00:00
|
|
|
if (multimatch(path, jsFiles).length && !multimatch(path, ignoreMask).length) {
|
2022-05-25 17:56:23 +00:00
|
|
|
result = await getJS(path, { ignore: ignoreMask }, signatures);
|
2021-02-24 15:36:27 +00:00
|
|
|
onSuccess(await cleanUp(signatures));
|
|
|
|
}
|
2022-05-25 17:56:23 +00:00
|
|
|
if (!result) {
|
|
|
|
for (var i = 0; i < scssFiles.length; i++) {
|
|
|
|
if (multimatch(path, scssFiles[i]).length) {
|
|
|
|
result = await getSass(scssFiles[i], { ignore: ignoreMask }); // eslint-disable-line no-await-in-loop
|
|
|
|
break;
|
|
|
|
}
|
2017-05-22 23:10:03 +00:00
|
|
|
}
|
2021-02-24 15:36:27 +00:00
|
|
|
}
|
2022-05-25 17:56:23 +00:00
|
|
|
if (!result && multimatch(path, copyDirs.map(d => `${d}/**`)).length) {
|
|
|
|
result = await getCopy(path, {}, signatures);
|
2021-02-24 15:36:27 +00:00
|
|
|
}
|
2022-05-25 17:56:23 +00:00
|
|
|
if (!result && multimatch(path, symlinks).length) {
|
|
|
|
result = await getSymlinks(path, { nodir: true }, signatures);
|
|
|
|
}
|
|
|
|
|
|
|
|
onSuccess(result);
|
|
|
|
onSuccess(await cleanUp(signatures));
|
|
|
|
|
|
|
|
if (shouldAddOmni && result.outFiles?.length) {
|
|
|
|
onSuccess(await addOmniFiles(result.outFiles));
|
2017-06-20 23:18:46 +00:00
|
|
|
}
|
2021-02-24 15:36:27 +00:00
|
|
|
}
|
|
|
|
catch (err) {
|
2017-06-20 23:18:46 +00:00
|
|
|
onError(err);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.on('unlink', async () => {
|
|
|
|
const signatures = await getSignatures();
|
|
|
|
onSuccess(await cleanUp(signatures));
|
|
|
|
});
|
|
|
|
|
|
|
|
watcher.add(source);
|
2022-05-25 17:56:23 +00:00
|
|
|
console.log(`Watching files for changes (omni updates ${shouldAddOmni ? 'enabled' : 'disabled'})...`);
|
2017-06-20 23:18:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = getWatch;
|
|
|
|
|
|
|
|
if (require.main === module) {
|
|
|
|
(async () => {
|
|
|
|
signatures = await getSignatures();
|
|
|
|
getWatch();
|
|
|
|
})();
|
2022-05-25 17:56:23 +00:00
|
|
|
}
|