domagic-turbopack-plugin 1.1.25 → 1.1.27
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cli.js +74 -11
- package/dist/init.d.ts +20 -0
- package/dist/init.js +179 -0
- package/package.json +3 -2
package/dist/cli.js
CHANGED
|
@@ -1,16 +1,76 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
+
import { initDomagicTurbopackProject } from './init.js';
|
|
2
3
|
import { startDomagicTurbopackServer } from './server.js';
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
}
|
|
4
|
+
void main();
|
|
5
|
+
async function main() {
|
|
6
|
+
const command = getCliCommand();
|
|
7
|
+
if (command === 'init') {
|
|
8
|
+
await runInitCommand();
|
|
9
|
+
return;
|
|
10
|
+
}
|
|
11
|
+
if (command === 'help' || command === '--help' || command === '-h') {
|
|
12
|
+
printHelp();
|
|
13
|
+
return;
|
|
14
|
+
}
|
|
15
|
+
if (command && command !== 'server') {
|
|
16
|
+
console.error(`[domagic] Unknown command: ${command}`);
|
|
17
|
+
printHelp();
|
|
18
|
+
process.exitCode = 1;
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
21
|
+
startServerCommand();
|
|
22
|
+
}
|
|
23
|
+
async function runInitCommand() {
|
|
24
|
+
try {
|
|
25
|
+
const result = await initDomagicTurbopackProject({
|
|
26
|
+
cwd: parseStringArg('--cwd'),
|
|
27
|
+
});
|
|
28
|
+
const scriptAction = result.scriptStatus === 'added'
|
|
29
|
+
? '已写入 package.json script'
|
|
30
|
+
: 'package.json 已存在 script,未覆盖';
|
|
31
|
+
console.log(`[domagic] ${scriptAction}: domagic-server -> ${result.scriptCommand}`);
|
|
32
|
+
if (result.agent) {
|
|
33
|
+
console.log(`[domagic] 已自动检测本地 agent: ${result.agent.provider}`);
|
|
34
|
+
console.log(`[domagic] agent command: ${result.agent.command}`);
|
|
35
|
+
console.log(`[domagic] 已写入配置: ${result.configPath}`);
|
|
36
|
+
}
|
|
37
|
+
else {
|
|
38
|
+
console.log('[domagic] 未检测到可用的本地 agent,可稍后在 DOMagic 面板中配置。');
|
|
39
|
+
}
|
|
40
|
+
console.log('[domagic] 启动服务: npm run domagic-server');
|
|
41
|
+
}
|
|
42
|
+
catch (error) {
|
|
43
|
+
console.error(`[domagic] init failed: ${formatError(error)}`);
|
|
44
|
+
process.exitCode = 1;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
function startServerCommand() {
|
|
48
|
+
const port = parseNumberArg('--port') ?? parseNumberEnv('DOMAGIC_TURBOPACK_PORT');
|
|
49
|
+
const host = parseStringArg('--host') ?? process.env.DOMAGIC_TURBOPACK_HOST;
|
|
50
|
+
startDomagicTurbopackServer({
|
|
51
|
+
host,
|
|
52
|
+
port,
|
|
53
|
+
agent: {
|
|
54
|
+
provider: parseProviderArg('--provider') ?? parseProviderEnv('DOMAGIC_AGENT_PROVIDER'),
|
|
55
|
+
command: parseStringArg('--command'),
|
|
56
|
+
cwd: parseStringArg('--cwd'),
|
|
57
|
+
},
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
function getCliCommand() {
|
|
61
|
+
const firstArg = process.argv[2];
|
|
62
|
+
return firstArg && !firstArg.startsWith('-') ? firstArg : undefined;
|
|
63
|
+
}
|
|
64
|
+
function printHelp() {
|
|
65
|
+
console.log([
|
|
66
|
+
'Usage:',
|
|
67
|
+
' domagic-turbopack-plugin init [--cwd <project-root>]',
|
|
68
|
+
' domagic-turbopack-plugin server [--host <host>] [--port <port>] [--provider <provider>] [--command <agent-command>] [--cwd <project-root>]',
|
|
69
|
+
'',
|
|
70
|
+
'Compatibility:',
|
|
71
|
+
' domagic-turbopack-server [options] # starts the server',
|
|
72
|
+
].join('\n'));
|
|
73
|
+
}
|
|
14
74
|
function parseNumberArg(name) {
|
|
15
75
|
const index = process.argv.indexOf(name);
|
|
16
76
|
const value = index >= 0 ? process.argv[index + 1] : undefined;
|
|
@@ -49,3 +109,6 @@ function parseNumberEnv(name) {
|
|
|
49
109
|
const parsed = Number(value);
|
|
50
110
|
return Number.isFinite(parsed) ? parsed : undefined;
|
|
51
111
|
}
|
|
112
|
+
function formatError(error) {
|
|
113
|
+
return error instanceof Error ? error.message : String(error);
|
|
114
|
+
}
|
package/dist/init.d.ts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
type AgentProvider = 'codex' | 'claude-code' | 'gemini' | 'generic-cli';
|
|
2
|
+
export type DomagicTurbopackInitResult = {
|
|
3
|
+
projectRoot: string;
|
|
4
|
+
packageJsonPath: string;
|
|
5
|
+
scriptStatus: 'added' | 'exists';
|
|
6
|
+
scriptCommand: string;
|
|
7
|
+
configPath: string;
|
|
8
|
+
agent?: {
|
|
9
|
+
provider: AgentProvider;
|
|
10
|
+
command: string;
|
|
11
|
+
cwd: string;
|
|
12
|
+
};
|
|
13
|
+
};
|
|
14
|
+
type CommandLookup = (command: string) => string | null;
|
|
15
|
+
export declare function initDomagicTurbopackProject(input?: {
|
|
16
|
+
cwd?: string;
|
|
17
|
+
env?: NodeJS.ProcessEnv;
|
|
18
|
+
lookupCommand?: CommandLookup;
|
|
19
|
+
}): Promise<DomagicTurbopackInitResult>;
|
|
20
|
+
export {};
|
package/dist/init.js
ADDED
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
import { accessSync, constants } from 'node:fs';
|
|
3
|
+
import { mkdir, readFile, writeFile } from 'node:fs/promises';
|
|
4
|
+
const DOMAGIC_SERVER_SCRIPT_NAME = 'domagic-server';
|
|
5
|
+
const DOMAGIC_SERVER_SCRIPT_COMMAND = 'domagic-turbopack-plugin server';
|
|
6
|
+
const DOMAGIC_CONFIG_DIRECTORY_NAME = '.domagic';
|
|
7
|
+
const DOMAGIC_AGENT_CONFIG_FILE_NAME = 'agent-config.json';
|
|
8
|
+
const DEFAULT_PROVIDER_PRIORITY = [
|
|
9
|
+
'codex',
|
|
10
|
+
'claude-code',
|
|
11
|
+
'gemini',
|
|
12
|
+
];
|
|
13
|
+
export async function initDomagicTurbopackProject(input = {}) {
|
|
14
|
+
const projectRoot = path.resolve(input.cwd ?? process.cwd());
|
|
15
|
+
const packageJsonPath = path.join(projectRoot, 'package.json');
|
|
16
|
+
const packageJson = await readPackageJson(packageJsonPath);
|
|
17
|
+
const scriptStatus = writeDomagicServerScript(packageJson);
|
|
18
|
+
await writeFile(packageJsonPath, `${JSON.stringify(packageJson, null, 2)}\n`, 'utf8');
|
|
19
|
+
const configPath = path.join(projectRoot, DOMAGIC_CONFIG_DIRECTORY_NAME, DOMAGIC_AGENT_CONFIG_FILE_NAME);
|
|
20
|
+
const agent = await detectLocalAgent({
|
|
21
|
+
projectRoot,
|
|
22
|
+
env: input.env ?? process.env,
|
|
23
|
+
lookupCommand: input.lookupCommand ?? createPathLookup(input.env ?? process.env),
|
|
24
|
+
});
|
|
25
|
+
if (agent) {
|
|
26
|
+
await writeAgentConfig(configPath, agent);
|
|
27
|
+
}
|
|
28
|
+
else {
|
|
29
|
+
await mkdir(path.dirname(configPath), { recursive: true });
|
|
30
|
+
}
|
|
31
|
+
return {
|
|
32
|
+
projectRoot,
|
|
33
|
+
packageJsonPath,
|
|
34
|
+
scriptStatus,
|
|
35
|
+
scriptCommand: DOMAGIC_SERVER_SCRIPT_COMMAND,
|
|
36
|
+
configPath,
|
|
37
|
+
agent,
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
async function readPackageJson(packageJsonPath) {
|
|
41
|
+
let source;
|
|
42
|
+
try {
|
|
43
|
+
source = await readFile(packageJsonPath, 'utf8');
|
|
44
|
+
}
|
|
45
|
+
catch (error) {
|
|
46
|
+
throw new Error(`DOMagic init 未找到 package.json:${packageJsonPath},请在 Next.js 项目根目录执行。`, {
|
|
47
|
+
cause: error,
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
try {
|
|
51
|
+
const parsed = JSON.parse(source);
|
|
52
|
+
if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) {
|
|
53
|
+
throw new Error('package.json root must be an object');
|
|
54
|
+
}
|
|
55
|
+
return parsed;
|
|
56
|
+
}
|
|
57
|
+
catch (error) {
|
|
58
|
+
throw new Error(`DOMagic init 无法解析 package.json:${formatError(error)}`);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
function writeDomagicServerScript(packageJson) {
|
|
62
|
+
const scripts = packageJson.scripts && typeof packageJson.scripts === 'object'
|
|
63
|
+
? packageJson.scripts
|
|
64
|
+
: {};
|
|
65
|
+
packageJson.scripts = scripts;
|
|
66
|
+
if (typeof scripts[DOMAGIC_SERVER_SCRIPT_NAME] === 'string' && scripts[DOMAGIC_SERVER_SCRIPT_NAME].trim()) {
|
|
67
|
+
return 'exists';
|
|
68
|
+
}
|
|
69
|
+
scripts[DOMAGIC_SERVER_SCRIPT_NAME] = DOMAGIC_SERVER_SCRIPT_COMMAND;
|
|
70
|
+
return 'added';
|
|
71
|
+
}
|
|
72
|
+
async function detectLocalAgent(input) {
|
|
73
|
+
const explicit = detectExplicitAgent(input.env, input.lookupCommand);
|
|
74
|
+
if (explicit) {
|
|
75
|
+
return {
|
|
76
|
+
...explicit,
|
|
77
|
+
cwd: input.projectRoot,
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
for (const provider of DEFAULT_PROVIDER_PRIORITY) {
|
|
81
|
+
for (const command of getProviderCommandCandidates(provider)) {
|
|
82
|
+
const resolved = input.lookupCommand(command);
|
|
83
|
+
if (resolved) {
|
|
84
|
+
return {
|
|
85
|
+
provider,
|
|
86
|
+
command: resolved,
|
|
87
|
+
cwd: input.projectRoot,
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
return undefined;
|
|
93
|
+
}
|
|
94
|
+
function detectExplicitAgent(env, lookupCommand) {
|
|
95
|
+
const genericCommand = env.DOMAGIC_AGENT_CMD?.trim();
|
|
96
|
+
if (genericCommand) {
|
|
97
|
+
return {
|
|
98
|
+
provider: 'generic-cli',
|
|
99
|
+
command: genericCommand,
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
for (const [provider, envName] of [
|
|
103
|
+
['codex', 'DOMAGIC_CODEX_CMD'],
|
|
104
|
+
['claude-code', 'DOMAGIC_CLAUDE_CODE_CMD'],
|
|
105
|
+
['gemini', 'DOMAGIC_GEMINI_CMD'],
|
|
106
|
+
]) {
|
|
107
|
+
const configured = env[envName]?.trim();
|
|
108
|
+
if (configured) {
|
|
109
|
+
return {
|
|
110
|
+
provider,
|
|
111
|
+
command: configured,
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
const provider = parseAgentProvider(env.DOMAGIC_AGENT_PROVIDER);
|
|
116
|
+
if (!provider || provider === 'generic-cli') {
|
|
117
|
+
return undefined;
|
|
118
|
+
}
|
|
119
|
+
for (const command of getProviderCommandCandidates(provider)) {
|
|
120
|
+
const resolved = lookupCommand(command);
|
|
121
|
+
if (resolved) {
|
|
122
|
+
return {
|
|
123
|
+
provider,
|
|
124
|
+
command: resolved,
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
return undefined;
|
|
129
|
+
}
|
|
130
|
+
function parseAgentProvider(value) {
|
|
131
|
+
const provider = value?.trim();
|
|
132
|
+
if (provider === 'codex' ||
|
|
133
|
+
provider === 'claude-code' ||
|
|
134
|
+
provider === 'gemini' ||
|
|
135
|
+
provider === 'generic-cli') {
|
|
136
|
+
return provider;
|
|
137
|
+
}
|
|
138
|
+
return undefined;
|
|
139
|
+
}
|
|
140
|
+
function getProviderCommandCandidates(provider) {
|
|
141
|
+
if (provider === 'codex') {
|
|
142
|
+
return ['codex'];
|
|
143
|
+
}
|
|
144
|
+
if (provider === 'claude-code') {
|
|
145
|
+
return ['claude', 'claude-code'];
|
|
146
|
+
}
|
|
147
|
+
return ['gemini', 'gemini-cli'];
|
|
148
|
+
}
|
|
149
|
+
function createPathLookup(env) {
|
|
150
|
+
const pathEntries = (env.PATH ?? '').split(path.delimiter).filter(Boolean);
|
|
151
|
+
return (command) => {
|
|
152
|
+
if (command.includes(path.sep)) {
|
|
153
|
+
return command;
|
|
154
|
+
}
|
|
155
|
+
for (const entry of pathEntries) {
|
|
156
|
+
const candidate = path.join(entry, command);
|
|
157
|
+
if (isExecutable(candidate)) {
|
|
158
|
+
return candidate;
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
return null;
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
function isExecutable(file) {
|
|
165
|
+
try {
|
|
166
|
+
accessSync(file, constants.X_OK);
|
|
167
|
+
return true;
|
|
168
|
+
}
|
|
169
|
+
catch {
|
|
170
|
+
return false;
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
async function writeAgentConfig(configPath, agent) {
|
|
174
|
+
await mkdir(path.dirname(configPath), { recursive: true });
|
|
175
|
+
await writeFile(configPath, `${JSON.stringify({ agent }, null, 2)}\n`, 'utf8');
|
|
176
|
+
}
|
|
177
|
+
function formatError(error) {
|
|
178
|
+
return error instanceof Error ? error.message : String(error);
|
|
179
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "domagic-turbopack-plugin",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.27",
|
|
4
4
|
"description": "A development-only Turbopack adapter for DOM source mapping and AI-assisted style edits.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -25,6 +25,7 @@
|
|
|
25
25
|
}
|
|
26
26
|
},
|
|
27
27
|
"bin": {
|
|
28
|
+
"domagic-turbopack-plugin": "dist/cli.js",
|
|
28
29
|
"domagic-turbopack-server": "dist/cli.js"
|
|
29
30
|
},
|
|
30
31
|
"files": [
|
|
@@ -48,7 +49,7 @@
|
|
|
48
49
|
"test": "vitest run --config vitest.config.ts"
|
|
49
50
|
},
|
|
50
51
|
"dependencies": {
|
|
51
|
-
"domagic-vite-plugin": "1.1.
|
|
52
|
+
"domagic-vite-plugin": "1.1.27",
|
|
52
53
|
"typescript": "^5.9.3"
|
|
53
54
|
},
|
|
54
55
|
"peerDependencies": {
|