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) {
const buffer = await fs.readFile(sourcePath);
const hash = md5(buffer.toString('base64'));
const js = `module.exports = \`${buffer.toString('base64')}\`;`;
const outputPath = `${outputDir}/${destPath}.base64.js`;
console.info(`Encoding "${sourcePath}" => "${outputPath}"`);
await utils.mkdirp(utils.dirname(outputPath));
await fs.writeFile(outputPath, js);
for (let i = 0; i < 3; i++) {
try {
const buffer = await fs.readFile(sourcePath);
const hash = md5(buffer.toString('base64'));
const js = `module.exports = \`${buffer.toString('base64')}\`;`;
const outputPath = `${outputDir}/${destPath}.base64.js`;
console.info(`Encoding "${sourcePath}" => "${outputPath}"`);
await utils.mkdirp(utils.dirname(outputPath));
await fs.writeFile(outputPath, js);
const ext = utils.fileExtension(sourcePath).toLowerCase();
let mime = 'application/octet-stream';
if (ext === 'js') mime = 'application/javascript';
if (ext === 'css') mime = 'text/css';
const ext = utils.fileExtension(sourcePath).toLowerCase();
let mime = 'application/octet-stream';
if (ext === 'js') mime = 'application/javascript';
if (ext === 'css') mime = 'text/css';
return {
encoding: 'base64',
name: destPath,
encodedName: `${destPath}.base64.js`,
mime: mime,
hash: hash,
};
return {
encoding: 'base64',
name: destPath,
encodedName: `${destPath}.base64.js`,
mime: mime,
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() {

View File

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