From d959dbe7a99d093652f743b1bc56dc887776da8b Mon Sep 17 00:00:00 2001 From: Alexander Wunschik Date: Mon, 6 Dec 2021 11:32:00 +0100 Subject: [PATCH 1/3] chore: "strict" is unnecessary inside of modules --- index.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/index.js b/index.js index 94ab165..a9612f4 100644 --- a/index.js +++ b/index.js @@ -1,3 +1 @@ -'use strict'; - -module.exports = require('./app'); \ No newline at end of file +module.exports = require('./app'); From 18cd1e114c1731de696ddd816e6a97d1285d5d88 Mon Sep 17 00:00:00 2001 From: Alexander Wunschik Date: Mon, 6 Dec 2021 11:58:58 +0100 Subject: [PATCH 2/3] refactor: move arg filtering to seperate async function --- app/cmd.js | 11 +++++++---- app/globFiles.js | 11 +++++++++++ 2 files changed, 18 insertions(+), 4 deletions(-) create mode 100644 app/globFiles.js diff --git a/app/cmd.js b/app/cmd.js index d4c9d59..a815491 100755 --- a/app/cmd.js +++ b/app/cmd.js @@ -6,6 +6,7 @@ const path = require('path'); // NPM dependencies const minimist = require('minimist'); const sortJson = require('./'); +const globFiles = require('./globFiles'); const alias = { depth: ['d'], @@ -15,9 +16,11 @@ const alias = { noFinalNewLine: ['no-final-newline', 'nn'], }; -const argv = minimist(process.argv.slice(2), { alias }); +(async () => { + const argv = minimist(process.argv.slice(2), { alias }); -// Get all the files -const files = argv._.filter(arg => arg.endsWith('.json') || arg.endsWith('.rc')); + // Get all the files + const files = await globFiles(argv._); -sortJson.overwrite(files.map(file => path.resolve(file)), argv); + sortJson.overwrite(files.map(file => path.resolve(file)), argv); +})(); diff --git a/app/globFiles.js b/app/globFiles.js new file mode 100644 index 0000000..3d3c901 --- /dev/null +++ b/app/globFiles.js @@ -0,0 +1,11 @@ +/** + * Takes in a list of file-paths and returns a list of filtered file-paths. + * @param {String|Array} args - String: path to json file to sort and overwrite + * - Array: paths to json files to sort and overwrite + * @returns {String[]} - List of all json files that are included in the args + */ +async function globFiles(args) { + return args.filter(arg => arg.endsWith('.json') || arg.endsWith('.rc')); +} + +module.exports = globFiles; From 07ee4bcf12066d3e39bb73aac6e1f11ae71cb6e0 Mon Sep 17 00:00:00 2001 From: Alexander Wunschik Date: Mon, 6 Dec 2021 12:00:23 +0100 Subject: [PATCH 3/3] docs: add files section to README --- README.md | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 3409a3f..00e0e2d 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ It takes a JSON file and returns a copy of the same file, but with the sorted ke ## Installation -` [sudo] npm -g install sort-json` +`[sudo] npm -g install sort-json` ## Usage @@ -24,13 +24,13 @@ sortJson.overwrite(['some/absolute/path1.json', 'some/absolute/path2.json'], opt ## CLI usage -`sort-json filename [options]` +`sort-json [options] [filenames]` Sorts and overwrites .json or .rc files. _Example_ -`sort-json test.json --ignore-case` +`sort-json --ignore-case test.json` - **Options** +### Options `--ignore-case, -i`\ Ignore case when sorting. @@ -49,6 +49,12 @@ Use a number greater then 0 for the _SIZE_ value. `--no-final-newline, -nn`\ No final new line will be added to the end of the file. +### Files + +List of json files that should be sorted. +The paths can be absolute or relative to the current directory. + +Filenames must end with `.json` or `.rc`. All other files are ignored. ## Upgrade to version 2.x