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

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,15 +204,13 @@ 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');
@ -223,7 +238,7 @@ function parse(code) {
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: ', ''))}`));
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;
}
@ -245,11 +260,10 @@ function parse(code) {
}
//console.log(inspect(parsed, false, 10));
const luaFilename = f.replace('.d.ts', '.lua');
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)`)}`);
console.log(`check ${chalk.cyanBright(outPath)}`);