Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --silent flag to hide console output #14

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ yarn add -D tscpaths
| -p --project | project configuration file (tsconfig.json) |
| -s --src | source code root directory |
| -o --out | output directory of transpiled code (tsc --outDir) |
| --silent | silence the console output |

You need to provide -s (--src) and -o (--out), because it's hard to predict source and output paths based on tsconfig.json.

Expand Down
20 changes: 13 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ program
.option('-p, --project <file>', 'path to tsconfig.json')
.option('-s, --src <path>', 'source root path')
.option('-o, --out <path>', 'output root path')
.option('-v, --verbose', 'output logs');
.option('-v, --verbose', 'output logs')
.option('--silent', 'silence the console output');

program.on('--help', () => {
console.log(`
Expand All @@ -22,11 +23,12 @@ program.on('--help', () => {

program.parse(process.argv);

const { project, src, out, verbose } = program as {
const { project, src, out, verbose, silent } = program as {
project?: string;
src?: string;
out?: string;
verbose?: boolean;
silent?: boolean;
};

if (!project) {
Expand All @@ -36,6 +38,12 @@ if (!src) {
throw new Error('--src must be specified');
}

const log = (...args: any[]): void => {
if (!silent) {
console.log(...args);
}
};

const verboseLog = (...args: any[]): void => {
if (verbose) {
console.log(...args);
Expand All @@ -48,9 +56,7 @@ const srcRoot = resolve(src);

const outRoot = out && resolve(out);

console.log(
`tscpaths --project ${configFile} --src ${srcRoot} --out ${outRoot}`
);
log(`tscpaths --project ${configFile} --src ${srcRoot} --out ${outRoot}`);

const { baseUrl, outDir, paths } = loadConfig(configFile);

Expand Down Expand Up @@ -174,9 +180,9 @@ for (let i = 0; i < flen; i += 1) {
const newText = replaceAlias(text, file);
if (text !== newText) {
changedFileCount += 1;
console.log(`${file}: replaced ${replaceCount - prevReplaceCount} paths`);
log(`${file}: replaced ${replaceCount - prevReplaceCount} paths`);
writeFileSync(file, newText, 'utf8');
}
}

console.log(`Replaced ${replaceCount} paths in ${changedFileCount} files`);
log(`Replaced ${replaceCount} paths in ${changedFileCount} files`);