diff --git a/README.MD b/README.MD index e93af75..771248c 100644 --- a/README.MD +++ b/README.MD @@ -63,6 +63,7 @@ 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. @@ -70,7 +71,7 @@ It accepts relative path but also fully qualified paths. Usage: ```bash -cli [options] +turbo-json [options] ``` Options: @@ -78,10 +79,7 @@ Options: -o, --output-file File name in which all the json files will be merged (default: "combined.json") -I, --validate-input Check if input JSON files are valid (default: true) -O, --validate-output Check if output JSON is a valid JSON (default: false) -``` - -```bash -turbo-json --output-file (default: "combined.json") + -q, --quiet Quiet mode, no logs are outputed (default: false) ``` **Example** @@ -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 }); })(); ``` diff --git a/__tests__/combine.tests.js b/__tests__/combine.tests.js index e578832..ca6e475 100644 --- a/__tests__/combine.tests.js +++ b/__tests__/combine.tests.js @@ -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( @@ -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( @@ -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( @@ -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( @@ -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(() => @@ -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(() => @@ -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( @@ -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( @@ -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') @@ -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') @@ -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') diff --git a/src/cli.js b/src/cli.js index 72c68d4..2581f12 100755 --- a/src/cli.js +++ b/src/cli.js @@ -6,6 +6,7 @@ const combineJson = require('./combine-json') const program = new commander.Command() program + .name('turbo-json') .argument('', 'Directory from witch to fetch the json files') .version(pkg.version) .option( @@ -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 ', - 'Check if JSON file is valid', - false - ) - .option( - '-O, --validate-output ', + '-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) }) diff --git a/src/combine-json.js b/src/combine-json.js index 4e67057..e6cea27 100755 --- a/src/combine-json.js +++ b/src/combine-json.js @@ -24,6 +24,7 @@ async function combine({ outputFilePath, validateInput, validateOutput, + quiet, }) { createOutputArrayFile(outputFilePath) const numberOfFiles = inputFiles.length @@ -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, + }) + } } } } @@ -116,6 +119,7 @@ async function combine({ await verifyJson({ jsonFile: outputFilePath }) } catch (e) { logFailedOutputValidityCheck(e) + throw e } } return 1 @@ -127,6 +131,7 @@ async function combineJson( outputFile = 'combined.json', validateInput = false, validateOutput = false, + quiet = false, } ) { const { inputDirPath, filesName } = inputFilesAndDir({ inputDir }) @@ -138,6 +143,7 @@ async function combineJson( outputFilePath, validateInput, validateOutput, + quiet, }) }