|
1 | 1 | import { cac } from 'cac'
|
2 | 2 | import { name, version } from '../../package.json'
|
3 |
| -import type { CliOptions } from './types' |
4 | 3 | import { build, serve } from './index'
|
5 | 4 |
|
6 |
| -export async function startCli( |
7 |
| - cwd = process.cwd(), |
8 |
| - argv = process.argv, |
9 |
| - options: CliOptions = {}, |
10 |
| -) { |
| 5 | +export async function startCli(cwd = process.cwd(), argv = process.argv) { |
11 | 6 | const cli = cac(name)
|
12 | 7 |
|
13 | 8 | cli
|
14 |
| - .command('serve [file]', 'Panel input file', { |
15 |
| - ignoreOptionDefaultValue: true, |
16 |
| - }) |
17 |
| - .action(async (file, flags) => { |
18 |
| - Object.assign(options, { |
19 |
| - cwd, |
20 |
| - ...flags, |
21 |
| - }) |
22 |
| - |
23 |
| - if (file) |
24 |
| - options.entry = file |
25 |
| - |
26 |
| - const server = await serve(options) |
27 |
| - process.on('SIGINT', () => server.close()) |
28 |
| - }) |
29 |
| - |
30 |
| - cli |
31 |
| - .command('[file]', 'Panel input file', { |
32 |
| - ignoreOptionDefaultValue: true, |
33 |
| - }) |
34 |
| - .option('-d, --out-dir <dir>', 'Output directory', { |
35 |
| - default: cwd, |
36 |
| - }) |
| 9 | + .command('<file>', 'Compile the panel plugin to index.js and index.css') |
| 10 | + .option('-d, --out-dir <dir>', 'Output directory', { default: cwd }) |
37 | 11 | .option(
|
38 | 12 | '-w, --watch [path]',
|
39 |
| - 'Watch for file changes. If no path is specified, the folder of the input file will be watched. Repeat "--watch" for multiple paths.', |
| 13 | + 'Watch for file changes. If no path is specified, the folder of the input file will be watched', |
| 14 | + { default: false }, |
40 | 15 | )
|
41 |
| - .action(async (file: string, flags) => { |
42 |
| - Object.assign(options, { |
43 |
| - cwd, |
44 |
| - ...flags, |
45 |
| - }) |
| 16 | + .example('kirbyup src/index.js') |
| 17 | + .example('kirbyup src/index.js --out-dir ~/kirby-site/site/plugins/demo') |
| 18 | + .example('kirbyup src/index.js --watch src/**/*.{js,css} --watch assets/*\n') |
| 19 | + .action(async (file: string, options: { outDir: string; watch: boolean | string | string[] }) => { |
| 20 | + await build({ cwd, entry: file, ...options }) |
| 21 | + }) |
46 | 22 |
|
47 |
| - if (file) |
48 |
| - options.entry = file |
| 23 | + cli |
| 24 | + .command('serve <file>', 'Start development server with live reload') |
| 25 | + .option('--no-watch', 'Don\'t watch .php files for changes', { default: '\0' }) |
| 26 | + .option('-w, --watch <path>', 'Watch additional files', { default: './**/*.php' }) |
| 27 | + .example('kirbyup serve src/index.js') |
| 28 | + .example('kirbyup serve src/index.js --no-watch') |
| 29 | + .example('kirbyup serve src/index.js --watch snippets/*.php --watch templates/*.php\n') |
| 30 | + .action(async (file: string, options: { watch: false | string | string[] }) => { |
| 31 | + const server = await serve({ cwd, entry: file, ...options }) |
49 | 32 |
|
50 |
| - await build(options) |
| 33 | + process.on('SIGINT', () => server.close()) |
| 34 | + process.on('exit', () => server.close()) |
| 35 | + process.on('uncaughtException', () => { |
| 36 | + process.exitCode = 1 |
| 37 | + server.close() |
| 38 | + }) |
51 | 39 | })
|
52 | 40 |
|
53 |
| - cli.help() |
| 41 | + // Filter out unnecessary "default" output for negated options (zerobyte acts as marker) |
| 42 | + cli.help(s => s.map(msg => ({ ...msg, body: msg.body.replace(' (default: \0)', '') }))) |
54 | 43 | cli.version(version)
|
55 | 44 |
|
56 | 45 | // Parse CLI args without running the command to
|
|
0 commit comments