Refactor image conversion (#163)

* Refactor image conversion

* Docs
This commit is contained in:
Brahim Hadriche authored and GitHub committed 2023-10-07 18:21:54 -04:00
1 parent 7818f778ea
commit 508306c6f4
26 files changed
+300 -85

No files matched your search

+107
View File
@@ -0,0 +1,107 @@
// This plugin converts svg files to png/jpeg files
import {
CompilerPlugin, ProgramBuilder,
} from 'brighterscript';
import { readFileSync, renameSync, rmSync, writeFileSync } from 'fs';
import { existsSync } from 'fs-extra';
import { globSync } from 'glob';
import md5 from 'crypto-js/md5';
import { join as joinPath, relative as relativePath } from 'path';
import json5 from 'json5';
const shell = require('shelljs');
const META_EXT = '.meta.json5';
export class ImageGenPlugin implements CompilerPlugin {
public name = 'ImageGenPlugin';
beforeProgramCreate(builder: ProgramBuilder) {
const rootDir = builder.options.rootDir!;
const svgFiles = globSync(`**/*.svg`, { cwd: rootDir });
svgFiles.forEach((svg) => {
// Web app can use svg files, no need to convert
if (svg.includes('www')) {
return;
}
const svgFile = relativePath(process.cwd(), joinPath(rootDir, svg));
const metafile = `${svgFile}${META_EXT}`;
if (!existsSync(metafile)) {
this.createDefaultMetaFile(svgFile, metafile);
}
const meta = json5.parse(readFileSync(metafile, 'utf8'));
this.generateImages(svgFile, meta, metafile);
});
}
createDefaultMetaFile(svgFile: string, metafile: string) {
const meta = {
inputHash: '',
outputs: [{
outputFilePath: svgFile.replace('.svg', '.png'),
outputHash: '',
width: 64,
height: 64,
}]
}
writeFileSync(metafile, json5.stringify(meta, null, 2));
}
generateImages(svgFile: string, meta: any, metafile: string) {
let metaChanged = false;
const inputHash = this.checkFileHash(svgFile, meta.inputHash);
if (!inputHash.valid) {
meta.inputHash = inputHash.hash;
metaChanged = true;
}
for (var i in meta.outputs) {
const output = meta.outputs[i];
let outputHash = this.checkFileHash(output.outputFilePath, output.outputHash);
if (inputHash.valid && outputHash.valid) {
continue;
}
this.generateImage(svgFile, output);
outputHash = this.checkFileHash(output.outputFilePath, output.outputHash);
output.outputHash = outputHash.hash;
metaChanged = true;
}
if (metaChanged) {
writeFileSync(metafile, json5.stringify(meta, null, 2));
}
}
generateImage(svgFile: string, output: any) {
shell.exec(`node ../tools/convert-image.js --input "${svgFile}" --options '${JSON.stringify(output)}'`)
}
checkFileHash(file: string, hash: string) {
if (!existsSync(file)) {
return {
valid: false,
hash: ''
}
}
const outputHash = md5(readFileSync(file, 'binary')).toString();
return {
valid: outputHash === hash,
hash: outputHash
}
}
}
export default () => {
return new ImageGenPlugin();
};
+19
View File
@@ -0,0 +1,19 @@
// Description: Converts SVGs to PNGs and resizes them to the correct dimensions.
const { ArgumentParser } = require('argparse')
const { convertFile } = require('convert-svg-to-png');
(async () => {
const parser = new ArgumentParser({
description: 'Sync Youtube profile with Invidious'
});
parser.add_argument('--input', { help: 'Input file path' });
parser.add_argument('--options', { help: 'Options as json string' });
const args = parser.parse_args()
const input = args.input
const options = JSON.parse(args.options)
await convertFile(input, options);
})();
-79
View File
@@ -1,79 +0,0 @@
// Description: Generates images for the project.
// Converts SVGs to PNGs and resizes them to the correct dimensions.
const fs = require('fs');
const path = require('path');
const { convertFile } = require('convert-svg-to-png');
const logoPoster = './playlet-app/src/images/vector/logo-light.svg';
const logoSplash = './playlet-app/src/images/vector/logo-dark.svg';
const logoOutput = {
// Logo
"splash-screen_fhd.jpg": {
width: 1920,
height: 1080,
background: "#242424",
from: logoSplash
},
"splash-screen_hd.jpg": {
width: 1280,
height: 720,
background: "#242424",
from: logoSplash
},
"splash-screen_sd.jpg": {
width: 720,
height: 480,
background: "#242424",
from: logoSplash
},
"channel-poster_fhd.png": {
width: 540,
height: 405,
background: "#FFFFFF",
from: logoPoster
},
"channel-poster_hd.png": {
width: 290,
height: 218,
background: "#FFFFFF",
from: logoPoster
},
"channel-poster_sd.png": {
width: 246,
height: 140,
background: "#FFFFFF",
from: logoPoster
},
};
const iconsInput = './playlet-lib/src/images/vector/icons';
const iconsOutput = './playlet-lib/src/images/icons';
(async () => {
for (var logo in logoOutput) {
await convertFile(logoOutput[logo].from, {
outputFilePath: './playlet-app/src/images/' + logo,
background: logoOutput[logo].background,
height: logoOutput[logo].height,
width: logoOutput[logo].width,
});
console.log(`Generated ${logo}`);
}
const iconFiles = fs.readdirSync(iconsInput);
for (var i in iconFiles) {
const input = path.join(iconsInput, iconFiles[i]);
const output = path.join(iconsOutput, iconFiles[i].replace('.svg', '.png'));
await convertFile(input, {
outputFilePath: output,
width: 64,
height: 64,
});
console.log(`Generated ${output}`);
}
})();