|
1 | | -import { program } from 'commander'; |
| 1 | +import { type Command, program } from 'commander'; |
2 | 2 | import { build } from '../build'; |
3 | | -import type { RslibConfig } from '../types'; |
| 3 | +import { loadConfig } from '../config'; |
| 4 | +import { logger } from '../utils/logger'; |
| 5 | + |
| 6 | +export type CommonOptions = { |
| 7 | + config?: string; |
| 8 | + envMode?: string; |
| 9 | +}; |
| 10 | + |
| 11 | +export type BuildOptions = CommonOptions & { |
| 12 | + watch?: boolean; |
| 13 | +}; |
| 14 | + |
| 15 | +const applyCommonOptions = (command: Command) => { |
| 16 | + command |
| 17 | + .option( |
| 18 | + '-c --config <config>', |
| 19 | + 'specify the configuration file, can be a relative or absolute path', |
| 20 | + ) |
| 21 | + .option( |
| 22 | + '--env-mode <mode>', |
| 23 | + 'specify the env mode to load the `.env.[mode]` file', |
| 24 | + ); |
| 25 | +}; |
4 | 26 |
|
5 | 27 | export function runCli() { |
6 | 28 | program.name('rslib').usage('<command> [options]').version(RSLIB_VERSION); |
7 | 29 |
|
8 | 30 | const buildCommand = program.command('build'); |
9 | 31 |
|
| 32 | + [buildCommand].forEach(applyCommonOptions); |
| 33 | + |
10 | 34 | buildCommand |
11 | | - .description('build the app for production') |
12 | | - .option('--entry <entry>', 'entrypoint file to build') |
13 | | - .option('--outDir <outDir>', 'dist directory to build to') |
14 | | - .action(async (options: RslibConfig) => { |
| 35 | + .option('-w --watch', 'turn on watch mode, watch for changes and rebuild') |
| 36 | + .description('build the library for production') |
| 37 | + .action(async (options: BuildOptions) => { |
15 | 38 | try { |
16 | | - await build({ |
17 | | - entry: options.entry, |
18 | | - outDir: options.outDir, |
19 | | - }); |
| 39 | + const rslibConfig = await loadConfig(options.config, options.envMode); |
| 40 | + await build(rslibConfig, options); |
20 | 41 | } catch (err) { |
| 42 | + logger.error('Failed to build.'); |
| 43 | + logger.error(err); |
21 | 44 | process.exit(1); |
22 | 45 | } |
23 | 46 | }); |
|
0 commit comments