You've already forked joplin
mirror of
https://github.com/laurent22/joplin.git
synced 2025-12-05 22:57:29 +02:00
All: Extract note renderer to separate package (WIP) (#2206)
* Started updating to use external renderer package * Added way to build renderer assets * Done mobile compatilibty * Upgrade joplin-renderer * Added joplin-renderer package
This commit is contained in:
74
ReactNativeClient/encodeAssets.js
Normal file
74
ReactNativeClient/encodeAssets.js
Normal file
@@ -0,0 +1,74 @@
|
||||
require('app-module-path').addPath(`${__dirname}`);
|
||||
|
||||
const fs = require('fs-extra');
|
||||
const { dirname, fileExtension } = require('lib/path-utils');
|
||||
const md5 = require('md5');
|
||||
|
||||
const rootDir = __dirname;
|
||||
const outputDir = `${rootDir}/pluginAssets`;
|
||||
|
||||
var walk = function(dir) {
|
||||
var results = [];
|
||||
var list = fs.readdirSync(dir);
|
||||
list.forEach(function(file) {
|
||||
file = `${dir}/${file}`;
|
||||
var stat = fs.statSync(file);
|
||||
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`;
|
||||
await fs.mkdirp(dirname(outputPath));
|
||||
await fs.writeFile(outputPath, js);
|
||||
|
||||
const ext = 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,
|
||||
};
|
||||
}
|
||||
|
||||
async function main() {
|
||||
await fs.mkdirp(outputDir);
|
||||
|
||||
const encodedFiles = [];
|
||||
const sourceAssetDir = `${rootDir}/node_modules/joplin-renderer/assets`;
|
||||
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};`);
|
||||
}
|
||||
|
||||
main().catch((error) => {
|
||||
console.error(error);
|
||||
process.exit(1);
|
||||
});
|
||||
Reference in New Issue
Block a user