-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: separate cli from index.js (#101)
- Loading branch information
Showing
4 changed files
with
75 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
#!/usr/bin/env node | ||
const fs = require('fs') | ||
const globby = require('globby') | ||
const sortPackageJson = require('.') | ||
|
||
const isCheckFlag = argument => argument === '--check' || argument === '-c' | ||
|
||
const cliArguments = process.argv.slice(2) | ||
const isCheck = cliArguments.some(isCheckFlag) | ||
|
||
const patterns = cliArguments.filter(argument => !isCheckFlag(argument)) | ||
|
||
if (!patterns.length) { | ||
patterns[0] = 'package.json' | ||
} | ||
|
||
const files = globby.sync(patterns) | ||
|
||
if (files.length === 0) { | ||
console.log('No matching files.') | ||
process.exit(1) | ||
} | ||
|
||
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) { | ||
console.log() | ||
if (notSortedFiles) { | ||
console.log( | ||
notSortedFiles === 1 | ||
? `${notSortedFiles} of ${files.length} matched file is not sorted.` | ||
: `${notSortedFiles} of ${files.length} matched files are not sorted.`, | ||
) | ||
} else { | ||
console.log( | ||
files.length === 1 | ||
? `${files.length} matched file is sorted.` | ||
: `${files.length} matched files are sorted.`, | ||
) | ||
} | ||
process.exit(notSortedFiles) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,15 +19,16 @@ | |
"author": "Keith Cirkel <[email protected]> (http://keithcirkel.co.uk/)", | ||
"files": [ | ||
"index.js", | ||
"index.d.ts" | ||
"index.d.ts", | ||
"cli.js" | ||
], | ||
"main": "index.js", | ||
"types": "index.d.ts", | ||
"bin": "index.js", | ||
"bin": "cli.js", | ||
"scripts": { | ||
"lint": "eslint .", | ||
"semantic-release": "semantic-release", | ||
"sort-package-json": "node index.js package.json --check", | ||
"sort-package-json": "node cli.js package.json --check", | ||
"test": "node test.js" | ||
}, | ||
"husky": { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters