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 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; 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');