2021-02-24 15:36:27 +00:00
|
|
|
/* eslint-disable no-await-in-loop */
|
2017-06-20 23:18:46 +00:00
|
|
|
|
|
|
|
const universalify = require('universalify');
|
2020-10-30 12:15:30 +00:00
|
|
|
const sass = require('sass');
|
2017-06-20 23:18:46 +00:00
|
|
|
const globby = require('globby');
|
|
|
|
const path = require('path');
|
|
|
|
const fs = require('fs-extra');
|
|
|
|
const { getSignatures, writeSignatures, cleanUp, compareSignatures, getFileSignature, onSuccess, onError, onProgress, getPathRelativeTo } = require('./utils');
|
2021-02-24 15:36:27 +00:00
|
|
|
const { scssFiles, ignoreMask } = require('./config');
|
2017-06-20 23:18:46 +00:00
|
|
|
const sassRender = universalify.fromCallback(sass.render);
|
|
|
|
|
|
|
|
const ROOT = path.resolve(__dirname, '..');
|
|
|
|
|
2022-05-25 17:56:23 +00:00
|
|
|
async function getSass(source, options, signatures = {}) {
|
2017-06-20 23:18:46 +00:00
|
|
|
const t1 = Date.now();
|
2021-02-24 15:36:27 +00:00
|
|
|
const files = await globby(source, Object.assign({ cwd: ROOT }, options));
|
2017-06-20 23:18:46 +00:00
|
|
|
const totalCount = files.length;
|
2022-05-25 17:56:23 +00:00
|
|
|
const outFiles = [];
|
|
|
|
var shouldRebuild = false;
|
2017-06-20 23:18:46 +00:00
|
|
|
|
2021-02-24 15:36:27 +00:00
|
|
|
for (const f of files) {
|
|
|
|
// if any file changed, rebuild all onSuccess
|
2017-06-20 23:18:46 +00:00
|
|
|
let newFileSignature = await getFileSignature(f);
|
2021-02-24 15:36:27 +00:00
|
|
|
if (!compareSignatures(newFileSignature, signatures[f])) {
|
|
|
|
signatures[f] = newFileSignature;
|
|
|
|
shouldRebuild = true;
|
2018-12-18 11:47:11 +00:00
|
|
|
}
|
2021-02-24 15:36:27 +00:00
|
|
|
}
|
2017-06-20 23:18:46 +00:00
|
|
|
|
2021-02-24 15:36:27 +00:00
|
|
|
var f;
|
|
|
|
if (shouldRebuild) {
|
|
|
|
const filesToBuild = files.filter(f => !path.basename(f).startsWith('_'));
|
|
|
|
while ((f = filesToBuild.pop())) {
|
|
|
|
let newFileSignature = await getFileSignature(f);
|
|
|
|
let destFile = getPathRelativeTo(f, 'scss');
|
|
|
|
destFile = path.join(path.dirname(destFile), path.basename(destFile, '.scss') + '.css');
|
2022-05-25 13:10:30 +00:00
|
|
|
let dest = path.join('build', 'chrome', 'skin', 'default', 'zotero', destFile);
|
2021-02-24 15:36:27 +00:00
|
|
|
|
|
|
|
if (['win', 'mac', 'unix'].some(platform => f.endsWith(`-${platform}.scss`))) {
|
|
|
|
let platform = f.slice(f.lastIndexOf('-') + 1, f.lastIndexOf('.'));
|
|
|
|
destFile = destFile.slice(0, destFile.lastIndexOf('-'))
|
|
|
|
+ destFile.slice(destFile.lastIndexOf('-') + 1 + platform.length);
|
2022-05-25 13:10:30 +00:00
|
|
|
dest = path.join('build', 'chrome', 'content', 'zotero-platform', platform, destFile);
|
2017-06-20 23:18:46 +00:00
|
|
|
}
|
|
|
|
|
2021-02-24 15:36:27 +00:00
|
|
|
try {
|
|
|
|
const sass = await sassRender({
|
|
|
|
file: f,
|
|
|
|
outFile: dest,
|
|
|
|
sourceMap: true,
|
|
|
|
outputStyle: 'compressed'
|
|
|
|
});
|
|
|
|
|
|
|
|
await fs.outputFile(dest, sass.css);
|
|
|
|
await fs.outputFile(`${dest}.map`, sass.map);
|
|
|
|
onProgress(f, dest, 'sass');
|
|
|
|
signatures[f] = newFileSignature;
|
2022-05-25 17:56:23 +00:00
|
|
|
outFiles.push(dest);
|
2021-02-24 15:36:27 +00:00
|
|
|
}
|
|
|
|
catch (err) {
|
|
|
|
throw new Error(`Failed on ${f}: ${err}`);
|
|
|
|
}
|
2017-06-20 23:18:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const t2 = Date.now();
|
|
|
|
return {
|
|
|
|
action: 'sass',
|
2022-05-25 17:56:23 +00:00
|
|
|
count: outFiles.length,
|
|
|
|
outFiles,
|
2017-06-20 23:18:46 +00:00
|
|
|
totalCount,
|
|
|
|
processingTime: t2 - t1
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = getSass;
|
|
|
|
|
|
|
|
if (require.main === module) {
|
|
|
|
(async () => {
|
|
|
|
try {
|
|
|
|
const signatures = await getSignatures();
|
2021-02-24 15:36:27 +00:00
|
|
|
for (var i = 0; i < scssFiles.length; i++) {
|
|
|
|
onSuccess(await getSass(scssFiles[i], { ignore: ignoreMask }, signatures));
|
|
|
|
}
|
2017-06-20 23:18:46 +00:00
|
|
|
onSuccess(await cleanUp(signatures));
|
|
|
|
await writeSignatures(signatures);
|
2021-02-24 15:36:27 +00:00
|
|
|
}
|
|
|
|
catch (err) {
|
2017-06-20 23:18:46 +00:00
|
|
|
process.exitCode = 1;
|
|
|
|
global.isError = true;
|
|
|
|
onError(err);
|
|
|
|
}
|
|
|
|
})();
|
2021-02-24 15:36:27 +00:00
|
|
|
}
|