2017-06-20 23:18:46 +00:00
|
|
|
const path = require('path');
|
|
|
|
const fs = require('fs-extra');
|
|
|
|
const colors = require('colors/safe');
|
|
|
|
const green = colors.green;
|
|
|
|
const blue = colors.blue;
|
|
|
|
const yellow = colors.yellow;
|
|
|
|
const isWindows = /^win/.test(process.platform);
|
|
|
|
|
|
|
|
const ROOT = path.resolve(__dirname, '..');
|
|
|
|
const NODE_ENV = process.env.NODE_ENV;
|
|
|
|
|
|
|
|
|
|
|
|
function onError(err) {
|
2022-05-25 17:37:20 +00:00
|
|
|
console.log('\u0007'); //🔔
|
2017-06-20 23:18:46 +00:00
|
|
|
console.log(colors.red('Error:'), err);
|
|
|
|
}
|
|
|
|
|
|
|
|
function onSuccess(result) {
|
|
|
|
var msg = `${green('Success:')} ${blue(`[${result.action}]`)} ${result.count} files processed`;
|
|
|
|
if (result.totalCount) {
|
2021-02-24 15:36:27 +00:00
|
|
|
msg += ` | ${result.totalCount} checked`;
|
2017-06-20 23:18:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
msg += ` [${yellow(`${result.processingTime}ms`)}]`;
|
|
|
|
|
|
|
|
console.log(msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
function onProgress(sourcefile, outfile, operation) {
|
|
|
|
if ('isError' in global && global.isError) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (NODE_ENV == 'debug') {
|
|
|
|
console.log(`${colors.blue(`[${operation}]`)} ${sourcefile} -> ${outfile}`);
|
|
|
|
} else {
|
|
|
|
console.log(`${colors.blue(`[${operation}]`)} ${sourcefile}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function getSignatures() {
|
|
|
|
let signaturesFile = path.resolve(ROOT, '.signatures.json');
|
|
|
|
var signatures = {};
|
|
|
|
try {
|
|
|
|
signatures = await fs.readJson(signaturesFile);
|
|
|
|
} catch (_) {
|
2019-01-21 01:39:27 +00:00
|
|
|
// if signatures files doesn't exist, return empty object instead
|
2017-06-20 23:18:46 +00:00
|
|
|
}
|
|
|
|
return signatures;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function writeSignatures(signatures) {
|
|
|
|
let signaturesFile = path.resolve(ROOT, '.signatures.json');
|
|
|
|
NODE_ENV == 'debug' && console.log('writing signatures to .signatures.json');
|
|
|
|
await fs.outputJson(signaturesFile, signatures);
|
|
|
|
}
|
|
|
|
|
|
|
|
async function cleanUp(signatures) {
|
|
|
|
const t1 = Date.now();
|
|
|
|
var removedCount = 0, invalidCount = 0;
|
|
|
|
|
|
|
|
for (let f of Object.keys(signatures)) {
|
|
|
|
try {
|
|
|
|
// check if file from signatures exists in source
|
|
|
|
await fs.access(f, fs.constants.F_OK);
|
|
|
|
} catch (_) {
|
|
|
|
invalidCount++;
|
|
|
|
NODE_ENV == 'debug' && console.log(`File ${f} found in signatures but not in src, deleting from build`);
|
|
|
|
try {
|
|
|
|
await fs.remove(path.join('build', f));
|
|
|
|
removedCount++;
|
|
|
|
} catch (_) {
|
|
|
|
// file wasn't in the build either
|
|
|
|
}
|
|
|
|
delete signatures[f];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const t2 = Date.now();
|
|
|
|
return {
|
|
|
|
action: 'cleanup',
|
|
|
|
count: removedCount,
|
|
|
|
totalCount: invalidCount,
|
|
|
|
processingTime: t2 - t1
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
async function getFileSignature(file) {
|
|
|
|
let stats = await fs.stat(file);
|
|
|
|
return {
|
|
|
|
mode: stats.mode,
|
|
|
|
mtime: stats.mtimeMs || stats.mtime.getTime(),
|
|
|
|
isDirectory: stats.isDirectory(),
|
|
|
|
isFile: stats.isFile()
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function compareSignatures(a, b) {
|
|
|
|
return typeof a === 'object'
|
|
|
|
&& typeof b === 'object'
|
|
|
|
&& a != null
|
|
|
|
&& b != null
|
|
|
|
&& ['mode', 'mtime', 'isDirectory', 'isFile'].reduce((acc, k) => {
|
|
|
|
return acc ? k in a && k in b && a[k] == b[k] : false;
|
|
|
|
}, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
function getPathRelativeTo(f, dirName) {
|
|
|
|
return path.relative(path.join(ROOT, dirName), path.join(ROOT, f));
|
|
|
|
}
|
|
|
|
|
|
|
|
const formatDirsForMatcher = dirs => {
|
|
|
|
return dirs.length > 1 ? `{${dirs.join(',')}}` : dirs[0];
|
|
|
|
};
|
|
|
|
|
2020-07-28 17:02:08 +00:00
|
|
|
function comparePaths(actualPath, testedPath) {
|
|
|
|
// compare paths after normalizing os-specific path separator
|
|
|
|
return path.normalize(actualPath) === path.normalize(testedPath);
|
|
|
|
}
|
|
|
|
|
2022-05-25 17:56:23 +00:00
|
|
|
const envCheckTrue = env => !!(env && (parseInt(env) || env === true || env === "true"));
|
|
|
|
|
2017-06-20 23:18:46 +00:00
|
|
|
module.exports = {
|
2020-07-28 17:02:08 +00:00
|
|
|
cleanUp,
|
|
|
|
comparePaths,
|
|
|
|
compareSignatures,
|
2022-05-25 17:56:23 +00:00
|
|
|
envCheckTrue,
|
2020-07-28 17:02:08 +00:00
|
|
|
formatDirsForMatcher,
|
|
|
|
getFileSignature,
|
|
|
|
getPathRelativeTo,
|
|
|
|
getSignatures,
|
2017-06-20 23:18:46 +00:00
|
|
|
isWindows,
|
|
|
|
onError,
|
|
|
|
onProgress,
|
|
|
|
onSuccess,
|
|
|
|
writeSignatures,
|
2020-07-28 17:02:08 +00:00
|
|
|
};
|