@agent-native/core 0.70.1 → 0.70.2
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/corpus/README.md +1 -1
- package/corpus/core/CHANGELOG.md +6 -0
- package/corpus/core/package.json +1 -1
- package/corpus/core/src/cli/sync-builder-starter-manifest.ts +275 -0
- package/dist/cli/sync-builder-starter-manifest.d.ts +32 -0
- package/dist/cli/sync-builder-starter-manifest.d.ts.map +1 -0
- package/dist/cli/sync-builder-starter-manifest.js +179 -0
- package/dist/cli/sync-builder-starter-manifest.js.map +1 -0
- package/package.json +1 -1
package/corpus/README.md
CHANGED
package/corpus/core/CHANGELOG.md
CHANGED
package/corpus/core/package.json
CHANGED
|
@@ -0,0 +1,275 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import os from "node:os";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import { fileURLToPath } from "node:url";
|
|
5
|
+
import { _postProcessStandalone } from "./create.js";
|
|
6
|
+
|
|
7
|
+
export const STARTER_APP_NAME = "builder-agent-native-starter";
|
|
8
|
+
export const CHAT_TEMPLATE = "chat";
|
|
9
|
+
|
|
10
|
+
type PackageJson = Record<string, unknown>;
|
|
11
|
+
|
|
12
|
+
export function findAgentNativeRoot(startDir = process.cwd()): string {
|
|
13
|
+
let dir = path.resolve(startDir);
|
|
14
|
+
for (let i = 0; i < 12; i++) {
|
|
15
|
+
const chatPackageJson = path.join(
|
|
16
|
+
dir,
|
|
17
|
+
"templates",
|
|
18
|
+
CHAT_TEMPLATE,
|
|
19
|
+
"package.json",
|
|
20
|
+
);
|
|
21
|
+
if (fs.existsSync(chatPackageJson)) {
|
|
22
|
+
return dir;
|
|
23
|
+
}
|
|
24
|
+
const parent = path.dirname(dir);
|
|
25
|
+
if (parent === dir) break;
|
|
26
|
+
dir = parent;
|
|
27
|
+
}
|
|
28
|
+
throw new Error(
|
|
29
|
+
"Could not find agent-native repo root (expected templates/chat/package.json).",
|
|
30
|
+
);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export function generateStandaloneChatManifest(repoRoot?: string): {
|
|
34
|
+
packageJson: PackageJson;
|
|
35
|
+
pnpmWorkspaceYaml: string | null;
|
|
36
|
+
} {
|
|
37
|
+
const root = repoRoot ?? findAgentNativeRoot();
|
|
38
|
+
const chatTemplateDir = path.join(root, "templates", CHAT_TEMPLATE);
|
|
39
|
+
const tempDir = fs.mkdtempSync(
|
|
40
|
+
path.join(os.tmpdir(), "an-builder-starter-sync-"),
|
|
41
|
+
);
|
|
42
|
+
|
|
43
|
+
try {
|
|
44
|
+
fs.cpSync(chatTemplateDir, tempDir, { recursive: true });
|
|
45
|
+
_postProcessStandalone(STARTER_APP_NAME, tempDir, CHAT_TEMPLATE);
|
|
46
|
+
|
|
47
|
+
const packageJson = JSON.parse(
|
|
48
|
+
fs.readFileSync(path.join(tempDir, "package.json"), "utf-8"),
|
|
49
|
+
) as PackageJson;
|
|
50
|
+
|
|
51
|
+
const workspacePath = path.join(tempDir, "pnpm-workspace.yaml");
|
|
52
|
+
const pnpmWorkspaceYaml = fs.existsSync(workspacePath)
|
|
53
|
+
? fs.readFileSync(workspacePath, "utf-8")
|
|
54
|
+
: null;
|
|
55
|
+
|
|
56
|
+
return { packageJson, pnpmWorkspaceYaml };
|
|
57
|
+
} finally {
|
|
58
|
+
fs.rmSync(tempDir, { recursive: true, force: true });
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export function mergeStarterManifest(
|
|
63
|
+
starterPackageJson: PackageJson,
|
|
64
|
+
canonicalPackageJson: PackageJson,
|
|
65
|
+
): PackageJson {
|
|
66
|
+
const merged = structuredClone(canonicalPackageJson) as PackageJson;
|
|
67
|
+
|
|
68
|
+
merged.name = starterPackageJson.name ?? STARTER_APP_NAME;
|
|
69
|
+
if (typeof starterPackageJson.displayName === "string") {
|
|
70
|
+
merged.displayName = starterPackageJson.displayName;
|
|
71
|
+
}
|
|
72
|
+
if (typeof starterPackageJson.description === "string") {
|
|
73
|
+
merged.description = starterPackageJson.description;
|
|
74
|
+
}
|
|
75
|
+
if (starterPackageJson.private !== undefined) {
|
|
76
|
+
merged.private = starterPackageJson.private;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const starterDeps =
|
|
80
|
+
(starterPackageJson.dependencies as Record<string, string> | undefined) ??
|
|
81
|
+
{};
|
|
82
|
+
const canonicalDeps =
|
|
83
|
+
(canonicalPackageJson.dependencies as Record<string, string> | undefined) ??
|
|
84
|
+
{};
|
|
85
|
+
merged.dependencies = {
|
|
86
|
+
...canonicalDeps,
|
|
87
|
+
...(starterDeps["@agent-native/core"]
|
|
88
|
+
? { "@agent-native/core": starterDeps["@agent-native/core"] }
|
|
89
|
+
: {}),
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
return merged;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export function workspaceFileSyncChanged(
|
|
96
|
+
existingWorkspace: string | null,
|
|
97
|
+
canonicalWorkspace: string | null,
|
|
98
|
+
): boolean {
|
|
99
|
+
if (canonicalWorkspace === null) {
|
|
100
|
+
return existingWorkspace !== null;
|
|
101
|
+
}
|
|
102
|
+
return existingWorkspace !== canonicalWorkspace;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
export function applyWorkspaceFileSync(
|
|
106
|
+
targetPath: string,
|
|
107
|
+
canonicalWorkspace: string | null,
|
|
108
|
+
): void {
|
|
109
|
+
if (canonicalWorkspace === null) {
|
|
110
|
+
if (fs.existsSync(targetPath)) {
|
|
111
|
+
fs.unlinkSync(targetPath);
|
|
112
|
+
}
|
|
113
|
+
return;
|
|
114
|
+
}
|
|
115
|
+
fs.writeFileSync(targetPath, canonicalWorkspace);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
function stableJson(value: unknown): string {
|
|
119
|
+
return `${JSON.stringify(value, null, 2)}\n`;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
export type SyncStarterManifestResult = {
|
|
123
|
+
changed: boolean;
|
|
124
|
+
packageJson: PackageJson;
|
|
125
|
+
pnpmWorkspaceYaml: string | null;
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
export function syncStarterManifestFiles(options: {
|
|
129
|
+
starterPackageJsonPath: string;
|
|
130
|
+
starterPnpmWorkspacePath?: string;
|
|
131
|
+
repoRoot?: string;
|
|
132
|
+
write?: boolean;
|
|
133
|
+
}): SyncStarterManifestResult {
|
|
134
|
+
const { packageJson: canonical, pnpmWorkspaceYaml } =
|
|
135
|
+
generateStandaloneChatManifest(options.repoRoot);
|
|
136
|
+
|
|
137
|
+
const starterPackageJson = JSON.parse(
|
|
138
|
+
fs.readFileSync(options.starterPackageJsonPath, "utf-8"),
|
|
139
|
+
) as PackageJson;
|
|
140
|
+
const mergedPackageJson = mergeStarterManifest(starterPackageJson, canonical);
|
|
141
|
+
|
|
142
|
+
let workspaceChanged = false;
|
|
143
|
+
const existingWorkspace =
|
|
144
|
+
options.starterPnpmWorkspacePath &&
|
|
145
|
+
fs.existsSync(options.starterPnpmWorkspacePath)
|
|
146
|
+
? fs.readFileSync(options.starterPnpmWorkspacePath, "utf-8")
|
|
147
|
+
: null;
|
|
148
|
+
|
|
149
|
+
workspaceChanged = workspaceFileSyncChanged(
|
|
150
|
+
existingWorkspace,
|
|
151
|
+
pnpmWorkspaceYaml,
|
|
152
|
+
);
|
|
153
|
+
|
|
154
|
+
const packageChanged =
|
|
155
|
+
stableJson(starterPackageJson) !== stableJson(mergedPackageJson);
|
|
156
|
+
const changed = packageChanged || workspaceChanged;
|
|
157
|
+
|
|
158
|
+
if (options.write && changed) {
|
|
159
|
+
if (packageChanged) {
|
|
160
|
+
fs.writeFileSync(
|
|
161
|
+
options.starterPackageJsonPath,
|
|
162
|
+
stableJson(mergedPackageJson),
|
|
163
|
+
);
|
|
164
|
+
}
|
|
165
|
+
if (workspaceChanged && options.starterPnpmWorkspacePath) {
|
|
166
|
+
applyWorkspaceFileSync(
|
|
167
|
+
options.starterPnpmWorkspacePath,
|
|
168
|
+
pnpmWorkspaceYaml,
|
|
169
|
+
);
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
return {
|
|
174
|
+
changed,
|
|
175
|
+
packageJson: mergedPackageJson,
|
|
176
|
+
pnpmWorkspaceYaml,
|
|
177
|
+
};
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
export function parseSyncStarterManifestArgs(argv: string[]): {
|
|
181
|
+
command: "merge" | "generate";
|
|
182
|
+
starterPackageJsonPath?: string;
|
|
183
|
+
starterPnpmWorkspacePath?: string;
|
|
184
|
+
write: boolean;
|
|
185
|
+
repoRoot?: string;
|
|
186
|
+
} {
|
|
187
|
+
const [commandRaw, ...rest] = argv;
|
|
188
|
+
const command = commandRaw === "generate" ? "generate" : "merge";
|
|
189
|
+
let starterPackageJsonPath: string | undefined;
|
|
190
|
+
let starterPnpmWorkspacePath: string | undefined;
|
|
191
|
+
let write = false;
|
|
192
|
+
let repoRoot: string | undefined;
|
|
193
|
+
|
|
194
|
+
for (let i = 0; i < rest.length; i++) {
|
|
195
|
+
const arg = rest[i];
|
|
196
|
+
if (arg === "--write") {
|
|
197
|
+
write = true;
|
|
198
|
+
continue;
|
|
199
|
+
}
|
|
200
|
+
if (arg === "--starter-package-json") {
|
|
201
|
+
starterPackageJsonPath = rest[++i];
|
|
202
|
+
continue;
|
|
203
|
+
}
|
|
204
|
+
if (arg === "--starter-pnpm-workspace") {
|
|
205
|
+
starterPnpmWorkspacePath = rest[++i];
|
|
206
|
+
continue;
|
|
207
|
+
}
|
|
208
|
+
if (arg === "--repo-root") {
|
|
209
|
+
repoRoot = rest[++i];
|
|
210
|
+
continue;
|
|
211
|
+
}
|
|
212
|
+
throw new Error(`Unknown argument: ${arg}`);
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
if (command === "merge" && !starterPackageJsonPath) {
|
|
216
|
+
throw new Error(
|
|
217
|
+
"merge requires --starter-package-json <path> [--starter-pnpm-workspace <path>] [--write] [--repo-root <path>]",
|
|
218
|
+
);
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
return {
|
|
222
|
+
command,
|
|
223
|
+
starterPackageJsonPath,
|
|
224
|
+
starterPnpmWorkspacePath,
|
|
225
|
+
write,
|
|
226
|
+
repoRoot,
|
|
227
|
+
};
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
export function runSyncStarterManifestCli(argv: string[]): number {
|
|
231
|
+
const args = parseSyncStarterManifestArgs(argv);
|
|
232
|
+
|
|
233
|
+
if (args.command === "generate") {
|
|
234
|
+
const { packageJson, pnpmWorkspaceYaml } = generateStandaloneChatManifest(
|
|
235
|
+
args.repoRoot,
|
|
236
|
+
);
|
|
237
|
+
process.stdout.write(stableJson(packageJson));
|
|
238
|
+
if (pnpmWorkspaceYaml) {
|
|
239
|
+
process.stdout.write("\n--- pnpm-workspace.yaml ---\n");
|
|
240
|
+
process.stdout.write(pnpmWorkspaceYaml);
|
|
241
|
+
}
|
|
242
|
+
return 0;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
const result = syncStarterManifestFiles({
|
|
246
|
+
starterPackageJsonPath: args.starterPackageJsonPath!,
|
|
247
|
+
starterPnpmWorkspacePath: args.starterPnpmWorkspacePath,
|
|
248
|
+
repoRoot: args.repoRoot,
|
|
249
|
+
write: args.write,
|
|
250
|
+
});
|
|
251
|
+
|
|
252
|
+
if (result.changed) {
|
|
253
|
+
console.log(
|
|
254
|
+
args.write
|
|
255
|
+
? "Updated builder-agent-native-starter manifest from templates/chat."
|
|
256
|
+
: "builder-agent-native-starter manifest is out of date with templates/chat.",
|
|
257
|
+
);
|
|
258
|
+
return args.write ? 0 : 1;
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
console.log(
|
|
262
|
+
"builder-agent-native-starter manifest already matches templates/chat.",
|
|
263
|
+
);
|
|
264
|
+
return 0;
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
const isDirectRun =
|
|
268
|
+
process.argv[1] &&
|
|
269
|
+
path.resolve(process.argv[1]) ===
|
|
270
|
+
path.resolve(fileURLToPath(import.meta.url));
|
|
271
|
+
|
|
272
|
+
if (isDirectRun) {
|
|
273
|
+
const exitCode = runSyncStarterManifestCli(process.argv.slice(2));
|
|
274
|
+
process.exit(exitCode);
|
|
275
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
export declare const STARTER_APP_NAME = "builder-agent-native-starter";
|
|
2
|
+
export declare const CHAT_TEMPLATE = "chat";
|
|
3
|
+
type PackageJson = Record<string, unknown>;
|
|
4
|
+
export declare function findAgentNativeRoot(startDir?: string): string;
|
|
5
|
+
export declare function generateStandaloneChatManifest(repoRoot?: string): {
|
|
6
|
+
packageJson: PackageJson;
|
|
7
|
+
pnpmWorkspaceYaml: string | null;
|
|
8
|
+
};
|
|
9
|
+
export declare function mergeStarterManifest(starterPackageJson: PackageJson, canonicalPackageJson: PackageJson): PackageJson;
|
|
10
|
+
export declare function workspaceFileSyncChanged(existingWorkspace: string | null, canonicalWorkspace: string | null): boolean;
|
|
11
|
+
export declare function applyWorkspaceFileSync(targetPath: string, canonicalWorkspace: string | null): void;
|
|
12
|
+
export type SyncStarterManifestResult = {
|
|
13
|
+
changed: boolean;
|
|
14
|
+
packageJson: PackageJson;
|
|
15
|
+
pnpmWorkspaceYaml: string | null;
|
|
16
|
+
};
|
|
17
|
+
export declare function syncStarterManifestFiles(options: {
|
|
18
|
+
starterPackageJsonPath: string;
|
|
19
|
+
starterPnpmWorkspacePath?: string;
|
|
20
|
+
repoRoot?: string;
|
|
21
|
+
write?: boolean;
|
|
22
|
+
}): SyncStarterManifestResult;
|
|
23
|
+
export declare function parseSyncStarterManifestArgs(argv: string[]): {
|
|
24
|
+
command: "merge" | "generate";
|
|
25
|
+
starterPackageJsonPath?: string;
|
|
26
|
+
starterPnpmWorkspacePath?: string;
|
|
27
|
+
write: boolean;
|
|
28
|
+
repoRoot?: string;
|
|
29
|
+
};
|
|
30
|
+
export declare function runSyncStarterManifestCli(argv: string[]): number;
|
|
31
|
+
export {};
|
|
32
|
+
//# sourceMappingURL=sync-builder-starter-manifest.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sync-builder-starter-manifest.d.ts","sourceRoot":"","sources":["../../src/cli/sync-builder-starter-manifest.ts"],"names":[],"mappings":"AAMA,eAAO,MAAM,gBAAgB,iCAAiC,CAAC;AAC/D,eAAO,MAAM,aAAa,SAAS,CAAC;AAEpC,KAAK,WAAW,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;AAE3C,wBAAgB,mBAAmB,CAAC,QAAQ,SAAgB,GAAG,MAAM,CAmBpE;AAED,wBAAgB,8BAA8B,CAAC,QAAQ,CAAC,EAAE,MAAM,GAAG;IACjE,WAAW,EAAE,WAAW,CAAC;IACzB,iBAAiB,EAAE,MAAM,GAAG,IAAI,CAAC;CAClC,CAwBA;AAED,wBAAgB,oBAAoB,CAClC,kBAAkB,EAAE,WAAW,EAC/B,oBAAoB,EAAE,WAAW,GAChC,WAAW,CA4Bb;AAED,wBAAgB,wBAAwB,CACtC,iBAAiB,EAAE,MAAM,GAAG,IAAI,EAChC,kBAAkB,EAAE,MAAM,GAAG,IAAI,GAChC,OAAO,CAKT;AAED,wBAAgB,sBAAsB,CACpC,UAAU,EAAE,MAAM,EAClB,kBAAkB,EAAE,MAAM,GAAG,IAAI,GAChC,IAAI,CAQN;AAMD,MAAM,MAAM,yBAAyB,GAAG;IACtC,OAAO,EAAE,OAAO,CAAC;IACjB,WAAW,EAAE,WAAW,CAAC;IACzB,iBAAiB,EAAE,MAAM,GAAG,IAAI,CAAC;CAClC,CAAC;AAEF,wBAAgB,wBAAwB,CAAC,OAAO,EAAE;IAChD,sBAAsB,EAAE,MAAM,CAAC;IAC/B,wBAAwB,CAAC,EAAE,MAAM,CAAC;IAClC,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,KAAK,CAAC,EAAE,OAAO,CAAC;CACjB,GAAG,yBAAyB,CA6C5B;AAED,wBAAgB,4BAA4B,CAAC,IAAI,EAAE,MAAM,EAAE,GAAG;IAC5D,OAAO,EAAE,OAAO,GAAG,UAAU,CAAC;IAC9B,sBAAsB,CAAC,EAAE,MAAM,CAAC;IAChC,wBAAwB,CAAC,EAAE,MAAM,CAAC;IAClC,KAAK,EAAE,OAAO,CAAC;IACf,QAAQ,CAAC,EAAE,MAAM,CAAC;CACnB,CA0CA;AAED,wBAAgB,yBAAyB,CAAC,IAAI,EAAE,MAAM,EAAE,GAAG,MAAM,CAmChE"}
|
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import os from "node:os";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import { fileURLToPath } from "node:url";
|
|
5
|
+
import { _postProcessStandalone } from "./create.js";
|
|
6
|
+
export const STARTER_APP_NAME = "builder-agent-native-starter";
|
|
7
|
+
export const CHAT_TEMPLATE = "chat";
|
|
8
|
+
export function findAgentNativeRoot(startDir = process.cwd()) {
|
|
9
|
+
let dir = path.resolve(startDir);
|
|
10
|
+
for (let i = 0; i < 12; i++) {
|
|
11
|
+
const chatPackageJson = path.join(dir, "templates", CHAT_TEMPLATE, "package.json");
|
|
12
|
+
if (fs.existsSync(chatPackageJson)) {
|
|
13
|
+
return dir;
|
|
14
|
+
}
|
|
15
|
+
const parent = path.dirname(dir);
|
|
16
|
+
if (parent === dir)
|
|
17
|
+
break;
|
|
18
|
+
dir = parent;
|
|
19
|
+
}
|
|
20
|
+
throw new Error("Could not find agent-native repo root (expected templates/chat/package.json).");
|
|
21
|
+
}
|
|
22
|
+
export function generateStandaloneChatManifest(repoRoot) {
|
|
23
|
+
const root = repoRoot ?? findAgentNativeRoot();
|
|
24
|
+
const chatTemplateDir = path.join(root, "templates", CHAT_TEMPLATE);
|
|
25
|
+
const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), "an-builder-starter-sync-"));
|
|
26
|
+
try {
|
|
27
|
+
fs.cpSync(chatTemplateDir, tempDir, { recursive: true });
|
|
28
|
+
_postProcessStandalone(STARTER_APP_NAME, tempDir, CHAT_TEMPLATE);
|
|
29
|
+
const packageJson = JSON.parse(fs.readFileSync(path.join(tempDir, "package.json"), "utf-8"));
|
|
30
|
+
const workspacePath = path.join(tempDir, "pnpm-workspace.yaml");
|
|
31
|
+
const pnpmWorkspaceYaml = fs.existsSync(workspacePath)
|
|
32
|
+
? fs.readFileSync(workspacePath, "utf-8")
|
|
33
|
+
: null;
|
|
34
|
+
return { packageJson, pnpmWorkspaceYaml };
|
|
35
|
+
}
|
|
36
|
+
finally {
|
|
37
|
+
fs.rmSync(tempDir, { recursive: true, force: true });
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
export function mergeStarterManifest(starterPackageJson, canonicalPackageJson) {
|
|
41
|
+
const merged = structuredClone(canonicalPackageJson);
|
|
42
|
+
merged.name = starterPackageJson.name ?? STARTER_APP_NAME;
|
|
43
|
+
if (typeof starterPackageJson.displayName === "string") {
|
|
44
|
+
merged.displayName = starterPackageJson.displayName;
|
|
45
|
+
}
|
|
46
|
+
if (typeof starterPackageJson.description === "string") {
|
|
47
|
+
merged.description = starterPackageJson.description;
|
|
48
|
+
}
|
|
49
|
+
if (starterPackageJson.private !== undefined) {
|
|
50
|
+
merged.private = starterPackageJson.private;
|
|
51
|
+
}
|
|
52
|
+
const starterDeps = starterPackageJson.dependencies ??
|
|
53
|
+
{};
|
|
54
|
+
const canonicalDeps = canonicalPackageJson.dependencies ??
|
|
55
|
+
{};
|
|
56
|
+
merged.dependencies = {
|
|
57
|
+
...canonicalDeps,
|
|
58
|
+
...(starterDeps["@agent-native/core"]
|
|
59
|
+
? { "@agent-native/core": starterDeps["@agent-native/core"] }
|
|
60
|
+
: {}),
|
|
61
|
+
};
|
|
62
|
+
return merged;
|
|
63
|
+
}
|
|
64
|
+
export function workspaceFileSyncChanged(existingWorkspace, canonicalWorkspace) {
|
|
65
|
+
if (canonicalWorkspace === null) {
|
|
66
|
+
return existingWorkspace !== null;
|
|
67
|
+
}
|
|
68
|
+
return existingWorkspace !== canonicalWorkspace;
|
|
69
|
+
}
|
|
70
|
+
export function applyWorkspaceFileSync(targetPath, canonicalWorkspace) {
|
|
71
|
+
if (canonicalWorkspace === null) {
|
|
72
|
+
if (fs.existsSync(targetPath)) {
|
|
73
|
+
fs.unlinkSync(targetPath);
|
|
74
|
+
}
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
fs.writeFileSync(targetPath, canonicalWorkspace);
|
|
78
|
+
}
|
|
79
|
+
function stableJson(value) {
|
|
80
|
+
return `${JSON.stringify(value, null, 2)}\n`;
|
|
81
|
+
}
|
|
82
|
+
export function syncStarterManifestFiles(options) {
|
|
83
|
+
const { packageJson: canonical, pnpmWorkspaceYaml } = generateStandaloneChatManifest(options.repoRoot);
|
|
84
|
+
const starterPackageJson = JSON.parse(fs.readFileSync(options.starterPackageJsonPath, "utf-8"));
|
|
85
|
+
const mergedPackageJson = mergeStarterManifest(starterPackageJson, canonical);
|
|
86
|
+
let workspaceChanged = false;
|
|
87
|
+
const existingWorkspace = options.starterPnpmWorkspacePath &&
|
|
88
|
+
fs.existsSync(options.starterPnpmWorkspacePath)
|
|
89
|
+
? fs.readFileSync(options.starterPnpmWorkspacePath, "utf-8")
|
|
90
|
+
: null;
|
|
91
|
+
workspaceChanged = workspaceFileSyncChanged(existingWorkspace, pnpmWorkspaceYaml);
|
|
92
|
+
const packageChanged = stableJson(starterPackageJson) !== stableJson(mergedPackageJson);
|
|
93
|
+
const changed = packageChanged || workspaceChanged;
|
|
94
|
+
if (options.write && changed) {
|
|
95
|
+
if (packageChanged) {
|
|
96
|
+
fs.writeFileSync(options.starterPackageJsonPath, stableJson(mergedPackageJson));
|
|
97
|
+
}
|
|
98
|
+
if (workspaceChanged && options.starterPnpmWorkspacePath) {
|
|
99
|
+
applyWorkspaceFileSync(options.starterPnpmWorkspacePath, pnpmWorkspaceYaml);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
return {
|
|
103
|
+
changed,
|
|
104
|
+
packageJson: mergedPackageJson,
|
|
105
|
+
pnpmWorkspaceYaml,
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
export function parseSyncStarterManifestArgs(argv) {
|
|
109
|
+
const [commandRaw, ...rest] = argv;
|
|
110
|
+
const command = commandRaw === "generate" ? "generate" : "merge";
|
|
111
|
+
let starterPackageJsonPath;
|
|
112
|
+
let starterPnpmWorkspacePath;
|
|
113
|
+
let write = false;
|
|
114
|
+
let repoRoot;
|
|
115
|
+
for (let i = 0; i < rest.length; i++) {
|
|
116
|
+
const arg = rest[i];
|
|
117
|
+
if (arg === "--write") {
|
|
118
|
+
write = true;
|
|
119
|
+
continue;
|
|
120
|
+
}
|
|
121
|
+
if (arg === "--starter-package-json") {
|
|
122
|
+
starterPackageJsonPath = rest[++i];
|
|
123
|
+
continue;
|
|
124
|
+
}
|
|
125
|
+
if (arg === "--starter-pnpm-workspace") {
|
|
126
|
+
starterPnpmWorkspacePath = rest[++i];
|
|
127
|
+
continue;
|
|
128
|
+
}
|
|
129
|
+
if (arg === "--repo-root") {
|
|
130
|
+
repoRoot = rest[++i];
|
|
131
|
+
continue;
|
|
132
|
+
}
|
|
133
|
+
throw new Error(`Unknown argument: ${arg}`);
|
|
134
|
+
}
|
|
135
|
+
if (command === "merge" && !starterPackageJsonPath) {
|
|
136
|
+
throw new Error("merge requires --starter-package-json <path> [--starter-pnpm-workspace <path>] [--write] [--repo-root <path>]");
|
|
137
|
+
}
|
|
138
|
+
return {
|
|
139
|
+
command,
|
|
140
|
+
starterPackageJsonPath,
|
|
141
|
+
starterPnpmWorkspacePath,
|
|
142
|
+
write,
|
|
143
|
+
repoRoot,
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
export function runSyncStarterManifestCli(argv) {
|
|
147
|
+
const args = parseSyncStarterManifestArgs(argv);
|
|
148
|
+
if (args.command === "generate") {
|
|
149
|
+
const { packageJson, pnpmWorkspaceYaml } = generateStandaloneChatManifest(args.repoRoot);
|
|
150
|
+
process.stdout.write(stableJson(packageJson));
|
|
151
|
+
if (pnpmWorkspaceYaml) {
|
|
152
|
+
process.stdout.write("\n--- pnpm-workspace.yaml ---\n");
|
|
153
|
+
process.stdout.write(pnpmWorkspaceYaml);
|
|
154
|
+
}
|
|
155
|
+
return 0;
|
|
156
|
+
}
|
|
157
|
+
const result = syncStarterManifestFiles({
|
|
158
|
+
starterPackageJsonPath: args.starterPackageJsonPath,
|
|
159
|
+
starterPnpmWorkspacePath: args.starterPnpmWorkspacePath,
|
|
160
|
+
repoRoot: args.repoRoot,
|
|
161
|
+
write: args.write,
|
|
162
|
+
});
|
|
163
|
+
if (result.changed) {
|
|
164
|
+
console.log(args.write
|
|
165
|
+
? "Updated builder-agent-native-starter manifest from templates/chat."
|
|
166
|
+
: "builder-agent-native-starter manifest is out of date with templates/chat.");
|
|
167
|
+
return args.write ? 0 : 1;
|
|
168
|
+
}
|
|
169
|
+
console.log("builder-agent-native-starter manifest already matches templates/chat.");
|
|
170
|
+
return 0;
|
|
171
|
+
}
|
|
172
|
+
const isDirectRun = process.argv[1] &&
|
|
173
|
+
path.resolve(process.argv[1]) ===
|
|
174
|
+
path.resolve(fileURLToPath(import.meta.url));
|
|
175
|
+
if (isDirectRun) {
|
|
176
|
+
const exitCode = runSyncStarterManifestCli(process.argv.slice(2));
|
|
177
|
+
process.exit(exitCode);
|
|
178
|
+
}
|
|
179
|
+
//# sourceMappingURL=sync-builder-starter-manifest.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sync-builder-starter-manifest.js","sourceRoot":"","sources":["../../src/cli/sync-builder-starter-manifest.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,SAAS,CAAC;AACzB,OAAO,EAAE,MAAM,SAAS,CAAC;AACzB,OAAO,IAAI,MAAM,WAAW,CAAC;AAC7B,OAAO,EAAE,aAAa,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AClE,OAAO,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;AACzB,CAAC","sourcesContent":["import fs from \"node:fs\";\nimport os from \"node:os\";\nimport path from \"node:path\";\nimport { fileURLToPath } from \"node:url\";\nimport { _postProcessStandalone } from \"./create.js\";\n\nexport const STARTER_APP_NAME = \"builder-agent-native-starter\";\nexport const CHAT_TEMPLATE = \"chat\";\n\ntype PackageJson = Record<string, unknown>;\n\nexport function findAgentNativeRoot(startDir = process.cwd()): string {\n let dir = path.resolve(startDir);\n for (let i = 0; i < 12; i++) {\n const chatPackageJson = path.join(\n dir,\n \"templates\",\n CHAT_TEMPLATE,\n \"package.json\",\n );\n if (fs.existsSync(chatPackageJson)) {\n return dir;\n }\n const parent = path.dirname(dir);\n if (parent === dir) break;\n dir = parent;\n }\n throw new Error(\n \"Could not find agent-native repo root (expected templates/chat/package.json).\",\n );\n}\n\nexport function generateStandaloneChatManifest(repoRoot?: string): {\n packageJson: PackageJson;\n pnpmWorkspaceYaml: string | null;\n} {\n const root = repoRoot ?? findAgentNativeRoot();\n const chatTemplateDir = path.join(root, \"templates\", CHAT_TEMPLATE);\n const tempDir = fs.mkdtempSync(\n path.join(os.tmpdir(), \"an-builder-starter-sync-\"),\n );\n\n try {\n fs.cpSync(chatTemplateDir, tempDir, { recursive: true });\n _postProcessStandalone(STARTER_APP_NAME, tempDir, CHAT_TEMPLATE);\n\n const packageJson = JSON.parse(\n fs.readFileSync(path.join(tempDir, \"package.json\"), \"utf-8\"),\n ) as PackageJson;\n\n const workspacePath = path.join(tempDir, \"pnpm-workspace.yaml\");\n const pnpmWorkspaceYaml = fs.existsSync(workspacePath)\n ? fs.readFileSync(workspacePath, \"utf-8\")\n : null;\n\n return { packageJson, pnpmWorkspaceYaml };\n } finally {\n fs.rmSync(tempDir, { recursive: true, force: true });\n }\n}\n\nexport function mergeStarterManifest(\n starterPackageJson: PackageJson,\n canonicalPackageJson: PackageJson,\n): PackageJson {\n const merged = structuredClone(canonicalPackageJson) as PackageJson;\n\n merged.name = starterPackageJson.name ?? STARTER_APP_NAME;\n if (typeof starterPackageJson.displayName === \"string\") {\n merged.displayName = starterPackageJson.displayName;\n }\n if (typeof starterPackageJson.description === \"string\") {\n merged.description = starterPackageJson.description;\n }\n if (starterPackageJson.private !== undefined) {\n merged.private = starterPackageJson.private;\n }\n\n const starterDeps =\n (starterPackageJson.dependencies as Record<string, string> | undefined) ??\n {};\n const canonicalDeps =\n (canonicalPackageJson.dependencies as Record<string, string> | undefined) ??\n {};\n merged.dependencies = {\n ...canonicalDeps,\n ...(starterDeps[\"@agent-native/core\"]\n ? { \"@agent-native/core\": starterDeps[\"@agent-native/core\"] }\n : {}),\n };\n\n return merged;\n}\n\nexport function workspaceFileSyncChanged(\n existingWorkspace: string | null,\n canonicalWorkspace: string | null,\n): boolean {\n if (canonicalWorkspace === null) {\n return existingWorkspace !== null;\n }\n return existingWorkspace !== canonicalWorkspace;\n}\n\nexport function applyWorkspaceFileSync(\n targetPath: string,\n canonicalWorkspace: string | null,\n): void {\n if (canonicalWorkspace === null) {\n if (fs.existsSync(targetPath)) {\n fs.unlinkSync(targetPath);\n }\n return;\n }\n fs.writeFileSync(targetPath, canonicalWorkspace);\n}\n\nfunction stableJson(value: unknown): string {\n return `${JSON.stringify(value, null, 2)}\\n`;\n}\n\nexport type SyncStarterManifestResult = {\n changed: boolean;\n packageJson: PackageJson;\n pnpmWorkspaceYaml: string | null;\n};\n\nexport function syncStarterManifestFiles(options: {\n starterPackageJsonPath: string;\n starterPnpmWorkspacePath?: string;\n repoRoot?: string;\n write?: boolean;\n}): SyncStarterManifestResult {\n const { packageJson: canonical, pnpmWorkspaceYaml } =\n generateStandaloneChatManifest(options.repoRoot);\n\n const starterPackageJson = JSON.parse(\n fs.readFileSync(options.starterPackageJsonPath, \"utf-8\"),\n ) as PackageJson;\n const mergedPackageJson = mergeStarterManifest(starterPackageJson, canonical);\n\n let workspaceChanged = false;\n const existingWorkspace =\n options.starterPnpmWorkspacePath &&\n fs.existsSync(options.starterPnpmWorkspacePath)\n ? fs.readFileSync(options.starterPnpmWorkspacePath, \"utf-8\")\n : null;\n\n workspaceChanged = workspaceFileSyncChanged(\n existingWorkspace,\n pnpmWorkspaceYaml,\n );\n\n const packageChanged =\n stableJson(starterPackageJson) !== stableJson(mergedPackageJson);\n const changed = packageChanged || workspaceChanged;\n\n if (options.write && changed) {\n if (packageChanged) {\n fs.writeFileSync(\n options.starterPackageJsonPath,\n stableJson(mergedPackageJson),\n );\n }\n if (workspaceChanged && options.starterPnpmWorkspacePath) {\n applyWorkspaceFileSync(\n options.starterPnpmWorkspacePath,\n pnpmWorkspaceYaml,\n );\n }\n }\n\n return {\n changed,\n packageJson: mergedPackageJson,\n pnpmWorkspaceYaml,\n };\n}\n\nexport function parseSyncStarterManifestArgs(argv: string[]): {\n command: \"merge\" | \"generate\";\n starterPackageJsonPath?: string;\n starterPnpmWorkspacePath?: string;\n write: boolean;\n repoRoot?: string;\n} {\n const [commandRaw, ...rest] = argv;\n const command = commandRaw === \"generate\" ? \"generate\" : \"merge\";\n let starterPackageJsonPath: string | undefined;\n let starterPnpmWorkspacePath: string | undefined;\n let write = false;\n let repoRoot: string | undefined;\n\n for (let i = 0; i < rest.length; i++) {\n const arg = rest[i];\n if (arg === \"--write\") {\n write = true;\n continue;\n }\n if (arg === \"--starter-package-json\") {\n starterPackageJsonPath = rest[++i];\n continue;\n }\n if (arg === \"--starter-pnpm-workspace\") {\n starterPnpmWorkspacePath = rest[++i];\n continue;\n }\n if (arg === \"--repo-root\") {\n repoRoot = rest[++i];\n continue;\n }\n throw new Error(`Unknown argument: ${arg}`);\n }\n\n if (command === \"merge\" && !starterPackageJsonPath) {\n throw new Error(\n \"merge requires --starter-package-json <path> [--starter-pnpm-workspace <path>] [--write] [--repo-root <path>]\",\n );\n }\n\n return {\n command,\n starterPackageJsonPath,\n starterPnpmWorkspacePath,\n write,\n repoRoot,\n };\n}\n\nexport function runSyncStarterManifestCli(argv: string[]): number {\n const args = parseSyncStarterManifestArgs(argv);\n\n if (args.command === \"generate\") {\n const { packageJson, pnpmWorkspaceYaml } = generateStandaloneChatManifest(\n args.repoRoot,\n );\n process.stdout.write(stableJson(packageJson));\n if (pnpmWorkspaceYaml) {\n process.stdout.write(\"\\n--- pnpm-workspace.yaml ---\\n\");\n process.stdout.write(pnpmWorkspaceYaml);\n }\n return 0;\n }\n\n const result = syncStarterManifestFiles({\n starterPackageJsonPath: args.starterPackageJsonPath!,\n starterPnpmWorkspacePath: args.starterPnpmWorkspacePath,\n repoRoot: args.repoRoot,\n write: args.write,\n });\n\n if (result.changed) {\n console.log(\n args.write\n ? \"Updated builder-agent-native-starter manifest from templates/chat.\"\n : \"builder-agent-native-starter manifest is out of date with templates/chat.\",\n );\n return args.write ? 0 : 1;\n }\n\n console.log(\n \"builder-agent-native-starter manifest already matches templates/chat.\",\n );\n return 0;\n}\n\nconst isDirectRun =\n process.argv[1] &&\n path.resolve(process.argv[1]) ===\n path.resolve(fileURLToPath(import.meta.url));\n\nif (isDirectRun) {\n const exitCode = runSyncStarterManifestCli(process.argv.slice(2));\n process.exit(exitCode);\n}\n"]}
|