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

Added --silent flag to silence console logging. #60

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
3 changes: 2 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ const packageJson = require("../package.json");
sade("react-scanner", true)
.version(packageJson.version)
.describe(packageJson.description)
.option("-s, --silent", "Silence logging")
.option("-c, --config", "Path to config file")
.example("-c /path/to/react-scanner.config.js")
.action((options) => {
const configPath = path.resolve(process.cwd(), options.config);
const configDir = path.dirname(configPath);
const config = require(configPath);
run(config, configDir, "cli");
run(config, configDir, "cli", options.silent);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd probably convert the params to an object at this point. What do you think?

})
.parse(process.argv);
25 changes: 25 additions & 0 deletions src/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,4 +209,29 @@ Index("no files found", async () => {
}
});

Index("silent - no error", async () => {
const { exitCode, stdout } = await execa("./bin/react-scanner", [
"-c",
"./test/configs/noProcessors.config.js",
"--silent",
]);
const { firstLine, restOutput } = parseStdout(stdout);
assert.is(exitCode, 0);
assert.ok(/^$/.test(firstLine));
assert.ok(/^$/, restOutput);
});

Index("silent - error", async () => {
try {
await execa("./bin/react-scanner", [
"-c",
"./test/configs/noFilesFound.config.js",
"--silent",
]);
} catch ({ exitCode, stderr }) {
assert.is(exitCode, 1);
assert.is(stderr, "");
}
});

Index.run();
25 changes: 16 additions & 9 deletions src/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ async function run({
crawlFrom,
startTime,
method = "cli",
silent,
}) {
const rootDir = config.rootDir || configDir;
const globs = config.globs || DEFAULT_GLOBS;
Expand All @@ -30,7 +31,9 @@ async function run({
.sync();

if (files.length === 0) {
console.error(`No files found to scan.`);
if (!silent) {
console.error(`No files found to scan.`);
}
process.exit(1);
}

Expand Down Expand Up @@ -61,12 +64,14 @@ async function run({

const endTime = process.hrtime.bigint();

// eslint-disable-next-line no-console
console.log(
`Scanned ${pluralize(files.length, "file")} in ${
Number(endTime - startTime) / 1e9
} seconds`
);
if (!silent) {
// eslint-disable-next-line no-console
console.log(
`Scanned ${pluralize(files.length, "file")} in ${
Number(endTime - startTime) / 1e9
} seconds`
);
}

const processors =
config.processors && config.processors.length > 0
Expand All @@ -82,8 +87,10 @@ async function run({

switch (dest) {
case "stdout": {
// eslint-disable-next-line no-console
console.log(dataStr);
if (!silent) {
// eslint-disable-next-line no-console
console.log(dataStr);
}
break;
}
case "return": {
Expand Down
13 changes: 8 additions & 5 deletions src/scanner.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const { validateConfig } = require("./utils");
const runScan = require("./run");

const scanner = {
run: async function run(config, configDir, method = "programmatic") {
run: async function run(config, configDir, method = "programmatic", silent) {
const { crawlFrom, errors } = validateConfig(config, configDir);

if (errors.length === 0) {
Expand All @@ -14,13 +14,16 @@ const scanner = {
crawlFrom,
startTime,
method: method,
silent,
});
} else {
console.error(`Config errors:`);
if (!silent) {
console.error(`Config errors:`);

errors.forEach((error) => {
console.error(`- ${error}`);
});
errors.forEach((error) => {
console.error(`- ${error}`);
});
}

process.exit(1);
}
Expand Down