Skip to content

Commit

Permalink
Add quiet mode to remove logging
Browse files Browse the repository at this point in the history
  • Loading branch information
bidoubiwa committed Aug 20, 2021
1 parent dad9fd4 commit d2a45c5
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 29 deletions.
10 changes: 4 additions & 6 deletions README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -63,25 +63,23 @@ and options:
- Output file _optional_ : path to the output file (default: `combined.json`).
- Validate Input JSON's: All input JSON files are validated to be a correct [JSON](https://www.json.org/json-en.html) (default: `true`).
- Validate Output JSON's: Validate if the outputed JSON is a correct [JSON](https://www.json.org/json-en.html) (default: `false`).
- Quiet mode : no logs are outputed (default: `false`)

It accepts relative path but also fully qualified paths.

### CLI usage:

Usage:
```bash
cli [options] <input-directory>
turbo-json [options] <input-directory>
```

Options:
```
-o, --output-file <file-name> File name in which all the json files will be merged (default: "combined.json")
-I, --validate-input <file-name> Check if input JSON files are valid (default: true)
-O, --validate-output <file-name> Check if output JSON is a valid JSON (default: false)
```

```bash
turbo-json <input-directory> --output-file <file-path> (default: "combined.json")
-q, --quiet Quiet mode, no logs are outputed (default: false)
```

**Example**
Expand All @@ -96,7 +94,7 @@ turbo-json /data -o combined_data.json
const { combineJson } = require('./src');

(async () => {
await combineJson('misc', { outputFile: 'combined_data.json', validateInput: true, validateOutput: false });
await combineJson('misc', { outputFile: 'combined_data.json', validateInput: true, validateOutput: false, quiet: false });
})();
```

Expand Down
29 changes: 18 additions & 11 deletions __tests__/combine.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ test('Tests on 1 empty file', async () => {
const res = await combineJson('misc/one_empty', {
outputFile: 'test-output/combine_empty.json',
validateInput: true,
quiet: true,
})

const data = JSON.parse(
Expand All @@ -28,6 +29,7 @@ test('Tests on 1 empty file', async () => {
const res = await combineJson('misc/one_empty', {
outputFile: 'test-output/combine_empty.json',
validateInput: false,
quiet: true,
})

const data = JSON.parse(
Expand All @@ -42,6 +44,7 @@ test('Tests on multiple empty files', async () => {
const res = await combineJson('misc/multiple_empty', {
outputFile: 'test-output/combine_multiple_empty.json',
validateInput: true,
quiet: true,
})
const data = JSON.parse(
fs.readFileSync(
Expand All @@ -58,6 +61,7 @@ test('Tests on some empty files and some valid', async () => {
const res = await combineJson('misc/some_empty', {
outputFile: 'test-output/combine_some_empty.json',
validateInput: true,
quiet: true,
})
const data = JSON.parse(
fs.readFileSync(
Expand All @@ -78,6 +82,7 @@ test('Tests on some invalid files and some valid', async () => {
await combineJson('misc/some_non_valid', {
outputFile: 'test-output/combine_some_non_valid.json',
validateInput: false,
quiet: true,
})

expect(() =>
Expand All @@ -94,6 +99,7 @@ test('Tests on some invalid files and some valid with no validation', async () =
await combineJson('misc/some_non_valid', {
outputFile: 'test-output/combine_some_non_valid.json',
validateInput: false,
quiet: true,
})

expect(() =>
Expand All @@ -107,26 +113,23 @@ test('Tests on some invalid files and some valid with no validation', async () =
})

test('Tests on some invalid files and some valid with no validation, but validate output file', async () => {
await combineJson('misc/some_non_valid', {
outputFile: 'test-output/combine_some_non_valid.json',
const res = await combineJson('misc/some_non_valid', {
outputFile: 'test-output/failed.json',
validateInput: false,
validateOutput: true,
})
quiet: true,
}).catch(e => e)

expect(() =>
JSON.parse(
fs.readFileSync(
`${process.cwd()}/test-output/combine_some_non_valid.json`,
'utf-8'
)
)
).toThrow()
expect(res.error).toBe(
'ERROR at 2 (2, 1): Verifier cannot parse input: expected a value'
)
})

test('Tests on some empty files and some valid, validity check disabled', async () => {
const res = await combineJson('misc/some_empty', {
outputFile: 'test-output/combine_some_empty.json',
validateInput: false,
quiet: true,
})
const data = JSON.parse(
fs.readFileSync(
Expand All @@ -147,6 +150,7 @@ test('Tests on some empty files and some valid, validity check disabled', async
test('Tests if on 1 file containing one primitive', async () => {
const res = await combineJson('misc/one_primitive', {
outputFile: 'test-output/combine_a_single_primitive.json',
quiet: true,
})
const data = JSON.parse(
fs.readFileSync(
Expand All @@ -162,6 +166,7 @@ test('Tests if on 1 file containing one primitive', async () => {
test('Tests if on 1 files', async () => {
const res = await combineJson('misc/one_file', {
outputFile: 'test-output/combine_one.json',
quiet: true,
})
const data = JSON.parse(
fs.readFileSync(`${process.cwd()}/test-output/combine_one.json`, 'utf-8')
Expand All @@ -174,6 +179,7 @@ test('Tests if on 1 files', async () => {
test('Tests on 3 array', async () => {
const res = await combineJson('misc/array_test', {
outputFile: 'test-output/combine_array.json',
quiet: true,
})
const data = JSON.parse(
fs.readFileSync(`${process.cwd()}/test-output/combine_array.json`, 'utf-8')
Expand All @@ -192,6 +198,7 @@ test('Tests on 3 array', async () => {
test('Tests if on all files', async () => {
const res = await combineJson('misc', {
outputFile: 'test-output/combine_all.json',
quiet: true,
})
const data = JSON.parse(
fs.readFileSync(`${process.cwd()}/test-output/combine_all.json`, 'utf-8')
Expand Down
10 changes: 4 additions & 6 deletions src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const combineJson = require('./combine-json')
const program = new commander.Command()

program
.name('turbo-json')
.argument('<input-directory>', 'Directory from witch to fetch the json files')
.version(pkg.version)
.option(
Expand All @@ -17,16 +18,13 @@ program
'File name in which all the json files will be merged',
'combined.json'
)
.option('-I, --validate-input', 'Check if JSON file is valid', false)
.option(
'-I, --validate-input <file-name>',
'Check if JSON file is valid',
false
)
.option(
'-O, --validate-output <file-name>',
'-O, --validate-output',
'Check if output JSON is a valid JSON',
false
)
.option('-q, --quiet', 'Quiet mode, no logs are outputed', true)
.action(async (directory, options) => {
await combineJson(directory, options)
})
Expand Down
18 changes: 12 additions & 6 deletions src/combine-json.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ async function combine({
outputFilePath,
validateInput,
validateOutput,
quiet,
}) {
createOutputArrayFile(outputFilePath)
const numberOfFiles = inputFiles.length
Expand Down Expand Up @@ -89,15 +90,17 @@ async function combine({
})
})

logSuccessfullWrite(fileName, index, numberOfFiles)
if (!quiet) logSuccessfullWrite(fileName, index, numberOfFiles)
} catch (e) {
if (e.file) {
logFailedValidityCheck(e)
if (!quiet) logFailedValidityCheck(e)
} else {
logFailedValidityCheck({
file: inputFiles[index],
error: e.message,
})
if (!quiet) {
logFailedValidityCheck({
file: inputFiles[index],
error: e.message,
})
}
}
}
}
Expand All @@ -116,6 +119,7 @@ async function combine({
await verifyJson({ jsonFile: outputFilePath })
} catch (e) {
logFailedOutputValidityCheck(e)
throw e
}
}
return 1
Expand All @@ -127,6 +131,7 @@ async function combineJson(
outputFile = 'combined.json',
validateInput = false,
validateOutput = false,
quiet = false,
}
) {
const { inputDirPath, filesName } = inputFilesAndDir({ inputDir })
Expand All @@ -138,6 +143,7 @@ async function combineJson(
outputFilePath,
validateInput,
validateOutput,
quiet,
})
}

Expand Down

0 comments on commit d2a45c5

Please sign in to comment.