prettier logs

This commit is contained in:
Jill 2021-11-03 11:37:52 +03:00
parent ff84493fac
commit c0b1105ee4
1 changed files with 15 additions and 2 deletions

View File

@ -93,6 +93,8 @@ function removeComments(code) {
return code; return code;
} }
let parsedElements = 0;
/** /**
* @param {string} code * @param {string} code
*/ */
@ -113,6 +115,7 @@ function parse(code) {
let interfaceFreeCode = code; let interfaceFreeCode = code;
const interface = /interface (\w+)(?: extends (\w+))? ?{([^}]*)}/g; const interface = /interface (\w+)(?: extends (\w+))? ?{([^}]*)}/g;
while ((match = interface.exec(code)) !== null) { while ((match = interface.exec(code)) !== null) {
parsedElements++;
interfaceFreeCode = interfaceFreeCode.replace(match[0], ''); interfaceFreeCode = interfaceFreeCode.replace(match[0], '');
elements.push(['interface', {name: match[1], extends: match[2], contents: parse(match[3])}]); elements.push(['interface', {name: match[1], extends: match[2], contents: parse(match[3])}]);
} }
@ -122,6 +125,7 @@ function parse(code) {
let namespaceFreeCode = code; let namespaceFreeCode = code;
const namespace = /namespace (\w+) ?{([^}]*)}/g; const namespace = /namespace (\w+) ?{([^}]*)}/g;
while ((match = namespace.exec(code)) !== null) { while ((match = namespace.exec(code)) !== null) {
parsedElements++;
namespaceFreeCode = namespaceFreeCode.replace(match[0], ''); namespaceFreeCode = namespaceFreeCode.replace(match[0], '');
elements.push(['namespace', {name: match[1], contents: parse(match[2])}]); elements.push(['namespace', {name: match[1], contents: parse(match[2])}]);
} }
@ -131,6 +135,7 @@ function parse(code) {
let functionFreeCode = code; let functionFreeCode = code;
const functions = /(function)?\s(\w+)\s*\(([^;]*)\)(\s*:\s*([\w\[\]\s|<>,]+))?/g; // wow this sucks const functions = /(function)?\s(\w+)\s*\(([^;]*)\)(\s*:\s*([\w\[\]\s|<>,]+))?/g; // wow this sucks
while ((match = functions.exec(code)) !== null) { while ((match = functions.exec(code)) !== null) {
parsedElements++;
functionFreeCode = functionFreeCode.replace(match[0], ''); functionFreeCode = functionFreeCode.replace(match[0], '');
let arguments = match[3].split(',').map(s => { let arguments = match[3].split(',').map(s => {
const split = s.split(':').map(s => s.trim()); const split = s.split(':').map(s => s.trim());
@ -144,6 +149,7 @@ function parse(code) {
let enumFreeCode = code; let enumFreeCode = code;
const enums = /enum (\w+) ?{([^}]*)}/g; const enums = /enum (\w+) ?{([^}]*)}/g;
while ((match = enums.exec(code)) !== null) { while ((match = enums.exec(code)) !== null) {
parsedElements++;
enumFreeCode = enumFreeCode.replace(match[0], ''); enumFreeCode = enumFreeCode.replace(match[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()}})}]); 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()}})}]);
} }
@ -153,6 +159,7 @@ function parse(code) {
let constFreeCode = code; let constFreeCode = code;
const consts = /(\w+)\s*:\s*(\w+)/g; const consts = /(\w+)\s*:\s*(\w+)/g;
while ((match = consts.exec(code)) !== null) { while ((match = consts.exec(code)) !== null) {
parsedElements++;
constFreeCode = constFreeCode.replace(match[0], ''); constFreeCode = constFreeCode.replace(match[0], '');
elements.push(['const', {name: match[1], type: match[2]}]); elements.push(['const', {name: match[1], type: match[2]}]);
} }
@ -181,6 +188,7 @@ function parse(code) {
let errorReason = {}; let errorReason = {};
const files = await fs.readdir(declarationsPath); const files = await fs.readdir(declarationsPath);
const filesAmt = files.length;
let i = 0; let i = 0;
for (const f of files) { for (const f of files) {
i++; i++;
@ -188,17 +196,22 @@ function parse(code) {
const stat = await fs.lstat(filePath); const stat = await fs.lstat(filePath);
if (stat.isFile()) { if (stat.isFile()) {
console.log(` ${chalk.cyanBright(f)}`);
const file = await fs.readFile(filePath, 'utf8'); const file = await fs.readFile(filePath, 'utf8');
let startParse = Date.now(); let startParse = Date.now();
parsedElements = 0;
const parsed = parse(file); const parsed = parse(file);
console.log(` parsed ${chalk.magentaBright(parsed.length)} elements from ${chalk.cyanBright(f)}`); console.log(` 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')}`); console.log(`${chalk.redBright('!')} no elements parsed, ${chalk.gray('not writing anything')}`);
continue; continue;
} }
if (parsed.length > 10) {
console.log(`${chalk.redBright('!')} over 10 top-level objects were parsed - this may be a bad idea`);
}
let transpiled; let transpiled;
try { try {
@ -238,6 +251,6 @@ function parse(code) {
} }
} }
console.log(`\nfinished transpiling in ${chalk.magentaBright(timeTotal + 'ms')} ${chalk.gray(`(${Math.floor(timeParsing/timeTotal * 1000 + 0.5) / 10}% spent parsing)`)}`); 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)}`); console.log(`check ${chalk.cyanBright(outPath)}`);
})(); })();