chore: clean comment (#220)

* build: add clean comment script

* chore: clean comment to hide sensitive information
This commit is contained in:
Hayden
2023-11-28 21:28:20 +08:00
committed by GitHub
parent 31a811d1bb
commit 4fbb85dfba
37 changed files with 173 additions and 34 deletions

View File

@ -4,6 +4,7 @@ const fs = require('fs');
const argv = require('yargs-parser')(process.argv.slice(2));
const inquirer = require('inquirer');
const chalk = require('chalk');
const { cleanComment } = require('./cleanComment');
const grammarsPath = path.resolve(__dirname, '../src/grammar');
const outputPath = path.resolve(__dirname, '../src/lib');
@ -29,7 +30,8 @@ function compile(language) {
chalk.gray(err)
);
} else {
console.log(chalk.greenBright(`\nCompile ${language} succeeded!`));
cleanComment(language);
console.log(chalk.greenBright(`Compile ${language} succeeded!`));
}
});
}

37
scripts/cleanComment.js Normal file
View File

@ -0,0 +1,37 @@
const fs = require('fs');
const path = require('path');
const { globSync } = require('glob');
const chalk = require('chalk');
const basePath = path.resolve(__dirname, '../src/lib');
function processFile(filePath) {
try {
const content = fs.readFileSync(filePath, 'utf-8');
const firstLineIdx =
content.indexOf('\r\n') === -1 ? content.indexOf('\n') : content.indexOf('\r\n');
if (firstLineIdx === -1) return;
let firstLineContent = content.slice(0, firstLineIdx);
const restContent = content.slice(firstLineIdx);
const slices = firstLineContent.split('/src/grammar/');
if (slices.length !== 2) return;
firstLineContent = `// Generated from dt-sql-parser/src/grammar/` + slices[1];
fs.writeFileSync(filePath, firstLineContent + restContent, 'utf-8');
} catch (error) {
console.error(error);
}
}
function main(language) {
const base = basePath + (language ? `/${language}` : '');
console.info(chalk.green(`\nCleaning comment in:`, chalk.gray(`${base}/**/*.ts`)));
const filePaths = globSync(`${base}/**/*.ts`, { absolute: true, nodir: true });
filePaths.forEach(processFile);
}
module.exports = {
cleanComment: main,
};

View File

@ -0,0 +1,3 @@
const { cleanComment } = require('./cleanComment');
cleanComment();