Skip to content

Commit

Permalink
Add option to mark current file as entrypoint
Browse files Browse the repository at this point in the history
which by default makes the file not to be considered as needed for format conversion which is only useful in entrypoints
  • Loading branch information
Davilarek committed Jul 24, 2024
1 parent 3f36e2e commit ea4dcad
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
8 changes: 6 additions & 2 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ import { transformSync } from "@babel/core";
import { IModImplementation } from "./api/ModImplementation.js";
// import { addCode } from "./api/RuntimeGenerators.js";

if (process.argv.length != 5) {
if (process.argv[5] != undefined && process.argv[5] !== "--entrypoint") {
console.error(`Usage:\n\t${myPackageName} <input file> <target client mod> <output file> --entrypoint\nExample:\n\t${myPackageName} ./index.js BetterDiscord ./dist/index.js --entrypoint`);
process.exit(1);
}
if (process.argv.length < 5) { // TODO: make a proper option parser
console.error(`Usage:\n\t${myPackageName} <input file> <target client mod> <output file>\nExample:\n\t${myPackageName} ./index.js BetterDiscord ./dist/index.js`);
process.exit(1);
}
Expand Down Expand Up @@ -48,7 +52,7 @@ const filler = import(url.pathToFileURL(`${__dirname}/converters/${targetDiscord
filler.then(async (x: { default: IModImplementation }) => {
if (x.default.importsForbidden)
console.warn('\x1b[33m%s\x1b[0m', `Warning: Target mod ${targetDiscordMod} requires your code to be bundled into single file`);
const out = await converter(ast as File & { errors: [] }, x);
const out = await converter(ast as File & { errors: [] }, x, process.argv[5] !== undefined ? process.argv[5] === "--entrypoint" : true);
const outMod = {
...ast,
program: {
Expand Down
4 changes: 2 additions & 2 deletions src/converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ function deepFind<K>(obj: any, path: string): K | undefined {
return current;
}

export default async function (ast: ParseResult<File>, targetedDiscordModApiLibrary: { default: IModImplementation }): Promise<Statement[]> {
export default async function (ast: ParseResult<File>, targetedDiscordModApiLibrary: { default: IModImplementation }, shouldConvertFormat: boolean = true): Promise<Statement[]> {
const parsedBody = ast.program.body;
const importStatements = parsedBody.filter(x => x.type == "ImportDeclaration") as Statement[];
const importAliasMap = [] as { internalName: string, codeName: string }[];
Expand Down Expand Up @@ -183,7 +183,7 @@ export default async function (ast: ParseResult<File>, targetedDiscordModApiLibr
}
}
parsedBodyWithoutOurImports.unshift(...await addCode(targetedDiscordModApiLibrary.default));
if ((targetedDiscordModApiLibrary as { default: IModImplementation } & { convertFormat: (ast_: Statement[]) => Statement[] }).convertFormat == undefined)
if (shouldConvertFormat == false || (targetedDiscordModApiLibrary as { default: IModImplementation } & { convertFormat: (ast_: Statement[]) => Statement[] }).convertFormat == undefined)
return parsedBodyWithoutOurImports;
return (targetedDiscordModApiLibrary as { default: IModImplementation } & { convertFormat: (ast_: Statement[]) => Statement[] }).convertFormat(parsedBodyWithoutOurImports);
}

0 comments on commit ea4dcad

Please sign in to comment.