pcific 0.0.0-stage → 0.2.0
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/LICENSE +21 -0
- package/README.md +50 -2
- package/dist/SKILL.md +127 -0
- package/dist/api/catalog.js +637 -0
- package/dist/api/run.js +960 -0
- package/dist/api/search.js +171 -0
- package/dist/api/shortcuts.js +799 -0
- package/dist/bin.js +648 -0
- package/dist/commands/auth.js +513 -0
- package/dist/commands/doctor.js +410 -0
- package/dist/commands/init.js +306 -0
- package/dist/commands/sync.js +214 -0
- package/dist/detect.js +214 -0
- package/dist/env.js +339 -0
- package/dist/goLive.js +629 -0
- package/dist/keyLink.js +305 -0
- package/dist/mcp/server.js +206 -0
- package/dist/mcp/tools.js +689 -0
- package/dist/python/template.js +793 -0
- package/dist/schema/discover.js +324 -0
- package/dist/schema/helpers.js +396 -0
- package/dist/schema/prismaToJsonSchema.js +197 -0
- package/dist/schema/push.js +570 -0
- package/dist/schema/tsToJsonSchema.js +405 -0
- package/dist/schema/zodToJsonSchema.js +442 -0
- package/dist/templates/openapi.json +1 -0
- package/dist/templates/sdk/auth.ts +139 -0
- package/dist/templates/sdk/client.ts +1059 -0
- package/dist/templates/sdk/errors.ts +76 -0
- package/dist/templates/sdk/generated-types.ts +5746 -0
- package/dist/templates/sdk/http.ts +359 -0
- package/dist/templates/sdk/index.ts +84 -0
- package/dist/templates/sdk/json.ts +24 -0
- package/dist/templates/sdk/kit.ts +516 -0
- package/dist/templates/sdk/multipart.ts +99 -0
- package/dist/templates/sdk/sse.ts +131 -0
- package/dist/templates/sdk/verify.ts +766 -0
- package/dist/templates/sdk-js/auth.d.ts +20 -0
- package/dist/templates/sdk-js/auth.js +85 -0
- package/dist/templates/sdk-js/client.d.ts +324 -0
- package/dist/templates/sdk-js/client.js +607 -0
- package/dist/templates/sdk-js/errors.d.ts +51 -0
- package/dist/templates/sdk-js/errors.js +48 -0
- package/dist/templates/sdk-js/generated-types.d.ts +5736 -0
- package/dist/templates/sdk-js/generated-types.js +5 -0
- package/dist/templates/sdk-js/http.d.ts +50 -0
- package/dist/templates/sdk-js/http.js +263 -0
- package/dist/templates/sdk-js/index.d.ts +9 -0
- package/dist/templates/sdk-js/index.js +7 -0
- package/dist/templates/sdk-js/json.d.ts +11 -0
- package/dist/templates/sdk-js/json.js +16 -0
- package/dist/templates/sdk-js/kit.d.ts +220 -0
- package/dist/templates/sdk-js/kit.js +208 -0
- package/dist/templates/sdk-js/multipart.d.ts +23 -0
- package/dist/templates/sdk-js/multipart.js +61 -0
- package/dist/templates/sdk-js/sse.d.ts +12 -0
- package/dist/templates/sdk-js/sse.js +99 -0
- package/dist/templates/sdk-js/verify.d.ts +114 -0
- package/dist/templates/sdk-js/verify.js +506 -0
- package/dist/templates.js +925 -0
- package/dist/ui.js +399 -0
- package/dist/webhooks/events.js +171 -0
- package/dist/webhooks/listen.js +772 -0
- package/package.json +32 -4
|
@@ -0,0 +1,306 @@
|
|
|
1
|
+
import { mkdirSync, writeFileSync } from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { detectProject } from "../detect.js";
|
|
4
|
+
import { CliError, ensureEnvGitignored, resolveCliConfig, } from "../env.js";
|
|
5
|
+
import { collectKeyLink, openBrowser as defaultOpenBrowser, startKeyLink, waitForKeyLink, } from "../keyLink.js";
|
|
6
|
+
import { buildPcificGuide, formatIsoDate, packageInstallCommand, resolveInsideWorkspace, runPackageManagerCommand, scaffoldProject, shouldGenerateRouteByDefault, shouldIncludeKitByDefault, } from "../templates.js";
|
|
7
|
+
import { runDoctor, verifyTestKey } from "./doctor.js";
|
|
8
|
+
async function tryRunPythonTemplate(options) {
|
|
9
|
+
const specifier = "../python/template.js";
|
|
10
|
+
try {
|
|
11
|
+
// SAFETY: Optional sibling module built by parallel CLI feature; checked before invoking.
|
|
12
|
+
const mod = (await import(specifier));
|
|
13
|
+
if (!mod.renderPythonClient) {
|
|
14
|
+
return null;
|
|
15
|
+
}
|
|
16
|
+
const date = formatIsoDate();
|
|
17
|
+
const targetDir = (options.dir ?? ".").replace(/\/+$/, "") || ".";
|
|
18
|
+
const outDir = resolveInsideWorkspace(options.cwd, targetDir);
|
|
19
|
+
mkdirSync(outDir, { recursive: true });
|
|
20
|
+
const clientAbs = path.join(outDir, "pcific_client.py");
|
|
21
|
+
const clientRel = path.relative(options.cwd, clientAbs).replace(/\\/g, "/");
|
|
22
|
+
writeFileSync(clientAbs, mod.renderPythonClient({ generatedDate: date, defaultBaseUrl: options.baseUrl }), "utf8");
|
|
23
|
+
const guideAbs = path.join(options.cwd, "PCIFIC.md");
|
|
24
|
+
writeFileSync(guideAbs, buildPcificGuide({
|
|
25
|
+
mode: "copy",
|
|
26
|
+
date,
|
|
27
|
+
baseUrl: options.baseUrl,
|
|
28
|
+
clientImportPath: `./${clientRel}`,
|
|
29
|
+
targetDir,
|
|
30
|
+
}), "utf8");
|
|
31
|
+
return {
|
|
32
|
+
mode: "copy",
|
|
33
|
+
date,
|
|
34
|
+
targetDir,
|
|
35
|
+
clientFile: clientRel,
|
|
36
|
+
guideFile: "PCIFIC.md",
|
|
37
|
+
filesWritten: [clientRel, "PCIFIC.md"],
|
|
38
|
+
filesSkipped: [],
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
catch {
|
|
42
|
+
return null;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
export async function runInit(options) {
|
|
46
|
+
const { cwd, ui } = options;
|
|
47
|
+
const fetchImpl = options.fetch ?? globalThis.fetch;
|
|
48
|
+
const openBrowserFn = options.openBrowser ?? defaultOpenBrowser;
|
|
49
|
+
const runPm = options.runPackageManager ?? runPackageManagerCommand;
|
|
50
|
+
ui.header("Init", "Link a test key, scaffold your client, and verify your setup");
|
|
51
|
+
const project = detectProject(cwd);
|
|
52
|
+
const config = resolveCliConfig(cwd, {
|
|
53
|
+
baseUrl: options.baseUrl,
|
|
54
|
+
env: options.env,
|
|
55
|
+
allowReplaceDotEnvLiveKey: Boolean(options.reauth),
|
|
56
|
+
});
|
|
57
|
+
if (options.dir !== undefined) {
|
|
58
|
+
resolveInsideWorkspace(cwd, options.dir);
|
|
59
|
+
}
|
|
60
|
+
ui.info(`Detected ${project.language} (${project.framework}) using ${project.packageManager}${project.agentFiles.length > 0 ? ` | agent docs: ${project.agentFiles.join(", ")}` : ""}`);
|
|
61
|
+
// 1. Ensure .gitignore covers .env BEFORE collecting any key into .env
|
|
62
|
+
const gitignore = ensureEnvGitignored(cwd);
|
|
63
|
+
if (gitignore.status === "already-ignored") {
|
|
64
|
+
ui.ok(".env is already ignored in .gitignore.");
|
|
65
|
+
}
|
|
66
|
+
else if (gitignore.status === "added") {
|
|
67
|
+
ui.ok("Added .env to .gitignore.");
|
|
68
|
+
}
|
|
69
|
+
else {
|
|
70
|
+
ui.ok("Created .gitignore with .env.");
|
|
71
|
+
}
|
|
72
|
+
// 2. Key linking (skip if existing PCIFIC_TEST_KEY already verifies unless --reauth)
|
|
73
|
+
let authStatus = "skipped";
|
|
74
|
+
if (!options.skipAuth) {
|
|
75
|
+
let needsLink = true;
|
|
76
|
+
if (config.testKey && !options.reauth) {
|
|
77
|
+
const checkSpin = ui.spinner("Checking existing PCIFIC_TEST_KEY");
|
|
78
|
+
const verification = await verifyTestKey({
|
|
79
|
+
baseUrl: config.baseUrl,
|
|
80
|
+
testKey: config.testKey,
|
|
81
|
+
fetch: fetchImpl,
|
|
82
|
+
});
|
|
83
|
+
if (verification.valid && (!options.shop || verification.marketplaceName === options.shop)) {
|
|
84
|
+
checkSpin.succeed(`Existing PCIFIC_TEST_KEY is valid${verification.name ? ` (${verification.name})` : ""}; skipping re-link.`);
|
|
85
|
+
authStatus = "reused";
|
|
86
|
+
needsLink = false;
|
|
87
|
+
}
|
|
88
|
+
else if (verification.valid && options.shop) {
|
|
89
|
+
checkSpin.warn(`Existing PCIFIC_TEST_KEY is for ${verification.marketplaceName ?? "all shops"}, not "${options.shop}"; starting a fresh link.`);
|
|
90
|
+
}
|
|
91
|
+
else {
|
|
92
|
+
checkSpin.warn(`Existing PCIFIC_TEST_KEY could not be verified (${verification.error ?? "rejected"}); starting a fresh link.`);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
if (needsLink) {
|
|
96
|
+
const startSpin = ui.spinner("Starting PCIFIC test key connection");
|
|
97
|
+
let started;
|
|
98
|
+
try {
|
|
99
|
+
started = await startKeyLink({
|
|
100
|
+
baseUrl: config.baseUrl,
|
|
101
|
+
suggestedName: project.projectName,
|
|
102
|
+
shop: options.shop,
|
|
103
|
+
fetch: fetchImpl,
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
catch (failure) {
|
|
107
|
+
startSpin.fail("Could not start PCIFIC test key connection.");
|
|
108
|
+
throw failure;
|
|
109
|
+
}
|
|
110
|
+
startSpin.succeed("Key connection started.");
|
|
111
|
+
const boxLines = [
|
|
112
|
+
`Confirmation code: ${started.confirmationCode}`,
|
|
113
|
+
`Approval link: ${started.approvalUrl}`,
|
|
114
|
+
"",
|
|
115
|
+
"Open the link, type the code, review permissions, and click Generate.",
|
|
116
|
+
];
|
|
117
|
+
if (started.shopNote) {
|
|
118
|
+
boxLines.push("", started.shopNote);
|
|
119
|
+
}
|
|
120
|
+
if (ui.json) {
|
|
121
|
+
const errStream = options.stderr ?? process.stderr;
|
|
122
|
+
errStream.write(`${boxLines.join("\n")}\n`);
|
|
123
|
+
}
|
|
124
|
+
else {
|
|
125
|
+
ui.box("Approve test key in PCIFIC", boxLines);
|
|
126
|
+
}
|
|
127
|
+
if (ui.interactive) {
|
|
128
|
+
const opened = await openBrowserFn(started.approvalUrl);
|
|
129
|
+
if (opened) {
|
|
130
|
+
ui.info("Opened approval link in your default browser.");
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
const waitSpin = ui.spinner("Waiting for key approval in browser");
|
|
134
|
+
let waitResult;
|
|
135
|
+
try {
|
|
136
|
+
waitResult = await waitForKeyLink({
|
|
137
|
+
baseUrl: config.baseUrl,
|
|
138
|
+
id: started.id,
|
|
139
|
+
pollToken: started.pollToken,
|
|
140
|
+
maxRounds: options.maxWaitRounds ?? 12,
|
|
141
|
+
waitSeconds: options.waitSeconds ?? 25,
|
|
142
|
+
fetch: fetchImpl,
|
|
143
|
+
onTick: ({ round, maxRounds }) => {
|
|
144
|
+
waitSpin.update(`Waiting for key approval (round ${round}/${maxRounds})`);
|
|
145
|
+
},
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
catch (failure) {
|
|
149
|
+
waitSpin.fail("Waiting for key approval failed.");
|
|
150
|
+
throw failure;
|
|
151
|
+
}
|
|
152
|
+
if (waitResult.status === "ready") {
|
|
153
|
+
waitSpin.succeed("Approval received. Collecting test key into .env.");
|
|
154
|
+
const collected = await collectKeyLink({
|
|
155
|
+
baseUrl: config.baseUrl,
|
|
156
|
+
id: started.id,
|
|
157
|
+
pollToken: started.pollToken,
|
|
158
|
+
cwd,
|
|
159
|
+
fetch: fetchImpl,
|
|
160
|
+
});
|
|
161
|
+
ui.ok(`Saved ${collected.keysWritten.join(", ")} to .env (mode 0600, secret never printed).`);
|
|
162
|
+
authStatus = "linked";
|
|
163
|
+
}
|
|
164
|
+
else if (waitResult.timedOut) {
|
|
165
|
+
waitSpin.fail("Timed out waiting for key approval.");
|
|
166
|
+
throw new CliError("Timed out waiting for the key connection to be approved.", {
|
|
167
|
+
code: "KEY_LINK_TIMEOUT",
|
|
168
|
+
tryThis: "Run npx pcific init again and click Generate within 5 minutes.",
|
|
169
|
+
});
|
|
170
|
+
}
|
|
171
|
+
else {
|
|
172
|
+
waitSpin.fail(`Key connection ended with status "${waitResult.status}".`);
|
|
173
|
+
throw new CliError(`Key connection cannot be collected because it is ${waitResult.status}.`, {
|
|
174
|
+
code: "KEY_LINK_ENDED",
|
|
175
|
+
tryThis: "Run npx pcific init to start a new key connection.",
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
else {
|
|
181
|
+
ui.info("Skipped key linking (--skip-auth).");
|
|
182
|
+
}
|
|
183
|
+
// 3. Choose Copy mode (default #1) vs Package mode (#2)
|
|
184
|
+
let chosenMode;
|
|
185
|
+
if (options.mode) {
|
|
186
|
+
chosenMode = options.mode;
|
|
187
|
+
}
|
|
188
|
+
else if (options.yes || !ui.interactive) {
|
|
189
|
+
chosenMode = project.existingMode === "package" ? "package" : "copy";
|
|
190
|
+
}
|
|
191
|
+
else {
|
|
192
|
+
chosenMode = await ui.select("How should PCIFIC be added to this project?", [
|
|
193
|
+
{
|
|
194
|
+
value: "copy",
|
|
195
|
+
label: "Copy mode (Recommended)",
|
|
196
|
+
hint: "Zero-dependency SDK files copied directly into your repo",
|
|
197
|
+
},
|
|
198
|
+
{
|
|
199
|
+
value: "package",
|
|
200
|
+
label: "Package mode",
|
|
201
|
+
hint: "Install @pcific/sdk and create a thin client wrapper",
|
|
202
|
+
},
|
|
203
|
+
], project.existingMode === "package" ? 1 : 0);
|
|
204
|
+
}
|
|
205
|
+
// 4. Scaffold files
|
|
206
|
+
let scaffold;
|
|
207
|
+
if (project.language === "python" && chosenMode === "copy") {
|
|
208
|
+
const pyResult = await tryRunPythonTemplate({
|
|
209
|
+
cwd,
|
|
210
|
+
baseUrl: config.baseUrl,
|
|
211
|
+
dir: options.dir,
|
|
212
|
+
});
|
|
213
|
+
if (pyResult) {
|
|
214
|
+
ui.ok(`Scaffolded Python client (${pyResult.filesWritten.join(", ")}).`);
|
|
215
|
+
scaffold = pyResult;
|
|
216
|
+
}
|
|
217
|
+
else {
|
|
218
|
+
throw new CliError("Python Copy mode template module is not installed in this build.", {
|
|
219
|
+
code: "PYTHON_TEMPLATE_MISSING",
|
|
220
|
+
tryThis: "Rebuild pcific with the Python template module included, or use TypeScript/JavaScript.",
|
|
221
|
+
});
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
else {
|
|
225
|
+
if (chosenMode === "package" && !options.skipInstall) {
|
|
226
|
+
const installCmd = packageInstallCommand(project.packageManager);
|
|
227
|
+
const installSpin = ui.spinner(`Installing @pcific/sdk (${installCmd.display})`);
|
|
228
|
+
await runPm(cwd, installCmd.command, installCmd.args);
|
|
229
|
+
installSpin.succeed(`Installed @pcific/sdk via ${installCmd.display}.`);
|
|
230
|
+
}
|
|
231
|
+
const withRoute = options.noRoute
|
|
232
|
+
? false
|
|
233
|
+
: options.withRoute !== undefined
|
|
234
|
+
? options.withRoute
|
|
235
|
+
: shouldGenerateRouteByDefault(project.framework);
|
|
236
|
+
const scaffoldSpin = ui.spinner(chosenMode === "copy"
|
|
237
|
+
? "Copying zero-dependency PCIFIC SDK files"
|
|
238
|
+
: "Writing PCIFIC client wrapper");
|
|
239
|
+
scaffold = scaffoldProject({
|
|
240
|
+
cwd,
|
|
241
|
+
mode: chosenMode,
|
|
242
|
+
project,
|
|
243
|
+
baseUrl: config.baseUrl,
|
|
244
|
+
libDir: options.dir,
|
|
245
|
+
withKit: options.withKit ?? shouldIncludeKitByDefault(project.framework),
|
|
246
|
+
withRoute,
|
|
247
|
+
});
|
|
248
|
+
scaffoldSpin.succeed(`Wrote ${scaffold.filesWritten.length} file(s) (${scaffold.mode} mode -> ${scaffold.clientFile}).`);
|
|
249
|
+
if (scaffold.filesSkipped.length > 0) {
|
|
250
|
+
ui.info(`Kept existing file(s) untouched: ${scaffold.filesSkipped.join(", ")}`);
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
// 5. Run doctor check at the end unless auth was skipped
|
|
254
|
+
let doctor;
|
|
255
|
+
if (!options.skipAuth) {
|
|
256
|
+
ui.blank();
|
|
257
|
+
ui.step("Verifying setup with pcific doctor");
|
|
258
|
+
doctor = await runDoctor({
|
|
259
|
+
cwd,
|
|
260
|
+
baseUrl: config.baseUrl,
|
|
261
|
+
ui,
|
|
262
|
+
env: options.env,
|
|
263
|
+
fetch: fetchImpl,
|
|
264
|
+
suppressHeader: true,
|
|
265
|
+
suppressJson: true,
|
|
266
|
+
});
|
|
267
|
+
}
|
|
268
|
+
// 6. Next steps
|
|
269
|
+
const nextSteps = [
|
|
270
|
+
`Client: ${scaffold.clientFile}`,
|
|
271
|
+
...(scaffold.kitFile ? [`PCIFIC Kit: ${scaffold.kitFile}`] : []),
|
|
272
|
+
...(scaffold.routeFile ? [`Embed route: ${scaffold.routeFile}`] : []),
|
|
273
|
+
`Agent guide: ${scaffold.guideFile}`,
|
|
274
|
+
"",
|
|
275
|
+
"Run `npx pcific doctor` anytime to re-check connectivity.",
|
|
276
|
+
"Run `npx pcific sync` to check /changelog.txt and refresh managed files.",
|
|
277
|
+
];
|
|
278
|
+
ui.box("PCIFIC is ready", nextSteps);
|
|
279
|
+
const ok = doctor ? doctor.ok : true;
|
|
280
|
+
if (ui.json) {
|
|
281
|
+
const payload = {
|
|
282
|
+
command: "init",
|
|
283
|
+
ok,
|
|
284
|
+
mode: scaffold.mode,
|
|
285
|
+
authStatus,
|
|
286
|
+
gitignoreStatus: gitignore.status,
|
|
287
|
+
clientFile: scaffold.clientFile,
|
|
288
|
+
kitFile: scaffold.kitFile ?? null,
|
|
289
|
+
routeFile: scaffold.routeFile ?? null,
|
|
290
|
+
guideFile: scaffold.guideFile,
|
|
291
|
+
filesWritten: scaffold.filesWritten,
|
|
292
|
+
filesSkipped: scaffold.filesSkipped,
|
|
293
|
+
doctorOk: doctor ? doctor.ok : null,
|
|
294
|
+
};
|
|
295
|
+
ui.emitJson(payload);
|
|
296
|
+
}
|
|
297
|
+
return {
|
|
298
|
+
ok,
|
|
299
|
+
exitCode: ok ? 0 : 1,
|
|
300
|
+
mode: scaffold.mode,
|
|
301
|
+
authStatus,
|
|
302
|
+
gitignore,
|
|
303
|
+
scaffold,
|
|
304
|
+
doctor,
|
|
305
|
+
};
|
|
306
|
+
}
|
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { detectProject } from "../detect.js";
|
|
4
|
+
import { CliError, resolveCliConfig } from "../env.js";
|
|
5
|
+
import { renderPythonClient } from "../python/template.js";
|
|
6
|
+
import { formatIsoDate, listManagedCopyFiles, packageUpdateCommand, readPcificGenerationMeta, refreshManagedCopyFiles, resolveInsideWorkspace, runPackageManagerCommand, } from "../templates.js";
|
|
7
|
+
async function fetchChangelog(fetchImpl, baseUrl, since) {
|
|
8
|
+
const query = since ? `?since=${encodeURIComponent(since)}` : "";
|
|
9
|
+
const url = `${baseUrl}/changelog.txt${query}`;
|
|
10
|
+
try {
|
|
11
|
+
const response = await fetchImpl(url, {
|
|
12
|
+
method: "GET",
|
|
13
|
+
headers: { accept: "text/plain" },
|
|
14
|
+
});
|
|
15
|
+
if (!response.ok) {
|
|
16
|
+
return { warning: `Could not fetch ${url} (HTTP ${response.status}).` };
|
|
17
|
+
}
|
|
18
|
+
const text = (await response.text()).trim();
|
|
19
|
+
return { text };
|
|
20
|
+
}
|
|
21
|
+
catch (failure) {
|
|
22
|
+
const message = failure instanceof Error ? failure.message : "Network error";
|
|
23
|
+
return { warning: `Could not reach ${url} (${message}).` };
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
export async function runSync(options) {
|
|
27
|
+
const { cwd, ui } = options;
|
|
28
|
+
const fetchImpl = options.fetch ?? globalThis.fetch;
|
|
29
|
+
const runPm = options.runPackageManager ?? runPackageManagerCommand;
|
|
30
|
+
ui.header("Sync", "Checking changelog and updating managed PCIFIC files");
|
|
31
|
+
const config = resolveCliConfig(cwd, { baseUrl: options.baseUrl, env: options.env });
|
|
32
|
+
const project = detectProject(cwd);
|
|
33
|
+
const meta = readPcificGenerationMeta(cwd, project);
|
|
34
|
+
if (!meta) {
|
|
35
|
+
throw new CliError("No existing PCIFIC setup was found in this project.", {
|
|
36
|
+
code: "PCIFIC_NOT_INITIALIZED",
|
|
37
|
+
tryThis: "Run npx pcific init to scaffold PCIFIC files first.",
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
const changelogSpin = ui.spinner(meta.date
|
|
41
|
+
? `Fetching changelog since ${meta.date}`
|
|
42
|
+
: "Fetching PCIFIC changelog");
|
|
43
|
+
const changelogResult = await fetchChangelog(fetchImpl, config.baseUrl, meta.date);
|
|
44
|
+
if (changelogResult.warning) {
|
|
45
|
+
changelogSpin.warn(changelogResult.warning);
|
|
46
|
+
}
|
|
47
|
+
else if (changelogResult.text) {
|
|
48
|
+
changelogSpin.succeed(meta.date ? `Loaded changelog since ${meta.date}` : "Loaded latest PCIFIC changelog");
|
|
49
|
+
const previewLines = changelogResult.text.split(/\r?\n/).slice(0, 12);
|
|
50
|
+
ui.box("What changed", previewLines);
|
|
51
|
+
}
|
|
52
|
+
else {
|
|
53
|
+
changelogSpin.succeed("No new changelog entries since your last sync.");
|
|
54
|
+
}
|
|
55
|
+
if (meta.mode === "package") {
|
|
56
|
+
const updateCmd = packageUpdateCommand(project.packageManager);
|
|
57
|
+
let ranUpdate = false;
|
|
58
|
+
if (options.apply) {
|
|
59
|
+
const spin = ui.spinner(`Running ${updateCmd.display}`);
|
|
60
|
+
await runPm(cwd, updateCmd.command, updateCmd.args);
|
|
61
|
+
spin.succeed(`Updated @pcific/sdk via ${updateCmd.display}`);
|
|
62
|
+
ranUpdate = true;
|
|
63
|
+
}
|
|
64
|
+
else if (ui.interactive) {
|
|
65
|
+
const confirmed = await ui.confirm(`Run "${updateCmd.display}" now to update @pcific/sdk?`, true);
|
|
66
|
+
if (confirmed) {
|
|
67
|
+
const spin = ui.spinner(`Running ${updateCmd.display}`);
|
|
68
|
+
await runPm(cwd, updateCmd.command, updateCmd.args);
|
|
69
|
+
spin.succeed(`Updated @pcific/sdk via ${updateCmd.display}`);
|
|
70
|
+
ranUpdate = true;
|
|
71
|
+
}
|
|
72
|
+
else {
|
|
73
|
+
ui.info(`Run "${updateCmd.display}" whenever you want to update @pcific/sdk.`);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
else {
|
|
77
|
+
ui.info(`Package mode detected. Run "${updateCmd.display}" to update @pcific/sdk.`);
|
|
78
|
+
}
|
|
79
|
+
if (ui.json) {
|
|
80
|
+
ui.emitJson({
|
|
81
|
+
command: "sync",
|
|
82
|
+
ok: true,
|
|
83
|
+
mode: "package",
|
|
84
|
+
since: meta.date ?? null,
|
|
85
|
+
changelog: changelogResult.text ?? null,
|
|
86
|
+
updateCommand: updateCmd.display,
|
|
87
|
+
updated: ranUpdate,
|
|
88
|
+
filesUpdated: [],
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
return {
|
|
92
|
+
ok: true,
|
|
93
|
+
exitCode: 0,
|
|
94
|
+
mode: "package",
|
|
95
|
+
since: meta.date,
|
|
96
|
+
changelog: changelogResult.text,
|
|
97
|
+
filesUpdated: [],
|
|
98
|
+
updateCommand: updateCmd.display,
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
const isPython = project.language === "python" || Boolean(meta.clientFile?.endsWith(".py"));
|
|
102
|
+
const rawTargetDir = options.dir ??
|
|
103
|
+
meta.targetDir ??
|
|
104
|
+
(isPython ? "." : `${project.defaultLibDir}/pcific`);
|
|
105
|
+
const targetDir = rawTargetDir.replace(/\/+$/, "") || ".";
|
|
106
|
+
const absTargetDir = resolveInsideWorkspace(cwd, targetDir);
|
|
107
|
+
let pythonClientAbs;
|
|
108
|
+
let pythonClientRel;
|
|
109
|
+
if (isPython) {
|
|
110
|
+
if (!options.dir && meta.clientFile && meta.clientFile.endsWith(".py")) {
|
|
111
|
+
const cleanedClient = meta.clientFile.replace(/^\.\//, "");
|
|
112
|
+
pythonClientAbs = resolveInsideWorkspace(cwd, cleanedClient);
|
|
113
|
+
}
|
|
114
|
+
else {
|
|
115
|
+
pythonClientAbs = path.join(absTargetDir, "pcific_client.py");
|
|
116
|
+
}
|
|
117
|
+
pythonClientRel = path.relative(cwd, pythonClientAbs).replace(/\\/g, "/");
|
|
118
|
+
}
|
|
119
|
+
const filesWouldUpdate = isPython
|
|
120
|
+
? [
|
|
121
|
+
pythonClientRel,
|
|
122
|
+
...(existsSync(path.join(cwd, "PCIFIC.md")) ? ["PCIFIC.md"] : []),
|
|
123
|
+
]
|
|
124
|
+
: listManagedCopyFiles({
|
|
125
|
+
cwd,
|
|
126
|
+
targetDir,
|
|
127
|
+
language: project.language,
|
|
128
|
+
});
|
|
129
|
+
let shouldApply = Boolean(options.apply);
|
|
130
|
+
if (!shouldApply && ui.interactive) {
|
|
131
|
+
shouldApply = await ui.confirm(`Refresh ${filesWouldUpdate.length} managed file(s) in ${targetDir}?`, true);
|
|
132
|
+
}
|
|
133
|
+
if (!shouldApply) {
|
|
134
|
+
ui.info(`Preview only (no files modified). ${filesWouldUpdate.length} managed file(s) would refresh in ${targetDir}.`);
|
|
135
|
+
ui.info("Run `npx pcific sync --apply` to apply these updates.");
|
|
136
|
+
if (ui.json) {
|
|
137
|
+
ui.emitJson({
|
|
138
|
+
command: "sync",
|
|
139
|
+
ok: true,
|
|
140
|
+
mode: "copy",
|
|
141
|
+
applied: false,
|
|
142
|
+
since: meta.date ?? null,
|
|
143
|
+
targetDir,
|
|
144
|
+
changelog: changelogResult.text ?? null,
|
|
145
|
+
filesUpdated: [],
|
|
146
|
+
filesWouldUpdate,
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
return {
|
|
150
|
+
ok: true,
|
|
151
|
+
exitCode: 0,
|
|
152
|
+
mode: "copy",
|
|
153
|
+
applied: false,
|
|
154
|
+
since: meta.date,
|
|
155
|
+
changelog: changelogResult.text,
|
|
156
|
+
filesUpdated: [],
|
|
157
|
+
filesWouldUpdate,
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
let syncedDate;
|
|
161
|
+
let filesUpdated;
|
|
162
|
+
if (isPython) {
|
|
163
|
+
const refreshSpin = ui.spinner(`Refreshing Python client (${pythonClientRel})`);
|
|
164
|
+
syncedDate = formatIsoDate();
|
|
165
|
+
mkdirSync(path.dirname(pythonClientAbs), { recursive: true });
|
|
166
|
+
writeFileSync(pythonClientAbs, renderPythonClient({ generatedDate: syncedDate, defaultBaseUrl: config.baseUrl }), "utf8");
|
|
167
|
+
filesUpdated = [pythonClientRel];
|
|
168
|
+
const guidePath = path.join(cwd, "PCIFIC.md");
|
|
169
|
+
if (existsSync(guidePath)) {
|
|
170
|
+
const existing = readFileSync(guidePath, "utf8");
|
|
171
|
+
const updated = existing
|
|
172
|
+
.replace(/<!-- @pcific-generated \d{4}-\d{2}-\d{2} mode=copy -->/, `<!-- @pcific-generated ${syncedDate} mode=copy -->`)
|
|
173
|
+
.replace(/- Generated: `\d{4}-\d{2}-\d{2}`/, `- Generated: \`${syncedDate}\``)
|
|
174
|
+
.replace(/\?since=\d{4}-\d{2}-\d{2}/, `?since=${syncedDate}`);
|
|
175
|
+
writeFileSync(guidePath, updated, "utf8");
|
|
176
|
+
filesUpdated.push("PCIFIC.md");
|
|
177
|
+
}
|
|
178
|
+
refreshSpin.succeed(`Updated ${filesUpdated.length} managed file(s) (${filesUpdated.join(", ")}).`);
|
|
179
|
+
}
|
|
180
|
+
else {
|
|
181
|
+
const refreshSpin = ui.spinner(`Refreshing managed PCIFIC files in ${targetDir}`);
|
|
182
|
+
const refreshed = refreshManagedCopyFiles({
|
|
183
|
+
cwd,
|
|
184
|
+
targetDir,
|
|
185
|
+
language: project.language,
|
|
186
|
+
baseUrl: config.baseUrl,
|
|
187
|
+
});
|
|
188
|
+
syncedDate = refreshed.date;
|
|
189
|
+
filesUpdated = refreshed.filesUpdated;
|
|
190
|
+
refreshSpin.succeed(`Updated ${filesUpdated.length} managed file(s) in ${targetDir} (user routes untouched).`);
|
|
191
|
+
}
|
|
192
|
+
if (ui.json) {
|
|
193
|
+
ui.emitJson({
|
|
194
|
+
command: "sync",
|
|
195
|
+
ok: true,
|
|
196
|
+
mode: "copy",
|
|
197
|
+
applied: true,
|
|
198
|
+
since: meta.date ?? null,
|
|
199
|
+
syncedDate,
|
|
200
|
+
targetDir,
|
|
201
|
+
changelog: changelogResult.text ?? null,
|
|
202
|
+
filesUpdated,
|
|
203
|
+
});
|
|
204
|
+
}
|
|
205
|
+
return {
|
|
206
|
+
ok: true,
|
|
207
|
+
exitCode: 0,
|
|
208
|
+
mode: "copy",
|
|
209
|
+
applied: true,
|
|
210
|
+
since: meta.date,
|
|
211
|
+
changelog: changelogResult.text,
|
|
212
|
+
filesUpdated,
|
|
213
|
+
};
|
|
214
|
+
}
|