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
package/dist/detect.js
ADDED
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
import { existsSync, readFileSync, statSync } from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
const AGENT_FILE_CANDIDATES = [
|
|
4
|
+
"CLAUDE.md",
|
|
5
|
+
"AGENTS.md",
|
|
6
|
+
".cursor/rules",
|
|
7
|
+
".github/copilot-instructions.md",
|
|
8
|
+
];
|
|
9
|
+
const COPY_DIR_CANDIDATES = [
|
|
10
|
+
"src/lib/pcific",
|
|
11
|
+
"lib/pcific",
|
|
12
|
+
"src/pcific",
|
|
13
|
+
"pcific",
|
|
14
|
+
];
|
|
15
|
+
function isJsonObject(value) {
|
|
16
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
17
|
+
}
|
|
18
|
+
function isString(value) {
|
|
19
|
+
return typeof value === "string";
|
|
20
|
+
}
|
|
21
|
+
function readPackageJson(cwd) {
|
|
22
|
+
const pkgPath = path.join(cwd, "package.json");
|
|
23
|
+
if (!existsSync(pkgPath))
|
|
24
|
+
return null;
|
|
25
|
+
try {
|
|
26
|
+
// SAFETY: JSON.parse returns JSON values matching CliJsonValue.
|
|
27
|
+
const parsed = JSON.parse(readFileSync(pkgPath, "utf8"));
|
|
28
|
+
return isJsonObject(parsed) ? parsed : null;
|
|
29
|
+
}
|
|
30
|
+
catch {
|
|
31
|
+
return null;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
function collectDependencies(pkg) {
|
|
35
|
+
const deps = new Set();
|
|
36
|
+
if (!pkg)
|
|
37
|
+
return deps;
|
|
38
|
+
for (const field of ["dependencies", "devDependencies", "peerDependencies"]) {
|
|
39
|
+
const section = pkg[field];
|
|
40
|
+
if (isJsonObject(section)) {
|
|
41
|
+
for (const name of Object.keys(section)) {
|
|
42
|
+
deps.add(name);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
return deps;
|
|
47
|
+
}
|
|
48
|
+
function detectPackageManager(cwd, pkg) {
|
|
49
|
+
if (existsSync(path.join(cwd, "pnpm-lock.yaml")))
|
|
50
|
+
return "pnpm";
|
|
51
|
+
if (existsSync(path.join(cwd, "yarn.lock")))
|
|
52
|
+
return "yarn";
|
|
53
|
+
if (existsSync(path.join(cwd, "bun.lock")) || existsSync(path.join(cwd, "bun.lockb"))) {
|
|
54
|
+
return "bun";
|
|
55
|
+
}
|
|
56
|
+
if (pkg && isString(pkg.packageManager)) {
|
|
57
|
+
const field = pkg.packageManager.toLowerCase();
|
|
58
|
+
if (field.startsWith("pnpm@"))
|
|
59
|
+
return "pnpm";
|
|
60
|
+
if (field.startsWith("yarn@"))
|
|
61
|
+
return "yarn";
|
|
62
|
+
if (field.startsWith("bun@"))
|
|
63
|
+
return "bun";
|
|
64
|
+
}
|
|
65
|
+
return "npm";
|
|
66
|
+
}
|
|
67
|
+
function isDirectory(targetPath) {
|
|
68
|
+
if (!existsSync(targetPath))
|
|
69
|
+
return false;
|
|
70
|
+
try {
|
|
71
|
+
return statSync(targetPath).isDirectory();
|
|
72
|
+
}
|
|
73
|
+
catch {
|
|
74
|
+
return false;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
function detectPython(cwd, hasPackageJson) {
|
|
78
|
+
const hasPythonMarker = existsSync(path.join(cwd, "pyproject.toml")) ||
|
|
79
|
+
existsSync(path.join(cwd, "requirements.txt")) ||
|
|
80
|
+
existsSync(path.join(cwd, "setup.py")) ||
|
|
81
|
+
existsSync(path.join(cwd, "Pipfile"));
|
|
82
|
+
return hasPythonMarker && !hasPackageJson;
|
|
83
|
+
}
|
|
84
|
+
function detectLanguage(cwd, hasPackageJson, deps) {
|
|
85
|
+
if (detectPython(cwd, hasPackageJson)) {
|
|
86
|
+
return "python";
|
|
87
|
+
}
|
|
88
|
+
if (existsSync(path.join(cwd, "tsconfig.json")) ||
|
|
89
|
+
existsSync(path.join(cwd, "tsconfig.base.json")) ||
|
|
90
|
+
deps.has("typescript")) {
|
|
91
|
+
return "typescript";
|
|
92
|
+
}
|
|
93
|
+
return "javascript";
|
|
94
|
+
}
|
|
95
|
+
function detectFramework(cwd, language, deps) {
|
|
96
|
+
if (language === "python") {
|
|
97
|
+
return "python";
|
|
98
|
+
}
|
|
99
|
+
if (deps.has("next")) {
|
|
100
|
+
if (isDirectory(path.join(cwd, "src", "app")) || isDirectory(path.join(cwd, "app"))) {
|
|
101
|
+
return "nextjs-app";
|
|
102
|
+
}
|
|
103
|
+
if (isDirectory(path.join(cwd, "src", "pages")) || isDirectory(path.join(cwd, "pages"))) {
|
|
104
|
+
return "nextjs-pages";
|
|
105
|
+
}
|
|
106
|
+
return "nextjs-app";
|
|
107
|
+
}
|
|
108
|
+
if (deps.has("astro")) {
|
|
109
|
+
return "astro";
|
|
110
|
+
}
|
|
111
|
+
if (deps.has("@sveltejs/kit")) {
|
|
112
|
+
return "sveltekit";
|
|
113
|
+
}
|
|
114
|
+
if (deps.has("@remix-run/node") || deps.has("@remix-run/react") || deps.has("react-router")) {
|
|
115
|
+
return "remix";
|
|
116
|
+
}
|
|
117
|
+
if (deps.has("express")) {
|
|
118
|
+
return "express";
|
|
119
|
+
}
|
|
120
|
+
return "node";
|
|
121
|
+
}
|
|
122
|
+
function findExistingCopyDir(cwd) {
|
|
123
|
+
for (const relDir of COPY_DIR_CANDIDATES) {
|
|
124
|
+
const absDir = path.join(cwd, relDir);
|
|
125
|
+
if (!isDirectory(absDir))
|
|
126
|
+
continue;
|
|
127
|
+
if (existsSync(path.join(absDir, "client.ts")) ||
|
|
128
|
+
existsSync(path.join(absDir, "client.js")) ||
|
|
129
|
+
existsSync(path.join(absDir, "index.ts")) ||
|
|
130
|
+
existsSync(path.join(absDir, "index.js")) ||
|
|
131
|
+
existsSync(path.join(absDir, "client.py"))) {
|
|
132
|
+
return relDir;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
return undefined;
|
|
136
|
+
}
|
|
137
|
+
function detectExistingMode(cwd, deps) {
|
|
138
|
+
const copyDir = findExistingCopyDir(cwd);
|
|
139
|
+
if (copyDir) {
|
|
140
|
+
return { mode: "copy", copyDir };
|
|
141
|
+
}
|
|
142
|
+
if (existsSync(path.join(cwd, "pcific_client.py"))) {
|
|
143
|
+
return { mode: "copy", copyDir: "." };
|
|
144
|
+
}
|
|
145
|
+
if (deps.has("@pcific/sdk")) {
|
|
146
|
+
return { mode: "package" };
|
|
147
|
+
}
|
|
148
|
+
const guidePath = path.join(cwd, "PCIFIC.md");
|
|
149
|
+
if (existsSync(guidePath)) {
|
|
150
|
+
const guide = readFileSync(guidePath, "utf8");
|
|
151
|
+
if (/mode:\s*copy/i.test(guide))
|
|
152
|
+
return { mode: "copy" };
|
|
153
|
+
if (/mode:\s*package/i.test(guide))
|
|
154
|
+
return { mode: "package" };
|
|
155
|
+
}
|
|
156
|
+
return { mode: "none" };
|
|
157
|
+
}
|
|
158
|
+
function sanitizeProjectName(raw) {
|
|
159
|
+
const cleaned = raw
|
|
160
|
+
.replace(/^@[^/]+\//, "")
|
|
161
|
+
.replace(/[<>]/g, "")
|
|
162
|
+
.replace(/[\r\n\t]+/g, " ")
|
|
163
|
+
.trim();
|
|
164
|
+
if (!cleaned)
|
|
165
|
+
return "My shop";
|
|
166
|
+
return cleaned.slice(0, 80);
|
|
167
|
+
}
|
|
168
|
+
function detectProjectName(cwd, pkg) {
|
|
169
|
+
if (pkg && isString(pkg.name) && pkg.name.trim().length > 0) {
|
|
170
|
+
return sanitizeProjectName(pkg.name);
|
|
171
|
+
}
|
|
172
|
+
const pyprojectPath = path.join(cwd, "pyproject.toml");
|
|
173
|
+
if (existsSync(pyprojectPath)) {
|
|
174
|
+
const content = readFileSync(pyprojectPath, "utf8");
|
|
175
|
+
const match = content.match(/^\s*name\s*=\s*["']([^"']+)["']/m);
|
|
176
|
+
if (match?.[1]) {
|
|
177
|
+
return sanitizeProjectName(match[1]);
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
return sanitizeProjectName(path.basename(cwd));
|
|
181
|
+
}
|
|
182
|
+
/**
|
|
183
|
+
* Inspects a workspace directory to detect language, framework, package manager,
|
|
184
|
+
* source layout, existing PCIFIC integration mode, and coding-agent instruction files.
|
|
185
|
+
*/
|
|
186
|
+
export function detectProject(cwd) {
|
|
187
|
+
const pkg = readPackageJson(cwd);
|
|
188
|
+
const hasPackageJson = pkg !== null;
|
|
189
|
+
const deps = collectDependencies(pkg);
|
|
190
|
+
const language = detectLanguage(cwd, hasPackageJson, deps);
|
|
191
|
+
const framework = detectFramework(cwd, language, deps);
|
|
192
|
+
const packageManager = detectPackageManager(cwd, pkg);
|
|
193
|
+
const srcDir = isDirectory(path.join(cwd, "src")) ? "src" : "";
|
|
194
|
+
const defaultLibDir = srcDir ? "src/lib" : "lib";
|
|
195
|
+
const existing = detectExistingMode(cwd, deps);
|
|
196
|
+
const agentFiles = [];
|
|
197
|
+
for (const candidate of AGENT_FILE_CANDIDATES) {
|
|
198
|
+
if (existsSync(path.join(cwd, candidate))) {
|
|
199
|
+
agentFiles.push(candidate);
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
return {
|
|
203
|
+
language,
|
|
204
|
+
framework,
|
|
205
|
+
packageManager,
|
|
206
|
+
srcDir,
|
|
207
|
+
defaultLibDir,
|
|
208
|
+
existingMode: existing.mode,
|
|
209
|
+
existingCopyDir: existing.copyDir,
|
|
210
|
+
agentFiles,
|
|
211
|
+
projectName: detectProjectName(cwd, pkg),
|
|
212
|
+
hasPackageJson,
|
|
213
|
+
};
|
|
214
|
+
}
|
package/dist/env.js
ADDED
|
@@ -0,0 +1,339 @@
|
|
|
1
|
+
import { randomBytes } from "node:crypto";
|
|
2
|
+
import { chmodSync, closeSync, existsSync, openSync, readFileSync, renameSync, unlinkSync, writeFileSync, writeSync, } from "node:fs";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
export const DEFAULT_BASE_URL = "https://business.pcific.ai";
|
|
5
|
+
export class CliError extends Error {
|
|
6
|
+
code;
|
|
7
|
+
tryThis;
|
|
8
|
+
constructor(message, options) {
|
|
9
|
+
super(message);
|
|
10
|
+
this.name = "CliError";
|
|
11
|
+
this.code = options.code ?? "PCIFIC_CLI_ERROR";
|
|
12
|
+
this.tryThis = options.tryThis;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
const VALID_ENV_KEY = /^[A-Za-z_][A-Za-z0-9_]*$/;
|
|
16
|
+
function nonEmpty(value) {
|
|
17
|
+
if (value === undefined)
|
|
18
|
+
return undefined;
|
|
19
|
+
const trimmed = value.trim();
|
|
20
|
+
return trimmed.length > 0 ? trimmed : undefined;
|
|
21
|
+
}
|
|
22
|
+
function unquoteEnvValue(raw) {
|
|
23
|
+
const trimmed = raw.trim();
|
|
24
|
+
if (trimmed.startsWith('"')) {
|
|
25
|
+
let result = "";
|
|
26
|
+
for (let i = 1; i < trimmed.length; i += 1) {
|
|
27
|
+
const ch = trimmed[i];
|
|
28
|
+
if (ch === "\\" && i + 1 < trimmed.length) {
|
|
29
|
+
const next = trimmed[i + 1];
|
|
30
|
+
if (next === "n")
|
|
31
|
+
result += "\n";
|
|
32
|
+
else if (next === "r")
|
|
33
|
+
result += "\r";
|
|
34
|
+
else if (next === '"')
|
|
35
|
+
result += '"';
|
|
36
|
+
else if (next === "\\")
|
|
37
|
+
result += "\\";
|
|
38
|
+
else
|
|
39
|
+
result += `\\${next}`;
|
|
40
|
+
i += 1;
|
|
41
|
+
continue;
|
|
42
|
+
}
|
|
43
|
+
if (ch === '"') {
|
|
44
|
+
return result;
|
|
45
|
+
}
|
|
46
|
+
result += ch;
|
|
47
|
+
}
|
|
48
|
+
return result;
|
|
49
|
+
}
|
|
50
|
+
if (trimmed.startsWith("'")) {
|
|
51
|
+
const closeIndex = trimmed.indexOf("'", 1);
|
|
52
|
+
if (closeIndex !== -1) {
|
|
53
|
+
return trimmed.slice(1, closeIndex);
|
|
54
|
+
}
|
|
55
|
+
return trimmed.slice(1);
|
|
56
|
+
}
|
|
57
|
+
const hashIndex = trimmed.indexOf("#");
|
|
58
|
+
return (hashIndex === -1 ? trimmed : trimmed.slice(0, hashIndex)).trim();
|
|
59
|
+
}
|
|
60
|
+
function formatEnvValue(value) {
|
|
61
|
+
if (/[\r\n\0]/.test(value)) {
|
|
62
|
+
throw new CliError("Environment value cannot contain newline or NUL characters.", {
|
|
63
|
+
code: "INVALID_ENV_VALUE",
|
|
64
|
+
tryThis: "Provide a single-line plain text value for .env entries.",
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
if (/[\s#'"\\]/.test(value)) {
|
|
68
|
+
const escaped = value.replace(/\\/g, "\\\\").replace(/"/g, '\\"');
|
|
69
|
+
return `"${escaped}"`;
|
|
70
|
+
}
|
|
71
|
+
return value;
|
|
72
|
+
}
|
|
73
|
+
function parseEnvLineKey(line) {
|
|
74
|
+
const trimmed = line.trim();
|
|
75
|
+
if (!trimmed || trimmed.startsWith("#"))
|
|
76
|
+
return null;
|
|
77
|
+
const withoutExport = /^export\s+/.test(trimmed) ? trimmed.replace(/^export\s+/, "") : trimmed;
|
|
78
|
+
const eqIndex = withoutExport.indexOf("=");
|
|
79
|
+
if (eqIndex <= 0)
|
|
80
|
+
return null;
|
|
81
|
+
const key = withoutExport.slice(0, eqIndex).trim();
|
|
82
|
+
return VALID_ENV_KEY.test(key) ? key : null;
|
|
83
|
+
}
|
|
84
|
+
function parseEnvText(content) {
|
|
85
|
+
const result = {};
|
|
86
|
+
for (const rawLine of content.split(/\r?\n/)) {
|
|
87
|
+
const trimmed = rawLine.trim();
|
|
88
|
+
if (!trimmed || trimmed.startsWith("#"))
|
|
89
|
+
continue;
|
|
90
|
+
const withoutExport = /^export\s+/.test(trimmed) ? trimmed.replace(/^export\s+/, "") : trimmed;
|
|
91
|
+
const eqIndex = withoutExport.indexOf("=");
|
|
92
|
+
if (eqIndex <= 0)
|
|
93
|
+
continue;
|
|
94
|
+
const key = withoutExport.slice(0, eqIndex).trim();
|
|
95
|
+
if (!VALID_ENV_KEY.test(key))
|
|
96
|
+
continue;
|
|
97
|
+
const rawValue = withoutExport.slice(eqIndex + 1);
|
|
98
|
+
result[key] = unquoteEnvValue(rawValue);
|
|
99
|
+
}
|
|
100
|
+
return result;
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Parses `.env` (and `.env.local` by default, where `.env.local` overrides `.env`)
|
|
104
|
+
* into a plain key-value map without executing anything.
|
|
105
|
+
* Returns an empty object when neither file exists.
|
|
106
|
+
*/
|
|
107
|
+
export function readEnvFile(cwd, options = {}) {
|
|
108
|
+
const includeLocal = options.includeLocal ?? true;
|
|
109
|
+
const result = {};
|
|
110
|
+
const fileNames = includeLocal ? [".env", ".env.local"] : [".env"];
|
|
111
|
+
for (const fileName of fileNames) {
|
|
112
|
+
const envPath = path.join(cwd, fileName);
|
|
113
|
+
if (!existsSync(envPath))
|
|
114
|
+
continue;
|
|
115
|
+
const parsed = parseEnvText(readFileSync(envPath, "utf8"));
|
|
116
|
+
for (const [key, value] of Object.entries(parsed)) {
|
|
117
|
+
result[key] = value;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
return result;
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Merges `.env`, `.env.local` (overrides `.env`), and `processEnv` (highest precedence).
|
|
124
|
+
*/
|
|
125
|
+
export function readMergedProjectEnv(cwd, processEnv = process.env) {
|
|
126
|
+
const merged = { ...readEnvFile(cwd) };
|
|
127
|
+
for (const [key, value] of Object.entries(processEnv)) {
|
|
128
|
+
if (value !== undefined) {
|
|
129
|
+
merged[key] = value;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
return merged;
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Throws a clear error if a live API key (`pcific_live_...`) is ever handed to CLI or MCP tooling.
|
|
136
|
+
*/
|
|
137
|
+
export function assertTestKeyOnly(key) {
|
|
138
|
+
if (key.trim().startsWith("pcific_live_")) {
|
|
139
|
+
throw new CliError("Refused to use a live PCIFIC API key (pcific_live_...) in the CLI.", {
|
|
140
|
+
code: "LIVE_KEY_NOT_ALLOWED",
|
|
141
|
+
tryThis: "Use a test key (pcific_test_...) in PCIFIC_TEST_KEY for local development, CLI, and MCP. Keep your live key only in your production server settings.",
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* Atomically updates, appends, or deletes (`null`) keys in `.env` using a private `0600` temporary file.
|
|
147
|
+
* Preserves all unrelated lines and comments intact.
|
|
148
|
+
*/
|
|
149
|
+
export function writeEnvKeysAtomically(cwd, entries) {
|
|
150
|
+
const normalizedEntries = new Map();
|
|
151
|
+
const keysToDelete = new Set();
|
|
152
|
+
for (const [key, value] of Object.entries(entries)) {
|
|
153
|
+
if (value === undefined)
|
|
154
|
+
continue;
|
|
155
|
+
if (!VALID_ENV_KEY.test(key)) {
|
|
156
|
+
throw new CliError(`Invalid environment variable name "${key}".`, {
|
|
157
|
+
code: "INVALID_ENV_KEY",
|
|
158
|
+
tryThis: "Use uppercase letters, digits, and underscores such as PCIFIC_TEST_KEY.",
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
if (value === null) {
|
|
162
|
+
keysToDelete.add(key);
|
|
163
|
+
continue;
|
|
164
|
+
}
|
|
165
|
+
if (key === "PCIFIC_TEST_KEY") {
|
|
166
|
+
assertTestKeyOnly(value);
|
|
167
|
+
}
|
|
168
|
+
normalizedEntries.set(key, formatEnvValue(value));
|
|
169
|
+
}
|
|
170
|
+
const envPath = path.join(cwd, ".env");
|
|
171
|
+
const existingContent = existsSync(envPath) ? readFileSync(envPath, "utf8") : "";
|
|
172
|
+
const existingLines = existingContent.length > 0 ? existingContent.split(/\r?\n/) : [];
|
|
173
|
+
if (existingLines.length > 0 && existingLines[existingLines.length - 1] === "") {
|
|
174
|
+
existingLines.pop();
|
|
175
|
+
}
|
|
176
|
+
const seenKeys = new Set();
|
|
177
|
+
const nextLines = [];
|
|
178
|
+
for (const line of existingLines) {
|
|
179
|
+
const key = parseEnvLineKey(line);
|
|
180
|
+
if (key && keysToDelete.has(key)) {
|
|
181
|
+
continue;
|
|
182
|
+
}
|
|
183
|
+
if (key && normalizedEntries.has(key)) {
|
|
184
|
+
if (!seenKeys.has(key)) {
|
|
185
|
+
nextLines.push(`${key}=${normalizedEntries.get(key)}`);
|
|
186
|
+
seenKeys.add(key);
|
|
187
|
+
}
|
|
188
|
+
continue;
|
|
189
|
+
}
|
|
190
|
+
nextLines.push(line);
|
|
191
|
+
}
|
|
192
|
+
for (const [key, value] of normalizedEntries.entries()) {
|
|
193
|
+
if (!seenKeys.has(key)) {
|
|
194
|
+
nextLines.push(`${key}=${value}`);
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
const output = nextLines.length > 0 ? `${nextLines.join("\n")}\n` : "";
|
|
198
|
+
const tempPath = path.join(cwd, `.env.pcific.${randomBytes(6).toString("hex")}.tmp`);
|
|
199
|
+
let fd = null;
|
|
200
|
+
try {
|
|
201
|
+
fd = openSync(tempPath, "wx", 0o600);
|
|
202
|
+
writeSync(fd, output, 0, "utf8");
|
|
203
|
+
closeSync(fd);
|
|
204
|
+
fd = null;
|
|
205
|
+
chmodSync(tempPath, 0o600);
|
|
206
|
+
renameSync(tempPath, envPath);
|
|
207
|
+
chmodSync(envPath, 0o600);
|
|
208
|
+
}
|
|
209
|
+
catch (failure) {
|
|
210
|
+
if (fd !== null) {
|
|
211
|
+
try {
|
|
212
|
+
closeSync(fd);
|
|
213
|
+
}
|
|
214
|
+
catch {
|
|
215
|
+
// Ignore close error during cleanup.
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
try {
|
|
219
|
+
if (existsSync(tempPath))
|
|
220
|
+
unlinkSync(tempPath);
|
|
221
|
+
}
|
|
222
|
+
catch {
|
|
223
|
+
// Ignore temp cleanup error.
|
|
224
|
+
}
|
|
225
|
+
throw failure;
|
|
226
|
+
}
|
|
227
|
+
return envPath;
|
|
228
|
+
}
|
|
229
|
+
function gitignoreCoversEnv(content) {
|
|
230
|
+
let ignored = false;
|
|
231
|
+
for (const rawLine of content.split(/\r?\n/)) {
|
|
232
|
+
const line = rawLine.trim();
|
|
233
|
+
if (!line || line.startsWith("#"))
|
|
234
|
+
continue;
|
|
235
|
+
if (line === "!.env" || line === "!/.env") {
|
|
236
|
+
ignored = false;
|
|
237
|
+
continue;
|
|
238
|
+
}
|
|
239
|
+
if (line === ".env" ||
|
|
240
|
+
line === "/.env" ||
|
|
241
|
+
line === "**/.env" ||
|
|
242
|
+
line === ".env*" ||
|
|
243
|
+
line === "/.env*" ||
|
|
244
|
+
line === "*.env") {
|
|
245
|
+
ignored = true;
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
return ignored;
|
|
249
|
+
}
|
|
250
|
+
/**
|
|
251
|
+
* Ensures `.env` is ignored in `.gitignore`. Creates or updates `.gitignore` if needed.
|
|
252
|
+
*/
|
|
253
|
+
export function ensureEnvGitignored(cwd) {
|
|
254
|
+
const gitignorePath = path.join(cwd, ".gitignore");
|
|
255
|
+
if (existsSync(gitignorePath)) {
|
|
256
|
+
const content = readFileSync(gitignorePath, "utf8");
|
|
257
|
+
if (gitignoreCoversEnv(content)) {
|
|
258
|
+
return { status: "already-ignored", path: gitignorePath };
|
|
259
|
+
}
|
|
260
|
+
const prefix = content.length === 0 || content.endsWith("\n") ? "" : "\n";
|
|
261
|
+
writeFileSync(gitignorePath, `${content}${prefix}.env\n`, "utf8");
|
|
262
|
+
return { status: "added", path: gitignorePath };
|
|
263
|
+
}
|
|
264
|
+
writeFileSync(gitignorePath, ".env\n", "utf8");
|
|
265
|
+
return { status: "created", path: gitignorePath };
|
|
266
|
+
}
|
|
267
|
+
export function normalizeBaseUrl(rawUrl) {
|
|
268
|
+
const trimmed = rawUrl.trim();
|
|
269
|
+
let parsed;
|
|
270
|
+
try {
|
|
271
|
+
parsed = new URL(trimmed);
|
|
272
|
+
}
|
|
273
|
+
catch {
|
|
274
|
+
throw new CliError(`Invalid base URL "${trimmed}".`, {
|
|
275
|
+
code: "INVALID_BASE_URL",
|
|
276
|
+
tryThis: `Pass a full http(s) URL such as "${DEFAULT_BASE_URL}" with --base-url or PCIFIC_BASE_URL.`,
|
|
277
|
+
});
|
|
278
|
+
}
|
|
279
|
+
if (parsed.protocol !== "https:" && parsed.protocol !== "http:") {
|
|
280
|
+
throw new CliError(`Unsupported base URL protocol "${parsed.protocol}".`, {
|
|
281
|
+
code: "INVALID_BASE_URL",
|
|
282
|
+
tryThis: `Use an https:// or http:// URL such as "${DEFAULT_BASE_URL}".`,
|
|
283
|
+
});
|
|
284
|
+
}
|
|
285
|
+
return `${parsed.origin}${parsed.pathname}`.replace(/\/+$/, "");
|
|
286
|
+
}
|
|
287
|
+
/**
|
|
288
|
+
* Resolves CLI configuration from flags, process environment, and `.env`.
|
|
289
|
+
* Always rejects live keys (`pcific_live_...`).
|
|
290
|
+
*/
|
|
291
|
+
export function resolveCliConfig(cwd, flags = {}) {
|
|
292
|
+
const env = flags.env ?? process.env;
|
|
293
|
+
const envFilePath = path.join(cwd, ".env");
|
|
294
|
+
const localEnvPath = path.join(cwd, ".env.local");
|
|
295
|
+
const envFileExists = existsSync(envFilePath) || existsSync(localEnvPath);
|
|
296
|
+
const dotEnv = readEnvFile(cwd, { includeLocal: false });
|
|
297
|
+
const localEnv = existsSync(localEnvPath) ? parseEnvText(readFileSync(localEnvPath, "utf8")) : {};
|
|
298
|
+
const envFile = { ...dotEnv, ...localEnv };
|
|
299
|
+
const rawBaseUrl = nonEmpty(flags.baseUrl) ??
|
|
300
|
+
nonEmpty(env.PCIFIC_BASE_URL) ??
|
|
301
|
+
nonEmpty(envFile.PCIFIC_BASE_URL) ??
|
|
302
|
+
DEFAULT_BASE_URL;
|
|
303
|
+
const baseUrl = normalizeBaseUrl(rawBaseUrl);
|
|
304
|
+
const procTestKey = nonEmpty(env.PCIFIC_TEST_KEY);
|
|
305
|
+
const localTestKey = nonEmpty(localEnv.PCIFIC_TEST_KEY);
|
|
306
|
+
const dotTestKey = nonEmpty(dotEnv.PCIFIC_TEST_KEY);
|
|
307
|
+
const testKey = procTestKey ?? localTestKey ?? dotTestKey;
|
|
308
|
+
const testKeySource = procTestKey
|
|
309
|
+
? "process.env"
|
|
310
|
+
: localTestKey
|
|
311
|
+
? ".env.local"
|
|
312
|
+
: dotTestKey
|
|
313
|
+
? ".env"
|
|
314
|
+
: undefined;
|
|
315
|
+
let effectiveTestKey = testKey;
|
|
316
|
+
if (effectiveTestKey) {
|
|
317
|
+
if (flags.allowReplaceDotEnvLiveKey &&
|
|
318
|
+
testKeySource === ".env" &&
|
|
319
|
+
effectiveTestKey.startsWith("pcific_live_")) {
|
|
320
|
+
effectiveTestKey = undefined;
|
|
321
|
+
}
|
|
322
|
+
else {
|
|
323
|
+
assertTestKeyOnly(effectiveTestKey);
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
const marketplaceId = nonEmpty(env.PCIFIC_MARKETPLACE_ID) ?? nonEmpty(envFile.PCIFIC_MARKETPLACE_ID);
|
|
327
|
+
const mode = nonEmpty(env.PCIFIC_MODE) ?? nonEmpty(envFile.PCIFIC_MODE) ?? "test";
|
|
328
|
+
const liveKeyPresent = Boolean(nonEmpty(env.PCIFIC_LIVE_KEY) ?? nonEmpty(envFile.PCIFIC_LIVE_KEY));
|
|
329
|
+
return {
|
|
330
|
+
baseUrl,
|
|
331
|
+
testKey: effectiveTestKey,
|
|
332
|
+
testKeySource: effectiveTestKey ? testKeySource : undefined,
|
|
333
|
+
marketplaceId,
|
|
334
|
+
mode,
|
|
335
|
+
envFileExists,
|
|
336
|
+
envFilePath,
|
|
337
|
+
liveKeyPresent,
|
|
338
|
+
};
|
|
339
|
+
}
|