1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-24 10:27:10 +02:00

Tools: Trying to make encodeAssets script more robust on CI

This commit is contained in:
Laurent Cozic 2022-10-14 11:05:21 +01:00
parent 1fe6910089
commit bec97d6a42
2 changed files with 47 additions and 18 deletions

View File

@ -21,26 +21,47 @@ const walk = function(dir) {
}; };
async function encodeFile(sourcePath, destPath) { async function encodeFile(sourcePath, destPath) {
const buffer = await fs.readFile(sourcePath); for (let i = 0; i < 3; i++) {
const hash = md5(buffer.toString('base64')); try {
const js = `module.exports = \`${buffer.toString('base64')}\`;`; const buffer = await fs.readFile(sourcePath);
const outputPath = `${outputDir}/${destPath}.base64.js`; const hash = md5(buffer.toString('base64'));
console.info(`Encoding "${sourcePath}" => "${outputPath}"`); const js = `module.exports = \`${buffer.toString('base64')}\`;`;
await utils.mkdirp(utils.dirname(outputPath)); const outputPath = `${outputDir}/${destPath}.base64.js`;
await fs.writeFile(outputPath, js); console.info(`Encoding "${sourcePath}" => "${outputPath}"`);
await utils.mkdirp(utils.dirname(outputPath));
await fs.writeFile(outputPath, js);
const ext = utils.fileExtension(sourcePath).toLowerCase(); const ext = utils.fileExtension(sourcePath).toLowerCase();
let mime = 'application/octet-stream'; let mime = 'application/octet-stream';
if (ext === 'js') mime = 'application/javascript'; if (ext === 'js') mime = 'application/javascript';
if (ext === 'css') mime = 'text/css'; if (ext === 'css') mime = 'text/css';
return { return {
encoding: 'base64', encoding: 'base64',
name: destPath, name: destPath,
encodedName: `${destPath}.base64.js`, encodedName: `${destPath}.base64.js`,
mime: mime, mime: mime,
hash: hash, hash: hash,
}; };
} catch (error) {
// Although it makes no sense, the above function sometimes fails on CI
// with error "DEST does not exist", which of course it doesn't
// since we are trying to create it. So here we retry when it happens.
//
// Full error:
//
// Encoding "/home/runner/work/joplin/joplin/packages/app-mobile/tools/../../renderer/assets/katex/fonts/KaTeX_Math-BoldItalic.woff2" => "/home/runner/work/joplin/joplin/packages/app-mobile/tools/../pluginAssets/katex/fonts/KaTeX_Math-BoldItalic.woff2.base64.js"
// 'encodeAssets' errored after 115 ms
// Error: ENOENT: no such file or directory, open '/home/runner/work/joplin/joplin/packages/app-mobile/tools/../pluginAssets/katex/fonts/KaTeX_Math-BoldItalic.woff2.base64.js'
console.warn(`Could not encode file (${i}). Will try again...`);
console.warn('Error was:', error);
await utils.msleep(1000 + 1000 * i);
continue;
}
}
throw new Error('Could not encode file after multiple attempts. See above for errors.');
} }
async function main() { async function main() {

View File

@ -211,4 +211,12 @@ utils.getFilename = (path) => {
return splitted.join('.'); return splitted.join('.');
}; };
utils.msleep = (ms) => {
return new Promise((resolve) => {
setTimeout(() => {
resolve();
}, ms);
});
};
module.exports = utils; module.exports = utils;