Skip to content

Commit 34500ab

Browse files
authored
Merge pull request #438 from jansule/quiet-mode
Quiet mode
2 parents c6d0935 + adc869f commit 34500ab

File tree

4 files changed

+52
-15
lines changed

4 files changed

+52
-15
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ geostyler-cli -s qml -t sld -o point_simple.sld testdata/point_simple.qml
8585
To convert all files in a directory:
8686

8787
```bash
88-
geostyler-cli [options] -t qgis -o /path/to/output /path/to/input/
88+
geostyler-cli [options] -t qgis -o /path/to/output /path/to/input/ [--quiet]
8989
# example
9090
geostyler-cli -s sld -t qgis -o ./output-sld testdata/sld
9191
```
@@ -117,6 +117,7 @@ If not given, it will be guessed from the extension of the output file.
117117
Mapfiles are not currently supported as target.
118118
Mandatory if the the target is a directory.
119119
* `-v` / `--version` Display the version of the program.
120+
* `--quiet` Suppress all interactive output.
120121

121122
## Developing
122123

src/index.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -162,14 +162,11 @@ function handleResult(result: ReadStyleResult | WriteStyleResult, parser: StyleP
162162
throw errors;
163163
}
164164
if (warnings) {
165-
// eslint-disable-next-line no-console
166-
warnings.map(console.warn);
165+
warnings.map((warning) => process.stderr.write(warning));
167166
}
168167
if (unsupportedProperties) {
169-
// eslint-disable-next-line no-console
170-
console.log(`${stage} parser ${parser.title} does not support the following properties:`);
171-
// eslint-disable-next-line no-console
172-
console.log(unsupportedProperties);
168+
process.stderr.write(`${stage} parser ${parser.title} does not support the following properties:\n`);
169+
process.stderr.write(JSON.stringify(unsupportedProperties));
173170
}
174171
return output;
175172
}
@@ -224,8 +221,7 @@ async function writeFile(
224221
indicator.succeed(`File "${sourceFile}" translated successfully. Output written to ${targetFile}`);
225222
} else {
226223
indicator.succeed(`File "${sourceFile}" translated successfully. Output written to stdout:\n`);
227-
// eslint-disable-next-line no-console
228-
console.log(finalOutput);
224+
process.stdout.write(finalOutput);
229225
}
230226
return 0;
231227
} catch (error) {
@@ -248,7 +244,8 @@ async function main() {
248244
help,
249245
v,
250246
version,
251-
_: unnamedArgs
247+
quiet,
248+
_: unnamedArgs,
252249
} = args;
253250

254251
if (h || help) {
@@ -268,7 +265,10 @@ async function main() {
268265
const outputPath: string = o || output;
269266

270267
// Instantiate progress indicator
271-
const indicator = ora({ text: 'Starting Geostyler CLI' }).start();
268+
const indicator = ora({
269+
text: 'Starting Geostyler CLI',
270+
isSilent: !!quiet || false,
271+
}).start();
272272

273273
// Check source path arg.
274274
if (!sourcePath) {

src/logHelper.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import gradient from 'gradient-string';
22
import version from './version';
33

4-
export const logTitle = () :void => {
4+
export const logTitle = () => {
55
console.log(gradient('#611E82', '#272C82', '#00943D', '#FFED00', '#F48E00', '#E7000E').multiline(`
66
██████╗ ███████╗ ██████╗ ███████╗████████╗██╗ ██╗██╗ ███████╗██████╗
77
██╔════╝ ██╔════╝██╔═══██╗██╔════╝╚══██╔══╝╚██╗ ██╔╝██║ ██╔════╝██╔══██╗
@@ -16,8 +16,8 @@ export const logHelp = () :void => {
1616
logTitle();
1717
console.log(`
1818
Basic syntax:
19-
npx geostyler-cli [options] [input_file | [input_directory]]
20-
geostyler-cli [options] [input_file | input_directory]
19+
npx geostyler-cli [options] [input_file | [input_directory]] [--quiet]
20+
geostyler-cli [options] [input_file | input_directory] [--quiet]
2121
2222
Example:
2323
npx geostyler-cli -s sld -t qgis -o output.qml [YOUR_SLD.sld]
@@ -40,9 +40,10 @@ export const logHelp = () :void => {
4040
Mandatory if the the target is a directory.
4141
--from-stdin : Read input from stdin. If this option is set, the input file is ignored.
4242
-v / --version: Display the version of the program.
43+
--quiet : Suppress all interactive output.
4344
`);
4445
};
4546

46-
export const logVersion = () : void => {
47+
export const logVersion = () => {
4748
console.log(`v${version}`);
4849
};

test.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,13 @@ function runTest(args, outputFile) {
1919
console.log(`Status: ${result.status.toString()}`);
2020
console.log(`Output: ${result.stdout.toString()}`);
2121
console.log(`Error: ${result.stderr.toString()}`);
22+
return result;
2223
}
2324

2425
function runAllTests() {
2526

2627
let success = true;
28+
let testResult;
2729

2830
// test sld to qgis
2931
let outputFile = 'output.qml';
@@ -82,6 +84,39 @@ function runAllTests() {
8284
success = false;
8385
}
8486

87+
// test writing only styles to stdout
88+
args = ['start', '--', '-s', 'sld', '-t', 'sld', 'testdata/sld/point_simplepoint.sld'];
89+
testResult = runTest(args, outputFile);
90+
91+
const stdout = testResult.stdout.toString();
92+
// We have to remove the first 4 lines of the output
93+
// since we are running the tests via npm test which
94+
// adds the command itself to stdout.
95+
const cleanedStdout = stdout.split('\n').slice(4).join('\n');
96+
console.log(`stdout: ${cleanedStdout}`);
97+
if (!cleanedStdout.startsWith('<?xml version="1.0" encoding="UTF-8" standalone="yes"?>')) {
98+
console.log('Expected SLD output not found in stdout');
99+
success = false;
100+
}
101+
102+
// test writing everything else to stderr
103+
args = ['start', '--', '-s', 'sld', '-t', 'sld', 'testdata/sld/point_simplepoint.sld'];
104+
testResult = runTest(args, outputFile);
105+
106+
if (!testResult.stderr.toString().includes('translated successfully')) {
107+
console.log('Expected translation success message not found in stderr');
108+
success = false;
109+
}
110+
111+
// test not writing interactive messages in quiet mode
112+
args = ['start', '--', '-s', 'sld', '-t', 'sld', 'testdata/sld/point_simplepoint.sld', '--quiet'];
113+
testResult = runTest(args, outputFile);
114+
115+
if (testResult.stderr.toString().includes('translated successfully')) {
116+
console.log('Expected no interactive messages in quiet mode');
117+
success = false;
118+
}
119+
85120
return success;
86121
}
87122

0 commit comments

Comments
 (0)