-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.js
65 lines (53 loc) · 1.68 KB
/
setup.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
const spawn = require('cross-spawn-promise');
const { hasCommand, warn } = require('./util');
const stdio = 'ignore';
exports.install = function (cwd, isYarn) {
let cmd = isYarn ? 'yarn' : 'npm';
return spawn(cmd, ['install'], { cwd, stdio: 'inherit' });
};
exports.addScripts = async function (obj, cwd, isYarn) {
let cmd = isYarn ? 'yarn' : 'npm';
let args = isYarn ? ['add', '--dev'] : ['install', '--save-dev'];
// Install `if-env`
await spawn(cmd, [...args, 'if-env'], { cwd, stdio });
return {
build: 'preact build',
serve: 'preact build && preact serve',
start: `if-env NODE_ENV=production && ${cmd} run -s serve || ${cmd} run -s watch`,
watch: 'preact watch',
};
};
// Initializes the folder using `git init` and a proper `.gitignore` file
// if `git` is present in the $PATH.
exports.initGit = async function (target) {
let git = hasCommand('git');
if (git) {
const cwd = target;
await spawn('git', ['init'], { cwd });
await spawn('git', ['add', '-A'], { cwd });
let gitUser, gitEmail;
const defaultGitUser = 'Preact CLI';
const defaultGitEmail = 'preact-cli@users.noreply.github.com';
try {
gitEmail = (await spawn('git', ['config', 'user.email'])).toString();
} catch (e) {
gitEmail = defaultGitEmail;
}
try {
gitUser = (await spawn('git', ['config', 'user.name'])).toString();
} catch (e) {
gitUser = defaultGitUser;
}
await spawn('git', ['commit', '-m', 'initial commit from Preact CLI'], {
cwd,
env: {
GIT_COMMITTER_NAME: gitUser,
GIT_COMMITTER_EMAIL: gitEmail,
GIT_AUTHOR_NAME: defaultGitUser,
GIT_AUTHOR_EMAIL: defaultGitEmail,
},
});
} else {
warn('Could not locate `git` binary in `$PATH`. Skipping!');
}
};