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');
|
2024-03-08 06:26:24 +00:00
|
|
|
const { debounce, 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');
|
2024-03-08 06:26:24 +00:00
|
|
|
const colors = require('colors/safe');
|
2017-06-20 23:18:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
const ROOT = path.resolve(__dirname, '..');
|
2023-04-29 11:45:59 +00:00
|
|
|
const addOmniExecPath = path.join(ROOT, 'app', 'scripts', 'add_omni_file');
|
2022-05-25 17:56:23 +00:00
|
|
|
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);
|
2024-03-08 06:26:24 +00:00
|
|
|
process.exit(); // eslint-disable-line no-process-exit
|
2017-06-20 23:18:46 +00:00
|
|
|
});
|
|
|
|
|
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
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2024-03-08 06:26:24 +00:00
|
|
|
async function processFile(path) {
|
2022-05-25 17:56:23 +00:00
|
|
|
try {
|
2024-03-08 06:26:24 +00:00
|
|
|
var result = false;
|
|
|
|
if (multimatch(path, jsFiles).length && !multimatch(path, ignoreMask).length) {
|
|
|
|
result = await getJS(path, { ignore: ignoreMask }, signatures);
|
|
|
|
onSuccess(await cleanUp(signatures));
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!result && multimatch(path, copyDirs.map(d => `${d}/**`)).length) {
|
|
|
|
result = await getCopy(path, {}, signatures);
|
|
|
|
}
|
|
|
|
if (!result && multimatch(path, symlinks).length) {
|
|
|
|
result = await getSymlinks(path, { nodir: true }, signatures);
|
|
|
|
}
|
2022-05-25 17:56:23 +00:00
|
|
|
}
|
2024-03-08 06:26:24 +00:00
|
|
|
catch (err) {
|
2024-03-12 16:04:34 +00:00
|
|
|
onError(err);
|
2024-03-08 06:26:24 +00:00
|
|
|
result = false;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
2022-05-25 17:56:23 +00:00
|
|
|
|
2024-03-08 06:26:24 +00:00
|
|
|
async function processFiles(mutex) {
|
|
|
|
mutex.isLocked = true;
|
|
|
|
try {
|
|
|
|
const t1 = Date.now();
|
|
|
|
let paths = Array.from(mutex.batch);
|
|
|
|
let results = await Promise.all(paths.map(processFile));
|
|
|
|
let t2 = Date.now();
|
|
|
|
let aggrResult;
|
|
|
|
|
2024-03-12 16:04:34 +00:00
|
|
|
if (results.length === 1 && results[0]) {
|
2024-03-08 06:26:24 +00:00
|
|
|
onSuccess(results[0]);
|
|
|
|
aggrResult = results[0];
|
|
|
|
}
|
|
|
|
else if (results.length > 1) {
|
|
|
|
aggrResult = results.reduce((acc, result) => {
|
|
|
|
if (!(result.action in acc)) {
|
|
|
|
acc.actions[result.action] = 0;
|
2017-05-22 23:10:03 +00:00
|
|
|
}
|
2024-03-08 06:26:24 +00:00
|
|
|
acc.actions[result.action] += result.count;
|
|
|
|
acc.count += result.count;
|
|
|
|
acc.outFiles = acc.outFiles.concat(result.outFiles || []);
|
|
|
|
return acc;
|
|
|
|
}, { actions: {}, count: 0, processingTime: t2 - t1, outFiles: [] });
|
|
|
|
|
|
|
|
onSuccess({
|
|
|
|
action: Object.keys(aggrResult.actions).length > 1 ? 'multiple' : Object.keys(aggrResult.actions)[0],
|
|
|
|
count: aggrResult.count,
|
|
|
|
processingTime: aggrResult.processingTime,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
onSuccess(await cleanUp(signatures));
|
2022-05-25 17:56:23 +00:00
|
|
|
|
2024-03-12 16:04:34 +00:00
|
|
|
if (shouldAddOmni && aggrResult?.outFiles?.length) {
|
2024-03-08 06:26:24 +00:00
|
|
|
onSuccess(await addOmniFiles(aggrResult.outFiles));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
finally {
|
|
|
|
mutex.isLocked = false;
|
|
|
|
mutex.batch.clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-25 17:56:23 +00:00
|
|
|
|
2024-03-08 06:26:24 +00:00
|
|
|
async function batchProcessFiles(path, mutex, debouncedProcessFiles) {
|
|
|
|
let counter = 0;
|
|
|
|
let pollInterval = 250;
|
|
|
|
let started = Date.now();
|
|
|
|
|
|
|
|
// if there's a batch processing and another batch waiting, add to it
|
|
|
|
if (mutex.isLocked && mutex.nextBatch) {
|
|
|
|
mutex.nextBatch.add(path);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// else if there's a batch processing, create a new batch
|
|
|
|
else if (mutex.isLocked) {
|
|
|
|
mutex.nextBatch = new Set([path]);
|
|
|
|
}
|
|
|
|
while (mutex.isLocked) {
|
|
|
|
if (counter === 0) {
|
|
|
|
console.log(colors.yellow(`Waiting for previous batch to finish...`));
|
2021-02-24 15:36:27 +00:00
|
|
|
}
|
2024-03-08 06:26:24 +00:00
|
|
|
if (++counter >= 40) {
|
|
|
|
onError(`Batch processing timeout after ${counter * pollInterval}ms. ${mutex.nextBatch.size} files in this batch have not been processed 😢`);
|
|
|
|
mutex.batch.clear();
|
|
|
|
mutex.nextBatch = null;
|
|
|
|
mutex.isLocked = false;
|
|
|
|
return;
|
2017-06-20 23:18:46 +00:00
|
|
|
}
|
2024-03-08 06:26:24 +00:00
|
|
|
process.env.NODE_ENV === 'debug' && console.log(`waiting ${pollInterval}ms...`);
|
|
|
|
await new Promise(resolve => setTimeout(resolve, pollInterval));
|
|
|
|
}
|
|
|
|
if (counter > 0) {
|
|
|
|
console.log(colors.green(`Previous batch finished in ${Date.now() - started}ms. ${mutex.nextBatch.size} files in the next batch.`));
|
|
|
|
}
|
|
|
|
if (mutex.nextBatch) {
|
|
|
|
mutex.batch = new Set([...mutex.nextBatch]);
|
|
|
|
mutex.nextBatch = null;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
mutex.batch.add(path);
|
|
|
|
}
|
|
|
|
debouncedProcessFiles();
|
|
|
|
}
|
|
|
|
|
|
|
|
async function getWatch() {
|
|
|
|
try {
|
|
|
|
await fs.access(addOmniExecPath, fs.constants.F_OK);
|
|
|
|
shouldAddOmni = !envCheckTrue(process.env.SKIP_OMNI);
|
|
|
|
}
|
|
|
|
catch (_) {}
|
|
|
|
|
|
|
|
let mutex = { batch: new Set(), isLocked: false };
|
|
|
|
const debouncedProcessFiles = debounce(() => processFiles(mutex));
|
|
|
|
|
|
|
|
let watcher = chokidar.watch(source, { cwd: ROOT, ignoreInitial: true })
|
|
|
|
.on('change', (path) => {
|
|
|
|
batchProcessFiles(path, mutex, debouncedProcessFiles);
|
|
|
|
})
|
|
|
|
.on('add', (path) => {
|
|
|
|
batchProcessFiles(path, mutex, debouncedProcessFiles);
|
2017-06-20 23:18:46 +00:00
|
|
|
})
|
2024-03-08 06:26:24 +00:00
|
|
|
.on('unlink', debounce(async () => {
|
2017-06-20 23:18:46 +00:00
|
|
|
onSuccess(await cleanUp(signatures));
|
2024-03-08 06:26:24 +00:00
|
|
|
}));
|
2017-06-20 23:18:46 +00:00
|
|
|
|
|
|
|
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
|
|
|
}
|