2020-11-07 17:59:37 +02:00
|
|
|
const utils = require('@joplin/tools/gulp/utils');
|
2019-12-29 19:58:40 +02:00
|
|
|
const fs = require('fs-extra');
|
|
|
|
const md5 = require('md5');
|
|
|
|
|
2020-02-21 00:59:18 +02:00
|
|
|
const rootDir = `${__dirname}/..`;
|
2019-12-29 19:58:40 +02:00
|
|
|
const outputDir = `${rootDir}/pluginAssets`;
|
|
|
|
|
2020-03-14 01:46:14 +02:00
|
|
|
const walk = function(dir) {
|
|
|
|
let results = [];
|
|
|
|
const list = fs.readdirSync(dir);
|
2019-12-29 19:58:40 +02:00
|
|
|
list.forEach(function(file) {
|
|
|
|
file = `${dir}/${file}`;
|
2020-03-14 01:46:14 +02:00
|
|
|
const stat = fs.statSync(file);
|
2019-12-29 19:58:40 +02:00
|
|
|
if (stat && stat.isDirectory()) {
|
|
|
|
results = results.concat(walk(file));
|
|
|
|
} else {
|
|
|
|
results.push(file);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return results;
|
|
|
|
};
|
|
|
|
|
|
|
|
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`;
|
2020-02-21 00:59:18 +02:00
|
|
|
await fs.mkdirp(utils.dirname(outputPath));
|
2019-12-29 19:58:40 +02:00
|
|
|
await fs.writeFile(outputPath, js);
|
|
|
|
|
2020-02-21 00:59:18 +02:00
|
|
|
const ext = utils.fileExtension(sourcePath).toLowerCase();
|
2019-12-29 19:58:40 +02:00
|
|
|
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,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
async function main() {
|
2020-02-08 13:20:44 +02:00
|
|
|
await fs.remove(outputDir);
|
2019-12-29 19:58:40 +02:00
|
|
|
await fs.mkdirp(outputDir);
|
|
|
|
|
|
|
|
const encodedFiles = [];
|
2021-12-20 17:08:43 +02:00
|
|
|
const sourceAssetDir = `${rootDir}/../renderer/assets`;
|
2019-12-29 19:58:40 +02:00
|
|
|
const files = walk(sourceAssetDir);
|
|
|
|
|
|
|
|
for (const file of files) {
|
|
|
|
const destFile = file.substr(sourceAssetDir.length + 1);
|
|
|
|
encodedFiles.push(await encodeFile(file, destFile));
|
|
|
|
}
|
|
|
|
|
|
|
|
const hashes = [];
|
|
|
|
const indexJs = [];
|
|
|
|
for (const file of encodedFiles) {
|
|
|
|
indexJs.push(`'${file.name}': { data: require('./${file.encodedName}'), mime: '${file.mime}', encoding: '${file.encoding}' },`);
|
|
|
|
hashes.push(file.hash);
|
|
|
|
}
|
|
|
|
|
|
|
|
const hash = md5(hashes.join(''));
|
|
|
|
|
|
|
|
await fs.writeFile(`${outputDir}/index.js`, `module.exports = {\nhash:"${hash}", files: {\n${indexJs.join('\n')}\n}\n};`);
|
|
|
|
}
|
|
|
|
|
2020-02-21 00:59:18 +02:00
|
|
|
module.exports = main;
|