Files
lnreader-plugins/scripts/multisrc/generate.ts
T
nyagami cf8db556a6 refactor: Remove ignore generated multisrc .ts files when committing (#678)
* update clear multisrc script

* ignore generated multisrc .ts files

* Squashed commit of the following:

commit b3ef1a83a9
Author: Rajarshee Chatterjee <rajarshee.adm@gmail.com>
Date:   Sun Jan 7 16:56:12 2024 +0530

    plugins: update json files for new plugins (#679)

commit f36e93d0a6
Author: Rider21 <58046032+Rider21@users.noreply.github.com>
Date:   Sun Jan 7 14:25:02 2024 +0300

    fix: Update Plugins (#677)

    * typo

    * update

    * fix

    * fix

    * update

    * ifreedom

    * fix

    * fix ranobelib

    * ...

commit 8a267561cb
Author: Rajarshee Chatterjee <rajarshee.adm@gmail.com>
Date:   Sun Jan 7 16:54:09 2024 +0530

    plugins: update json files for new plugins (#673)

* clear generated multisrc files

* generate .js files
2024-01-07 17:34:39 +05:30

64 lines
1.8 KiB
TypeScript

import path from "path";
import fs from "fs";
type GeneratedScript = {
lang: string;
filename: string;
pluginScript: string;
};
export type ScrpitGeneratorFunction = (name: string) => GeneratedScript[];
const isScriptGenerator = (s: unknown): s is ScrpitGeneratorFunction => {
return !!s && typeof s === "function";
};
const generate = async (name: string): Promise<boolean> => {
try {
const generateAll: ScrpitGeneratorFunction | unknown =
require(`./${name}/generator`).generateAll;
if (!isScriptGenerator(generateAll)) return false;
const sources = generateAll(name);
for (let source of sources) {
const { lang, filename, pluginScript } = source;
if (!lang || !filename || !pluginScript) {
console.warn(
name,
": lang, filename, pluginScript are required!"
);
continue;
}
const pluginsDir = path.join(
path.dirname(path.dirname(__dirname)),
"plugins"
);
const filePath = path.join(
pluginsDir,
lang.toLowerCase(),
filename.replace(/[\s-\.]+/g, "") + `[${name}].ts`
);
fs.writeFileSync(filePath, pluginScript, { encoding: "utf-8" });
}
return true;
} catch (e) {
console.log(`${name} is broken! ${e}`);
return false;
}
};
const run = async () => {
const sources = fs
.readdirSync(__dirname)
.filter((name) =>
fs.lstatSync(path.join(__dirname, name)).isDirectory()
);
for (let name of sources) {
const success = await generate(name);
if (success) console.log(`[${name}] OK`);
}
console.log('Generating: .js files');
};
run();