-
Notifications
You must be signed in to change notification settings - Fork 11
/
index.js
297 lines (260 loc) · 8.82 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
const Blueprint = require('ember-cli/lib/models/blueprint');
const fs = require('fs');
const { join } = require('path');
const emberCliUpdate = require('./lib/ember-cli-update');
const copyWithTemplate = require('./lib/copy-with-template');
const { rm, rmdir, readFile, lstat } = require('fs/promises');
const appBlueprint = Blueprint.lookup('app');
async function isDirectory(path) {
try {
let stat = await lstat(path);
return stat.isDirectory();
} catch {
return false;
}
}
module.exports = {
locals(options) {
return appBlueprint.locals(options);
},
/**
* NOTE: we can't have a "shared" file here,
* as we can with files-override
* (so shared things should go in files-override/shared)
*/
filesPath(options) {
if (options.typescript) {
return join(this.path, 'files/ts');
}
return join(this.path, 'files/js');
},
beforeInstall(options) {
if (!appBlueprint) {
throw new Error('Cannot find app blueprint for generating test-app!');
}
return appBlueprint.install({
...options,
skipGit: true,
});
},
async updateDeps(options) {
let manifestPath = join(options.target, 'package.json');
let manifestBuffer = await readFile(manifestPath);
let manifest = JSON.parse(manifestBuffer.toString());
let existingDeps = [
...Object.keys(manifest.dependencies || {}),
...Object.keys(manifest.devDependencies || {}),
];
// this.addPackagesToProject doesn't respect the packageManager that the blueprint specified 🙈 so we're skipping a level here
let installTask = this.taskFor('npm-install');
let uninstallTask = this.taskFor('npm-uninstall');
await uninstallTask.run({
'save-dev': true,
verbose: false,
packages: [
// Not needed anymore
'ember-fetch',
'broccoli-asset-rev',
'ember-cli-app-version',
'ember-cli-clean-css',
'ember-cli-dependency-checker',
'ember-cli-inject-live-reload',
'ember-cli-sri',
'ember-cli-terser',
'webpack',
// Linting
// No longer needed because we explicitly define a babel config
'@babel/plugin-proposal-decorators',
// Upstream TypeScript blueprint is out of date, but
// There is concensus on removing all this from the upstream blueprint as well.
...(options.typescript
? [
'@types/ember',
'@types/ember-data',
'@types/ember-data__adapter',
'@types/ember-data__model',
'@types/ember-data__serializer',
'@types/ember-data__store',
'@types/ember__application',
'@types/ember__array',
'@types/ember__component',
'@types/ember__controller',
'@types/ember__debug',
'@types/ember__destroyable',
'@types/ember__engine',
'@types/ember__error',
'@types/ember__helper',
'@types/ember__modifier',
'@types/ember__object',
'@types/ember__owner',
'@types/ember__polyfills',
'@types/ember__routing',
'@types/ember__runloop',
'@types/ember__service',
'@types/ember__string',
'@types/ember__template',
'@types/ember__test',
'@types/ember__utils',
]
: []),
].filter((depToRemove) => existingDeps.includes(depToRemove)),
packageManager: options.packageManager,
});
await installTask.run({
'save-dev': true,
verbose: false,
packages: [
'@embroider/core@unstable',
'@embroider/vite@unstable',
'@embroider/compat@unstable',
'@embroider/test-setup@unstable',
'@embroider/config-meta-loader@unstable',
// once @embroider/vite is tested with v6 and has a wider peer dependency range, we should allow installing latest vite again
'vite@^5.4.11',
'@rollup/plugin-babel',
'decorator-transforms',
'@babel/plugin-transform-runtime',
// Dependencies out of date from upstream
// (or waiting for the release train to catch up)
'eslint@^9.14.0',
'eslint-plugin-ember@^12.3.1',
'eslint-plugin-n@^17.13.1',
'@babel/eslint-parser@^7.25.9',
'@babel/plugin-transform-runtime@^7.25.9',
'@babel/runtime@^7.26.0',
'ember-template-lint@^6.0.0',
'@ember/string@^4.0.0',
'@ember/test-helpers@^4.0.0',
'ember-resolver@^13.0.2',
'ember-load-initializers@^3.0.1',
'qunit@^2.22.0',
'qunit-dom@^3.3.0',
'concurrently@^9.1.0',
// Needed for eslint
'globals',
'babel-plugin-ember-template-compilation',
'prettier-plugin-ember-template-tag',
// TypeScript
// Note that Vite supports TypeScript with 0 configuration on the user's part
...(options.typescript
? [
// See RFC: https://github.com/emberjs/rfcs/pull/1046
'ember-route-template',
'@babel/plugin-transform-typescript',
// TODO: see if there is a better way we can
// sync these libraries' versions
// typescript-eslint doesn't support typescript 5.6 yet
'typescript@~5.5.0',
'@glint/core@unstable',
'@glint/environment-ember-loose@unstable', // currently required :(
'@glint/environment-ember-template-imports@unstable',
'@glint/template@unstable',
'@types/eslint__js',
'typescript-eslint@^8.14.1-alpha.8',
'@typescript-eslint/eslint-plugin@^8.14.1-alpha.8',
'@typescript-eslint/parser@^8.14.1-alpha.8',
]
: []),
],
packageManager: options.packageManager,
});
},
async afterInstall(options) {
const filesToDelete = [
// now in the project root
'app/index.html',
// replaced with .eslintrc.cjs
'.eslintrc.js',
// This file is not supported in ESLint 9
'.eslintignore',
// replaced with .prettierrc.cjs
'.prettierrc.js',
...(options.typescript
? [
// Until we add application.gjs
'app/templates/application.hbs',
// If folks are using models, they have this file.
// New projects should not be using it though
// 'types/ember-data/types/registries/model.d.ts',
'types/global.d.ts',
]
: []),
];
// TODO: we should probably keep this because enabling TS for JS dev
// is actually very nice.
// Only difference would be that we don't have a lint:types
// or in-editor type-checking
if (!options.typescript) {
filesToDelete.push('tsconfig.json');
}
for (let file of filesToDelete) {
let targetFile = join(options.target, file);
// Don't try to delete TS files (for example) in a JS project
if (!fs.existsSync(targetFile)) continue;
let isDir = await isDirectory(targetFile);
if (isDir) {
await rmdir(targetFile);
} else {
await rm(targetFile);
}
}
// there doesn't seem to be a way to tell ember-cli to not prompt to override files that were added in the beforeInstall
// so I'm just copying a few over at this stage
copyWithTemplate(
join(__dirname, 'files-override/shared'),
options.target,
options,
);
if (options.typescript) {
copyWithTemplate(
join(__dirname, 'files-override/ts'),
options.target,
options,
);
} else {
copyWithTemplate(
join(__dirname, 'files-override/js'),
options.target,
options,
);
}
let packageJson = join(options.target, 'package.json');
let json = JSON.parse(fs.readFileSync(packageJson));
json.scripts = {
...json.scripts,
build: 'vite build',
start: 'vite',
'test:ember': 'vite build --mode test && ember test --path dist',
};
if (json.scripts['lint:types']) {
json.scripts['lint:types'] = 'glint';
}
json['ember-addon'] = {
type: 'app',
version: 2,
};
json.exports = {
'./tests/*': './tests/*',
'./*': './app/*',
};
fs.writeFileSync(packageJson, JSON.stringify(json, null, 2));
await emberCliUpdate({
projectDir: options.target,
projectName: options.projectName,
version: require('./package.json').version,
options,
});
await this.updateDeps(options);
if (!options.skipLint) {
const lintFix = require('ember-cli/lib/utilities/lint-fix');
await lintFix.run({
// Mock Project
pkg: {
scripts: { 'lint:fix': true },
},
ui: this.ui,
root: options.target,
});
}
},
};