@shdwsdk/cli 1.2.1 → 1.2.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/LICENSE +21 -0
- package/README.md +26 -0
- package/dist/codegen.js +81 -81
- package/dist/connectivity.d.ts +1 -1
- package/dist/connectivity.d.ts.map +1 -1
- package/dist/connectivity.js +1 -1
- package/dist/connectivity.js.map +1 -1
- package/dist/wizard.d.ts.map +1 -1
- package/dist/wizard.js +22 -22
- package/dist/wizard.js.map +1 -1
- package/package.json +56 -47
- package/src/bin.ts +12 -12
- package/src/codegen.ts +167 -167
- package/src/connectivity.ts +131 -131
- package/src/env.ts +144 -144
- package/src/index.ts +31 -31
- package/src/wizard.ts +266 -267
package/src/env.ts
CHANGED
|
@@ -1,144 +1,144 @@
|
|
|
1
|
-
// ============================================================================
|
|
2
|
-
// @shdwsdk/cli — Safe Environment File Injection (.env / .env.local)
|
|
3
|
-
// Updates or appends SHDW_API_KEY and SHDW_GRID_REGION without overwriting
|
|
4
|
-
// unrelated environment variables or existing comments.
|
|
5
|
-
// ============================================================================
|
|
6
|
-
|
|
7
|
-
import { existsSync, readFileSync, writeFileSync } from "node:fs";
|
|
8
|
-
import { resolve } from "node:path";
|
|
9
|
-
|
|
10
|
-
export interface EnvInjectionResult {
|
|
11
|
-
filePath: string;
|
|
12
|
-
fileName: string;
|
|
13
|
-
createdNew: boolean;
|
|
14
|
-
updatedKeys: string[];
|
|
15
|
-
appendedKeys: string[];
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* Resolves the target environment file in `cwd`.
|
|
20
|
-
* Prefers an existing `.env.local` or `.env`; if neither exists and a Next.js
|
|
21
|
-
* config is detected, defaults to `.env.local`, otherwise `.env`.
|
|
22
|
-
*/
|
|
23
|
-
export function resolveTargetEnvFile(cwd: string): {
|
|
24
|
-
filePath: string;
|
|
25
|
-
fileName: string;
|
|
26
|
-
exists: boolean;
|
|
27
|
-
} {
|
|
28
|
-
const envLocalPath = resolve(cwd, ".env.local");
|
|
29
|
-
if (existsSync(envLocalPath)) {
|
|
30
|
-
return { filePath: envLocalPath, fileName: ".env.local", exists: true };
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
const envPath = resolve(cwd, ".env");
|
|
34
|
-
if (existsSync(envPath)) {
|
|
35
|
-
return { filePath: envPath, fileName: ".env", exists: true };
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
const isNextProject =
|
|
39
|
-
existsSync(resolve(cwd, "next.config.ts")) ||
|
|
40
|
-
existsSync(resolve(cwd, "next.config.mjs")) ||
|
|
41
|
-
existsSync(resolve(cwd, "next.config.js"));
|
|
42
|
-
|
|
43
|
-
const fileName = isNextProject ? ".env.local" : ".env";
|
|
44
|
-
return {
|
|
45
|
-
filePath: resolve(cwd, fileName),
|
|
46
|
-
fileName,
|
|
47
|
-
exists: false,
|
|
48
|
-
};
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
function formatEnvValue(val: string): string {
|
|
52
|
-
// Quote only if value contains spaces, hashes, or quotes
|
|
53
|
-
if (/[\s#"']/.test(val)) {
|
|
54
|
-
return `"${val.replace(/"/g, '\\"')}"`;
|
|
55
|
-
}
|
|
56
|
-
return val;
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
/**
|
|
60
|
-
* Upserts a set of key-value pairs into an `.env`-formatted string while
|
|
61
|
-
* preserving all unrelated variables, comments, and blank lines.
|
|
62
|
-
*/
|
|
63
|
-
export function upsertEnvContent(
|
|
64
|
-
existingContent: string,
|
|
65
|
-
entries: Readonly<Record<string, string>>
|
|
66
|
-
): {
|
|
67
|
-
content: string;
|
|
68
|
-
updatedKeys: string[];
|
|
69
|
-
appendedKeys: string[];
|
|
70
|
-
} {
|
|
71
|
-
const eol = existingContent.includes("\r\n") ? "\r\n" : "\n";
|
|
72
|
-
const lines = existingContent.length > 0 ? existingContent.split(/\r?\n/) : [];
|
|
73
|
-
const remaining = new Map<string, string>(Object.entries(entries));
|
|
74
|
-
const updatedKeys: string[] = [];
|
|
75
|
-
const appendedKeys: string[] = [];
|
|
76
|
-
|
|
77
|
-
const nextLines = lines.map((line) => {
|
|
78
|
-
const match = line.match(/^\s*(?:export\s+)?([A-Za-z_][A-Za-z0-9_]*)\s*=/);
|
|
79
|
-
if (!match) {
|
|
80
|
-
return line;
|
|
81
|
-
}
|
|
82
|
-
const key = match[1];
|
|
83
|
-
if (!key || !remaining.has(key)) {
|
|
84
|
-
return line;
|
|
85
|
-
}
|
|
86
|
-
const nextValue = remaining.get(key) ?? "";
|
|
87
|
-
remaining.delete(key);
|
|
88
|
-
updatedKeys.push(key);
|
|
89
|
-
const hasExportPrefix = /^\s*export\s+/.test(line);
|
|
90
|
-
return `${hasExportPrefix ? "export " : ""}${key}=${formatEnvValue(nextValue)}`;
|
|
91
|
-
});
|
|
92
|
-
|
|
93
|
-
if (remaining.size > 0) {
|
|
94
|
-
// Trim trailing empty lines before appending a clean SHDWsdk block
|
|
95
|
-
while (
|
|
96
|
-
nextLines.length > 0 &&
|
|
97
|
-
nextLines[nextLines.length - 1]?.trim() === ""
|
|
98
|
-
) {
|
|
99
|
-
nextLines.pop();
|
|
100
|
-
}
|
|
101
|
-
if (nextLines.length > 0) {
|
|
102
|
-
nextLines.push("");
|
|
103
|
-
}
|
|
104
|
-
nextLines.push("# SHDWsdk Scope 3 AI Carbon & AI-BOM Telemetry");
|
|
105
|
-
for (const [key, val] of remaining.entries()) {
|
|
106
|
-
nextLines.push(`${key}=${formatEnvValue(val)}`);
|
|
107
|
-
appendedKeys.push(key);
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
return {
|
|
112
|
-
content: nextLines.join(eol) + eol,
|
|
113
|
-
updatedKeys,
|
|
114
|
-
appendedKeys,
|
|
115
|
-
};
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
/**
|
|
119
|
-
* Injects `SHDW_API_KEY` and `SHDW_GRID_REGION` into `.env` or `.env.local`
|
|
120
|
-
* in the target directory without disturbing unrelated variables.
|
|
121
|
-
*/
|
|
122
|
-
export function injectShdwEnvVariables(params: {
|
|
123
|
-
cwd: string;
|
|
124
|
-
apiKey: string;
|
|
125
|
-
gridRegion: string;
|
|
126
|
-
}): EnvInjectionResult {
|
|
127
|
-
const target = resolveTargetEnvFile(params.cwd);
|
|
128
|
-
const existing = target.exists ? readFileSync(target.filePath, "utf8") : "";
|
|
129
|
-
|
|
130
|
-
const { content, updatedKeys, appendedKeys } = upsertEnvContent(existing, {
|
|
131
|
-
SHDW_API_KEY: params.apiKey,
|
|
132
|
-
SHDW_GRID_REGION: params.gridRegion,
|
|
133
|
-
});
|
|
134
|
-
|
|
135
|
-
writeFileSync(target.filePath, content, "utf8");
|
|
136
|
-
|
|
137
|
-
return {
|
|
138
|
-
filePath: target.filePath,
|
|
139
|
-
fileName: target.fileName,
|
|
140
|
-
createdNew: !target.exists,
|
|
141
|
-
updatedKeys,
|
|
142
|
-
appendedKeys,
|
|
143
|
-
};
|
|
144
|
-
}
|
|
1
|
+
// ============================================================================
|
|
2
|
+
// @shdwsdk/cli — Safe Environment File Injection (.env / .env.local)
|
|
3
|
+
// Updates or appends SHDW_API_KEY and SHDW_GRID_REGION without overwriting
|
|
4
|
+
// unrelated environment variables or existing comments.
|
|
5
|
+
// ============================================================================
|
|
6
|
+
|
|
7
|
+
import { existsSync, readFileSync, writeFileSync } from "node:fs";
|
|
8
|
+
import { resolve } from "node:path";
|
|
9
|
+
|
|
10
|
+
export interface EnvInjectionResult {
|
|
11
|
+
filePath: string;
|
|
12
|
+
fileName: string;
|
|
13
|
+
createdNew: boolean;
|
|
14
|
+
updatedKeys: string[];
|
|
15
|
+
appendedKeys: string[];
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Resolves the target environment file in `cwd`.
|
|
20
|
+
* Prefers an existing `.env.local` or `.env`; if neither exists and a Next.js
|
|
21
|
+
* config is detected, defaults to `.env.local`, otherwise `.env`.
|
|
22
|
+
*/
|
|
23
|
+
export function resolveTargetEnvFile(cwd: string): {
|
|
24
|
+
filePath: string;
|
|
25
|
+
fileName: string;
|
|
26
|
+
exists: boolean;
|
|
27
|
+
} {
|
|
28
|
+
const envLocalPath = resolve(cwd, ".env.local");
|
|
29
|
+
if (existsSync(envLocalPath)) {
|
|
30
|
+
return { filePath: envLocalPath, fileName: ".env.local", exists: true };
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const envPath = resolve(cwd, ".env");
|
|
34
|
+
if (existsSync(envPath)) {
|
|
35
|
+
return { filePath: envPath, fileName: ".env", exists: true };
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const isNextProject =
|
|
39
|
+
existsSync(resolve(cwd, "next.config.ts")) ||
|
|
40
|
+
existsSync(resolve(cwd, "next.config.mjs")) ||
|
|
41
|
+
existsSync(resolve(cwd, "next.config.js"));
|
|
42
|
+
|
|
43
|
+
const fileName = isNextProject ? ".env.local" : ".env";
|
|
44
|
+
return {
|
|
45
|
+
filePath: resolve(cwd, fileName),
|
|
46
|
+
fileName,
|
|
47
|
+
exists: false,
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function formatEnvValue(val: string): string {
|
|
52
|
+
// Quote only if value contains spaces, hashes, or quotes
|
|
53
|
+
if (/[\s#"']/.test(val)) {
|
|
54
|
+
return `"${val.replace(/"/g, '\\"')}"`;
|
|
55
|
+
}
|
|
56
|
+
return val;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Upserts a set of key-value pairs into an `.env`-formatted string while
|
|
61
|
+
* preserving all unrelated variables, comments, and blank lines.
|
|
62
|
+
*/
|
|
63
|
+
export function upsertEnvContent(
|
|
64
|
+
existingContent: string,
|
|
65
|
+
entries: Readonly<Record<string, string>>
|
|
66
|
+
): {
|
|
67
|
+
content: string;
|
|
68
|
+
updatedKeys: string[];
|
|
69
|
+
appendedKeys: string[];
|
|
70
|
+
} {
|
|
71
|
+
const eol = existingContent.includes("\r\n") ? "\r\n" : "\n";
|
|
72
|
+
const lines = existingContent.length > 0 ? existingContent.split(/\r?\n/) : [];
|
|
73
|
+
const remaining = new Map<string, string>(Object.entries(entries));
|
|
74
|
+
const updatedKeys: string[] = [];
|
|
75
|
+
const appendedKeys: string[] = [];
|
|
76
|
+
|
|
77
|
+
const nextLines = lines.map((line) => {
|
|
78
|
+
const match = line.match(/^\s*(?:export\s+)?([A-Za-z_][A-Za-z0-9_]*)\s*=/);
|
|
79
|
+
if (!match) {
|
|
80
|
+
return line;
|
|
81
|
+
}
|
|
82
|
+
const key = match[1];
|
|
83
|
+
if (!key || !remaining.has(key)) {
|
|
84
|
+
return line;
|
|
85
|
+
}
|
|
86
|
+
const nextValue = remaining.get(key) ?? "";
|
|
87
|
+
remaining.delete(key);
|
|
88
|
+
updatedKeys.push(key);
|
|
89
|
+
const hasExportPrefix = /^\s*export\s+/.test(line);
|
|
90
|
+
return `${hasExportPrefix ? "export " : ""}${key}=${formatEnvValue(nextValue)}`;
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
if (remaining.size > 0) {
|
|
94
|
+
// Trim trailing empty lines before appending a clean SHDWsdk block
|
|
95
|
+
while (
|
|
96
|
+
nextLines.length > 0 &&
|
|
97
|
+
nextLines[nextLines.length - 1]?.trim() === ""
|
|
98
|
+
) {
|
|
99
|
+
nextLines.pop();
|
|
100
|
+
}
|
|
101
|
+
if (nextLines.length > 0) {
|
|
102
|
+
nextLines.push("");
|
|
103
|
+
}
|
|
104
|
+
nextLines.push("# SHDWsdk Scope 3 AI Carbon & AI-BOM Telemetry");
|
|
105
|
+
for (const [key, val] of remaining.entries()) {
|
|
106
|
+
nextLines.push(`${key}=${formatEnvValue(val)}`);
|
|
107
|
+
appendedKeys.push(key);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
return {
|
|
112
|
+
content: nextLines.join(eol) + eol,
|
|
113
|
+
updatedKeys,
|
|
114
|
+
appendedKeys,
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Injects `SHDW_API_KEY` and `SHDW_GRID_REGION` into `.env` or `.env.local`
|
|
120
|
+
* in the target directory without disturbing unrelated variables.
|
|
121
|
+
*/
|
|
122
|
+
export function injectShdwEnvVariables(params: {
|
|
123
|
+
cwd: string;
|
|
124
|
+
apiKey: string;
|
|
125
|
+
gridRegion: string;
|
|
126
|
+
}): EnvInjectionResult {
|
|
127
|
+
const target = resolveTargetEnvFile(params.cwd);
|
|
128
|
+
const existing = target.exists ? readFileSync(target.filePath, "utf8") : "";
|
|
129
|
+
|
|
130
|
+
const { content, updatedKeys, appendedKeys } = upsertEnvContent(existing, {
|
|
131
|
+
SHDW_API_KEY: params.apiKey,
|
|
132
|
+
SHDW_GRID_REGION: params.gridRegion,
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
writeFileSync(target.filePath, content, "utf8");
|
|
136
|
+
|
|
137
|
+
return {
|
|
138
|
+
filePath: target.filePath,
|
|
139
|
+
fileName: target.fileName,
|
|
140
|
+
createdNew: !target.exists,
|
|
141
|
+
updatedKeys,
|
|
142
|
+
appendedKeys,
|
|
143
|
+
};
|
|
144
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -1,31 +1,31 @@
|
|
|
1
|
-
// ============================================================================
|
|
2
|
-
// @shdwsdk/cli — Public Library Entrypoint
|
|
3
|
-
// ============================================================================
|
|
4
|
-
|
|
5
|
-
export {
|
|
6
|
-
buildBoilerplateSource,
|
|
7
|
-
generateShdwBoilerplateFile,
|
|
8
|
-
type GenerateBoilerplateParams,
|
|
9
|
-
type GeneratedBoilerplateResult,
|
|
10
|
-
} from "./codegen.js";
|
|
11
|
-
|
|
12
|
-
export {
|
|
13
|
-
DEFAULT_SHDW_INGEST_ENDPOINT,
|
|
14
|
-
testShdwConnectivity,
|
|
15
|
-
type ConnectivityTestResult,
|
|
16
|
-
} from "./connectivity.js";
|
|
17
|
-
|
|
18
|
-
export {
|
|
19
|
-
injectShdwEnvVariables,
|
|
20
|
-
resolveTargetEnvFile,
|
|
21
|
-
upsertEnvContent,
|
|
22
|
-
type EnvInjectionResult,
|
|
23
|
-
} from "./env.js";
|
|
24
|
-
|
|
25
|
-
export {
|
|
26
|
-
parseCliArgs,
|
|
27
|
-
runCliFromArgv,
|
|
28
|
-
runInitWizard,
|
|
29
|
-
type CliWizardOptions,
|
|
30
|
-
type CliWizardResult,
|
|
31
|
-
} from "./wizard.js";
|
|
1
|
+
// ============================================================================
|
|
2
|
+
// @shdwsdk/cli — Public Library Entrypoint
|
|
3
|
+
// ============================================================================
|
|
4
|
+
|
|
5
|
+
export {
|
|
6
|
+
buildBoilerplateSource,
|
|
7
|
+
generateShdwBoilerplateFile,
|
|
8
|
+
type GenerateBoilerplateParams,
|
|
9
|
+
type GeneratedBoilerplateResult,
|
|
10
|
+
} from "./codegen.js";
|
|
11
|
+
|
|
12
|
+
export {
|
|
13
|
+
DEFAULT_SHDW_INGEST_ENDPOINT,
|
|
14
|
+
testShdwConnectivity,
|
|
15
|
+
type ConnectivityTestResult,
|
|
16
|
+
} from "./connectivity.js";
|
|
17
|
+
|
|
18
|
+
export {
|
|
19
|
+
injectShdwEnvVariables,
|
|
20
|
+
resolveTargetEnvFile,
|
|
21
|
+
upsertEnvContent,
|
|
22
|
+
type EnvInjectionResult,
|
|
23
|
+
} from "./env.js";
|
|
24
|
+
|
|
25
|
+
export {
|
|
26
|
+
parseCliArgs,
|
|
27
|
+
runCliFromArgv,
|
|
28
|
+
runInitWizard,
|
|
29
|
+
type CliWizardOptions,
|
|
30
|
+
type CliWizardResult,
|
|
31
|
+
} from "./wizard.js";
|