recursively load .d.ts files

This commit is contained in:
Jill 2021-11-03 11:58:21 +03:00
parent c0b1105ee4
commit a43df3e28b
1 changed files with 62 additions and 48 deletions

110
index.js
View File

@ -168,6 +168,23 @@ function parse(code) {
return elements;
}
async function recursiveReaddir(rpath, p, fileNames) {
p = p || '';
fileNames = (fileNames || []).slice();
const files = await fs.readdir(rpath);
for (const f of files) {
console.log(f);
const stat = await fs.lstat(path.join(rpath, f));
if (stat.isDirectory() || stat.isSymbolicLink()) {
fileNames.push(...await recursiveReaddir(path.join(rpath, f), p + f + '/', fileNames));
} else {
fileNames.push(p + f);
}
}
return fileNames;
}
(async () => {
let timeParsing = 0;
let timeTotal = 0;
@ -187,68 +204,65 @@ function parse(code) {
let errored = [];
let errorReason = {};
const files = await fs.readdir(declarationsPath);
const files = await recursiveReaddir(declarationsPath);
const filesAmt = files.length;
let i = 0;
for (const f of files) {
i++;
const filePath = path.resolve(declarationsPath, f);
const stat = await fs.lstat(filePath);
if (stat.isFile()) {
console.log(` ${chalk.cyanBright(f)}`);
const file = await fs.readFile(filePath, 'utf8');
console.log(` ${chalk.cyanBright(f)}`);
const file = await fs.readFile(filePath, 'utf8');
let startParse = Date.now();
parsedElements = 0;
const parsed = parse(file);
console.log(` parsed ${chalk.magentaBright(parsedElements)} objects from ${chalk.cyanBright(f)}`);
timeParsing += Date.now() - startParse;
let startParse = Date.now();
parsedElements = 0;
const parsed = parse(file);
console.log(` 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')}`);
continue;
}
if (parsed.length > 10) {
console.log(`${chalk.redBright('!')} over 10 top-level objects were parsed - this may be a bad idea`);
if (parsed.length === 0) {
console.log(`${chalk.redBright('!')} 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`);
}
let transpiled;
try {
transpiled = parsed.map(p => transpile(p)).join('\n').trim();
errored = errored.filter(e => e !== f);
} catch(err) {
errorReason[f] = err;
if (errored.length > files.length - i) {
console.error(`${chalk.redBright('!')} failed resolving extends!`);
console.error(`${chalk.redBright('!')} abandoned ${chalk.magentaBright(errored.length)} files:`);
let longest = errored.reduce((a, b) => Math.max(a.length || a || 0, b.length));
errored.forEach(e => console.error(` - ${chalk.cyanBright(e)}${' '.repeat(longest - e.length + 2)}${chalk.gray(' → ' + errorReason[e].message.replace('Class not found in class storage: ', ''))}`));
break;
}
let transpiled;
try {
transpiled = parsed.map(p => transpile(p)).join('\n').trim();
errored = errored.filter(e => e !== f);
} catch(err) {
errorReason[f] = err;
if (errored.length > files.length - i) {
console.error(`${chalk.redBright('!')} failed resolving extends!`);
console.error(`${chalk.redBright('!')} abandoned ${chalk.magentaBright(errored.length)} files:`);
let longest = errored.reduce((a, b) => Math.max(a.length || a || 0, b.length));
errored.forEach(e => console.error(` - ${chalk.cyanBright(e)}${' '.repeat(longest - e.length + 2)}${chalk.gray(' -> ' + errorReason[e].message.replace('Class not found in class storage: ', ''))}`));
break;
}
console.error(`${chalk.yellowBright('!')} ${err}`);
console.error(`${chalk.yellowBright('!')} pushing to end of queue`);
files.push(f);
errored.push(f);
console.error(`${chalk.yellowBright('!')} ${err}`);
console.error(`${chalk.yellowBright('!')} pushing to end of queue`);
files.push(f);
errored.push(f);
timeTotal += Date.now() - startParse;
continue;
}
console.log(` 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')}`);
continue;
}
//console.log(inspect(parsed, false, 10));
const luaFilename = f.replace('.d.ts', '.lua');
await fs.writeFile(path.resolve(outPath, luaFilename), transpiled);
console.log(` wrote ${chalk.cyanBright(luaFilename)}`);
continue;
}
console.log(` 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')}`);
continue;
}
//console.log(inspect(parsed, false, 10));
const luaFilename = f.replace('.d.ts', '.lua').split('/').pop();
await fs.writeFile(path.resolve(outPath, luaFilename), transpiled);
console.log(` wrote ${chalk.cyanBright(luaFilename)}`);
}
console.log(`\nfinished transpiling ${chalk.magentaBright(filesAmt)} ${chalk.gray(`+ ${files.length - filesAmt}`)} files in ${chalk.magentaBright(timeTotal + 'ms')} ${chalk.gray(`(${Math.floor(timeParsing/timeTotal * 1000 + 0.5) / 10}% spent parsing)`)}`);