Skip to content

Commit c93f739

Browse files
fiskerkeithamus
authored andcommittedDec 3, 2019
feat: support --check flag (#86)
* feat: `--check` flag * docs: update readme * test: add tests * docs: improve docs * test: more test * chore: remove `./`
1 parent 092b9d7 commit c93f739

File tree

8 files changed

+156
-12
lines changed

8 files changed

+156
-12
lines changed
 

‎README.md

+17
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,23 @@ $ sort-package-json packages/*/package.json
7272
$ sort-package-json my-package/package.json other-package/package.json
7373
```
7474

75+
#### `--check` flag
76+
77+
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.
78+
79+
```bash
80+
$ sort-package-json **/package.json --check
81+
# all files are sorted.
82+
83+
84+
$ sort-package-json **/package.json --check
85+
# foo/package.json
86+
# bar/package.json
87+
88+
# 2 files are not sorted.
89+
```
90+
91+
7592
### Run (with npm@5.2+)
7693

7794
```bash
+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"version": "0.0.0-development",
3+
"name": "sort-package-json"
4+
}

‎fixtures/not-sorted-1/package.json

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"version": "0.0.0-development",
3+
"name": "sort-package-json"
4+
}

‎fixtures/not-sorted-2/package.json

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"version": "0.0.0-development",
3+
"name": "sort-package-json"
4+
}

‎fixtures/sorted-1/package.json

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"name": "sort-package-json",
3+
"version": "0.0.0-development"
4+
}

‎fixtures/sorted-2/package.json

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"name": "sort-package-json",
3+
"version": "0.0.0-development"
4+
}

‎index.js

+45-12
Original file line numberDiff line numberDiff line change
@@ -216,20 +216,53 @@ module.exports.sortOrder = sortOrder;
216216

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

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

224-
paths.forEach(path => {
225-
const filesToProcess = glob.sync(path);
226-
filesToProcess.forEach(filePath => {
227-
const packageJson = fs.readFileSync(filePath, 'utf8');
228-
const sorted = sortPackageJson(packageJson);
229-
if (sorted !== packageJson) {
230-
fs.writeFileSync(filePath, sorted, 'utf8');
231-
console.log(`${filePath} is sorted!`);
224+
const patterns = cliArguments.filter(argument => !isCheckFlag(argument));
225+
226+
if (!patterns.length) {
227+
patterns[0] = 'package.json';
228+
}
229+
230+
const files = patterns.reduce(
231+
(files, pattern) => files.concat(glob.sync(pattern)),
232+
[]
233+
);
234+
235+
let notSortedFiles = 0;
236+
237+
files.forEach(file => {
238+
const packageJson = fs.readFileSync(file, 'utf8');
239+
const sorted = sortPackageJson(packageJson);
240+
241+
if (sorted !== packageJson) {
242+
if (isCheck) {
243+
notSortedFiles ++;
244+
console.log(file);
245+
} else {
246+
fs.writeFileSync(file, sorted, 'utf8');
247+
console.log(`${file} is sorted!`);
232248
}
233-
});
249+
}
234250
});
251+
252+
if (isCheck) {
253+
if (notSortedFiles) {
254+
console.log(
255+
notSortedFiles === 1 ?
256+
`${notSortedFiles} file is not sorted.`:
257+
`\n ${notSortedFiles} files are not sorted.`
258+
);
259+
} else {
260+
console.log(
261+
files.length === 1 ?
262+
`file is sorted.`:
263+
`all files are sorted.`
264+
);
265+
}
266+
process.exit(notSortedFiles)
267+
}
235268
}

‎test.js

+74
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const assert = require('assert');
22
const fs = require('fs');
33
const newline = require('newline');
44
const sortPackageJson = require('./');
5+
const { execFile } = require('child_process');
56

67
fs.readFile('./package.json', 'utf8', (error, contents) => {
78
if (error) {
@@ -117,3 +118,76 @@ fs.readFile('./package.json', 'utf8', (error, contents) => {
117118
);
118119
});
119120

121+
122+
// CLI `--check` flag tests
123+
124+
// make sure `--check` not fixing file
125+
// support `-c` as well
126+
const orignal = fs.readFileSync('fixtures/not-sorted-1/package.json', 'utf8');
127+
execFile(
128+
'node',
129+
['index.js', 'fixtures/not-sorted-1/package.json', '-c'],
130+
(error, stdout, stderr) => {
131+
assert.notEqual(
132+
orignal,
133+
sortPackageJson(orignal),
134+
'fixtures/not-sorted-1/package.json should be a unsorted file.'
135+
);
136+
assert.equal(
137+
orignal,
138+
fs.readFileSync('fixtures/not-sorted-1/package.json', 'utf8'),
139+
'file should not fixed when --check is enabled.'
140+
);
141+
assert.equal(error.code, 1, 'error.code should equals to unsorted file length');
142+
assert.equal(stderr, '');
143+
assert.equal(stdout.trim(), 'fixtures/not-sorted-1/package.json\n1 file is not sorted.');
144+
}
145+
);
146+
147+
148+
execFile(
149+
'node',
150+
['index.js', 'fixtures/not-sorted-*/package.json','--check'],
151+
(error, stdout, stderr) => {
152+
assert.equal(error.code, 2);
153+
assert.equal(stderr, '');
154+
assert.equal(stdout.includes('fixtures/not-sorted-1/package.json'), true);
155+
assert.equal(stdout.includes('fixtures/not-sorted-2/package.json'), true);
156+
assert.equal(stdout.includes('2 files are not sorted.'), true);
157+
}
158+
);
159+
160+
execFile(
161+
'node',
162+
['index.js', 'fixtures/sorted-1/package.json','--check'],
163+
(error, stdout, stderr) => {
164+
assert.equal(error, null);
165+
assert.equal(stderr, '');
166+
assert.equal(stdout.trim(), 'file is sorted.');
167+
}
168+
);
169+
170+
execFile(
171+
'node',
172+
['index.js', 'fixtures/sorted-*/package.json','--check'],
173+
(error, stdout, stderr) => {
174+
assert.equal(error, null);
175+
assert.equal(stderr, '');
176+
assert.equal(stdout.trim(), 'all files are sorted.');
177+
}
178+
);
179+
180+
execFile(
181+
'node',
182+
['index.js', 'fixtures/*/package.json','--check'],
183+
(error, stdout, stderr) => {
184+
assert.equal(error.code, 3);
185+
assert.equal(stderr, '');
186+
assert.equal(stdout.includes('fixtures/sorted-1/package.json'), false);
187+
assert.equal(stdout.includes('fixtures/sorted-2/package.json'), false);
188+
assert.equal(stdout.includes('fixtures/not-sorted-1/package.json'), true);
189+
assert.equal(stdout.includes('fixtures/not-sorted-2/package.json'), true);
190+
assert.equal(stdout.includes('fixtures/another-not-sorted/package.json'), true);
191+
assert.equal(stdout.includes('3 files are not sorted.'), true);
192+
}
193+
);

0 commit comments

Comments
 (0)
Please sign in to comment.