@clifn/core 0.1.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/README.md +383 -0
- package/dist/client.d.ts +46 -0
- package/dist/client.js +198 -0
- package/dist/config-loader.d.ts +18 -0
- package/dist/config-loader.js +125 -0
- package/dist/config.d.ts +13 -0
- package/dist/config.js +57 -0
- package/dist/credentials.d.ts +19 -0
- package/dist/credentials.js +79 -0
- package/dist/diagnostics.d.ts +19 -0
- package/dist/diagnostics.js +152 -0
- package/dist/env.d.ts +20 -0
- package/dist/env.js +74 -0
- package/dist/exec.d.ts +29 -0
- package/dist/exec.js +143 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.js +13 -0
- package/dist/output.d.ts +37 -0
- package/dist/output.js +217 -0
- package/dist/prompt.d.ts +18 -0
- package/dist/prompt.js +83 -0
- package/dist/runner.d.ts +36 -0
- package/dist/runner.js +94 -0
- package/dist/scaffold.d.ts +26 -0
- package/dist/scaffold.js +94 -0
- package/dist/stdio.d.ts +7 -0
- package/dist/stdio.js +33 -0
- package/dist/ui.d.ts +10 -0
- package/dist/ui.js +25 -0
- package/package.json +120 -0
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import { existsSync } from "node:fs";
|
|
2
|
+
import { readFile } from "node:fs/promises";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import { pathToFileURL } from "node:url";
|
|
5
|
+
import { createJiti } from "jiti";
|
|
6
|
+
const DEFAULT_CANDIDATES = [
|
|
7
|
+
"config.ts",
|
|
8
|
+
"config.js",
|
|
9
|
+
"config.mjs",
|
|
10
|
+
"config.cjs",
|
|
11
|
+
"config.json",
|
|
12
|
+
];
|
|
13
|
+
const SUPPORTED_EXTENSIONS = new Set([".ts", ".js", ".mjs", ".cjs", ".json"]);
|
|
14
|
+
export class ConfigLoaderError extends Error {
|
|
15
|
+
code;
|
|
16
|
+
constructor(code, message) {
|
|
17
|
+
super(message);
|
|
18
|
+
this.name = "ConfigLoaderError";
|
|
19
|
+
this.code = code;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
function isRemotePath(value) {
|
|
23
|
+
return /^[a-z][a-z0-9+.-]*:\/\//i.test(value);
|
|
24
|
+
}
|
|
25
|
+
function ensureLocalPath(value) {
|
|
26
|
+
if (isRemotePath(value)) {
|
|
27
|
+
throw new ConfigLoaderError("CLIFN_CONFIG_NOT_FOUND", "Remote config URLs are not supported.");
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
function resolveCandidates(options) {
|
|
31
|
+
if (options.configPath) {
|
|
32
|
+
return [options.configPath];
|
|
33
|
+
}
|
|
34
|
+
const candidates = options.candidates && options.candidates.length > 0 ? [...options.candidates] : [...DEFAULT_CANDIDATES];
|
|
35
|
+
if (candidates.length > 32) {
|
|
36
|
+
throw new ConfigLoaderError("CLIFN_CONFIG_INVALID", "Config candidate list exceeds the limit of 32 entries.");
|
|
37
|
+
}
|
|
38
|
+
return candidates;
|
|
39
|
+
}
|
|
40
|
+
async function resolveConfigValue(value) {
|
|
41
|
+
const resolved = typeof value === "function" ? value() : value;
|
|
42
|
+
return await Promise.resolve(resolved);
|
|
43
|
+
}
|
|
44
|
+
function resolveExportValue(moduleValue, exportNames) {
|
|
45
|
+
if (moduleValue && typeof moduleValue === "object") {
|
|
46
|
+
const record = moduleValue;
|
|
47
|
+
const prototype = Object.getPrototypeOf(record);
|
|
48
|
+
const looksLikeModuleWrapper = record.__esModule === true || prototype === null || prototype !== Object.prototype;
|
|
49
|
+
if (!looksLikeModuleWrapper) {
|
|
50
|
+
return moduleValue;
|
|
51
|
+
}
|
|
52
|
+
if (record.default !== undefined) {
|
|
53
|
+
return record.default;
|
|
54
|
+
}
|
|
55
|
+
for (const exportName of exportNames) {
|
|
56
|
+
if (record[exportName] !== undefined) {
|
|
57
|
+
return record[exportName];
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
return moduleValue;
|
|
62
|
+
}
|
|
63
|
+
async function loadJsonConfig(resolvedPath) {
|
|
64
|
+
try {
|
|
65
|
+
const raw = await readFile(resolvedPath, "utf8");
|
|
66
|
+
return JSON.parse(raw);
|
|
67
|
+
}
|
|
68
|
+
catch (error) {
|
|
69
|
+
const message = error instanceof Error ? error.message : "Invalid JSON config";
|
|
70
|
+
throw new ConfigLoaderError("CLIFN_CONFIG_INVALID", message);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
async function loadModuleConfig(resolvedPath) {
|
|
74
|
+
const extension = path.extname(resolvedPath).toLowerCase();
|
|
75
|
+
if (!SUPPORTED_EXTENSIONS.has(extension)) {
|
|
76
|
+
throw new ConfigLoaderError("CLIFN_CONFIG_UNSUPPORTED", `Unsupported config extension: ${extension || "<none>"}`);
|
|
77
|
+
}
|
|
78
|
+
if (extension === ".json") {
|
|
79
|
+
return loadJsonConfig(resolvedPath);
|
|
80
|
+
}
|
|
81
|
+
if (extension === ".ts") {
|
|
82
|
+
const jiti = createJiti(path.dirname(resolvedPath), {
|
|
83
|
+
moduleCache: false,
|
|
84
|
+
interopDefault: false,
|
|
85
|
+
extensions: [".ts", ".js", ".mjs", ".cjs", ".json"],
|
|
86
|
+
});
|
|
87
|
+
return await jiti.import(resolvedPath);
|
|
88
|
+
}
|
|
89
|
+
return import(pathToFileURL(resolvedPath).href);
|
|
90
|
+
}
|
|
91
|
+
export function defineConfig(config) {
|
|
92
|
+
return config;
|
|
93
|
+
}
|
|
94
|
+
export async function loadConfig(options = {}) {
|
|
95
|
+
const cwd = options.cwd ?? process.cwd();
|
|
96
|
+
const candidates = resolveCandidates(options);
|
|
97
|
+
const exportNames = options.exportNames ?? [];
|
|
98
|
+
for (const candidate of candidates) {
|
|
99
|
+
ensureLocalPath(candidate);
|
|
100
|
+
const resolvedPath = path.isAbsolute(candidate) ? candidate : path.resolve(cwd, candidate);
|
|
101
|
+
if (!existsSync(resolvedPath)) {
|
|
102
|
+
continue;
|
|
103
|
+
}
|
|
104
|
+
try {
|
|
105
|
+
const loadedModule = await loadModuleConfig(resolvedPath);
|
|
106
|
+
const configValue = await resolveConfigValue(resolveExportValue(loadedModule, exportNames));
|
|
107
|
+
const validated = options.validate ? options.validate(configValue, resolvedPath) : configValue;
|
|
108
|
+
return {
|
|
109
|
+
config: validated,
|
|
110
|
+
path: resolvedPath,
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
catch (error) {
|
|
114
|
+
if (error instanceof ConfigLoaderError) {
|
|
115
|
+
throw error;
|
|
116
|
+
}
|
|
117
|
+
const message = error instanceof Error ? error.message : `Config loading failed for ${resolvedPath}`;
|
|
118
|
+
throw new ConfigLoaderError("CLIFN_CONFIG_INVALID", message);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
if (options.configPath) {
|
|
122
|
+
throw new ConfigLoaderError("CLIFN_CONFIG_NOT_FOUND", `Config file not found: ${options.configPath}`);
|
|
123
|
+
}
|
|
124
|
+
throw new ConfigLoaderError("CLIFN_CONFIG_NOT_FOUND", `Config file not found. Checked: ${candidates.join(", ")}`);
|
|
125
|
+
}
|
package/dist/config.d.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export interface ProjectConfigStore<TConfig extends Record<string, unknown> = Record<string, unknown>> {
|
|
2
|
+
readonly path: string;
|
|
3
|
+
read(): TConfig;
|
|
4
|
+
write(next: TConfig): void;
|
|
5
|
+
get<TKey extends keyof TConfig>(key: TKey): TConfig[TKey] | undefined;
|
|
6
|
+
set<TKey extends string, TValue>(key: TKey, value: TValue): void;
|
|
7
|
+
}
|
|
8
|
+
export declare class InvalidConfigError extends Error {
|
|
9
|
+
readonly filePath: string;
|
|
10
|
+
readonly code = "CLIFN_INVALID_CONFIG";
|
|
11
|
+
constructor(filePath: string, detail: string);
|
|
12
|
+
}
|
|
13
|
+
export declare function createProjectConfig<TConfig extends Record<string, unknown> = Record<string, unknown>>(path?: string): ProjectConfigStore<TConfig>;
|
package/dist/config.js
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
2
|
+
import { dirname, join } from "node:path";
|
|
3
|
+
export class InvalidConfigError extends Error {
|
|
4
|
+
filePath;
|
|
5
|
+
code = "CLIFN_INVALID_CONFIG";
|
|
6
|
+
constructor(filePath, detail) {
|
|
7
|
+
super(`Invalid config at ${filePath}: ${detail}`);
|
|
8
|
+
this.name = "InvalidConfigError";
|
|
9
|
+
this.filePath = filePath;
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
const DEFAULT_CONFIG_PATH = join(process.cwd(), "conduct.config.json");
|
|
13
|
+
function parseConfig(path) {
|
|
14
|
+
if (!existsSync(path)) {
|
|
15
|
+
return {};
|
|
16
|
+
}
|
|
17
|
+
const raw = readFileSync(path, "utf8").trim();
|
|
18
|
+
if (raw.length === 0) {
|
|
19
|
+
return {};
|
|
20
|
+
}
|
|
21
|
+
try {
|
|
22
|
+
const parsed = JSON.parse(raw);
|
|
23
|
+
if (typeof parsed !== "object" || parsed === null || Array.isArray(parsed)) {
|
|
24
|
+
throw new InvalidConfigError(path, "config root must be a JSON object");
|
|
25
|
+
}
|
|
26
|
+
return parsed;
|
|
27
|
+
}
|
|
28
|
+
catch (error) {
|
|
29
|
+
if (error instanceof InvalidConfigError) {
|
|
30
|
+
throw error;
|
|
31
|
+
}
|
|
32
|
+
const message = error instanceof Error ? error.message : "unknown parse error";
|
|
33
|
+
throw new InvalidConfigError(path, message);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
export function createProjectConfig(path = DEFAULT_CONFIG_PATH) {
|
|
37
|
+
return {
|
|
38
|
+
path,
|
|
39
|
+
read() {
|
|
40
|
+
return parseConfig(path);
|
|
41
|
+
},
|
|
42
|
+
write(next) {
|
|
43
|
+
mkdirSync(dirname(path), { recursive: true });
|
|
44
|
+
writeFileSync(path, `${JSON.stringify(next, null, 2)}\n`, "utf8");
|
|
45
|
+
},
|
|
46
|
+
get(key) {
|
|
47
|
+
const parsed = parseConfig(path);
|
|
48
|
+
return parsed[key];
|
|
49
|
+
},
|
|
50
|
+
set(key, value) {
|
|
51
|
+
const parsed = parseConfig(path);
|
|
52
|
+
parsed[key] = value;
|
|
53
|
+
mkdirSync(dirname(path), { recursive: true });
|
|
54
|
+
writeFileSync(path, `${JSON.stringify(parsed, null, 2)}\n`, "utf8");
|
|
55
|
+
},
|
|
56
|
+
};
|
|
57
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export interface CredentialProfile {
|
|
2
|
+
backend: string;
|
|
3
|
+
key: string;
|
|
4
|
+
}
|
|
5
|
+
export interface CredentialStore {
|
|
6
|
+
readonly path: string;
|
|
7
|
+
getProfile(name: string): CredentialProfile;
|
|
8
|
+
setProfile(name: string, profile: CredentialProfile): void;
|
|
9
|
+
hasProfile(name: string): boolean;
|
|
10
|
+
listProfiles(): string[];
|
|
11
|
+
removeProfile(name: string): void;
|
|
12
|
+
}
|
|
13
|
+
export declare class MissingProfileError extends Error {
|
|
14
|
+
readonly profile: string;
|
|
15
|
+
readonly filePath: string;
|
|
16
|
+
readonly code = "CLIFN_MISSING_PROFILE";
|
|
17
|
+
constructor(profile: string, filePath: string);
|
|
18
|
+
}
|
|
19
|
+
export declare function createCredentialStore(path?: string): CredentialStore;
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { chmodSync, existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
2
|
+
import { homedir } from "node:os";
|
|
3
|
+
import { dirname, join } from "node:path";
|
|
4
|
+
import ini from "ini";
|
|
5
|
+
export class MissingProfileError extends Error {
|
|
6
|
+
profile;
|
|
7
|
+
filePath;
|
|
8
|
+
code = "CLIFN_MISSING_PROFILE";
|
|
9
|
+
constructor(profile, filePath) {
|
|
10
|
+
super(`Profile "${profile}" was not found in credentials file: ${filePath}`);
|
|
11
|
+
this.name = "MissingProfileError";
|
|
12
|
+
this.profile = profile;
|
|
13
|
+
this.filePath = filePath;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
const DEFAULT_CREDENTIALS_PATH = join(homedir(), ".conduct", "credentials");
|
|
17
|
+
function readProfiles(path) {
|
|
18
|
+
if (!existsSync(path)) {
|
|
19
|
+
return {};
|
|
20
|
+
}
|
|
21
|
+
const raw = readFileSync(path, "utf8");
|
|
22
|
+
const parsed = ini.parse(raw);
|
|
23
|
+
const out = {};
|
|
24
|
+
for (const [name, value] of Object.entries(parsed)) {
|
|
25
|
+
if (typeof value !== "object" || value === null) {
|
|
26
|
+
continue;
|
|
27
|
+
}
|
|
28
|
+
const candidate = value;
|
|
29
|
+
if (typeof candidate.backend === "string" && typeof candidate.key === "string") {
|
|
30
|
+
out[name] = {
|
|
31
|
+
backend: candidate.backend,
|
|
32
|
+
key: candidate.key,
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
return out;
|
|
37
|
+
}
|
|
38
|
+
function writeProfiles(path, profiles) {
|
|
39
|
+
const directory = dirname(path);
|
|
40
|
+
mkdirSync(directory, { recursive: true, mode: 0o700 });
|
|
41
|
+
chmodSync(directory, 0o700);
|
|
42
|
+
const serialized = ini.stringify(profiles);
|
|
43
|
+
writeFileSync(path, serialized, { encoding: "utf8", mode: 0o600 });
|
|
44
|
+
chmodSync(path, 0o600);
|
|
45
|
+
}
|
|
46
|
+
export function createCredentialStore(path = DEFAULT_CREDENTIALS_PATH) {
|
|
47
|
+
return {
|
|
48
|
+
path,
|
|
49
|
+
getProfile(name) {
|
|
50
|
+
const profiles = readProfiles(path);
|
|
51
|
+
const profile = profiles[name];
|
|
52
|
+
if (!profile) {
|
|
53
|
+
throw new MissingProfileError(name, path);
|
|
54
|
+
}
|
|
55
|
+
return profile;
|
|
56
|
+
},
|
|
57
|
+
setProfile(name, profile) {
|
|
58
|
+
const profiles = readProfiles(path);
|
|
59
|
+
profiles[name] = profile;
|
|
60
|
+
writeProfiles(path, profiles);
|
|
61
|
+
},
|
|
62
|
+
hasProfile(name) {
|
|
63
|
+
const profiles = readProfiles(path);
|
|
64
|
+
return Boolean(profiles[name]);
|
|
65
|
+
},
|
|
66
|
+
listProfiles() {
|
|
67
|
+
const profiles = readProfiles(path);
|
|
68
|
+
return Object.keys(profiles);
|
|
69
|
+
},
|
|
70
|
+
removeProfile(name) {
|
|
71
|
+
const profiles = readProfiles(path);
|
|
72
|
+
if (!profiles[name]) {
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
delete profiles[name];
|
|
76
|
+
writeProfiles(path, profiles);
|
|
77
|
+
},
|
|
78
|
+
};
|
|
79
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export type DiagnosticSeverity = "error" | "warning" | "info";
|
|
2
|
+
export type DiagnosticDetails = Record<string, unknown>;
|
|
3
|
+
export interface Diagnostic {
|
|
4
|
+
code: string;
|
|
5
|
+
severity: DiagnosticSeverity;
|
|
6
|
+
message: string;
|
|
7
|
+
path?: string;
|
|
8
|
+
details?: DiagnosticDetails;
|
|
9
|
+
}
|
|
10
|
+
export interface DiagnosticReport {
|
|
11
|
+
ok: boolean;
|
|
12
|
+
diagnostics: readonly Diagnostic[];
|
|
13
|
+
}
|
|
14
|
+
export declare function redactValue(value: unknown, seen?: WeakSet<object>): unknown;
|
|
15
|
+
export declare function createDiagnostic(input: Diagnostic): Diagnostic;
|
|
16
|
+
export declare function sortDiagnostics(input: readonly Diagnostic[]): Diagnostic[];
|
|
17
|
+
export declare function redactDiagnostics(input: readonly Diagnostic[]): Diagnostic[];
|
|
18
|
+
export declare function formatDiagnosticsText(input: readonly Diagnostic[]): string;
|
|
19
|
+
export declare function formatDiagnosticsJson(input: readonly Diagnostic[]): string;
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
const SENSITIVE_KEY_PATTERN = /(authorization|token|secret|password|cookie|api[-_]?key|session[-_]?id|access[-_]?key|refresh[-_]?token)/i;
|
|
2
|
+
const SEVERITY_ORDER = {
|
|
3
|
+
error: 0,
|
|
4
|
+
warning: 1,
|
|
5
|
+
info: 2,
|
|
6
|
+
};
|
|
7
|
+
const CIRCULAR_SENTINEL = "[Circular]";
|
|
8
|
+
function cloneValue(value, seen = new WeakSet()) {
|
|
9
|
+
if (Array.isArray(value)) {
|
|
10
|
+
if (seen.has(value)) {
|
|
11
|
+
return CIRCULAR_SENTINEL;
|
|
12
|
+
}
|
|
13
|
+
seen.add(value);
|
|
14
|
+
try {
|
|
15
|
+
return value.map((item) => cloneValue(item, seen));
|
|
16
|
+
}
|
|
17
|
+
finally {
|
|
18
|
+
seen.delete(value);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
if (value && typeof value === "object") {
|
|
22
|
+
if (seen.has(value)) {
|
|
23
|
+
return CIRCULAR_SENTINEL;
|
|
24
|
+
}
|
|
25
|
+
seen.add(value);
|
|
26
|
+
try {
|
|
27
|
+
return Object.fromEntries(Object.entries(value).map(([key, nested]) => [key, cloneValue(nested, seen)]));
|
|
28
|
+
}
|
|
29
|
+
finally {
|
|
30
|
+
seen.delete(value);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
return value;
|
|
34
|
+
}
|
|
35
|
+
export function redactValue(value, seen = new WeakSet()) {
|
|
36
|
+
if (Array.isArray(value)) {
|
|
37
|
+
if (seen.has(value)) {
|
|
38
|
+
return CIRCULAR_SENTINEL;
|
|
39
|
+
}
|
|
40
|
+
seen.add(value);
|
|
41
|
+
try {
|
|
42
|
+
return value.map((item) => redactValue(item, seen));
|
|
43
|
+
}
|
|
44
|
+
finally {
|
|
45
|
+
seen.delete(value);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
if (value && typeof value === "object") {
|
|
49
|
+
if (seen.has(value)) {
|
|
50
|
+
return CIRCULAR_SENTINEL;
|
|
51
|
+
}
|
|
52
|
+
seen.add(value);
|
|
53
|
+
try {
|
|
54
|
+
return Object.fromEntries(Object.entries(value).map(([key, nested]) => {
|
|
55
|
+
if (SENSITIVE_KEY_PATTERN.test(key)) {
|
|
56
|
+
return [key, "[REDACTED]"];
|
|
57
|
+
}
|
|
58
|
+
return [key, redactValue(nested, seen)];
|
|
59
|
+
}));
|
|
60
|
+
}
|
|
61
|
+
finally {
|
|
62
|
+
seen.delete(value);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
return value;
|
|
66
|
+
}
|
|
67
|
+
function normalizeForSerialization(value, seen = new WeakSet()) {
|
|
68
|
+
if (Array.isArray(value)) {
|
|
69
|
+
if (seen.has(value)) {
|
|
70
|
+
return CIRCULAR_SENTINEL;
|
|
71
|
+
}
|
|
72
|
+
seen.add(value);
|
|
73
|
+
try {
|
|
74
|
+
return value.map((item) => normalizeForSerialization(item, seen));
|
|
75
|
+
}
|
|
76
|
+
finally {
|
|
77
|
+
seen.delete(value);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
if (value && typeof value === "object") {
|
|
81
|
+
if (seen.has(value)) {
|
|
82
|
+
return CIRCULAR_SENTINEL;
|
|
83
|
+
}
|
|
84
|
+
seen.add(value);
|
|
85
|
+
try {
|
|
86
|
+
return Object.fromEntries(Object.keys(value)
|
|
87
|
+
.sort((left, right) => left.localeCompare(right))
|
|
88
|
+
.map((key) => [key, normalizeForSerialization(value[key], seen)]));
|
|
89
|
+
}
|
|
90
|
+
finally {
|
|
91
|
+
seen.delete(value);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
return value;
|
|
95
|
+
}
|
|
96
|
+
function compareDiagnostics(left, right) {
|
|
97
|
+
return (SEVERITY_ORDER[left.severity] - SEVERITY_ORDER[right.severity] ||
|
|
98
|
+
left.code.localeCompare(right.code) ||
|
|
99
|
+
left.message.localeCompare(right.message) ||
|
|
100
|
+
(left.path ?? "").localeCompare(right.path ?? ""));
|
|
101
|
+
}
|
|
102
|
+
function toReport(input) {
|
|
103
|
+
const diagnostics = sortDiagnostics(redactDiagnostics(input));
|
|
104
|
+
return {
|
|
105
|
+
ok: diagnostics.every((diagnostic) => diagnostic.severity !== "error"),
|
|
106
|
+
diagnostics,
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
function stableStringify(value) {
|
|
110
|
+
return JSON.stringify(normalizeForSerialization(value));
|
|
111
|
+
}
|
|
112
|
+
export function createDiagnostic(input) {
|
|
113
|
+
return {
|
|
114
|
+
code: input.code,
|
|
115
|
+
severity: input.severity,
|
|
116
|
+
message: input.message,
|
|
117
|
+
path: input.path,
|
|
118
|
+
details: input.details ? cloneValue(input.details) : undefined,
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
export function sortDiagnostics(input) {
|
|
122
|
+
return input
|
|
123
|
+
.map((diagnostic, index) => ({
|
|
124
|
+
diagnostic: createDiagnostic(diagnostic),
|
|
125
|
+
index,
|
|
126
|
+
}))
|
|
127
|
+
.sort((left, right) => compareDiagnostics(left.diagnostic, right.diagnostic) || left.index - right.index)
|
|
128
|
+
.map((entry) => entry.diagnostic);
|
|
129
|
+
}
|
|
130
|
+
export function redactDiagnostics(input) {
|
|
131
|
+
return input.map((diagnostic) => ({
|
|
132
|
+
...createDiagnostic(diagnostic),
|
|
133
|
+
details: diagnostic.details ? redactValue(diagnostic.details) : undefined,
|
|
134
|
+
}));
|
|
135
|
+
}
|
|
136
|
+
export function formatDiagnosticsText(input) {
|
|
137
|
+
const report = toReport(input);
|
|
138
|
+
const lines = [];
|
|
139
|
+
for (const diagnostic of report.diagnostics) {
|
|
140
|
+
lines.push(`[${diagnostic.severity}] ${diagnostic.code}: ${diagnostic.message}`);
|
|
141
|
+
if (diagnostic.path) {
|
|
142
|
+
lines.push(` path: ${diagnostic.path}`);
|
|
143
|
+
}
|
|
144
|
+
if (diagnostic.details) {
|
|
145
|
+
lines.push(` details: ${stableStringify(diagnostic.details)}`);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
return `${lines.join("\n")}\n`;
|
|
149
|
+
}
|
|
150
|
+
export function formatDiagnosticsJson(input) {
|
|
151
|
+
return `${stableStringify(toReport(input))}\n`;
|
|
152
|
+
}
|
package/dist/env.d.ts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
export type EnvErrorCode = "CLIFN_ENV_INVALID" | "CLIFN_ENV_MISSING" | "CLIFN_ENV_OUT_OF_RANGE";
|
|
2
|
+
export interface StringEnvOptions {
|
|
3
|
+
defaultValue?: string;
|
|
4
|
+
allowEmpty?: boolean;
|
|
5
|
+
env?: NodeJS.ProcessEnv;
|
|
6
|
+
}
|
|
7
|
+
export interface IntEnvOptions {
|
|
8
|
+
defaultValue?: number;
|
|
9
|
+
min?: number;
|
|
10
|
+
max?: number;
|
|
11
|
+
env?: NodeJS.ProcessEnv;
|
|
12
|
+
}
|
|
13
|
+
export interface BooleanEnvOptions {
|
|
14
|
+
defaultValue?: boolean;
|
|
15
|
+
env?: NodeJS.ProcessEnv;
|
|
16
|
+
}
|
|
17
|
+
export declare function readRequiredStringEnv(key: string, options?: Omit<StringEnvOptions, "defaultValue">): string;
|
|
18
|
+
export declare function readStringEnv(key: string, options?: StringEnvOptions): string;
|
|
19
|
+
export declare function readIntEnv(key: string, options?: IntEnvOptions): number;
|
|
20
|
+
export declare function readBooleanEnv(key: string, options?: BooleanEnvOptions): boolean;
|
package/dist/env.js
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
class EnvError extends Error {
|
|
2
|
+
code;
|
|
3
|
+
constructor(code, message) {
|
|
4
|
+
super(message);
|
|
5
|
+
this.name = "EnvError";
|
|
6
|
+
this.code = code;
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
function getEnvValue(key, env) {
|
|
10
|
+
return (env ?? process.env)[key];
|
|
11
|
+
}
|
|
12
|
+
export function readRequiredStringEnv(key, options = {}) {
|
|
13
|
+
const raw = getEnvValue(key, options.env);
|
|
14
|
+
if (raw === undefined || (!options.allowEmpty && raw.trim().length === 0)) {
|
|
15
|
+
throw new EnvError("CLIFN_ENV_MISSING", `${key} is required`);
|
|
16
|
+
}
|
|
17
|
+
return raw;
|
|
18
|
+
}
|
|
19
|
+
export function readStringEnv(key, options = {}) {
|
|
20
|
+
const raw = getEnvValue(key, options.env);
|
|
21
|
+
if (raw === undefined) {
|
|
22
|
+
if (options.defaultValue !== undefined) {
|
|
23
|
+
return options.defaultValue;
|
|
24
|
+
}
|
|
25
|
+
throw new EnvError("CLIFN_ENV_MISSING", `${key} is required`);
|
|
26
|
+
}
|
|
27
|
+
if (!options.allowEmpty && raw.trim().length === 0) {
|
|
28
|
+
throw new EnvError("CLIFN_ENV_INVALID", `${key} must not be empty`);
|
|
29
|
+
}
|
|
30
|
+
return raw;
|
|
31
|
+
}
|
|
32
|
+
export function readIntEnv(key, options = {}) {
|
|
33
|
+
const raw = getEnvValue(key, options.env);
|
|
34
|
+
if (raw === undefined || raw === "") {
|
|
35
|
+
if (options.defaultValue !== undefined) {
|
|
36
|
+
return validateInt(key, options.defaultValue, options);
|
|
37
|
+
}
|
|
38
|
+
throw new EnvError("CLIFN_ENV_MISSING", `${key} is required`);
|
|
39
|
+
}
|
|
40
|
+
const parsed = Number.parseInt(raw, 10);
|
|
41
|
+
if (!Number.isInteger(parsed) || String(parsed) !== raw.trim()) {
|
|
42
|
+
throw new EnvError("CLIFN_ENV_INVALID", `${key} must be an integer`);
|
|
43
|
+
}
|
|
44
|
+
return validateInt(key, parsed, options);
|
|
45
|
+
}
|
|
46
|
+
export function readBooleanEnv(key, options = {}) {
|
|
47
|
+
const raw = getEnvValue(key, options.env);
|
|
48
|
+
if (raw === undefined || raw === "") {
|
|
49
|
+
if (options.defaultValue !== undefined) {
|
|
50
|
+
return options.defaultValue;
|
|
51
|
+
}
|
|
52
|
+
throw new EnvError("CLIFN_ENV_MISSING", `${key} is required`);
|
|
53
|
+
}
|
|
54
|
+
const normalized = raw.trim().toLowerCase();
|
|
55
|
+
if (["1", "true", "yes", "on"].includes(normalized)) {
|
|
56
|
+
return true;
|
|
57
|
+
}
|
|
58
|
+
if (["0", "false", "no", "off"].includes(normalized)) {
|
|
59
|
+
return false;
|
|
60
|
+
}
|
|
61
|
+
throw new EnvError("CLIFN_ENV_INVALID", `${key} must be a boolean-like value`);
|
|
62
|
+
}
|
|
63
|
+
function validateInt(key, value, options) {
|
|
64
|
+
if (!Number.isInteger(value)) {
|
|
65
|
+
throw new EnvError("CLIFN_ENV_INVALID", `${key} must be an integer`);
|
|
66
|
+
}
|
|
67
|
+
if (options.min !== undefined && value < options.min) {
|
|
68
|
+
throw new EnvError("CLIFN_ENV_OUT_OF_RANGE", `${key} must be >= ${options.min}`);
|
|
69
|
+
}
|
|
70
|
+
if (options.max !== undefined && value > options.max) {
|
|
71
|
+
throw new EnvError("CLIFN_ENV_OUT_OF_RANGE", `${key} must be <= ${options.max}`);
|
|
72
|
+
}
|
|
73
|
+
return value;
|
|
74
|
+
}
|
package/dist/exec.d.ts
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
export type ExecErrorCode = "CLIFN_EXEC_TIMEOUT";
|
|
2
|
+
export interface ExecOptions {
|
|
3
|
+
cwd?: string;
|
|
4
|
+
env?: NodeJS.ProcessEnv;
|
|
5
|
+
stdin?: string;
|
|
6
|
+
timeoutMs?: number;
|
|
7
|
+
streamOutput?: boolean;
|
|
8
|
+
maxBufferBytes?: number;
|
|
9
|
+
stdout?: (chunk: string) => void;
|
|
10
|
+
stderr?: (chunk: string) => void;
|
|
11
|
+
}
|
|
12
|
+
export interface ExecResult {
|
|
13
|
+
command: string;
|
|
14
|
+
exitCode: number | null;
|
|
15
|
+
signal: NodeJS.Signals | null;
|
|
16
|
+
stdout: string;
|
|
17
|
+
stderr: string;
|
|
18
|
+
timedOut: boolean;
|
|
19
|
+
durationMs: number;
|
|
20
|
+
}
|
|
21
|
+
export interface ExecService {
|
|
22
|
+
command(file: string, args?: readonly string[], options?: ExecOptions): Promise<ExecResult>;
|
|
23
|
+
}
|
|
24
|
+
export declare class ExecTimeoutError extends Error {
|
|
25
|
+
readonly code: ExecErrorCode;
|
|
26
|
+
readonly result: ExecResult;
|
|
27
|
+
constructor(message: string, result: ExecResult);
|
|
28
|
+
}
|
|
29
|
+
export declare function createExec(): ExecService;
|