-
-
Notifications
You must be signed in to change notification settings - Fork 165
/
Copy pathindex.js
403 lines (348 loc) · 10.1 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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
'use strict';
const _ = require('lodash');
const extend = _.merge;
const Generator = require('yeoman-generator');
const parseAuthor = require('parse-author');
const githubUsername = require('github-username');
const path = require('path');
const askName = require('inquirer-npm-name');
const chalk = require('chalk');
const validatePackageName = require('validate-npm-package-name');
const pkgJson = require('../../package.json');
module.exports = class extends Generator {
constructor(args, options) {
super(args, options);
this.option('travis', {
type: Boolean,
required: false,
default: true,
desc: 'Include travis config'
});
this.option('boilerplate', {
type: Boolean,
required: false,
default: true,
desc: 'Include boilerplate files'
});
this.option('cli', {
type: Boolean,
required: false,
default: false,
desc: 'Add a CLI'
});
this.option('coveralls', {
type: Boolean,
required: false,
desc: 'Include coveralls config'
});
this.option('editorconfig', {
type: Boolean,
required: false,
default: true,
desc: 'Include a .editorconfig file'
});
this.option('license', {
type: Boolean,
required: false,
default: true,
desc: 'Include a license'
});
this.option('name', {
type: String,
required: false,
desc: 'Project name'
});
this.option('githubAccount', {
type: String,
required: false,
desc: 'GitHub username or organization'
});
this.option('repositoryName', {
type: String,
required: false,
desc: 'Name of the GitHub repository'
});
this.option('projectRoot', {
type: String,
required: false,
default: 'lib',
desc: 'Relative path to the project code root'
});
this.option('readme', {
type: String,
required: false,
desc: 'Content to insert in the README.md file'
});
}
initializing() {
this.pkg = this.fs.readJSON(this.destinationPath('package.json'), {});
// Pre set the default props from the information we have at this point
this.props = {
name: this.pkg.name,
description: this.pkg.description,
version: this.pkg.version,
homepage: this.pkg.homepage,
repositoryName: this.options.repositoryName
};
if (this.options.name) {
const name = this.options.name;
const packageNameValidity = validatePackageName(name);
if (packageNameValidity.validForNewPackages) {
this.props.name = name;
} else {
this.emit(
'error',
new Error(
_.get(packageNameValidity, 'errors.0') ||
'The name option is not a valid npm package name.'
)
);
}
}
if (_.isObject(this.pkg.author)) {
this.props.authorName = this.pkg.author.name;
this.props.authorEmail = this.pkg.author.email;
this.props.authorUrl = this.pkg.author.url;
} else if (_.isString(this.pkg.author)) {
const info = parseAuthor(this.pkg.author);
this.props.authorName = info.name;
this.props.authorEmail = info.email;
this.props.authorUrl = info.url;
}
}
_getModuleNameParts(name) {
const moduleName = {
name,
repositoryName: this.props.repositoryName
};
if (moduleName.name.startsWith('@')) {
const nameParts = moduleName.name.slice(1).split('/');
Object.assign(moduleName, {
scopeName: nameParts[0],
localName: nameParts[1]
});
} else {
moduleName.localName = moduleName.name;
}
if (!moduleName.repositoryName) {
moduleName.repositoryName = moduleName.localName;
}
return moduleName;
}
_askForModuleName() {
let askedName;
if (this.props.name) {
askedName = Promise.resolve({
name: this.props.name
});
} else {
askedName = askName(
{
name: 'name',
default: path.basename(process.cwd())
},
this
);
}
return askedName.then(answer => {
const moduleNameParts = this._getModuleNameParts(answer.name);
Object.assign(this.props, moduleNameParts);
});
}
_askFor() {
const prompts = [
{
name: 'description',
message: 'Description',
when: !this.props.description
},
{
name: 'homepage',
message: 'Project homepage url',
when: !this.props.homepage
},
{
name: 'authorName',
message: "Author's Name",
when: !this.props.authorName,
default: this.user.git.name(),
store: true
},
{
name: 'authorEmail',
message: "Author's Email",
when: !this.props.authorEmail,
default: this.user.git.email(),
store: true
},
{
name: 'authorUrl',
message: "Author's Homepage",
when: !this.props.authorUrl,
store: true
},
{
name: 'keywords',
message: 'Package keywords (comma to split)',
when: !this.pkg.keywords,
filter(words) {
return words.split(/\s*,\s*/g);
}
},
{
name: 'includeCoveralls',
type: 'confirm',
message: 'Send coverage reports to coveralls',
when: this.options.coveralls === undefined
}
];
return this.prompt(prompts).then(props => {
this.props = extend(this.props, props);
});
}
_askForTravis() {
const prompts = [
{
name: 'node',
message: 'Enter Node versions (comma separated)',
when: this.options.travis
}
];
return this.prompt(prompts).then(props => {
this.props = extend(this.props, props);
});
}
_askForGithubAccount() {
if (this.options.githubAccount) {
this.props.githubAccount = this.options.githubAccount;
return Promise.resolve();
}
let usernamePromise;
if (this.props.scopeName) {
usernamePromise = Promise.resolve(this.props.scopeName);
} else {
usernamePromise = githubUsername(this.props.authorEmail).then(
username => username,
() => ''
);
}
return usernamePromise.then(username => {
return this.prompt({
name: 'githubAccount',
message: 'GitHub username or organization',
default: username
}).then(prompt => {
this.props.githubAccount = prompt.githubAccount;
});
});
}
prompting() {
return this._askForModuleName()
.then(this._askFor.bind(this))
.then(this._askForTravis.bind(this))
.then(this._askForGithubAccount.bind(this));
}
writing() {
// Re-read the content at this point because a composed generator might modify it.
const currentPkg = this.fs.readJSON(this.destinationPath('package.json'), {});
const pkg = extend(
{
name: this.props.name,
version: '0.0.0',
description: this.props.description,
homepage: this.props.homepage,
author: {
name: this.props.authorName,
email: this.props.authorEmail,
url: this.props.authorUrl
},
files: [this.options.projectRoot],
main: path.join(this.options.projectRoot, 'index.js').replace(/\\/g, '/'),
keywords: [],
devDependencies: {},
engines: {
npm: '>= 4.0.0'
}
},
currentPkg
);
if (this.props.includeCoveralls) {
pkg.devDependencies.coveralls = pkgJson.devDependencies.coveralls;
}
// Combine the keywords
if (this.props.keywords && this.props.keywords.length) {
pkg.keywords = _.uniq(this.props.keywords.concat(pkg.keywords));
}
// Let's extend package.json so we're not overwriting user previous fields
this.fs.writeJSON(this.destinationPath('package.json'), pkg);
}
default() {
if (this.options.travis) {
let options = { config: {} };
if (this.props.node) {
// eslint-disable-next-line camelcase
options.config.node_js = this.props.node.split(',');
}
if (this.props.includeCoveralls) {
options.config.after_script = 'cat ./coverage/lcov.info | coveralls'; // eslint-disable-line camelcase
}
this.composeWith(require.resolve('generator-travis/generators/app'), options);
}
if (this.options.editorconfig) {
this.composeWith(require.resolve('../editorconfig'));
}
this.composeWith(require.resolve('../eslint'));
this.composeWith(require.resolve('../git'), {
name: this.props.name,
githubAccount: this.props.githubAccount,
repositoryName: this.props.repositoryName
});
this.composeWith(require.resolve('generator-jest/generators/app'), {
testEnvironment: 'node',
coveralls: false
});
if (this.options.boilerplate) {
this.composeWith(require.resolve('../boilerplate'), {
name: this.props.name
});
}
if (this.options.cli) {
this.composeWith(require.resolve('../cli'));
}
if (this.options.license && !this.pkg.license) {
this.composeWith(require.resolve('generator-license/app'), {
name: this.props.authorName,
email: this.props.authorEmail,
website: this.props.authorUrl
});
}
if (!this.fs.exists(this.destinationPath('README.md'))) {
this.composeWith(require.resolve('../readme'), {
name: this.props.name,
description: this.props.description,
githubAccount: this.props.githubAccount,
repositoryName: this.props.repositoryName,
authorName: this.props.authorName,
authorUrl: this.props.authorUrl,
coveralls: this.props.includeCoveralls,
content: this.options.readme
});
}
}
installing() {
this.npmInstall();
}
end() {
this.log('Thanks for using Yeoman.');
if (this.options.travis) {
let travisUrl = chalk.cyan(
`https://travis-ci.com/profile/${this.props.githubAccount || ''}`
);
this.log(`- Enable Travis integration at ${travisUrl}`);
}
if (this.props.includeCoveralls) {
let coverallsUrl = chalk.cyan('https://coveralls.io/repos/new');
this.log(`- Enable Coveralls integration at ${coverallsUrl}`);
}
}
};