-
-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathindex.js
More file actions
196 lines (164 loc) · 5.45 KB
/
index.js
File metadata and controls
196 lines (164 loc) · 5.45 KB
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
'use strict';
const stringUtil = require('ember-cli-string-utils');
const chalk = require('chalk');
const directoryForPackageName = require('./lib/directory-for-package-name');
const { sortPackageJson } = require('sort-package-json');
const { join } = require('path');
const { readFileSync } = require('fs');
const ejs = require('ejs');
const CONDITIONAL_FILES = join(__dirname, 'conditional-files');
function stringifyAndNormalize(contents) {
return `${JSON.stringify(contents, null, 2)}\n`;
}
/**
* This overrides ember-cli's default replace function,
* which is a call to ejs with the template locals.
*
* If we want to continue using ejs in any of these,
* we _may_ need to call ejs ourselves
* (in the case where we ignore the "contents" passed to these functions)
* (see `conditional-files`)
*/
const replacers = {
'package.json'(...args) {
return this.updatePackageJson(...args);
},
'eslint.config.mjs'(locals) {
let prefix = locals.typescript ? '_ts_' : '_js_';
let filePath = join(CONDITIONAL_FILES, prefix + 'eslint.config.mjs');
let raw = readFileSync(filePath).toString();
return ejs.render(raw, locals);
},
'babel.config.mjs'(locals) {
let prefix = locals.typescript ? '_ts_' : '_js_';
let filePath = join(CONDITIONAL_FILES, prefix + 'babel.config.mjs');
let raw = readFileSync(filePath).toString();
return ejs.render(raw, locals);
},
};
module.exports = {
description: 'The default blueprint for ember-cli projects.',
shouldTransformTypeScript: true,
filesToRemove: [
'app/styles/.gitkeep',
'app/templates/.gitkeep',
'app/views/.gitkeep',
'public/.gitkeep',
'Brocfile.js',
'testem.json',
],
locals(options) {
let entity = options.entity;
let rawName = entity.name;
let name = stringUtil.dasherize(rawName);
let namespace = stringUtil.classify(rawName);
let hasOptions =
!options.welcome || options.packageManager || options.ciProvider;
let blueprintOptions = '';
if (hasOptions) {
let indent = `\n `;
let outdent = `\n `;
blueprintOptions =
indent +
[
!options.welcome && '"--no-welcome"',
options.packageManager === 'yarn' && '"--yarn"',
options.packageManager === 'pnpm' && '"--pnpm"',
options.ciProvider && `"--ci-provider=${options.ciProvider}"`,
options.typescript && `"--typescript"`,
!options.emberData && `"--no-ember-data"`,
!options.warpDrive && `"--no-warp-drive"`,
]
.filter(Boolean)
.join(',\n ') +
outdent;
}
let invokeScriptPrefix = 'npm run';
let execBinPrefix = 'npm exec';
if (options.packageManager === 'yarn') {
invokeScriptPrefix = 'yarn';
execBinPrefix = 'yarn';
}
if (options.packageManager === 'pnpm') {
invokeScriptPrefix = 'pnpm';
execBinPrefix = 'pnpm';
}
return {
appDirectory: directoryForPackageName(name),
name,
modulePrefix: name,
namespace,
blueprintVersion: require('./package').version,
yarn: options.packageManager === 'yarn',
pnpm: options.packageManager === 'pnpm',
npm:
options.packageManager !== 'yarn' && options.packageManager !== 'pnpm',
invokeScriptPrefix,
execBinPrefix,
welcome: options.welcome,
blueprint: 'app',
blueprintOptions,
lang: options.lang,
warpDrive: options.warpDrive ?? options.emberData,
ciProvider: options.ciProvider,
typescript: options.typescript,
packageManager: options.packageManager ?? 'npm',
};
},
files(options) {
if (this._files) {
return this._files;
}
let files = this._super();
if (options.ciProvider !== 'github') {
files = files.filter((file) => file.indexOf('.github') < 0);
}
if (!options.typescript) {
// Exclude TypeScript-only files for JavaScript apps.
// Note: app/config/environment.ts is excluded explicitly because
// shouldTransformTypeScript strips trailing commas when converting to JS,
// causing Prettier failures. We use a separate .js file instead.
files = files.filter(
(file) =>
file !== 'tsconfig.json' &&
file !== 'app/config/environment.ts' &&
!file.startsWith('types/') &&
!file.endsWith('.d.ts'),
);
} else {
// For TypeScript apps, exclude the JS version of app/config/environment
files = files.filter((file) => file !== 'app/config/environment.js');
}
const warpDrive = options.warpDrive || options.emberData;
if (!warpDrive) {
files = files.filter((file) => !file.includes('services/store.ts'));
} else {
files = files.filter((file) => !file.includes('services/.gitkeep'));
}
this._files = files;
return this._files;
},
beforeInstall() {
const prependEmoji = require('./lib/prepend-emoji');
this.ui.writeLine(
prependEmoji(
'✨',
`Creating a new Ember app in ${chalk.yellow(process.cwd())}:`,
),
);
},
/**
* @override
*/
buildFileInfo(intoDir, templateVariables, file) {
let fileInfo = this._super.buildFileInfo.apply(this, arguments);
if (file in replacers) {
fileInfo.replacer = replacers[file].bind(this, templateVariables);
}
return fileInfo;
},
updatePackageJson(options, content) {
let contents = JSON.parse(content);
return stringifyAndNormalize(sortPackageJson(contents));
},
};