Skip to content

Commit

Permalink
refactor: separate cli from index.js (#101)
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker authored and keithamus committed Jan 7, 2020
1 parent 75ee93a commit aecd561
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 72 deletions.
57 changes: 57 additions & 0 deletions cli.js
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)
}
58 changes: 0 additions & 58 deletions index.js
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#!/usr/bin/env node
const sortObjectKeys = require('sort-object-keys')
const detectIndent = require('detect-indent')
const detectNewline = require('detect-newline').graceful
const globby = require('globby')
const gitHooks = require('git-hooks-list')

const hasOwnProperty = (object, property) =>
Expand Down Expand Up @@ -279,59 +277,3 @@ function sortPackageJson(jsonIsh, options = {}) {
module.exports = sortPackageJson
module.exports.sortPackageJson = sortPackageJson
module.exports.sortOrder = defaultSortOrder

if (require.main === module) {
const fs = require('fs')
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)
}
}
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,16 @@
"author": "Keith Cirkel <npm@keithcirkel.co.uk> (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": {
Expand Down
25 changes: 14 additions & 11 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -623,22 +623,25 @@ for (const field of [
])
}

// CLI should be executable
fs.accessSync('./cli.js', fs.constants.X_OK)

// 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')
const original = fs.readFileSync('fixtures/not-sorted-1/package.json', 'utf8')
execFile(
'node',
['index.js', 'fixtures/not-sorted-1/package.json', '-c'],
['cli.js', 'fixtures/not-sorted-1/package.json', '-c'],
(error, stdout, stderr) => {
assert.notStrictEqual(
orignal,
sortPackageJson(orignal),
original,
sortPackageJson(original),
'fixtures/not-sorted-1/package.json should be a unsorted file.',
)
assert.strictEqual(
orignal,
original,
fs.readFileSync('fixtures/not-sorted-1/package.json', 'utf8'),
'file should not fixed when --check is enabled.',
)
Expand All @@ -657,7 +660,7 @@ execFile(

execFile(
'node',
['index.js', 'fixtures/not-sorted-*/package.json', '--check'],
['cli.js', 'fixtures/not-sorted-*/package.json', '--check'],
(error, stdout, stderr) => {
assert.strictEqual(error.code, 2)
assert.strictEqual(stderr, '')
Expand All @@ -678,7 +681,7 @@ execFile(

execFile(
'node',
['index.js', 'fixtures/sorted-1/package.json', '--check'],
['cli.js', 'fixtures/sorted-1/package.json', '--check'],
(error, stdout, stderr) => {
assert.strictEqual(error, null)
assert.strictEqual(stderr, '')
Expand All @@ -688,7 +691,7 @@ execFile(

execFile(
'node',
['index.js', 'fixtures/sorted-*/package.json', '--check'],
['cli.js', 'fixtures/sorted-*/package.json', '--check'],
(error, stdout, stderr) => {
assert.strictEqual(error, null)
assert.strictEqual(stderr, '')
Expand All @@ -698,7 +701,7 @@ execFile(

execFile(
'node',
['index.js', 'fixtures/*/package.json', '--check'],
['cli.js', 'fixtures/*/package.json', '--check'],
(error, stdout, stderr) => {
assert.strictEqual(error.code, 3)
assert.strictEqual(stderr, '')
Expand All @@ -725,7 +728,7 @@ execFile(

execFile(
'node',
['index.js', 'NONE_EXISTS_FILE', '--check'],
['cli.js', 'NONE_EXISTS_FILE', '--check'],
(error, stdout, stderr) => {
assert.strictEqual(error.code, 1)
assert.strictEqual(stderr, '')
Expand All @@ -737,7 +740,7 @@ execFile(
execFile(
'node',
[
'index.js',
'cli.js',
'fixtures/not-sorted-1/package.json',
'fixtures/not-sorted-1/**/package.json',
'--check',
Expand Down

0 comments on commit aecd561

Please sign in to comment.