revamp cli tool, change formatting, add cli args
This commit is contained in:
parent
1ce244db94
commit
63f283b350
65
index.js
65
index.js
|
@ -1,11 +1,26 @@
|
|||
const fs = require('fs/promises');
|
||||
const path = require('path');
|
||||
const chalk = require('chalk');
|
||||
const { program } = require('commander');
|
||||
|
||||
const declarationsPath = path.resolve(__dirname, './isaac-typescript-definitions/typings/');
|
||||
const outPath = path.resolve(__dirname, './out/');
|
||||
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 tsToLuaTypes = {
|
||||
|
@ -131,6 +146,7 @@ function parse(code, originalCode) {
|
|||
while ((match = interface.exec(code)) !== null) {
|
||||
parsedElements++;
|
||||
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)}]);
|
||||
}
|
||||
code = interfaceFreeCode;
|
||||
|
@ -141,6 +157,7 @@ function parse(code, originalCode) {
|
|||
while ((match = namespace.exec(code)) !== null) {
|
||||
parsedElements++;
|
||||
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)}]);
|
||||
}
|
||||
code = namespaceFreeCode;
|
||||
|
@ -189,6 +206,7 @@ function parse(code, originalCode) {
|
|||
const c = comments.pop();
|
||||
//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]}]);
|
||||
}
|
||||
code = enumFreeCode;
|
||||
|
@ -236,6 +254,33 @@ async function recursiveReaddir(rpath, p) {
|
|||
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 () => {
|
||||
let timeParsing = 0;
|
||||
let timeTotal = 0;
|
||||
|
@ -255,22 +300,22 @@ async function recursiveReaddir(rpath, p) {
|
|||
let errored = [];
|
||||
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;
|
||||
let i = 0;
|
||||
for (const f of files) {
|
||||
//if (f !== 'Isaac.d.ts') continue;
|
||||
if (f.includes('unofficial/')) continue;
|
||||
i++;
|
||||
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 luaFilename = f.replace('.d.ts', '.lua').split('/').join('_');
|
||||
try {
|
||||
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));
|
||||
continue;
|
||||
}
|
||||
|
@ -281,15 +326,15 @@ async function recursiveReaddir(rpath, p) {
|
|||
let startParse = Date.now();
|
||||
parsedElements = 0;
|
||||
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;
|
||||
|
||||
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;
|
||||
}
|
||||
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;
|
||||
|
@ -316,11 +361,11 @@ async function recursiveReaddir(rpath, p) {
|
|||
|
||||
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;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
|
|
|
@ -9,6 +9,7 @@
|
|||
"author": "oatmealine",
|
||||
"license": "AGPL-3.0",
|
||||
"dependencies": {
|
||||
"chalk": "^4.1.2"
|
||||
"chalk": "^4.1.2",
|
||||
"commander": "^8.3.0"
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,9 +2,11 @@ lockfileVersion: 5.3
|
|||
|
||||
specifiers:
|
||||
chalk: ^4.1.2
|
||||
commander: ^8.3.0
|
||||
|
||||
dependencies:
|
||||
chalk: 4.1.2
|
||||
commander: 8.3.0
|
||||
|
||||
packages:
|
||||
|
||||
|
@ -34,6 +36,11 @@ packages:
|
|||
resolution: {integrity: sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==}
|
||||
dev: false
|
||||
|
||||
/commander/8.3.0:
|
||||
resolution: {integrity: sha512-OkTL9umf+He2DZkUq8f8J9of7yL6RJKI24dVITBmNfZBmri9zYZQrKkuXiKhyfPSu8tUhnVBB1iKXevvnlR4Ww==}
|
||||
engines: {node: '>= 12'}
|
||||
dev: false
|
||||
|
||||
/has-flag/4.0.0:
|
||||
resolution: {integrity: sha512-EykJT/Q1KjTWctppgIAgfSO0tKVuZUjhgMr17kqTumMl6Afv3EISleU7qZUzoXDFTAHTDC4NOoG/ZxU3EvlMPQ==}
|
||||
engines: {node: '>=8'}
|
||||
|
|
Loading…
Reference in New Issue