Skip to content

Commit 4734db4

Browse files
Merge pull request #82 from getflights/main
Make sure the package.json is sorted according to sort-package.json
2 parents 8882760 + 3f03e38 commit 4734db4

4 files changed

Lines changed: 78 additions & 0 deletions

File tree

index.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,21 @@
11
'use strict';
22

3+
const { sortPackageJson } = require('sort-package-json');
4+
35
let date = new Date();
46

57
const description = 'The default blueprint for Embroider v2 addons.';
68

9+
function stringifyAndNormalize(contents) {
10+
return `${JSON.stringify(contents, null, 2)}\n`;
11+
}
12+
13+
const replacers = {
14+
'package.json'(content) {
15+
return this.updatePackageJson(content);
16+
},
17+
};
18+
719
module.exports = {
820
description,
921

@@ -60,6 +72,36 @@ module.exports = {
6072

6173
return files;
6274
},
75+
76+
updatePackageJson(content) {
77+
let contents = JSON.parse(content);
78+
return stringifyAndNormalize(sortPackageJson(contents));
79+
},
80+
81+
/**
82+
* @override
83+
*
84+
* This modification of buildFileInfo allows our differing
85+
* input files to output to a single file, depending on the options.
86+
* For example:
87+
*
88+
* for javascript,
89+
* _ts_eslint.config.mjs is deleted
90+
* _js_eslint.config.mjs is renamed to eslint.config.mjs
91+
*
92+
* for typescript,
93+
* _js_eslint.config.mjs is deleted
94+
* _ts_eslint.config.mjs is renamed to eslint.config.mjs
95+
*/
96+
buildFileInfo(_intoDir, _templateVariables, file, _commandOptions) {
97+
let fileInfo = this._super.buildFileInfo.apply(this, arguments);
98+
99+
if (file in replacers) {
100+
fileInfo.replacer = replacers[file].bind(this);
101+
}
102+
103+
return fileInfo;
104+
},
63105
};
64106

65107
function buildBlueprintOptions(blueprintOptions) {

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,8 @@
1919
"prettier": "^3.5.3",
2020
"release-plan": "^0.16.0"
2121
},
22+
"dependencies": {
23+
"sort-package-json": "^3.4.0"
24+
},
2225
"packageManager": "[email protected]"
2326
}

pnpm-lock.yaml

Lines changed: 24 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/smoke-tests/defaults.test.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@ import {
1313
assertGeneratedCorrectly,
1414
dirContents,
1515
matchesFixture,
16+
packageJsonAt,
1617
SUPPORTED_PACKAGE_MANAGERS,
1718
} from '../helpers.js';
1819
import { existsSync } from 'node:fs';
20+
import sortPackageJson from 'sort-package-json';
1921

2022
/**
2123
* NOTE: tests run sequentially
@@ -108,6 +110,13 @@ for (let packageManager of SUPPORTED_PACKAGE_MANAGERS) {
108110
expect(exitCode).toEqual(0);
109111
});
110112

113+
it('has a sorted package.json', async () => {
114+
const originalPackageJson = await packageJsonAt(addonDir);
115+
const sortedPackageJson = await sortPackageJson(originalPackageJson);
116+
117+
expect(JSON.stringify(sortedPackageJson)).toEqual(JSON.stringify(originalPackageJson));
118+
});
119+
111120
describe('with fixture', () => {
112121
beforeEach(async () => {
113122
let addonFixture = fixturify.readSync('./fixtures/addon');

0 commit comments

Comments
 (0)