-
-
Notifications
You must be signed in to change notification settings - Fork 376
/
cli.js
54 lines (45 loc) 路 1.37 KB
/
cli.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
const { join } = require('path');
const { mkdir, symlink } = require('fs').promises;
const cmd = require('../../lib/commands');
const { tmpDir } = require('./output');
const shell = require('shelljs');
const root = join(__dirname, '../../../..');
async function linkPackage(name, from, to) {
await symlink(
join(from, 'node_modules', name),
join(to, 'node_modules', name)
);
}
const argv = {
_: [],
src: 'src',
dest: 'build',
config: 'preact.config.js',
prerenderUrls: 'prerender-urls.json',
'inline-css': true,
};
exports.create = async function (template, name) {
let dest = tmpDir();
name = name || `test-${template}`;
await cmd.create(template, dest, { name, cwd: '.' });
return dest;
};
exports.build = async function (cwd, options, installNodeModules = false) {
if (!installNodeModules) {
await mkdir(join(cwd, 'node_modules'), { recursive: true }); // ensure exists, avoid exit()
await linkPackage('preact', root, cwd);
await linkPackage('preact-render-to-string', root, cwd);
} else {
shell.cd(cwd);
shell.exec('npm i');
}
let opts = Object.assign({}, { cwd }, argv, options);
return await cmd.build(argv.src, opts);
};
exports.watch = function (cwd, port, host = '127.0.0.1') {
const args = { ...argv };
delete args.dest;
delete args['inline-css'];
let opts = Object.assign({ cwd, host, port, https: false }, args);
return cmd.watch(argv.src, opts);
};