-
Notifications
You must be signed in to change notification settings - Fork 1
/
saofile.js
365 lines (335 loc) · 11.5 KB
/
saofile.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
const spawn = require("cross-spawn");
const validate = require("validate-npm-package-name");
const serializePackage = require("./utils/serializePackage");
const createRollupConfig = require("./utils/createRollupConfig");
const createTsConfig = require("./utils/createTsConfig");
const createEsLintConfig = require("./utils/createEsLintConfig");
const createREADME = require("./utils/createREADME");
const createLicenseConfig = require("./utils/createLicenseConfig");
const createJestConfig = require("./utils/createJestConfig");
const createBabelConfig = require("./utils/createBabelConfig");
const createSemanticReleaseConfig = require("./utils/createSemanticReleaseConfig");
const createTravisConfig = require("./utils/createTravisConfig");
const spinner = require("./utils/spinner");
const createProcess = require("./utils/createProcess");
const prompts = require("./prompts");
module.exports = {
prompts: prompts,
templateData() {
const { cliOptions } = this.sao.opts;
const name = this.answers.name;
const description = this.answers.description;
const license = this.answers.license;
const type = this.answers.type;
const author = this.answers.author;
const email = this.answers.email;
const year = new Date().getFullYear();
const useEs = this.answers.useEs;
const useTests = this.answers.useTests;
const useSemanticRelease = this.answers.useSemanticRelease;
const useTravis = this.answers.useTravis;
const repository = this.answers.repository;
const useUMD = this.answers.useUMD;
const umdName = this.answers.umdName;
const pm = this.answers.pm;
const isTypescript = type === "tsx" || type === "ts";
const isReact = type === "tsx" || type === "jsx";
if (!["ts", "tsx", "js", "jsx"].includes(type)) {
console.error(
this
.chalk`{red No type ${type} is supported. Supported: js, jsx, ts, tsx}`
);
process.exit(1);
}
if (useUMD) {
if (!umdName || !umdName.trim()) {
console.error(
this
.chalk`{red No global UMD name defined while UMD module build is enabled}`
);
process.exit(1);
}
}
if (useSemanticRelease) {
if (!repository || !repository.trim()) {
console.error(
this
.chalk`{red No repository url defined while semantic release is enabled}`
);
process.exit(1);
}
}
if (useTravis) {
if (!repository || !repository.trim()) {
console.error(
this.chalk`{red No repository url defined while travis ci is enabled}`
);
process.exit(1);
}
}
const package = {
name: name,
description: description,
version: "0.0.1",
private: cliOptions.private ? true : undefined,
scripts: [{ build: "rollup -c" }, { watch: "rollup -cw" }],
main: "lib/index.js",
module: useEs ? "lib/index.es.js" : undefined,
types: isTypescript ? "lib/index.d.ts" : undefined,
files: ["lib"],
publishConfig: { access: "public" },
keywords: [],
author: undefined,
contributors: [],
repository: repository
? {
url: repository,
}
: undefined,
license: "ISC",
dependencies: [],
devDependencies: [],
peerDependencies: [],
};
const authorObject = [];
if (author) {
authorObject.push(author);
if (email) authorObject.push(`<${email}>`);
}
if (authorObject.length) {
package.author = authorObject.join(" ");
package.contributors.push(authorObject.join(" "));
}
if (useTests) {
package.scripts.push({ test: "jest" });
package.devDependencies.push({ jest: "^24.9.0" });
if (isReact) {
package.devDependencies.push(
{ enzyme: "^3.10.0" },
{ "react-test-renderer": "^16.8.6" },
{ "enzyme-adapter-react-16": "^1.10.0" }
);
}
if (isTypescript) {
package.devDependencies.push(
{ "@types/jest": "^24.0.23" },
{ "ts-jest": "^24.0.0" }
);
if (isReact) {
package.devDependencies.push(
{ "@types/enzyme": "^3.10.3" },
{ "@types/enzyme-adapter-react-16": "^1.0.5" }
);
}
}
} else {
package.scripts.push({
test: 'echo \\"Warn: No test specified\\" && exit 0"',
});
}
if (isReact) {
package.devDependencies.push({ react: "*" }, { "react-dom": "*" });
package.peerDependencies.push({ react: "*" }, { "react-dom": "*" });
}
if (isTypescript) {
package.devDependencies.push({ typescript: "^3.8.2" });
package.types = "lib/index.d.ts";
if (isReact) {
package.devDependencies.push(
{ "@types/react": "^16.8.5" },
{ "@types/react-dom": "^16.8.2" }
);
}
}
let travisConfig;
if (useTravis) {
travisConfig = createTravisConfig({ useSemanticRelease });
package.scripts.push(...travisConfig.scripts);
}
const rollupConfig = createRollupConfig({
es: useEs,
useTests,
umd: useUMD,
umd_name: umdName,
isReact,
isTypescript,
});
package.devDependencies.push(...rollupConfig.devDependencies);
const babelConfig = createBabelConfig({ isReact });
package.devDependencies.push(...babelConfig.devDependencies);
let tsconfigConfig;
if (isTypescript) {
tsconfigConfig = createTsConfig({ isReact, useTests });
}
const licenseConfig = createLicenseConfig({ license, year, author, email });
package.license = licenseConfig.license;
const semanticReleaseConfig = createSemanticReleaseConfig();
package.devDependencies.push(...semanticReleaseConfig.devDependencies);
package.scripts.push(...semanticReleaseConfig.scripts);
const pmRun = pm === "yarn" ? "yarn" : "npm run";
const eslintConfig = createEsLintConfig({ isReact, isTypescript });
package.devDependencies.push(...eslintConfig.devDependencies);
if (isTypescript) {
const ext = isReact ? ".tsx,.ts,.jsx,.js" : ".ts,.js";
package.scripts.push({ lint: `eslint --ext ${ext} src/` });
} else {
const ext = isReact ? ".jsx,.jsx" : ".js";
package.scripts.push({ lint: `eslint --ext ${ext} src/` });
}
const readmeContent = createREADME({
name,
description,
author,
email,
type,
license: licenseConfig.license,
licenseContent: licenseConfig.licenseContent,
repository: repository,
useTravis,
useSemanticRelease,
useUMD,
umdName,
useEs,
});
return {
tsconfig: tsconfigConfig ? tsconfigConfig.tsconfig : "",
package: serializePackage(package),
rollup: rollupConfig.rollup,
eslint: eslintConfig ? eslintConfig.eslint : "",
travis: travisConfig ? travisConfig.travis : "",
jestContent: createJestConfig({ isTypescript }),
babelrc: babelConfig.babelrc,
licenseContent: licenseConfig.licenseContent,
semanticrelease: semanticReleaseConfig.semanticrelease,
readmeContent,
pmRun,
};
},
actions() {
const name = this.answers && this.answers.name;
const validation = validate(name || this.outFolder);
validation.warnings &&
validation.warnings.forEach((warn) => {
console.warn("Warning:", warn);
});
validation.errors &&
validation.errors.forEach((err) => {
console.error("Error:", err);
});
validation.errors && validation.errors.length && process.exit(1);
const actions = [
{
type: "add",
files: "**",
templateDir: "templates",
filters: {
"__tests__/**": "useTests",
"__tests__/index_spec_ts": `type=="ts"`,
"__tests__/index_spec_tsx": `type=="tsx"`,
"__tests__/index_spec_js": `type=="js"`,
"__tests__/index_spec_jsx": `type=="jsx"`,
"src/index_ts": `type=="ts"`,
"src/index_tsx": `type=="tsx"`,
"src/index_js": `type=="js"`,
"src/index_jsx": `type=="jsx"`,
_tsconfig_json: `type=="tsx" || type=="ts"`,
jest_config_js: "useTests",
travis_yml: "useTravis",
releaserc: "useSemanticRelease",
},
},
];
actions.push({
type: "move",
patterns: {
"src/index_tsx": "src/index.tsx",
"src/index_ts": "src/index.ts",
"src/index_js": "src/index.js",
"src/index_jsx": "src/index.jsx",
"__tests__/index_spec_tsx": "__tests__/index.spec.tsx",
"__tests__/index_spec_ts": "__tests__/index.spec.ts",
"__tests__/index_spec_js": "__tests__/index.spec.js",
"__tests__/index_spec_jsx": "__tests__/index.spec.jsx",
gitignore: ".gitignore",
babelrc: "babel.config.js",
travis_yml: ".travis.yml",
jest_config_js: "jest.config.js",
rollup_config_js: "rollup.config.js",
releaserc: ".releaserc",
README_md: "README.md",
_package_json: "package.json",
_tsconfig_json: "tsconfig.json",
_eslint_json: ".eslintrc.json",
},
});
return actions;
},
async completed() {
const { cliOptions } = this.sao.opts;
const isSilentMode = cliOptions.silent;
const useSemanticRelease = this.answers.useSemanticRelease;
const skipSemanticReleaseSetup = cliOptions.skipSemanticReleaseSetup;
const pm = this.answers.pm;
await spinner(this.gitInit(), {
startMessage: "Initializing git...",
endMessage: "Initialized git",
failMessage: "Failed to initialize git",
});
await spinner(createProcess(pm, ["install"], { cwd: this.outDir }), {
startMessage: "Installing packages with " + pm + "...",
endMessage: "Package installed with " + pm,
failMessage: "Failed to install packages with " + pm,
});
if (!skipSemanticReleaseSetup && !isSilentMode && useSemanticRelease) {
await spinner(
createProcess(
pm,
[pm === "npm" ? "install" : "add", "semantic-release-cli"],
{ cwd: this.outDir }
),
{
startMessage: "Installing semantic-release-cli...",
endMessage: "Installed semantic-release-cli",
failMessage: "Failed to install semantic-release-cli",
}
);
console.log("✓ Prepared semantic release for setup");
spawn.sync("./node_modules/.bin/semantic-release-cli", ["setup"], {
cwd: this.outDir,
stdio: "inherit",
});
await spinner(
createProcess(
pm,
[pm === "npm" ? "uninstall" : "remove", "semantic-release-cli"],
{ cwd: this.outDir }
),
{
startMessage: "Removing semantic-release-cli",
endMessage: "Removed semantic-release-cli",
failMessage: "Failed to remove semantic-release-cli",
}
);
}
if (isSilentMode && useSemanticRelease) {
console.log(
"- Semantic release setup was skipped, because of the silent mode. You will have to do it manually. See more on https://github.com/epranka/create-package#silent-mode"
);
}
if (!skipSemanticReleaseSetup && useSemanticRelease) {
await spinner(
createProcess(
"./node_modules/.bin/commitizen",
["init", "cz-conventional-changelog"],
{ cwd: this.outDir }
),
{
startMessage:
"Initializing commitizen (this may take a bit longer)...",
endMessage: "Initialized commitizen",
failMessage: "Failed to initiate commitizen",
}
);
}
},
};