forked from preactjs/preact-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate.js
199 lines (162 loc) · 4.39 KB
/
create.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
import asyncCommand from '../lib/async-command';
import fs from 'fs.promised';
import copy from 'recursive-copy';
import mkdirp from 'mkdirp';
import ora from 'ora';
import promisify from 'es6-promisify';
import spawn from 'cross-spawn-promise';
import path from 'path';
import install from '../lib/install-dependencies';
import which from 'which';
const TEMPLATES = {
default: 'examples/full',
full: 'examples/full',
empty: 'examples/empty',
root: 'examples/root',
simple: 'examples/simple'
};
export default asyncCommand({
command: 'create <name> [dest]',
desc: 'Create a new application.',
builder: {
name: {
description: 'directory and package name for the new app'
},
dest: {
description: 'Directory to create the app within',
defaultDescription: '<name>'
},
type: {
description: 'A project template to start from',
choices: [
'default',
'root',
'simple',
'empty'
],
default: 'default'
},
less: {
description: 'Pre-install LESS support',
type: 'boolean',
default: false
},
sass: {
description: 'Pre-install SASS/SCSS support',
type: 'boolean',
default: false
},
install: {
description: 'Install dependencies',
type: 'boolean',
default: true
}
},
async handler(argv) {
let template = TEMPLATES[argv.type];
if (!template) {
throw Error(`Unknown app template "${argv.type}".`);
}
let target = path.resolve(process.cwd(), argv.dest || argv.name);
let exists = false;
try {
exists = (await fs.stat(target)).isDirectory();
}
catch (err) {}
if (exists) {
throw Error('Directory already exists.');
}
let spinner = ora({
text: 'Creating project',
color: 'magenta'
}).start();
await promisify(mkdirp)(target);
await copy(
path.resolve(__dirname, '../..', template),
target,
{ filter: ['**/*', '!build'] }
);
spinner.text = 'Initializing project';
await spawn('npm', ['init', '-y'], { cwd: target, stdio: 'ignore' });
let pkg = JSON.parse(await fs.readFile(path.resolve(target, 'package.json')));
pkg.scripts = {
...(pkg.scripts || {}),
start: 'if-env NODE_ENV=production && npm run -s serve || npm run -s dev',
build: 'preact build',
serve: 'preact build && preact serve',
dev: 'preact watch',
test: 'eslint src && preact test'
};
try {
await fs.stat(path.resolve(target, 'src'));
}
catch (err) {
pkg.scripts.test = pkg.scripts.test.replace('src', '.');
}
pkg.eslintConfig = {
extends: 'eslint-config-synacor'
};
await fs.writeFile(path.resolve(target, 'package.json'), JSON.stringify(pkg, null, 2));
if (argv.install) {
spinner.text = 'Installing dev dependencies';
await install(target, [
'preact-cli',
'if-env',
'eslint',
'eslint-config-synacor',
// install sass setup if --sass
...(argv.sass ? [
'node-sass',
'sass-loader'
] : []),
// install less setup if --less
...(argv.less ? [
'less',
'less-loader'
] : [])
], 'dev');
spinner.text = 'Installing dependencies';
await install(target, [
'preact',
'preact-compat',
'preact-router'
]);
spinner.succeed('Done!\n');
await initializeVersionControl(target);
}
return trimLeft(`
To get started, cd into the new directory:
\u001b[32mcd ${path.relative(process.cwd(), target)}\u001b[39m
To start a development live-reload server:
\u001b[32mnpm start\u001b[39m
To create a production build (in ./build):
\u001b[32mnpm run build\u001b[39m
To start a production HTTP/2 server:
\u001b[32mnpm run serve\u001b[39m
`) + '\n';
}
});
const trimLeft = (string) => string.trim().replace(/^\t+/gm, '');
const npm = (cwd, args) => spawn('npm', args, { cwd, stdio: 'ignore' });
// Initializes the folder using `git init` and a proper `.gitignore` file
// if `git` is present in the $PATH.
async function initializeVersionControl(target) {
let git;
try {
git = await promisify(which)('git');
} catch (e) {}
if (git) {
const gitignore = trimLeft(`
node_modules
/build
/*.log
`) + '\n';
const gitignorePath = path.resolve(target, '.gitignore');
await fs.writeFile(gitignorePath, gitignore);
const cwd = target;
await spawn('git', ['init'], { cwd });
await spawn('git', ['add', '-A'], { cwd });
const gitUser = 'Preact CLI<[email protected]>';
await spawn('git', ['commit', '--author', gitUser, '-m', 'initial commit from Preact CLI'], { cwd });
}
}