Skip to content

Commit

Permalink
feat: support --check flag (#86)
Browse files Browse the repository at this point in the history
* feat: `--check` flag

* docs: update readme

* test: add tests

* docs: improve docs

* test: more test

* chore: remove `./`
  • Loading branch information
fisker authored and keithamus committed Dec 3, 2019
1 parent 092b9d7 commit c93f739
Show file tree
Hide file tree
Showing 8 changed files with 156 additions and 12 deletions.
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,23 @@ $ sort-package-json packages/*/package.json
$ sort-package-json my-package/package.json other-package/package.json
```

#### `--check` flag

When you want to check if your files are sorted, you can run CLI with the `--check` flag (or `-c`). This will output a list of not sorted files, if any.

```bash
$ sort-package-json **/package.json --check
# all files are sorted.


$ sort-package-json **/package.json --check
# foo/package.json
# bar/package.json

# 2 files are not sorted.
```


### Run (with [email protected]+)

```bash
Expand Down
4 changes: 4 additions & 0 deletions fixtures/another-not-sorted/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"version": "0.0.0-development",
"name": "sort-package-json"
}
4 changes: 4 additions & 0 deletions fixtures/not-sorted-1/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"version": "0.0.0-development",
"name": "sort-package-json"
}
4 changes: 4 additions & 0 deletions fixtures/not-sorted-2/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"version": "0.0.0-development",
"name": "sort-package-json"
}
4 changes: 4 additions & 0 deletions fixtures/sorted-1/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "sort-package-json",
"version": "0.0.0-development"
}
4 changes: 4 additions & 0 deletions fixtures/sorted-2/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "sort-package-json",
"version": "0.0.0-development"
}
57 changes: 45 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,20 +216,53 @@ module.exports.sortOrder = sortOrder;

if (require.main === module) {
const fs = require('fs');
const isCheckFlag = argument => argument === '--check' || argument === '-c';

const paths = process.argv[2]
? process.argv.slice(2)
: [`${process.cwd()}/package.json`];
const cliArguments = process.argv.slice(2);
const isCheck = cliArguments.some(isCheckFlag);

paths.forEach(path => {
const filesToProcess = glob.sync(path);
filesToProcess.forEach(filePath => {
const packageJson = fs.readFileSync(filePath, 'utf8');
const sorted = sortPackageJson(packageJson);
if (sorted !== packageJson) {
fs.writeFileSync(filePath, sorted, 'utf8');
console.log(`${filePath} is sorted!`);
const patterns = cliArguments.filter(argument => !isCheckFlag(argument));

if (!patterns.length) {
patterns[0] = 'package.json';
}

const files = patterns.reduce(
(files, pattern) => files.concat(glob.sync(pattern)),
[]
);

let notSortedFiles = 0;

files.forEach(file => {
const packageJson = fs.readFileSync(file, 'utf8');
const sorted = sortPackageJson(packageJson);

if (sorted !== packageJson) {
if (isCheck) {
notSortedFiles ++;
console.log(file);
} else {
fs.writeFileSync(file, sorted, 'utf8');
console.log(`${file} is sorted!`);
}
});
}
});

if (isCheck) {
if (notSortedFiles) {
console.log(
notSortedFiles === 1 ?
`${notSortedFiles} file is not sorted.`:
`\n ${notSortedFiles} files are not sorted.`
);
} else {
console.log(
files.length === 1 ?
`file is sorted.`:
`all files are sorted.`
);
}
process.exit(notSortedFiles)
}
}
74 changes: 74 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const assert = require('assert');
const fs = require('fs');
const newline = require('newline');
const sortPackageJson = require('./');
const { execFile } = require('child_process');

fs.readFile('./package.json', 'utf8', (error, contents) => {
if (error) {
Expand Down Expand Up @@ -117,3 +118,76 @@ fs.readFile('./package.json', 'utf8', (error, contents) => {
);
});


// CLI `--check` flag tests

// make sure `--check` not fixing file
// support `-c` as well
const orignal = fs.readFileSync('fixtures/not-sorted-1/package.json', 'utf8');
execFile(
'node',
['index.js', 'fixtures/not-sorted-1/package.json', '-c'],
(error, stdout, stderr) => {
assert.notEqual(
orignal,
sortPackageJson(orignal),
'fixtures/not-sorted-1/package.json should be a unsorted file.'
);
assert.equal(
orignal,
fs.readFileSync('fixtures/not-sorted-1/package.json', 'utf8'),
'file should not fixed when --check is enabled.'
);
assert.equal(error.code, 1, 'error.code should equals to unsorted file length');
assert.equal(stderr, '');
assert.equal(stdout.trim(), 'fixtures/not-sorted-1/package.json\n1 file is not sorted.');
}
);


execFile(
'node',
['index.js', 'fixtures/not-sorted-*/package.json','--check'],
(error, stdout, stderr) => {
assert.equal(error.code, 2);
assert.equal(stderr, '');
assert.equal(stdout.includes('fixtures/not-sorted-1/package.json'), true);
assert.equal(stdout.includes('fixtures/not-sorted-2/package.json'), true);
assert.equal(stdout.includes('2 files are not sorted.'), true);
}
);

execFile(
'node',
['index.js', 'fixtures/sorted-1/package.json','--check'],
(error, stdout, stderr) => {
assert.equal(error, null);
assert.equal(stderr, '');
assert.equal(stdout.trim(), 'file is sorted.');
}
);

execFile(
'node',
['index.js', 'fixtures/sorted-*/package.json','--check'],
(error, stdout, stderr) => {
assert.equal(error, null);
assert.equal(stderr, '');
assert.equal(stdout.trim(), 'all files are sorted.');
}
);

execFile(
'node',
['index.js', 'fixtures/*/package.json','--check'],
(error, stdout, stderr) => {
assert.equal(error.code, 3);
assert.equal(stderr, '');
assert.equal(stdout.includes('fixtures/sorted-1/package.json'), false);
assert.equal(stdout.includes('fixtures/sorted-2/package.json'), false);
assert.equal(stdout.includes('fixtures/not-sorted-1/package.json'), true);
assert.equal(stdout.includes('fixtures/not-sorted-2/package.json'), true);
assert.equal(stdout.includes('fixtures/another-not-sorted/package.json'), true);
assert.equal(stdout.includes('3 files are not sorted.'), true);
}
);

0 comments on commit c93f739

Please sign in to comment.