Skip to content

Commit

Permalink
Merge pull request #41 from hapipal/order-deps
Browse files Browse the repository at this point in the history
Ensure hpal new maintains alphabetical order of deps lists
  • Loading branch information
devinivy authored May 3, 2020
2 parents 490a6d1 + 6a3d9d9 commit ec42192
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 6 deletions.
23 changes: 18 additions & 5 deletions lib/commands/new.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down Expand Up @@ -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([
Expand 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 {
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
26 changes: 26 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit ec42192

Please sign in to comment.