-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·95 lines (88 loc) · 2.4 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
#!/usr/bin/env node
const pwd = process.cwd();
const fs = require('fs');
const path = require('path');
const glob = require('glob');
const inquirer = require('inquirer');
const memFs = require('mem-fs');
const memFsEditor = require('mem-fs-editor');
const execSync = require('child_process').execSync;
const cwd = path.join(__dirname, 'files');
const mfe = memFsEditor.create(memFs.create());
const isGitRepo = fs.existsSync('.git');
const repoUrl = isGitRepo && execGitSync('git ls-remote --get-url origin') || '';
const match = repoUrl.match(/github\.com[:/]([^/]+)\/([^/]+)\.git/);
const group = match && match[1] || '';
const repo = match && match[2] || '';
function execGitSync(command, trim = true) {
try {
const ret = execSync(command).toString('utf-8');
return trim ? ret.trim() : ret;
} catch(err) {
return '';
}
}
function ask() {
const name = repo || path.basename(pwd);
return inquirer.prompt([
{
name: 'name',
default: name,
validate(input) {
return input.trim() ? true : 'Please input name'
}
}, {
name: 'group',
default: group,
validate(input) {
return input.trim() ? true : 'Please input group'
}
}, {
name: 'description',
default(answers) {
return answers.name;
},
validate(input) {
return input.trim() ? true : 'Please input description'
}
}, {
name: 'keys',
default(answers) {
return answers.name.split(/-|_/).join(',')
},
validate(input) {
return input.trim() ? true : 'Please input keys'
}
}, {
name: 'author',
default: execGitSync('git config --get user.email') || '',
validate(input) {
return input.trim() ? true : 'Please input author'
}
}
]).then((answers) => {
answers.keys = JSON.stringify(answers.keys.split(',').filter(k => !!k).map(k => k.trim()), null, 2);
return answers;
});
}
function copy(answers) {
glob.sync('**/*', {
cwd,
mark: true,
dot: true
}).forEach((file) => {
mfe.copyTpl(path.join(cwd, file), path.join(pwd, file), answers);
});
return new Promise((resolve) => {
mfe.commit(function() {
resolve();
});
});
}
ask().then(copy).then(() => {
if (!isGitRepo) execSync('git init', { stdio: 'inherit' });
execSync('npm i', { stdio: 'inherit' });
console.log('Success!');
}).catch((err) => {
console.error(err);
});