Skip to content

Commit ae19d4d

Browse files
committed
sort the addon's package.json and assert it is sorted
1 parent 8882760 commit ae19d4d

4 files changed

Lines changed: 106 additions & 1 deletion

File tree

index.js

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

3+
const path = require('path');
4+
const { sortPackageJson } = require('sort-package-json');
5+
const FileInfo = require('@ember-tooling/blueprint-model/utilities/file-info');
6+
37
let date = new Date();
48

59
const description = 'The default blueprint for Embroider v2 addons.';
610

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

@@ -60,6 +74,45 @@ module.exports = {
6074

6175
return files;
6276
},
77+
78+
updatePackageJson(content) {
79+
let contents = JSON.parse(content);
80+
return stringifyAndNormalize(sortPackageJson(contents));
81+
},
82+
83+
/**
84+
* @override
85+
*
86+
* This modification of buildFileInfo allows our differing
87+
* input files to output to a single file, depending on the options.
88+
* For example:
89+
*
90+
* for javascript,
91+
* _ts_eslint.config.mjs is deleted
92+
* _js_eslint.config.mjs is renamed to eslint.config.mjs
93+
*
94+
* for typescript,
95+
* _js_eslint.config.mjs is deleted
96+
* _ts_eslint.config.mjs is renamed to eslint.config.mjs
97+
*/
98+
buildFileInfo(intoDir, templateVariables, file, _commandOptions) {
99+
let mappedPath = this.mapFile(file, templateVariables);
100+
let options = {
101+
action: 'write',
102+
outputBasePath: path.normalize(intoDir),
103+
outputPath: path.join(intoDir, mappedPath),
104+
displayPath: path.normalize(mappedPath),
105+
inputPath: this.srcPath(file),
106+
templateVariables,
107+
ui: this.ui,
108+
};
109+
110+
if (file in replacers) {
111+
options.replacer = replacers[file].bind(this);
112+
}
113+
114+
return new FileInfo(options);
115+
},
63116
};
64117

65118
function buildBlueprintOptions(blueprintOptions) {

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@
1616
"format": "prettier --write ."
1717
},
1818
"devDependencies": {
19+
"@ember-tooling/blueprint-model": "^0.3.0",
1920
"prettier": "^3.5.3",
20-
"release-plan": "^0.16.0"
21+
"release-plan": "^0.16.0",
22+
"sort-package-json": "^3.4.0"
2123
},
2224
"packageManager": "[email protected]"
2325
}

pnpm-lock.yaml

Lines changed: 41 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)