-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
239 lines (228 loc) · 6.78 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
'use strict';
var _ = require('underscore');
var child = require('child_process');
var fs = require('fs');
var inquirer = require('inquirer');
var mkdirp = require('mkdirp');
var os = require('os');
var path = require('path');
var Q = require('q');
var readline = require('readline');
require('colors');
// ## Declarations
var api = {};
var licenses = {
'Apache-2.0': ['name', 'years'],
'BSD-2-Clause': ['name', 'years'],
'BSD-3-Clause': ['name', 'years'],
'CDDL-1.0': [],
'EPL-1.0': [],
'GPL-2.0': ['name', 'software', 'description', 'years'],
'GPL-3.0': ['name', 'software', 'description', 'years'],
'ISC': ['name', 'email', 'years'],
'LGPL-2.1': ['name', 'software', 'description', 'years'],
'LGPL-3.0': [],
'MIT': ['name', 'email', 'years'],
'MPL-2.0': [],
};
// ### Intro `lincensr.intro`
// Displays the introduction header.
//
// __Return values__
// * `intro` The introduction text.
var intro = function () {
return Q.Promise(function (fulfill) {
var intro = 'Welcome to ' + ('Licensr'.magenta) + '!';
intro += '\nIf you don\'t know which license is best suited for you visit:'
intro += '\n * '.grey + 'http://opensource.org/licenses'
intro += '\n * '.grey + 'http://choosealicense.com/'
fulfill(intro);
});
};
api.intro = function (cb) {
return intro().nodeify(cb);
};
// ### Command
// Run a command in the shell and get the result.
//
// __Arguments__
// * `cmd` The command to run.
//
// __Return values__
// * `stdout` The result of the command.
// * `err` The error as issued by `child_process.exec`.
var command = function (cmd) {
return Q.Promise(function (fulfill, reject) {
child.exec(cmd, function (err, stdout) {
if (err) {
return reject(err);
}
return fulfill(stdout.split('\n')[0]);
});
});
};
api.command = function (cmd, cb) {
return command(cmd).nodeify(cb);
};
// ### Prompt License `lincensr.promptLicense`
// Ask user about the license they want.
//
// __Return values__
// * `licence` A license's name.
var promptLicense = function () {
return Q.Promise(function (fulfill) {
var choices = _.keys(licenses);
inquirer.prompt([{
type : 'list',
name : 'license',
message : 'Which license do you want',
paginated : true,
choices : choices,
default : _.indexOf(choices, 'ISC')
}], function (license) {
return fulfill(license);
});
});
};
api.promptLicense = function (cb) {
return promptLicense().nodeify(cb);
};
// ### Prompt Info `lincensr.promptInfo`
// Ask user about information required by the license.
//
// __Arguments__
// * `license` A license's name.
// * `infoDefault` Default values issued by a previous command.
//
// __Return values__
// * `answers` Information about the user.
var promptInfo = function (license, infoDefault) {
if (infoDefault === undefined) {
infoDefault = {
name : undefined,
email: undefined
};
}
return Q.Promise(function (fulfill) {
var needed = licenses[license];
var questions = [];
// What's your name?
if (_.contains(needed, 'name')) {
questions.push({
type : 'input',
name : 'name',
message : 'What\'s your name',
default : infoDefault.name || 'John Doe',
validate: function (name) {
return (name.trim().length === 0) ? 'Your name is required' : true;
}
});
}
// What's your email?
if (_.contains(needed, 'email')) {
questions.push({
type : 'input',
name : 'email',
message : 'What\'s your email',
default : infoDefault.email || '[email protected]',
validate: function (email) {
return (/^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/.test(email) === false)
? 'Enter a valid email'
: true;
}
});
}
// What's the name of your app?
if (_.contains(needed, 'software')) {
questions.push({
type : 'input',
name : 'software',
message : 'What\'s the name of your app',
default : path.basename(path.resolve('.')),
validate: function (software) {
return (software.trim().length === 0) ? 'A name is required' : true;
}
});
}
// Write a brief description of your app
if (_.contains(needed, 'description')) {
questions.push({
type : 'input',
name : 'description',
message : 'Write a brief description of your app',
validate: function (description) {
return (description.trim().length === 0)
? 'A description is required'
: true;
}
});
}
// On which years does it apply?
if (_.contains(needed, 'years')) {
questions.push({
type : 'input',
name : 'years',
message : 'On which years does it apply',
default : new Date().getFullYear(),
validate: function (years) {
return (/^\d{4}(-\d{4})?$/.test(years) === false)
? 'Enter a four digit year or an range of years'
: true;
}
});
}
// Where do you want to save the file?
questions.push({
type : 'input',
name : 'file',
message : 'Where do you want to save the file',
default : './LICENSE',
});
inquirer.prompt(questions, function (answers) {
return fulfill(answers);
});
});
};
api.promptInfo = function (license, infoDefault, cb) {
return promptInfo(license, infoDefault).nodeify(cb);
};
// ### Write
// Write the license.
//
// __Arguments__
// * `data` The summary fo needed data.
//
// __Return values__
// * `err` If there is any error.
// * `content` Your pimped license!
var write = function (data) {
return Q.Promise(function (fulfill, reject) {
var lics = fs.readdirSync(path.resolve(__dirname, 'licenses'));
if (!_.contains(lics, data.license)) {
return reject(new Error('Wrong license name'));
}
var read = path.resolve(__dirname, 'licenses', data.license);
var txt = fs.readFileSync(read).toString();
txt = txt.replace(/%NAME%/g, data.name);
txt = txt.replace(/%EMAIL%/g, data.email);
txt = txt.replace(/%SOFTWARE%/g, data.software);
txt = txt.replace(/%DESCRIPTION%/g, data.description);
txt = txt.replace(/%YEARS%/g, data.years);
mkdirp(path.dirname(path.resolve(data.file)), function (err) {
if (err) {
return reject(err);
}
fs.writeFile(path.resolve(data.file), txt, function (err) {
if (err) {
return reject(err);
}
return fulfill(txt);
});
});
});
};
api.write = function (data, cb) {
return write(data).nodeify(cb);
};
// Expose the API
module.exports = api;