forked from lassjs/lass
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sao.js
305 lines (287 loc) · 8.21 KB
/
sao.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
const fs = require('fs');
const path = require('path');
const process = require('process');
const camelcase = require('camelcase');
const debug = require('debug')('lass');
const execa = require('execa');
const fetchGithubUsername = require('github-username');
const fixpack = require('fixpack');
const githubUsernameRegex = require('github-username-regex');
const isEmail = require('is-email');
const isURL = require('is-url');
const isValidNpmName = require('is-valid-npm-name');
const npmConf = require('npm-conf');
const semver = require('semver');
const slug = require('speakingurl');
const spawn = require('cross-spawn');
const spdxLicenseList = require('spdx-license-list/full');
const superb = require('superb');
const uppercamelcase = require('uppercamelcase');
const { which } = require('shelljs');
const conf = npmConf();
const defaultLicense =
conf.get('init-license') === 'ISC' ? 'MIT' : conf.get('init-license');
module.exports = {
updateNotify: true,
enforceNewFolder: true,
templateOptions: {
context: {
camelcase,
uppercamelcase
}
},
prompts: {
name: {
message: 'What is the name of the new package',
default: () => process.argv[2] || ':folderName:',
validate(value) {
if (process.env.NODE_ENV === 'test' && value === 'lass') return true;
return isValidNpmName(value);
}
},
description: {
message: 'How would you describe the new package',
default: `my ${superb.random()} project`,
validate: (value) =>
/"/.test(value) ? 'Description cannot contain double quotes' : true
},
pm: {
message: 'Choose a package manager',
choices: ['npm', 'yarn'],
type: 'list',
default: 'npm',
store: true
},
public: {
message: 'Publicly available package',
type: 'confirm',
default: true
},
license: {
message: 'Choose a license',
choices: Object.keys(spdxLicenseList),
type: 'list',
default: defaultLicense,
store: true
},
version: {
message: 'Choose an initial semver version',
default: conf.get('init-version') || '0.0.0',
validate: (value) =>
semver.valid(value) ? true : 'Invalid semver version'
},
author: {
message: "What is your name (the author's)",
default: conf.get('init-author-name') || ':gitUser:',
store: true
},
email: {
message: "What is your email (the author's)",
default: conf.get('init-author-email') || ':gitEmail:',
store: true,
validate: (value) => (isEmail(value) ? true : 'Invalid email')
},
website: {
message: "What is your personal website (the author's)",
default: conf.get('init-author-url') || '',
store: true,
validate: (value) => (value === '' || isURL(value) ? true : 'Invalid URL')
},
username: {
message: 'What is your GitHub username or organization',
store: true,
async default(answers) {
if (answers.name.indexOf('@') === 0)
return answers.name.split('/')[0].slice(1);
try {
const githubUsername = await fetchGithubUsername(answers.email);
return githubUsername;
} catch (err) {
debug(err);
return ':gitUser:';
}
},
validate: (value) =>
githubUsernameRegex.test(value) ? true : 'Invalid GitHub username'
},
repo: {
message: "What is your GitHub repository's URL",
default(answers) {
const name =
answers.name.indexOf('@') === 0
? answers.name.split('/')[1]
: slug(answers.name);
return `https://github.com/${slug(answers.username, {
maintainCase: true
})}/${name}`;
},
validate(value) {
return isURL(value) &&
value.indexOf('https://github.com/') === 0 &&
value.lastIndexOf('/') !== value.length - 1
? true
: 'Please include a valid GitHub.com URL without a trailing slash';
}
},
keywords: {
message:
'Write some keywords related to your project (comma/space separated)',
default(answers) {
return `${answers.name} lass`;
}
},
coverage: {
message: 'Check code coverage after tests run',
type: 'confirm',
default: false,
store: true
},
threshold: {
when: (answers) => answers.coverage,
type: 'list',
message:
'Select code coverage threshold required to pass (across lines/functions/branches)',
choices: ['100', '90', '80', '70', '60', '50', '40', '30', '20', '10'],
store: true
},
linter: {
message: 'Choose a linter.',
choices: ['eslint', 'xo'],
type: 'list',
default: 'xo',
store: true
}
},
move: {
// We keep `.gitignore` as `gitignore` in the project
// Because when it's published to npm
// `.gitignore` file will be ignored!
gitignore: '.gitignore',
README: 'README.md',
package: 'package.json',
nycrc: '.nycrc'
},
filters: {
// Exclude MIT license from being copied
LICENSE: 'license === "MIT"',
// Until this issue is resolved we need this line:
// <https://github.com/saojs/sao/issues/59>
'node_modules/**': false,
'.eslintrc': 'linter === "eslint"'
},
async post(ctx) {
ctx.gitInit();
const gh = ctx.answers.repo
.replace('https://github.com/', '')
.replace('.git', '');
// <https://github.com/saojs/sao/issues/133>
const cmd = `remote add origin [email protected]:${gh}.git`;
const proc = spawn.sync('git', cmd.split(' '), {
cwd: ctx.folderPath,
stdio: 'inherit'
});
if (proc.error && proc.error.code === 'ENOENT') {
ctx.log.warn(
`${ctx.chalk.bold(
'git'
)} was not installed on this machine, therefore \`${cmd}\` was skipped.`
);
}
// Create `LICENSE` file with license selected
if (ctx.answers.license !== 'MIT') {
try {
fs.writeFileSync(
path.join(ctx.folderName, 'LICENSE'),
spdxLicenseList[ctx.answers.license].licenseText
);
ctx.logger.warn(
`You should update the ${ctx.chalk.yellow(
'LICENSE'
)} file accordingly (e.g. add your name/company/year)`
);
} catch (err) {
ctx.logger.error(err);
}
}
// Comment links for user
[
ctx.answers.repo,
`https://travis-ci.com/${gh}`,
`https://codecov.io/gh/${gh}`
].map((link) => console.log(`TODO: ${link}`));
// Fix package.json file
fixpack(`${ctx.folderPath}/package.json`);
// Install packages
if (ctx.answers.pm === 'yarn') ctx.yarnInstall();
else ctx.npmInstall();
// add husky hooks
await execa('./node_modules/.bin/husky', ['install'], {
cwd: ctx.folderPath,
stdio: 'inherit'
});
await Promise.all([
execa(
'./node_modules/.bin/husky',
[
'add',
'.husky/commit-msg',
`${
ctx.answers.pm === 'yarn' ? 'yarn' : 'npx --no-install'
} commitlint --edit $1`
],
{
cwd: ctx.folderPath,
stdio: 'inherit'
}
),
execa(
'./node_modules/.bin/husky',
[
'add',
'.husky/pre-commit',
`${
ctx.answers.pm === 'yarn' ? 'yarn' : 'npx --no-install'
} lint-staged`
],
{
cwd: ctx.folderPath,
stdio: 'inherit'
}
)
]);
// Format code with xo
if (ctx.answers.linter === 'eslint') {
await execa('./node_modules/.bin/eslint', ['.', '--fix'], {
cwd: ctx.folderPath,
stdio: 'inherit'
});
} else {
await execa('./node_modules/.bin/xo', ['--fix'], {
cwd: ctx.folderPath,
stdio: 'inherit'
});
}
// Run tests
await execa(which(ctx.answers.pm).stdout, ['test'], {
cwd: ctx.folderPath,
stdio: 'inherit'
});
// Make initial commit
spawn.sync('git', ['add', '-A'], {
cwd: ctx.folderPath
});
spawn.sync(
'git',
[
'commit',
'-m',
'feat: initial commit from https://lass.js.org',
'--no-verify'
],
{
cwd: ctx.folderPath
}
);
ctx.showTip();
}
};