Skip to content

Commit 2fefab0

Browse files
committed
Implement CLI integration
1 parent fb53d1e commit 2fefab0

57 files changed

Lines changed: 3102 additions & 585 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎.changeset/config.json‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
"changelog": "@changesets/cli/changelog",
44
"commit": false,
55
"fixed": [
6-
["@fraqjs/plugin-takumi", "@fraqjs/takumi-preview"],
7-
["@fraqjs/color-log", "@fraqjs/fraq", "@fraqjs/kernel"]
6+
["@fraqjs/color-log", "@fraqjs/fraq", "@fraqjs/kernel"],
7+
["@fraqjs/cli", "@fraqjs/plugin-cli-integration", "@fraqjs/cli-protocol"],
8+
["@fraqjs/plugin-takumi", "@fraqjs/takumi-preview"]
89
],
910
"linked": [],
1011
"access": "public",

‎.changeset/new-shrimps-stay.md‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"@fraqjs/cli": minor
3+
"@fraqjs/cli-protocol": minor
4+
"@fraqjs/plugin-cli-integration": minor
5+
---
6+
7+
实现 cli-integration 插件,通过 WebUI 管理 Fraq CLI

‎biome.json‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,10 @@
9999
}
100100
},
101101
{
102-
"includes": ["plugins/webui-gateway/webui/**/*.css"],
102+
"includes": [
103+
"plugins/webui-gateway/webui/**/*.css",
104+
"plugins/cli-integration/webui/**/*.css"
105+
],
103106
"css": {
104107
"parser": {
105108
"tailwindDirectives": true
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
---
2+
title: '@fraqjs/plugin-cli-integration'
3+
---
4+
5+
<NpmBadge packageName="@fraqjs/plugin-cli-integration" />
6+
7+
`@fraqjs/plugin-cli-integration` 提供了通过 WebUI 管理 Fraq CLI 的能力。
8+
9+
## 安装与配置
10+
11+
将插件添加到 `fraq.yml` 的 `plugins` 字段下:
12+
13+
```yaml
14+
plugins:
15+
# 本插件依赖 fraqjs/hono 和 fraqjs/webui-gateway,因此需要一并安装
16+
fraqjs/hono:
17+
# 在这里传入 HonoPlugin 的配置选项
18+
fraqjs/webui-gateway:
19+
# 在这里传入 WebuiGatewayPlugin 的配置选项
20+
fraqjs/cli-integration:
21+
```
22+
23+
仅在使用 `fraq start --watch` 时有效;普通 `fraq start` 或自行编写启动脚本时,插件将跳过加载。

‎packages/cli-protocol/package.json‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"name": "@fraqjs/cli-protocol",
3+
"version": "1.0.1",
4+
"type": "module",
5+
"description": "Shared protocol for Fraq CLI integrations",
6+
"files": [
7+
"dist"
8+
],
9+
"main": "src/index.ts",
10+
"publishConfig": {
11+
"main": "dist/index.mjs",
12+
"typings": "dist/index.d.mts"
13+
},
14+
"scripts": {
15+
"build": "tsdown",
16+
"prepack": "pnpm build"
17+
},
18+
"license": "MIT"
19+
}

‎packages/cli-protocol/src/index.ts‎

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
export const PROTOCOL_VERSION = 1;
2+
export const MAX_LOG_LINES = 1_000;
3+
export const MAX_LOG_BYTES = 4 * 1024 * 1024;
4+
export const MAX_LOG_LINE_BYTES = 16 * 1024;
5+
export const MAX_LOG_BATCH_BYTES = 256 * 1024;
6+
export const MAX_CONFIG_BYTES = 1024 * 1024;
7+
export const REQUEST_TIMEOUT_MS = 10_000;
8+
export const LOG_POLL_MS = 250;
9+
10+
export type ErrorCode = 'invalid' | 'conflict' | 'busy' | 'unavailable' | 'timeout' | 'internal';
11+
12+
export class ControlError extends Error {
13+
constructor(
14+
readonly code: ErrorCode,
15+
message: string,
16+
) {
17+
super(message);
18+
}
19+
}
20+
21+
export interface ConfigDocument {
22+
name: string;
23+
format: 'yaml' | 'json';
24+
content: string;
25+
revision: string;
26+
}
27+
28+
export interface AppStatus {
29+
session: string;
30+
state: 'starting' | 'running' | 'restarting' | 'stopped';
31+
fallback: boolean;
32+
error: string | null;
33+
generation: number;
34+
busy: boolean;
35+
}
36+
37+
export interface LogEntry {
38+
session: string;
39+
sequence: number;
40+
time: number;
41+
source: 'cli' | 'app' | 'install';
42+
stream: 'stdout' | 'stderr';
43+
text: string;
44+
bytes: number;
45+
truncated: boolean;
46+
}
47+
48+
export interface LogCursor {
49+
session?: string;
50+
after?: number;
51+
}
52+
53+
export interface LogBatch {
54+
session: string;
55+
entries: LogEntry[];
56+
cursor: number;
57+
gap: boolean;
58+
}
59+
60+
export interface Methods {
61+
hello: { input: undefined; output: { version: number; capabilities: string[] } };
62+
status: { input: undefined; output: AppStatus };
63+
config: { input: undefined; output: ConfigDocument };
64+
save: { input: { content: string; revision: string }; output: ConfigDocument };
65+
restart: { input: undefined; output: { accepted: true } };
66+
logs: { input: LogCursor; output: LogBatch };
67+
}
68+
69+
export type Method = keyof Methods;
70+
71+
export type ControlRequest = {
72+
[M in Method]: {
73+
type: 'fraq:control:request';
74+
version: 1;
75+
id: string;
76+
method: M;
77+
input: Methods[M]['input'];
78+
};
79+
}[Method];
80+
81+
export interface ControlResponse {
82+
type: 'fraq:control:response';
83+
version: 1;
84+
id: string;
85+
result?: unknown;
86+
error?: { code: ErrorCode; message: string };
87+
}
88+
89+
export interface ControlClient {
90+
request<M extends Method>(method: M, input: Methods[M]['input']): Promise<Methods[M]['output']>;
91+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"compilerOptions": {
3+
"target": "ESNext",
4+
"useDefineForClassFields": true,
5+
"module": "esnext",
6+
"lib": ["ESNext"],
7+
"skipLibCheck": true,
8+
"moduleResolution": "bundler",
9+
"allowSyntheticDefaultImports": true,
10+
"isolatedModules": true,
11+
"declaration": true,
12+
"strict": true,
13+
"noFallthroughCasesInSwitch": true,
14+
"types": ["node"]
15+
},
16+
"include": ["src/**/*.ts", "test/**/*.ts"]
17+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { defineConfig } from 'tsdown';
2+
3+
export default defineConfig({
4+
entry: 'src/index.ts',
5+
dts: true,
6+
});

‎packages/cli/package.json‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@
3535
"cmd-ts": "^0.15.0",
3636
"execa": "^10.0.1",
3737
"yaml": "^2.9.0",
38-
"zod": "^4.4.3"
38+
"zod": "^4.4.3",
39+
"@fraqjs/cli-protocol": "workspace:^"
3940
},
4041
"devDependencies": {
4142
"fdir": "^6.5.0"
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import chalk from 'chalk';
2+
import YAML from 'yaml';
3+
4+
import { loadConfig, loadProjectConfig } from '../config';
5+
import type { FileAccessHandler } from '../config/references';
6+
import { getPluginDependencyDiagnostic, normalizePluginName } from '../dependency';
7+
import { selectPackageManager } from '../package-manager';
8+
import { getVersionsPath } from '../paths';
9+
import { completeAndSyncVersions, readVersions } from '../versions';
10+
import { getWorkspacePluginEntryPoint } from '../workspace-plugins';
11+
import type { PreparedApp } from './lifecycle';
12+
13+
import { writeFileSync } from 'node:fs';
14+
import { isDeepStrictEqual } from 'node:util';
15+
16+
export async function syncVersions(
17+
options: { onFileAccess?: FileAccessHandler; silent?: boolean } = {},
18+
): Promise<boolean> {
19+
if (!options.silent) console.log(chalk.cyan('Syncing lockfile versions...'));
20+
const config = await loadConfig({ onFileAccess: options.onFileAccess, throwOnValidationError: true });
21+
const locked = readVersions();
22+
config.versions = { ...locked, ...config.versions };
23+
const completed = await completeAndSyncVersions(config, config.versions);
24+
const changed = !isDeepStrictEqual(locked, completed);
25+
if (changed) writeFileSync(getVersionsPath(), YAML.stringify(completed));
26+
if (!options.silent) console.log(chalk.green('Successfully synced lockfile versions.'));
27+
return changed;
28+
}
29+
30+
export async function prepareApp(
31+
options: { watch?: boolean; frozenLockfile?: boolean } = {},
32+
accessedFiles = new Set<string>(),
33+
): Promise<PreparedApp> {
34+
const onFileAccess = (file: string) => {
35+
accessedFiles.add(file);
36+
};
37+
accessedFiles.add(getVersionsPath());
38+
if (!options.frozenLockfile) await syncVersions({ onFileAccess, silent: options.watch });
39+
const config = await loadProjectConfig({ resolveAllReferences: true, onFileAccess });
40+
const diagnostic = await getPluginDependencyDiagnostic(config, { onFileAccess });
41+
if (diagnostic.status === 'missing') {
42+
throw new Error(`There are issues with the plugin dependencies:\n${diagnostic.message.join('\n')}`);
43+
}
44+
const restartFiles = new Set<string>();
45+
if (options.watch) {
46+
for (const name of Object.keys(config.workspacePlugins ?? {})) {
47+
const entry = getWorkspacePluginEntryPoint(config, name, normalizePluginName(name), onFileAccess);
48+
accessedFiles.add(entry);
49+
restartFiles.add(entry);
50+
}
51+
}
52+
return { config, packageManager: await selectPackageManager(config.packageManager), restartFiles };
53+
}

0 commit comments

Comments
 (0)