Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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.
Expand All @@ -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

Expand Down
11 changes: 7 additions & 4 deletions app/cmd.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
Expand All @@ -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);
})();
11 changes: 11 additions & 0 deletions app/globFiles.js
Original file line number Diff line number Diff line change
@@ -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;
4 changes: 1 addition & 3 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
'use strict';

module.exports = require('./app');
module.exports = require('./app');