revamp cli tool, change formatting, add cli args

This commit is contained in:
Jill 2021-12-16 04:48:46 +03:00
parent 1ce244db94
commit 63f283b350
3 changed files with 64 additions and 11 deletions

View File

@ -1,11 +1,26 @@
const fs = require('fs/promises'); const fs = require('fs/promises');
const path = require('path'); const path = require('path');
const chalk = require('chalk'); const chalk = require('chalk');
const { program } = require('commander');
const declarationsPath = path.resolve(__dirname, './isaac-typescript-definitions/typings/'); const declarationsPath = path.resolve(__dirname, './isaac-typescript-definitions/typings/');
const outPath = path.resolve(__dirname, './out/'); const outPath = path.resolve(__dirname, './out/');
const overridePath = path.resolve(__dirname, './override/'); const overridePath = path.resolve(__dirname, './override/');
program
.name('isaac-lua-definitions-transpiler')
.version('0.0.1')
.option('-W, --no-warn', 'don\'t show warnings')
.option('-s', 'don\'t show file info')
.option('-ss', 'don\'t show files')
.option('-sss', 'don\'t show anything')
.option('-v', 'show class names')
.option('-e, --exclude [files...]', 'don\'t compile specified files', '')
.option('-o, --only [files...]', 'only compile specified files', '');
program.parse(process.argv);
const options = program.opts();
let classes = {}; let classes = {};
let tsToLuaTypes = { let tsToLuaTypes = {
@ -131,6 +146,7 @@ function parse(code, originalCode) {
while ((match = interface.exec(code)) !== null) { while ((match = interface.exec(code)) !== null) {
parsedElements++; parsedElements++;
interfaceFreeCode = interfaceFreeCode.replace(match[0], ''); interfaceFreeCode = interfaceFreeCode.replace(match[0], '');
if (options.v) log(undefined, ' interface ' + chalk.blueBright(match[1]));
elements.push(['interface', {name: match[1], extends: match[2], contents: parse(match[3], originalCode)}]); elements.push(['interface', {name: match[1], extends: match[2], contents: parse(match[3], originalCode)}]);
} }
code = interfaceFreeCode; code = interfaceFreeCode;
@ -141,6 +157,7 @@ function parse(code, originalCode) {
while ((match = namespace.exec(code)) !== null) { while ((match = namespace.exec(code)) !== null) {
parsedElements++; parsedElements++;
namespaceFreeCode = namespaceFreeCode.replace(match[0], ''); namespaceFreeCode = namespaceFreeCode.replace(match[0], '');
if (options.v) log(undefined, ' namespace ' + chalk.redBright(match[1]));
elements.push(['namespace', {name: match[1], contents: parse(match[2], originalCode)}]); elements.push(['namespace', {name: match[1], contents: parse(match[2], originalCode)}]);
} }
code = namespaceFreeCode; code = namespaceFreeCode;
@ -189,6 +206,7 @@ function parse(code, originalCode) {
const c = comments.pop(); const c = comments.pop();
//if (c) c[0] += 'isaac-lua-definitions debug: comment distance = ' + (origIndex - c[1]).toString(); //if (c) c[0] += 'isaac-lua-definitions debug: comment distance = ' + (origIndex - c[1]).toString();
if (options.v) log(undefined, ' enum ' + chalk.greenBright(match[1]));
elements.push(['enum', {name: match[1], contents: match[2].split(',').filter(c => c.split('=').length === 2).map(c => {return {name: c.split('=')[0].trim(), value: c.split('=')[1].trim()}}), comment: (c || [null])[0]}]); elements.push(['enum', {name: match[1], contents: match[2].split(',').filter(c => c.split('=').length === 2).map(c => {return {name: c.split('=')[0].trim(), value: c.split('=')[1].trim()}}), comment: (c || [null])[0]}]);
} }
code = enumFreeCode; code = enumFreeCode;
@ -236,6 +254,33 @@ async function recursiveReaddir(rpath, p) {
return fileNames; return fileNames;
} }
let loggedFile = {};
function warn(file, string) {
if (!options.warn) return;
if (options.Sss) return;
if (!loggedFile[file]) {
console.log(` ${chalk.cyanBright(file)}`);
loggedFile[file] = true;
}
console.log(`${chalk.redBright('!')} ${string}`);
}
function log(file, string) {
if (options.s && string) return;
if (options.Ss) return;
if (options.Sss) return;
if (file && !loggedFile[file]) {
console.log(` ${chalk.cyanBright(file)}`);
loggedFile[file] = true;
}
if (string) console.log(' ' + string);
}
(async () => { (async () => {
let timeParsing = 0; let timeParsing = 0;
let timeTotal = 0; let timeTotal = 0;
@ -255,22 +300,22 @@ async function recursiveReaddir(rpath, p) {
let errored = []; let errored = [];
let errorReason = {}; let errorReason = {};
const files = await recursiveReaddir(declarationsPath); const files = (await recursiveReaddir(declarationsPath)).filter(f => {
return !f.includes('unofficial/') && (options.exclude.length == 0 || !options.exclude.includes(f)) && (options.only.length === 0 || options.only.includes(f))
});
const filesAmt = files.length; const filesAmt = files.length;
let i = 0; let i = 0;
for (const f of files) { for (const f of files) {
//if (f !== 'Isaac.d.ts') continue;
if (f.includes('unofficial/')) continue;
i++; i++;
const filePath = path.resolve(declarationsPath, f); const filePath = path.resolve(declarationsPath, f);
console.log(` ${chalk.cyanBright(f)}`); if (!options.Ss) log(f);
const override = path.resolve(overridePath, f.replace('.d.ts', '.lua')); const override = path.resolve(overridePath, f.replace('.d.ts', '.lua'));
const luaFilename = f.replace('.d.ts', '.lua').split('/').join('_'); const luaFilename = f.replace('.d.ts', '.lua').split('/').join('_');
try { try {
if (await fs.stat(override)) { if (await fs.stat(override)) {
console.log(' file exists in override/, ignoring'); log('file exists in override/, ignoring');
await fs.copyFile(override, path.resolve(outPath, luaFilename)); await fs.copyFile(override, path.resolve(outPath, luaFilename));
continue; continue;
} }
@ -281,15 +326,15 @@ async function recursiveReaddir(rpath, p) {
let startParse = Date.now(); let startParse = Date.now();
parsedElements = 0; parsedElements = 0;
const parsed = parse(file); const parsed = parse(file);
console.log(` parsed ${chalk.magentaBright(parsedElements)} objects from ${chalk.cyanBright(f)}`); log(f, `parsed ${chalk.magentaBright(parsedElements)} objects from ${chalk.cyanBright(f)}`);
timeParsing += Date.now() - startParse; timeParsing += Date.now() - startParse;
if (parsed.length === 0) { if (parsed.length === 0) {
console.log(`${chalk.redBright('!')} no elements parsed, ${chalk.gray('not writing anything')}`); warn(f, `no elements parsed, ${chalk.gray('not writing anything')}`);
continue; continue;
} }
if (parsed.length > 10) { if (parsed.length > 10) {
console.log(`${chalk.redBright('!')} over 10 top-level objects were parsed - this may be a bad idea`); warn(f, `over 10 top-level objects were parsed - this may be a bad idea`);
} }
let transpiled; let transpiled;
@ -316,11 +361,11 @@ async function recursiveReaddir(rpath, p) {
continue; continue;
} }
console.log(` transpiled w/ final length of ${chalk.magentaBright(transpiled.length + ' chars')}`); log(f, `transpiled w/ final length of ${chalk.magentaBright(transpiled.length + ' chars')}`);
timeTotal += Date.now() - startParse; timeTotal += Date.now() - startParse;
if (transpiled.length === 0) { if (transpiled.length === 0) {
console.log(`${chalk.redBright('!')} nothing transpiled, ${chalk.gray('not writing anything')}`); warn(f, `nothing transpiled, ${chalk.gray('not writing anything')}`);
continue; continue;
} }

View File

@ -9,6 +9,7 @@
"author": "oatmealine", "author": "oatmealine",
"license": "AGPL-3.0", "license": "AGPL-3.0",
"dependencies": { "dependencies": {
"chalk": "^4.1.2" "chalk": "^4.1.2",
"commander": "^8.3.0"
} }
} }

View File

@ -2,9 +2,11 @@ lockfileVersion: 5.3
specifiers: specifiers:
chalk: ^4.1.2 chalk: ^4.1.2
commander: ^8.3.0
dependencies: dependencies:
chalk: 4.1.2 chalk: 4.1.2
commander: 8.3.0
packages: packages:
@ -34,6 +36,11 @@ packages:
resolution: {integrity: sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==} resolution: {integrity: sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==}
dev: false dev: false
/commander/8.3.0:
resolution: {integrity: sha512-OkTL9umf+He2DZkUq8f8J9of7yL6RJKI24dVITBmNfZBmri9zYZQrKkuXiKhyfPSu8tUhnVBB1iKXevvnlR4Ww==}
engines: {node: '>= 12'}
dev: false
/has-flag/4.0.0: /has-flag/4.0.0:
resolution: {integrity: sha512-EykJT/Q1KjTWctppgIAgfSO0tKVuZUjhgMr17kqTumMl6Afv3EISleU7qZUzoXDFTAHTDC4NOoG/ZxU3EvlMPQ==} resolution: {integrity: sha512-EykJT/Q1KjTWctppgIAgfSO0tKVuZUjhgMr17kqTumMl6Afv3EISleU7qZUzoXDFTAHTDC4NOoG/ZxU3EvlMPQ==}
engines: {node: '>=8'} engines: {node: '>=8'}