Skip to content

Commit 7e02171

Browse files
committed
feat(gpt-runner-cli): complete cli
1 parent 5ce0bd6 commit 7e02171

File tree

29 files changed

+197
-280
lines changed

29 files changed

+197
-280
lines changed

.vscode/settings.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
"Chatgpt",
1717
"clsx",
1818
"codicon",
19+
"colorette",
20+
"consola",
1921
"esno",
2022
"execa",
2123
"gptr",

packages/gpt-runner-cli/build.config.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,6 @@ import { defineBuildConfig } from 'unbuild'
22

33
export default defineBuildConfig({
44
entries: [
5-
{
6-
builder: 'rollup',
7-
input: 'src/index',
8-
name: 'index',
9-
},
105
{
116
builder: 'rollup',
127
input: 'src/cli',

packages/gpt-runner-cli/package.json

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,18 @@
1313
"repository": {
1414
"type": "git",
1515
"url": "https://github.com/nicepkg/gpt-runner",
16-
"directory": "packages/cli"
16+
"directory": "packages/gpt-runner-cli"
1717
},
1818
"bugs": {
1919
"url": "https://github.com/nicepkg/gpt-runner/issues"
2020
},
21-
"keywords": [],
22-
"exports": {
23-
".": {
24-
"types": "./dist/index.d.ts",
25-
"import": "./dist/index.mjs"
26-
}
27-
},
28-
"main": "./dist/index.mjs",
29-
"module": "./dist/index.mjs",
30-
"types": "./dist/index.d.ts",
21+
"keywords": [
22+
"gpt-runner",
23+
"chatgpt",
24+
"prompt",
25+
"langchain",
26+
"ai"
27+
],
3128
"bin": {
3229
"gptr": "./bin/gpt-runner.mjs"
3330
},
@@ -43,18 +40,14 @@
4340
"stub": "unbuild --stub"
4441
},
4542
"dependencies": {
46-
"@ampproject/remapping": "^2.2.1",
47-
"@rollup/pluginutils": "^5.0.2",
48-
"@nicepkg/gpt-runner-config": "workspace:*",
4943
"@nicepkg/gpt-runner-core": "workspace:*",
5044
"@nicepkg/gpt-runner-shared": "workspace:*",
45+
"@nicepkg/gpt-runner-web": "workspace:*",
5146
"cac": "^6.7.14",
5247
"chokidar": "^3.5.3",
5348
"colorette": "^2.0.20",
5449
"consola": "^3.1.0",
55-
"fast-glob": "^3.2.12",
56-
"magic-string": "^0.30.0",
57-
"pathe": "^1.1.0",
58-
"perfect-debounce": "^1.0.0"
50+
"execa": "^7.1.1",
51+
"wait-port": "^1.0.4"
5952
}
60-
}
53+
}

packages/gpt-runner-cli/src/cli-start.ts

Lines changed: 67 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,87 @@
11
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'
49
import { version } from '../package.json'
510
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')
714

815
export async function startCli(cwd = process.cwd(), argv = process.argv, options: CliOptions = {}) {
916
const cli = cac('gptr')
1017

1118
cli
12-
.command('[...patterns]', 'Glob patterns', {
19+
.command('[...rootPaths]', 'root path', {
1320
ignoreOptionDefaultValue: true,
1421
})
15-
.option('-o, --out-file <file>', 'Output file', {
16-
default: cwd,
22+
.option('-p, --port [port number]', 'Server port', {
23+
default: 3003,
1724
})
18-
.option('-c, --config [file]', 'Config file')
25+
.option('--no-open', 'Open in browser')
26+
.option('-c, --config [file]', 'Config file path')
1927
.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) => {
2330
Object.assign(options, {
2431
cwd,
2532
...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,
2650
})
2751

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
3985
})
4086

4187
cli.help()

packages/gpt-runner-cli/src/index.ts

Lines changed: 0 additions & 150 deletions
This file was deleted.

packages/gpt-runner-cli/src/types.ts

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,9 @@
1-
/** Mark some properties as required, leaving others unchanged */
2-
declare type MarkRequired<T, RK extends keyof T> = Exclude<T, RK> & Required<Pick<T, RK>>
3-
41
export interface CliOptions {
52
cwd?: string
6-
patterns?: Array<string>
7-
outFile?: string
8-
watch?: boolean
3+
port?: number
4+
rootPath?: string
5+
open?: boolean
96
config?: string
10-
11-
// generate options
12-
preflights?: boolean
13-
minify?: boolean
7+
watch?: boolean
8+
debug?: boolean
149
}
15-
16-
export type ResolvedCliOptions = MarkRequired<CliOptions, 'patterns'>

packages/gpt-runner-cli/src/watcher.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { FSWatcher } from 'chokidar'
22
import type { CliOptions } from './types'
33

4+
// TODO: move into core
45
let watcher: FSWatcher
56

67
export async function getWatcher(options?: CliOptions) {
@@ -11,7 +12,7 @@ export async function getWatcher(options?: CliOptions) {
1112
const { watch } = await import('chokidar')
1213
const ignored = ['**/{.git,node_modules}/**']
1314
// cli may create multiple watchers
14-
const newWatcher = watch(options?.patterns as string[], {
15+
const newWatcher = watch([] as string[], {
1516
ignoreInitial: true,
1617
ignorePermissionErrors: true,
1718
ignored,

packages/gpt-runner-config/package.json

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,12 @@
1414
"bugs": {
1515
"url": "https://github.com/nicepkg/gpt-runner/issues"
1616
},
17-
"keywords": [],
17+
"keywords": [
18+
"gpt-runner",
19+
"chatgpt",
20+
"prompt",
21+
"ai"
22+
],
1823
"sideEffects": false,
1924
"exports": {
2025
".": {
@@ -41,4 +46,4 @@
4146
"@nicepkg/gpt-runner-shared": "workspace:*",
4247
"unconfig": "^0.3.9"
4348
}
44-
}
49+
}

0 commit comments

Comments
 (0)