Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure hpal new maintains alphabetical order of deps lists #41

Merged
merged 4 commits into from
May 3, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 20 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);

await Promise.all([
Helpers.writeFile(Path.join(dir, 'package.json'), `${finalPkgStringified}${Os.EOL}`),
Helpers.writeFile(Path.join(dir, 'README.md'), `# ${pkg.name}${Os.EOL}`)
Expand All @@ -102,6 +104,19 @@ module.exports = async (cwd, dir, ctx) => {
await Helpers.exec('git add package.json README.md', { cwd: dir });
};

// TODO Give credit
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is actionable for this TODO? Let's resolve this!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤦 lol, shit. I'd meant to mention this, even peeked that project's license, then promptly spaced. cool! Solution below looks chill!

// bic'd from here: https://github.com/domenic/sorted-object/blob/master/lib/sorted-object.js
// slightly tweaked and stylized per hapi linting, changed to accept a custom sorting function
zemccartney marked this conversation as resolved.
Show resolved Hide resolved
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 @@ -715,6 +735,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(lib).to.exist();
expect(test).to.exist();
Expand Down Expand Up @@ -766,6 +789,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(lib).to.exist();
expect(test).to.exist();
Expand Down