-
Notifications
You must be signed in to change notification settings - Fork 28
/
index.js
389 lines (314 loc) · 13.2 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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
'use strict';
const path = require('path');
const assert = require('assert');
const os = require('os');
const fs = require('fs-extra');
const SilentError = require('silent-error');
const sortPackageJson = require('sort-package-json');
const normalizeEntityName = require('ember-cli-normalize-entity-name');
const execa = require('execa');
const { merge } = require('lodash');
const { lt } = require('semver');
let date = new Date();
const { addonInfoFromOptions, testAppInfoFromOptions, withoutAddonOptions } = require('./src/info');
const { scripts } = require('./src/root-package-json');
const pnpm = require('./src/pnpm');
const directoryForPackageName = require('./src/directory-for-package-name');
const description = 'The default blueprint for Embroider v2 addons.';
async function createTmp() {
let prefix = 'v2-addon-blueprint--';
let prefixPath = path.join(os.tmpdir(), prefix);
let tmpDirPath = await fs.mkdtemp(prefixPath);
return tmpDirPath;
}
const filesToCopyFromRootToAddonDuringBuild = ['README.md', 'LICENSE.md'];
const filesToCopyFromRootToAddonInExistingMonorepo = ['README.md', 'CONTRIBUTING.md'];
const filesToCopyFromRootToAddonInAddonOnlyMode = [
'config/ember-cli-update.json',
'.editorconfig',
'.prettierignore',
'.prettierrc.cjs',
'CONTRIBUTING.md',
'LICENSE.md',
'README.md',
];
module.exports = {
description,
install(options) {
if (!options.addonOnly) {
if (fs.existsSync(path.join('..', 'package.json'))) {
options.ui.writeInfoLine(
"Existing monorepo detected! The blueprint will only create the addon and test-app folders, and omit any other files in the repo's root folder.",
);
this.isExistingMonorepo = true;
}
}
return this._super.install.apply(this, arguments);
},
async afterInstall(options) {
let tasks = [];
let addonInfo = addonInfoFromOptions(options);
let testAppInfo = testAppInfoFromOptions(options);
if (!options.addonOnly) {
tasks.push(this.createTestApp(options));
/**
* Setup root package.json scripts based on the packager
*/
tasks.push(
(async () => {
let packageJson = path.join(options.target, 'package.json');
let json = await fs.readJSON(packageJson);
json.scripts = scripts(options);
if (options.packageManager === 'pnpm' || options.pnpm) {
delete json.workspaces;
json.pnpm = {
// TODO: update the blueprint's output to ESLint 8
overrides: {
'@types/eslint': '^7.0.0',
},
};
}
await fs.writeFile(packageJson, JSON.stringify(json, null, 2));
})(),
);
if (isPnpm(options)) {
tasks.push(pnpm.createWorkspacesFile(options.target, addonInfo, testAppInfo));
}
if (options.releaseIt) {
tasks.push(this.setupReleaseIt(options.target));
}
}
await Promise.all(tasks);
if (this.isExistingMonorepo && !options.addonOnly) {
await this.moveFilesForExistingMonorepo(options);
this.ui.writeWarnLine(
`Make sure your workspaces are configured correctly to cover the newly created ${addonInfo.location} and ${testAppInfo.location} packages!`,
);
}
await this.sortAddonPackageJson(addonInfo);
},
async sortAddonPackageJson(addonInfo) {
let addonPackageJsonPath = path.join(addonInfo.location, 'package.json');
let addonPackageJson = await fs.readJSON(addonPackageJsonPath);
await fs.writeJSON(addonPackageJsonPath, sortPackageJson(addonPackageJson), { spaces: 2 });
},
// EmberCLI will always create a <addon-name> root folder for us, which makes no sense in an existing repo
// Until we have a better solution for this use case upstream, we are fixing this here by moving the generated
// files outside of this folder and deleting it
async moveFilesForExistingMonorepo(options) {
assert(
!options.addonOnly,
`When in --addon-only mode, we don't need to move files within an existing monorepo. ` +
`If you see this error, please open an issue at: https://github.com/embroider-build/addon-blueprint/issues`,
);
let addonInfo = addonInfoFromOptions(options);
let testAppInfo = testAppInfoFromOptions(options);
let tmpDir = await createTmp();
let tmpAddonDir = path.join(tmpDir, 'addon');
let tmpTestAppDir = path.join(tmpDir, 'test-app');
let originalAddonDir = path.resolve(addonInfo.location);
let originalTestAppDir = path.resolve(testAppInfo.location);
let finalAddonDir = path.resolve('..', addonInfo.location);
let finalTestAppDir = path.resolve('..', testAppInfo.location);
let extraneousDir = process.cwd();
await fs.move(originalAddonDir, tmpAddonDir);
await fs.move(originalTestAppDir, tmpTestAppDir);
for (let fileToCopy of filesToCopyFromRootToAddonInExistingMonorepo) {
await fs.move(fileToCopy, path.join(tmpAddonDir, fileToCopy));
}
process.chdir('..');
await fs.remove(extraneousDir);
await fs.move(tmpAddonDir, finalAddonDir);
await fs.move(tmpTestAppDir, finalTestAppDir);
},
async createTestApp(options) {
assert(
!options.addonOnly,
`When in --addon-only mode, we don't create a test-app. ` +
`If you see this error, please open an issue at: https://github.com/embroider-build/addon-blueprint/issues`,
);
const appBlueprint = this.lookupBlueprint('app');
if (!appBlueprint) {
throw new SilentError('Cannot find app blueprint for generating test-app!');
}
let testAppInfo = testAppInfoFromOptions(options);
let testAppPath = path.join(options.target, testAppInfo.location);
const appOptions = {
...withoutAddonOptions(options),
target: testAppPath,
skipNpm: true,
skipGit: true,
entity: { name: testAppInfo.name.raw },
name: testAppInfo.name.raw,
rawName: testAppInfo.name.raw,
ciProvider: 'travis', // we will delete this anyway below, as the CI config goes into the root folder
welcome: false,
};
await appBlueprint.install(appOptions);
await Promise.all([
this.updateTestAppPackageJson(path.join(testAppPath, 'package.json'), isPnpm(options)),
this.overrideTestAppFiles(testAppPath, path.join(options.target, 'test-app-overrides')),
fs.unlink(path.join(testAppPath, '.travis.yml')),
]);
if (options.typescript) {
const needsVersion = '4.9.0-beta.0';
const recommendedVersion = '5.2.0-beta.0';
const hasVersion = this.project.emberCLIVersion();
if (lt(hasVersion, needsVersion)) {
this.ui.writeWarnLine(
`Your version ${hasVersion} of Ember CLI does not support the --typescript flag yet. Please run \`ember install ember-cli-typescript\` in the ${testAppInfo.location} folder manually!`,
);
} else if (lt(hasVersion, recommendedVersion)) {
this.ui.writeWarnLine(
`We recommend using Ember CLI >= ${recommendedVersion} for the best blueprint support when using TypeScript!`,
);
}
}
},
async updateTestAppPackageJson(packageJsonPath, useWorkspaceProtocol) {
const pkg = await fs.readJSON(packageJsonPath);
const additions = require('./additional-test-app-package.json');
merge(pkg, additions);
pkg.description = `Test app for ${this.locals(this.options).addonName} addon`;
// we must explicitly add our own v2 addon here, the implicit magic of the legacy dummy app does not work
if (useWorkspaceProtocol) {
// https://pnpm.io/workspaces#workspace-protocol-workspace
// For an app, `*` is fine, but if we were wiring this up to multiple packages, we'd want `^`
pkg.devDependencies[this.locals(this.options).addonName] = 'workspace:*';
} else {
pkg.devDependencies[this.locals(this.options).addonName] = '^0.0.0';
}
return fs.writeFile(packageJsonPath, JSON.stringify(sortPackageJson(pkg), undefined, 2));
},
async overrideTestAppFiles(testAppPath, overridesPath) {
// we cannot us fs.move, as it will replace the directory, removing the other files of the app blueprint
// but fs.copy works as we need it. Just have to remove the overrides directory afterwards.
await fs.copy(overridesPath, testAppPath, {
overwrite: true,
});
await fs.remove(overridesPath);
},
async setupReleaseIt(rootPath) {
await execa('create-rwjblue-release-it-setup', ['--no-install'], {
cwd: rootPath,
preferLocal: true,
localDir: __dirname,
});
},
fileMapTokens(options) {
let { addonInfo, testAppInfo, ext } = options.locals;
return {
__addonLocation__: () => addonInfo.location,
__testAppLocation__: () => testAppInfo.location,
__ext__: () => ext,
};
},
mapFile(file, locals) {
// EmberCLI has builtin support for converting gitignore to .gitignore: https://github.com/ember-cli/ember-cli/blob/24ef75b2e28cadee4a69472c6ae9c0e4845d512e/lib/models/blueprint.js#L1633
// But this only works at the root folder, for the addon subfolder we have to do this ourselves here
file = file.replace(/\/gitignore/, '/.gitignore');
return this._super.mapFile.call(this, file, locals);
},
locals(options) {
let addonInfo = addonInfoFromOptions(options);
let testAppInfo = testAppInfoFromOptions(options);
let pathFromAddonToRoot = addonInfo.location
.split('/')
.map(() => '..')
.join('/');
return {
rootDirectory: directoryForPackageName(addonInfo.name.raw),
addonInfo,
testAppInfo,
addonName: addonInfo.name.dashed,
addonNamespace: addonInfo.name.classified,
blueprintVersion: require('./package.json').version,
year: date.getFullYear(),
packageManager: options.packageManager,
yarn: isYarn(options),
pnpm: isPnpm(options),
npm: isNpm(options),
typescript: options.typescript,
ext: options.typescript ? 'ts' : 'js',
blueprint: 'addon',
blueprintOptions: buildBlueprintOptions({
[`--addon-location=${options.addonLocation}`]: options.addonLocation,
[`--ci-provider=${options.ciProvider}`]: options.ciProvider,
'--pnpm': isPnpm(options),
'--release-it': options.releaseIt,
[`--test-app-location=${options.testAppLocation}`]: options.testAppLocation,
[`--test-app-name=${options.testAppName}`]: options.testAppName,
'--typescript': options.typescript,
'--yarn': isYarn(options),
}),
ciProvider: options.ciProvider,
pathFromAddonToRoot,
filesToCopyFromRootToAddon: filesToCopyFromRootToAddonDuringBuild,
isExistingMonorepo: this.isExistingMonorepo,
};
},
files(options) {
let files = this._super.files.apply(this, arguments);
if (options.addonOnly) {
files = files.filter((filename) => filename.includes('__addonLocation__') || filesToCopyFromRootToAddonInAddonOnlyMode.includes(filename));
} else {
// filter out the addon-specific npmrc, as it
// is only applicable during --addon-only
files = files.filter((filename) => !filename.includes('__addonLocation__/.npmrc'));
}
if (!options.typescript) {
let ignoredFiles = ['__addonLocation__/tsconfig.json'];
files = files.filter(
(filename) => !filename.match(/.*\.ts$/) && !ignoredFiles.includes(filename),
);
}
if (this.isExistingMonorepo) {
// The .gitignore is used for ignoring files that are moved to the addon from the root folder at build time
// But this setup does not apply for an existing monorepo (all root files are ignored), so we don't need the .gitignore
files = files.filter((filename) => filename !== '__addonLocation__/gitignore');
// In an existing monorepo, we typically have a single .npmrc for the whole repo.
// We don't want to generate an .npmrc for those situations.
files = files.filter((filename) => !filename.endsWith('.npmrc'));
}
if (!isYarn(options)) {
let ignoredFiles = ['.yarnrc.yml'];
files = files.filter((filename) => !ignoredFiles.includes(filename));
}
return files;
},
normalizeEntityName(entityName) {
entityName = normalizeEntityName(entityName);
if (this.project.isEmberCLIProject() && !this.project.isEmberCLIAddon()) {
throw new SilentError(
'Generating an addon in an existing ember-cli project is not supported.',
);
}
return entityName;
},
};
function buildBlueprintOptions(blueprintOptions) {
let blueprintOptionsFiltered = Object.keys(blueprintOptions).filter((option) => {
return Boolean(blueprintOptions[option]);
});
if (blueprintOptionsFiltered.length > 0) {
return (
`\n ` +
blueprintOptionsFiltered.map((option) => `"${option}"`).join(',\n ') +
`\n `
);
}
return '';
}
// These methods exist because in ember-cli 5.4, package manager handling
// had changed to solely use the packageManager key, however
// prior to ember-cli 5.4, pnpm, yarn, and npm, had their own booleans on
// the options object.
function isPnpm(options) {
return options.packageManager === 'pnpm' || options.pnpm;
}
function isYarn(options) {
return options.packageManager === 'yarn' || options.yarn;
}
function isNpm(options) {
return options.packageManager === 'npm' || options.npm;
}