-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathindex.js
173 lines (151 loc) · 4.32 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
'use strict';
const Generator = require('yeoman-generator');
const licenses = [
{ name: 'Apache 2.0', value: 'Apache-2.0' },
{ name: 'MIT', value: 'MIT' },
{ name: 'Mozilla Public License 2.0', value: 'MPL-2.0' },
{ name: 'BSD 2-Clause (FreeBSD) License', value: 'BSD-2-Clause-FreeBSD' },
{ name: 'BSD 3-Clause (NewBSD) License', value: 'BSD-3-Clause' },
{ name: 'Internet Systems Consortium (ISC) License', value: 'ISC' },
{ name: 'GNU AGPL 3.0', value: 'AGPL-3.0' },
{ name: 'GNU GPL 3.0', value: 'GPL-3.0' },
{ name: 'GNU LGPL 3.0', value: 'LGPL-3.0' },
{ name: 'Unlicense', value: 'Unlicense' },
{ name: 'No License (Copyrighted)', value: 'UNLICENSED' }
];
module.exports = class GeneratorLicense extends Generator {
constructor(args, opts) {
super(args, opts);
this.option('name', {
type: String,
desc: 'Name of the license owner',
required: false
});
this.option('email', {
type: String,
desc: 'Email of the license owner',
required: false
});
this.option('website', {
type: String,
desc: 'Website of the license owner',
required: false
});
this.option('year', {
type: String,
desc: 'Year(s) to include on the license',
required: false,
defaults: new Date().getFullYear()
});
this.option('licensePrompt', {
type: String,
desc: 'License prompt text',
defaults: 'Which license do you want to use?',
hide: true,
required: false
});
this.option('defaultLicense', {
type: String,
desc: 'Default license',
required: false
});
this.option('license', {
type: String,
desc: 'Select a license, so no license prompt will happen, in case you want to handle it outside of this generator',
required: false
});
this.option('output', {
type: String,
desc: 'Set the output file for the generated license',
required: false,
defaults: 'LICENSE'
});
this.option('publish', {
type: Boolean,
desc: 'Publish the package',
required: false
});
}
initializing() {
this.gitc = {
user: {
name: this.user.git.name(),
email: this.user.git.email()
}
};
}
prompting() {
const prompts = [
{
name: 'name',
message: "What's your name:",
default: this.options.name || this.gitc.user.name,
when: this.options.name === undefined
},
{
name: 'email',
message: 'Your email (optional):',
default: this.options.email || this.gitc.user.email,
when: this.options.email === undefined
},
{
name: 'website',
message: 'Your website (optional):',
default: this.options.website,
when: this.options.website === undefined
},
{
type: 'list',
name: 'license',
message: this.options.licensePrompt,
default: this.options.defaultLicense,
when:
!this.options.license ||
licenses.find((x) => x.value === this.options.license) === undefined,
choices: licenses
}
];
return this.prompt(prompts).then((props) => {
this.props = {
name: this.options.name,
email: this.options.email,
website: this.options.website,
license: this.options.license,
...props
};
});
}
writing() {
// License file
const filename = this.props.license + '.txt';
let author = this.props.name.trim();
if (this.props.email) {
author += ' <' + this.props.email.trim() + '>';
}
if (this.props.website) {
author += ' (' + this.props.website.trim() + ')';
}
this.fs.copyTpl(
this.templatePath(filename),
this.destinationPath(this.options.output),
{
year: this.options.year,
author: author
}
);
// Package
if (!this.fs.exists(this.destinationPath('package.json'))) {
return;
}
const pkg = this.fs.readJSON(this.destinationPath('package.json'), {});
pkg.license = this.props.license;
if (
(this.options.publish === undefined && this.props.license === 'UNLICENSED') ||
this.options.publish === false
) {
pkg.private = true;
}
this.fs.writeJSON(this.destinationPath('package.json'), pkg);
}
};
module.exports.licenses = licenses;