UNPKG

gtavjs-ts-bundler

Version:

Bundle GTAVJS mod declaration files into a multi-modules index.d.ts file.

59 lines (48 loc) 2.26 kB
#!/usr/bin/env node import { appendFile, readdir, readFile, rm, stat } from 'fs/promises' import { basename, join, resolve } from 'path' import { existsSync } from 'fs' import chalk from 'chalk' import commandLineArgs from 'command-line-args' console.log(chalk.blue(` _____ _____ _____ _____ __ _____ | __|_ _| _ | | |__| | __| | | | | | | | | | | |__ | |_____| |_| |__|__|\\___/|_____|_____| TS Bundler v1.0.1 `)) const options = commandLineArgs([ { name: 'declarationDir', alias: 'i', type: String, defaultValue: "scripts/" }, { name: 'outputFile', alias: 'o', type: String, defaultValue: "index.d.ts" } ]) console.log(chalk.blue("Bundler Options:\n"), `declarationDir = ${options.declarationDir}`, '\n', `outputFile = ${options.outputFile}`, '\n') async function getAllDeclarationFiles(path, arrayOfFiles) { const files = await readdir(path) arrayOfFiles = arrayOfFiles || [] for (const file of files) { if ((await stat(join(path, file))).isDirectory()) { arrayOfFiles = await getAllDeclarationFiles(join(path, file), arrayOfFiles) } else if (file.endsWith(".d.ts")) { arrayOfFiles.push(join(path, file)) } } return arrayOfFiles } async function execute() { const modName = basename(resolve("./")) console.log(chalk.blueBright(`Creating declaration bundle for '${modName}'...`)) await getAllDeclarationFiles(join("./", options.declarationDir)).then(async (files) => { const outFile = join("./", options.outputFile) if (existsSync(outFile)) await rm(outFile) for (const file of files) { let content = await readFile(file, "utf-8") content = content.split("declare ").join("").split("\n").join("\n ") content = `declare module \"${join(modName, file.replace(".d.ts", "")).split("\\").join("/")}\" {\n\n ` + content content = content + "\n}\n" await appendFile(outFile, content, "utf-8") } console.log(chalk.greenBright(`Bundled into './${outFile}' successfully!`)) }) } execute() .catch(error => console.error(chalk.red(error)))