diff --git a/lib/commands/new.js b/lib/commands/new.js index 545c2a2..0dcb7d5 100644 --- a/lib/commands/new.js +++ b/lib/commands/new.js @@ -3,7 +3,6 @@ const Os = require('os'); const Path = require('path'); const ChildProcess = require('child_process'); -const StableStringify = require('json-stable-stringify'); const Helpers = require('../helpers'); const DisplayError = require('../display-error'); @@ -87,13 +86,16 @@ module.exports = async (cwd, dir, ctx) => { const cmp = (x, y) => { - const xScore = order.indexOf(x.key) === -1 ? order.length : order.indexOf(x.key); - const yScore = order.indexOf(y.key) === -1 ? order.length : order.indexOf(y.key); - + const xScore = order.indexOf(x) === -1 ? order.length : order.indexOf(x); + const yScore = order.indexOf(y) === -1 ? order.length : order.indexOf(y); return xScore - yScore; }; - const finalPkgStringified = StableStringify(finalPkg, { cmp, space: 2 }); + finalPkg = internals.sortObject(finalPkg, cmp); + finalPkg.dependencies = internals.sortObject(finalPkg.dependencies); + finalPkg.devDependencies = internals.sortObject(finalPkg.devDependencies); + + const finalPkgStringified = JSON.stringify(finalPkg, null, 2); const projectName = finalPkg.name || Path.basename(dir); await Promise.all([ @@ -104,6 +106,17 @@ module.exports = async (cwd, dir, ctx) => { await Helpers.exec('git add package.json README.md', { cwd: dir }); }; +// Bic'd and adapted from domenic/sorted-object (WTFPL) +internals.sortObject = (input, fn) => { + + return Object.keys(input).sort(fn) + .reduce((output, key) => { + + output[key] = input[key]; + return output; + }, {}); +}; + internals.ensureGitAndNpm = async ({ colors }) => { try { diff --git a/package.json b/package.json index 86a66e6..b03441e 100644 --- a/package.json +++ b/package.json @@ -40,7 +40,6 @@ "@hapi/wreck": "15.x.x", "bin-v8-flags-filter": ">=1.2.0 <2", "glob": "7.x.x", - "json-stable-stringify": "1.x.x", "marked": "0.7.x", "marked-terminal": "3.x.x", "mkdirp": "0.5.x", diff --git a/test/index.js b/test/index.js index 2840063..2e8a323 100644 --- a/test/index.js +++ b/test/index.js @@ -678,6 +678,26 @@ describe('hpal', () => { } }; + const completePkgKeysOrder = [ + 'name', + 'version', + 'description', + 'author', + 'license', + 'main', + 'directories', + 'scripts', + 'dependencies', + 'devDependencies' + ]; + + const bailedPkgKeysOrder = [ + 'main', + 'scripts', + 'dependencies', + 'devDependencies' + ]; + it('creates a new pal project.', { timeout: 5000 }, async (flags) => { flags.onCleanup = async () => await rimraf('new/my-project'); @@ -718,6 +738,9 @@ describe('hpal', () => { expect(pkg.version).to.equal('1.0.0'); expect(pkg.dependencies).to.exist(); expect(pkg.devDependencies).to.exist(); + expect(Object.keys(pkg)).to.equal(completePkgKeysOrder); + expect(Object.keys(pkg.dependencies)).to.equal(Object.keys(pkg.dependencies).sort()); + expect(Object.keys(pkg.devDependencies)).to.equal(Object.keys(pkg.devDependencies).sort()); expect(pkgAsString.endsWith('\n')).to.equal(true); expect(readmeH1).to.equal('chosen-name'); expect(lib).to.exist(); @@ -773,6 +796,9 @@ describe('hpal', () => { expect(pkg.version).to.not.exist(); expect(pkg.dependencies).to.exist(); expect(pkg.devDependencies).to.exist(); + expect(Object.keys(pkg)).to.equal(bailedPkgKeysOrder); + expect(Object.keys(pkg.dependencies)).to.equal(Object.keys(pkg.dependencies).sort()); + expect(Object.keys(pkg.devDependencies)).to.equal(Object.keys(pkg.devDependencies).sort()); expect(pkgAsString.endsWith('\n')).to.equal(true); expect(readmeH1).to.equal('bail-on-npm-init'); expect(lib).to.exist();