|
1 | 1 | import { cac } from 'cac'
|
2 |
| -import { loadConfig } from '@nicepkg/gpt-runner-config' |
3 |
| -import { toArray } from '@nicepkg/gpt-runner-core' |
| 2 | +import { loadUserConfig } from '@nicepkg/gpt-runner-core' |
| 3 | +import { PathUtils, getLocalHostname, getPort, openInBrowser } from '@nicepkg/gpt-runner-shared/node' |
| 4 | +import { consola } from 'consola' |
| 5 | +import { cyan, green } from 'colorette' |
| 6 | +import { execa } from 'execa' |
| 7 | +import waitPort from 'wait-port' |
| 8 | +import { Debug } from '@nicepkg/gpt-runner-shared/common' |
4 | 9 | import { version } from '../package.json'
|
5 | 10 | import type { CliOptions } from './types'
|
6 |
| -import { build } from './index' |
| 11 | + |
| 12 | +const __dirname = PathUtils.getCurrentDirName(import.meta.url) |
| 13 | +const startServerJsPath = PathUtils.resolve(__dirname, '../node_modules/@nicepkg/gpt-runner-web/dist/start-server.mjs') |
7 | 14 |
|
8 | 15 | export async function startCli(cwd = process.cwd(), argv = process.argv, options: CliOptions = {}) {
|
9 | 16 | const cli = cac('gptr')
|
10 | 17 |
|
11 | 18 | cli
|
12 |
| - .command('[...patterns]', 'Glob patterns', { |
| 19 | + .command('[...rootPaths]', 'root path', { |
13 | 20 | ignoreOptionDefaultValue: true,
|
14 | 21 | })
|
15 |
| - .option('-o, --out-file <file>', 'Output file', { |
16 |
| - default: cwd, |
| 22 | + .option('-p, --port [port number]', 'Server port', { |
| 23 | + default: 3003, |
17 | 24 | })
|
18 |
| - .option('-c, --config [file]', 'Config file') |
| 25 | + .option('--no-open', 'Open in browser') |
| 26 | + .option('-c, --config [file]', 'Config file path') |
19 | 27 | .option('-w, --watch', 'Watch for file changes')
|
20 |
| - .option('--preflights', 'Enable preflights', { default: true }) |
21 |
| - .option('-m, --minify', 'Minify generated CSS', { default: false }) |
22 |
| - .action(async (patterns: Array<string>, flags) => { |
| 28 | + .option('--debug', 'Debug mode') |
| 29 | + .action(async (rootPaths: Array<string>, flags) => { |
23 | 30 | Object.assign(options, {
|
24 | 31 | cwd,
|
25 | 32 | ...flags,
|
| 33 | + rootPath: rootPaths?.[0] || options.rootPath, |
| 34 | + }) |
| 35 | + |
| 36 | + if (options.debug) |
| 37 | + process.env.DEBUG = 'enabled' |
| 38 | + |
| 39 | + const debug = new Debug('gpt-runner-cli') |
| 40 | + debug.log('parse cli options', options) |
| 41 | + |
| 42 | + const { config } = await loadUserConfig(options.rootPath || options.cwd, options.config) |
| 43 | + |
| 44 | + // TODO: add support for config file and watching |
| 45 | + debug.log('parse user config', config) |
| 46 | + |
| 47 | + const finalPort = await getPort({ |
| 48 | + defaultPort: options.port ?? 3003, |
| 49 | + autoFreePort: true, |
26 | 50 | })
|
27 | 51 |
|
28 |
| - if (patterns) |
29 |
| - options.patterns = patterns |
30 |
| - const { config } = await loadConfig(cwd, options.config) |
31 |
| - |
32 |
| - const entries = toArray(config.cli?.entry || options) |
33 |
| - await Promise.all(entries.map(entry => |
34 |
| - build({ |
35 |
| - ...options, |
36 |
| - ...entry, |
37 |
| - }), |
38 |
| - )) |
| 52 | + const startServerProcessPromise = execa('node', [startServerJsPath, '--port', String(finalPort)], { |
| 53 | + env: { |
| 54 | + ...process.env, |
| 55 | + NODE_OPTIONS: '--experimental-fetch', |
| 56 | + NODE_NO_WARNINGS: '1', |
| 57 | + }, |
| 58 | + }) |
| 59 | + |
| 60 | + startServerProcessPromise.on('error', (error) => { |
| 61 | + consola.error(error) |
| 62 | + }) |
| 63 | + |
| 64 | + const afterServerStartSuccess = () => { |
| 65 | + const getUrl = (isLocalIp = false) => { |
| 66 | + const localIp = getLocalHostname() |
| 67 | + return `http://${isLocalIp ? localIp : 'localhost'}:${finalPort}/#/chat?rootPath=${config.rootPath}` |
| 68 | + } |
| 69 | + |
| 70 | + consola.success(`\n\n${green(`GPT-Runner web is at:\n\n${cyan(getUrl())}\n\n${cyan(getUrl(true))}\n`)}`) |
| 71 | + |
| 72 | + if (options.open ?? true) { |
| 73 | + openInBrowser({ |
| 74 | + url: getUrl(), |
| 75 | + }) |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + waitPort({ |
| 80 | + port: finalPort, |
| 81 | + output: 'silent', |
| 82 | + }).then(afterServerStartSuccess).catch(consola.error) |
| 83 | + |
| 84 | + await startServerProcessPromise |
39 | 85 | })
|
40 | 86 |
|
41 | 87 | cli.help()
|
|
0 commit comments