diff --git a/cli.js b/cli.js new file mode 100755 index 00000000..dd2e19de --- /dev/null +++ b/cli.js @@ -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) +} diff --git a/index.js b/index.js old mode 100755 new mode 100644 index e6a18129..5e905521 --- a/index.js +++ b/index.js @@ -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) => @@ -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) - } -} diff --git a/package.json b/package.json index c2cbfef0..d1b0b3dc 100644 --- a/package.json +++ b/package.json @@ -19,15 +19,16 @@ "author": "Keith Cirkel (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": { diff --git a/test.js b/test.js index bb7323d1..2adbab5b 100644 --- a/test.js +++ b/test.js @@ -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.', ) @@ -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, '') @@ -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, '') @@ -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, '') @@ -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, '') @@ -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, '') @@ -737,7 +740,7 @@ execFile( execFile( 'node', [ - 'index.js', + 'cli.js', 'fixtures/not-sorted-1/package.json', 'fixtures/not-sorted-1/**/package.json', '--check',