-
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathindex.js
More file actions
151 lines (123 loc) · 3.98 KB
/
index.js
File metadata and controls
151 lines (123 loc) · 3.98 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
'use strict';
const { readFileSync } = require('fs');
const { join } = require('path');
const { sortPackageJson } = require('sort-package-json');
let date = new Date();
const description = 'The default blueprint for Embroider v2 addons.';
function stringifyAndNormalize(contents) {
return `${JSON.stringify(contents, null, 2)}\n`;
}
const workflows = join(__dirname, 'src/.github/workflows/');
const replacers = {
'.github/workflows/ci.yml'(templateVariables) {
let sourcePath = join(workflows, templateVariables.packageManager, 'ci.yml');
return readFileSync(sourcePath);
},
'.github/workflows/push-dist.yml'(templateVariables) {
let sourcePath = join(workflows, templateVariables.packageManager, 'push-dist.yml');
return readFileSync(sourcePath);
},
'package.json'(_templateVariables, content) {
return this.updatePackageJson(content);
},
};
module.exports = {
description,
fileMapTokens(options) {
let { ext } = options.locals;
return {
__ext__: () => ext,
};
},
locals(options) {
if (isYarn(options)) {
throw new Error(
`Please do not generate this project with --yarn. Omit '--yarn' when generating this blueprint'`,
);
}
return {
name: options.name,
blueprintVersion: require('./package.json').version,
year: date.getFullYear(),
packageManager: options.packageManager,
yarn: false,
pnpm: isPnpm(options),
npm: isNpm(options),
typescript: options.typescript,
ext: options.typescript ? 'ts' : 'js',
blueprint: 'addon',
blueprintOptions: buildBlueprintOptions({
[`--ci-provider=${options.ciProvider}`]: options.ciProvider,
'--pnpm': isPnpm(options),
'--npm': isNpm(options),
'--typescript': options.typescript,
}),
ciProvider: options.ciProvider,
};
},
files(options) {
let files = this._super.files.apply(this, arguments);
if (options.ciProvider !== 'github') {
files = files.filter((file) => file.indexOf('.github') < 0);
}
if (!options.typescript) {
let ignoredFiles = ['tsconfig.json'];
files = files.filter(
(filename) => !filename.match(/.*\.ts$/) && !ignoredFiles.includes(filename),
);
}
return files;
},
updatePackageJson(content) {
let contents = JSON.parse(content);
return stringifyAndNormalize(sortPackageJson(contents));
},
/**
* @override
*
* This modification of buildFileInfo allows our differing
* input files to output to a single file, depending on the options.
* For example:
*
* for javascript,
* _ts_eslint.config.mjs is deleted
* _js_eslint.config.mjs is renamed to eslint.config.mjs
*
* for typescript,
* _js_eslint.config.mjs is deleted
* _ts_eslint.config.mjs is renamed to eslint.config.mjs
*/
buildFileInfo(_intoDir, templateVariables, file, _commandOptions) {
let fileInfo = this._super.buildFileInfo.apply(this, arguments);
if (file in replacers) {
fileInfo.replacer = replacers[file].bind(this, templateVariables);
}
return fileInfo;
},
};
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;
}