Move image conversion out of plugins
This commit is contained in:
1 parent
3cb3c4b8ab
commit
1964040900
8 files changed
+134
-247
No files matched your search
@@ -1,124 +0,0 @@
|
||||
// This plugin converts svg files to png/jpeg files
|
||||
|
||||
import {
|
||||
CompilerPlugin, FileObj, ProgramBuilder,
|
||||
} from 'brighterscript';
|
||||
import { readFileSync, writeFileSync } from 'fs';
|
||||
import { existsSync } from 'fs-extra';
|
||||
import { globSync } from 'glob';
|
||||
import md5 from 'crypto-js/md5';
|
||||
import { join as joinPath } from 'path';
|
||||
import json5 from 'json5';
|
||||
const shell = require('shelljs');
|
||||
|
||||
const META_EXT = '.meta.json5';
|
||||
|
||||
export class ImageGenPlugin implements CompilerPlugin {
|
||||
public name = 'ImageGenPlugin';
|
||||
|
||||
beforePrepublish(builder: ProgramBuilder, files: FileObj[]) {
|
||||
// Force generate flag
|
||||
// @ts-ignore
|
||||
const forceGenerateImages = !!builder.options.forceGenerateImages;
|
||||
// Debug flag
|
||||
// @ts-ignore
|
||||
const debug = !!builder.options.debug;
|
||||
|
||||
if (debug && !forceGenerateImages) {
|
||||
// Since the plugin does a lot of scanning of files, it is fine not to run it in debug mode
|
||||
// The change will be picked up on release
|
||||
return;
|
||||
}
|
||||
|
||||
const rootDir = builder.options.rootDir!;
|
||||
|
||||
const svgFiles = globSync(`**/*.svg`, { cwd: rootDir });
|
||||
|
||||
svgFiles.forEach((svgFile) => {
|
||||
// Web app can use svg files, no need to convert
|
||||
if (svgFile.includes('www')) {
|
||||
return;
|
||||
}
|
||||
|
||||
const metafile = joinPath(rootDir, `${svgFile}${META_EXT}`);
|
||||
if (!existsSync(metafile)) {
|
||||
this.createDefaultMetaFile(svgFile, metafile);
|
||||
}
|
||||
|
||||
const meta = json5.parse(readFileSync(metafile, 'utf8'));
|
||||
|
||||
this.generateImages(svgFile, meta, metafile, rootDir, files, forceGenerateImages);
|
||||
});
|
||||
}
|
||||
|
||||
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, rootDir: string, files: FileObj[], forceGenerateImages: boolean) {
|
||||
let metaChanged = false;
|
||||
const inputHash = this.checkFileHash(joinPath(rootDir, svgFile), meta.inputHash);
|
||||
if (!inputHash.valid) {
|
||||
meta.inputHash = inputHash.hash;
|
||||
metaChanged = true;
|
||||
}
|
||||
|
||||
for (var i in meta.outputs) {
|
||||
const output = meta.outputs[i];
|
||||
|
||||
const outputFilePath = joinPath(rootDir, output.outputFilePath);
|
||||
let outputHash = this.checkFileHash(outputFilePath, output.outputHash);
|
||||
|
||||
if (inputHash.valid && outputHash.valid && !forceGenerateImages) {
|
||||
continue;
|
||||
}
|
||||
|
||||
this.generateImage(svgFile, output, rootDir);
|
||||
outputHash = this.checkFileHash(outputFilePath, output.outputHash);
|
||||
|
||||
files.push({ src: outputFilePath, dest: output.outputFilePath })
|
||||
|
||||
output.outputHash = outputHash.hash;
|
||||
metaChanged = true;
|
||||
}
|
||||
|
||||
if (metaChanged) {
|
||||
writeFileSync(metafile, json5.stringify(meta, null, 2));
|
||||
}
|
||||
}
|
||||
|
||||
generateImage(svgFile: string, output: any, rootDir: string) {
|
||||
shell.exec(`node ../../tools/convert-image.js --input "${svgFile}" --options '${JSON.stringify(output)}'`, {
|
||||
cwd: rootDir
|
||||
})
|
||||
}
|
||||
|
||||
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();
|
||||
};
|
||||
@@ -1,19 +0,0 @@
|
||||
// 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);
|
||||
})();
|
||||
@@ -0,0 +1,122 @@
|
||||
// Description: Converts SVGs to PNGs and resizes them to the correct dimensions.
|
||||
|
||||
const { ArgumentParser, BooleanOptionalAction } = require('argparse')
|
||||
const { globSync } = require('glob');
|
||||
const { convertFile } = require('convert-svg-to-png');
|
||||
const { existsSync, writeFileSync, readFileSync } = require('fs');
|
||||
const json5 = require('json5');
|
||||
const md5 = require('crypto-js/md5');
|
||||
const path = require('path');
|
||||
|
||||
const META_EXT = '.meta.json5';
|
||||
|
||||
(async () => {
|
||||
const parser = new ArgumentParser({
|
||||
description: 'Converts SVGs to PNGs'
|
||||
});
|
||||
|
||||
parser.add_argument('--force', { help: 'Force convert all images', action: BooleanOptionalAction, default: false });
|
||||
|
||||
const args = parser.parse_args()
|
||||
const force = args.force
|
||||
|
||||
const svgFiles = findSvgFiles();
|
||||
|
||||
console.log(`Found ${svgFiles.length} SVG files`);
|
||||
|
||||
for (const svgFile of svgFiles) {
|
||||
const metafile = `${svgFile.file}${META_EXT}`;
|
||||
if (!existsSync(metafile)) {
|
||||
createDefaultMetaFile(svgFile, metafile);
|
||||
}
|
||||
|
||||
const meta = json5.parse(readFileSync(metafile, 'utf8'));
|
||||
|
||||
await generateImages(svgFile, meta, metafile, force);
|
||||
}
|
||||
|
||||
console.log('Done.');
|
||||
})();
|
||||
|
||||
function findSvgFiles() {
|
||||
return [
|
||||
...findSvgFilesForProject('playlet-app/src'),
|
||||
...findSvgFilesForProject('playlet-lib/src')
|
||||
];
|
||||
}
|
||||
|
||||
function findSvgFilesForProject(project) {
|
||||
return globSync(`**/*.svg`, { cwd: project })
|
||||
// Web app can use svg files, no need to convert
|
||||
.filter(file => !file.startsWith('www'))
|
||||
.map(file => path.join(project, file))
|
||||
.map(file => { return { file: file, rootDir: project } });
|
||||
}
|
||||
|
||||
function createDefaultMetaFile(svgFile, metafile) {
|
||||
const meta = {
|
||||
inputHash: '',
|
||||
outputs: [{
|
||||
outputFilePath: svgFile.file.replace(`${svgFile.rootDir}/`, '').replace('.svg', '.png'),
|
||||
outputHash: '',
|
||||
width: 64,
|
||||
height: 64,
|
||||
}]
|
||||
}
|
||||
|
||||
writeFileSync(metafile, json5.stringify(meta, null, 2));
|
||||
}
|
||||
|
||||
async function generateImages(svgFile, meta, metafile, force) {
|
||||
let metaChanged = false;
|
||||
const inputHash = checkFileHash(svgFile.file, meta.inputHash);
|
||||
if (!inputHash.valid) {
|
||||
meta.inputHash = inputHash.hash;
|
||||
metaChanged = true;
|
||||
}
|
||||
|
||||
for (var i in meta.outputs) {
|
||||
const output = meta.outputs[i];
|
||||
|
||||
const outputFilePath = path.join(svgFile.rootDir, output.outputFilePath);
|
||||
let outputHash = checkFileHash(outputFilePath, output.outputHash);
|
||||
|
||||
if (inputHash.valid && outputHash.valid && !force) {
|
||||
continue;
|
||||
}
|
||||
|
||||
await generateImage(svgFile.file, output, svgFile.rootDir);
|
||||
outputHash = checkFileHash(outputFilePath, output.outputHash);
|
||||
|
||||
console.log(`Generated ${outputFilePath} (${output.width}x${output.height})`);
|
||||
|
||||
output.outputHash = outputHash.hash;
|
||||
metaChanged = true;
|
||||
}
|
||||
|
||||
if (metaChanged) {
|
||||
writeFileSync(metafile, json5.stringify(meta, null, 2));
|
||||
console.log(`Updated ${metafile}`);
|
||||
}
|
||||
}
|
||||
|
||||
function checkFileHash(file, hash) {
|
||||
if (!existsSync(file)) {
|
||||
return {
|
||||
valid: false,
|
||||
hash: ''
|
||||
}
|
||||
}
|
||||
|
||||
const outputHash = md5(readFileSync(file, 'binary')).toString();
|
||||
return {
|
||||
valid: outputHash === hash,
|
||||
hash: outputHash
|
||||
}
|
||||
}
|
||||
|
||||
async function generateImage(inputImage, options, rootDir) {
|
||||
const opts = { ...options };
|
||||
opts.outputFilePath = path.join(rootDir, opts.outputFilePath);
|
||||
await convertFile(inputImage, opts);
|
||||
}
|
||||
Reference in new issue
Block a user