@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
package/dist/scaffold.js
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { mkdir, realpath, stat, writeFile } from "node:fs/promises";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
export class ScaffoldError extends Error {
|
|
4
|
+
code;
|
|
5
|
+
constructor(code, message) {
|
|
6
|
+
super(message);
|
|
7
|
+
this.name = "ScaffoldError";
|
|
8
|
+
this.code = code;
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
function isWithinRoot(root, target) {
|
|
12
|
+
const relative = path.relative(root, target);
|
|
13
|
+
return relative === "" || (!relative.startsWith("..") && !path.isAbsolute(relative));
|
|
14
|
+
}
|
|
15
|
+
async function fileExists(filePath) {
|
|
16
|
+
try {
|
|
17
|
+
await stat(filePath);
|
|
18
|
+
return true;
|
|
19
|
+
}
|
|
20
|
+
catch {
|
|
21
|
+
return false;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
async function findNearestExistingPath(target) {
|
|
25
|
+
let current = target;
|
|
26
|
+
while (true) {
|
|
27
|
+
try {
|
|
28
|
+
await stat(current);
|
|
29
|
+
return current;
|
|
30
|
+
}
|
|
31
|
+
catch {
|
|
32
|
+
const parent = path.dirname(current);
|
|
33
|
+
if (parent === current) {
|
|
34
|
+
return current;
|
|
35
|
+
}
|
|
36
|
+
current = parent;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
async function assertWithinRoot(root, target) {
|
|
41
|
+
const realRoot = await realpath(root);
|
|
42
|
+
const nearestExisting = await findNearestExistingPath(target);
|
|
43
|
+
const realNearest = await realpath(nearestExisting);
|
|
44
|
+
if (!isWithinRoot(realRoot, realNearest)) {
|
|
45
|
+
throw new ScaffoldError("CLIFN_SCAFFOLD_INVALID_PATH", "Scaffold path escapes the configured working directory.");
|
|
46
|
+
}
|
|
47
|
+
const canonicalTarget = path.resolve(realNearest, path.relative(nearestExisting, target));
|
|
48
|
+
if (!isWithinRoot(realRoot, canonicalTarget)) {
|
|
49
|
+
throw new ScaffoldError("CLIFN_SCAFFOLD_INVALID_PATH", "Scaffold path escapes the configured working directory.");
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
async function resolveOperationPath(root, inputPath) {
|
|
53
|
+
const resolved = path.resolve(root, inputPath);
|
|
54
|
+
await assertWithinRoot(root, resolved);
|
|
55
|
+
return resolved;
|
|
56
|
+
}
|
|
57
|
+
export function createScaffold() {
|
|
58
|
+
return {
|
|
59
|
+
async apply(operations, options = {}) {
|
|
60
|
+
const cwd = path.resolve(options.cwd ?? process.cwd());
|
|
61
|
+
const dryRun = options.dryRun ?? false;
|
|
62
|
+
const written = [];
|
|
63
|
+
const skipped = [];
|
|
64
|
+
for (const operation of operations) {
|
|
65
|
+
const resolvedPath = await resolveOperationPath(cwd, operation.path);
|
|
66
|
+
const relativePath = path.relative(cwd, resolvedPath) || path.basename(resolvedPath);
|
|
67
|
+
if (operation.kind === "mkdir") {
|
|
68
|
+
if (!dryRun) {
|
|
69
|
+
await mkdir(resolvedPath, { recursive: true });
|
|
70
|
+
}
|
|
71
|
+
continue;
|
|
72
|
+
}
|
|
73
|
+
const ifExists = operation.ifExists ?? "error";
|
|
74
|
+
const exists = await fileExists(resolvedPath);
|
|
75
|
+
if (exists && ifExists === "skip") {
|
|
76
|
+
skipped.push(relativePath);
|
|
77
|
+
continue;
|
|
78
|
+
}
|
|
79
|
+
if (exists && ifExists === "error") {
|
|
80
|
+
throw new ScaffoldError("CLIFN_SCAFFOLD_EXISTS", `Scaffold target already exists: ${relativePath}`);
|
|
81
|
+
}
|
|
82
|
+
if (!dryRun) {
|
|
83
|
+
await mkdir(path.dirname(resolvedPath), { recursive: true });
|
|
84
|
+
await writeFile(resolvedPath, operation.content, "utf8");
|
|
85
|
+
}
|
|
86
|
+
written.push(relativePath);
|
|
87
|
+
}
|
|
88
|
+
return {
|
|
89
|
+
written,
|
|
90
|
+
skipped,
|
|
91
|
+
};
|
|
92
|
+
},
|
|
93
|
+
};
|
|
94
|
+
}
|
package/dist/stdio.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { Writable } from "node:stream";
|
|
2
|
+
export declare class InvalidJsonStdinError extends Error {
|
|
3
|
+
readonly code = "CLIFN_INVALID_STDIN_JSON";
|
|
4
|
+
constructor(message: string);
|
|
5
|
+
}
|
|
6
|
+
export declare function readJsonStdin<T>(input?: NodeJS.ReadableStream): Promise<T>;
|
|
7
|
+
export declare function writeJsonStdout<T>(value: T, output?: Writable): void;
|
package/dist/stdio.js
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
export class InvalidJsonStdinError extends Error {
|
|
2
|
+
code = "CLIFN_INVALID_STDIN_JSON";
|
|
3
|
+
constructor(message) {
|
|
4
|
+
super(message);
|
|
5
|
+
this.name = "InvalidJsonStdinError";
|
|
6
|
+
}
|
|
7
|
+
}
|
|
8
|
+
async function readAll(stream) {
|
|
9
|
+
return await new Promise((resolve, reject) => {
|
|
10
|
+
const chunks = [];
|
|
11
|
+
stream.on("data", (chunk) => {
|
|
12
|
+
chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(String(chunk)));
|
|
13
|
+
});
|
|
14
|
+
stream.on("error", reject);
|
|
15
|
+
stream.on("end", () => resolve(Buffer.concat(chunks).toString("utf8")));
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
export async function readJsonStdin(input = process.stdin) {
|
|
19
|
+
const raw = (await readAll(input)).trim();
|
|
20
|
+
if (raw.length === 0) {
|
|
21
|
+
throw new InvalidJsonStdinError("stdin is empty; expected one JSON document");
|
|
22
|
+
}
|
|
23
|
+
try {
|
|
24
|
+
return JSON.parse(raw);
|
|
25
|
+
}
|
|
26
|
+
catch (error) {
|
|
27
|
+
const message = error instanceof Error ? error.message : "unknown parse error";
|
|
28
|
+
throw new InvalidJsonStdinError(`stdin contains invalid JSON: ${message}`);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
export function writeJsonStdout(value, output = process.stdout) {
|
|
32
|
+
output.write(`${JSON.stringify(value)}\n`);
|
|
33
|
+
}
|
package/dist/ui.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { type Spinner } from "./output.js";
|
|
2
|
+
export type { Spinner } from "./output.js";
|
|
3
|
+
export declare const ui: {
|
|
4
|
+
success(message: string): void;
|
|
5
|
+
error(message: string): void;
|
|
6
|
+
warn(message: string): void;
|
|
7
|
+
info(message: string): void;
|
|
8
|
+
spinner(message: string): Spinner;
|
|
9
|
+
table(rows: Array<Record<string, string | number | boolean | null | undefined>>): void;
|
|
10
|
+
};
|
package/dist/ui.js
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { createOutput } from "./output.js";
|
|
2
|
+
function getOutput() {
|
|
3
|
+
return createOutput();
|
|
4
|
+
}
|
|
5
|
+
export const ui = {
|
|
6
|
+
success(message) {
|
|
7
|
+
getOutput().success(message);
|
|
8
|
+
},
|
|
9
|
+
error(message) {
|
|
10
|
+
getOutput().error(message);
|
|
11
|
+
},
|
|
12
|
+
warn(message) {
|
|
13
|
+
getOutput().warn(message);
|
|
14
|
+
},
|
|
15
|
+
info(message) {
|
|
16
|
+
getOutput().info(message);
|
|
17
|
+
},
|
|
18
|
+
spinner(message) {
|
|
19
|
+
return getOutput().spinner(message);
|
|
20
|
+
},
|
|
21
|
+
table(rows) {
|
|
22
|
+
const columns = rows.length === 0 ? [] : Object.keys(rows[0]);
|
|
23
|
+
getOutput().table({ columns, rows });
|
|
24
|
+
},
|
|
25
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@clifn/core",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Reusable CLI primitives runtime for the Superfunctions ecosystem",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"import": "./dist/index.js"
|
|
12
|
+
},
|
|
13
|
+
"./output": {
|
|
14
|
+
"types": "./dist/output.d.ts",
|
|
15
|
+
"import": "./dist/output.js"
|
|
16
|
+
},
|
|
17
|
+
"./runner": {
|
|
18
|
+
"types": "./dist/runner.d.ts",
|
|
19
|
+
"import": "./dist/runner.js"
|
|
20
|
+
},
|
|
21
|
+
"./config-loader": {
|
|
22
|
+
"types": "./dist/config-loader.d.ts",
|
|
23
|
+
"import": "./dist/config-loader.js"
|
|
24
|
+
},
|
|
25
|
+
"./env": {
|
|
26
|
+
"types": "./dist/env.d.ts",
|
|
27
|
+
"import": "./dist/env.js"
|
|
28
|
+
},
|
|
29
|
+
"./diagnostics": {
|
|
30
|
+
"types": "./dist/diagnostics.d.ts",
|
|
31
|
+
"import": "./dist/diagnostics.js"
|
|
32
|
+
},
|
|
33
|
+
"./exec": {
|
|
34
|
+
"types": "./dist/exec.d.ts",
|
|
35
|
+
"import": "./dist/exec.js"
|
|
36
|
+
},
|
|
37
|
+
"./scaffold": {
|
|
38
|
+
"types": "./dist/scaffold.d.ts",
|
|
39
|
+
"import": "./dist/scaffold.js"
|
|
40
|
+
},
|
|
41
|
+
"./credentials": {
|
|
42
|
+
"types": "./dist/credentials.d.ts",
|
|
43
|
+
"import": "./dist/credentials.js"
|
|
44
|
+
},
|
|
45
|
+
"./config": {
|
|
46
|
+
"types": "./dist/config.d.ts",
|
|
47
|
+
"import": "./dist/config.js"
|
|
48
|
+
},
|
|
49
|
+
"./client": {
|
|
50
|
+
"types": "./dist/client.d.ts",
|
|
51
|
+
"import": "./dist/client.js"
|
|
52
|
+
},
|
|
53
|
+
"./ui": {
|
|
54
|
+
"types": "./dist/ui.d.ts",
|
|
55
|
+
"import": "./dist/ui.js"
|
|
56
|
+
},
|
|
57
|
+
"./stdio": {
|
|
58
|
+
"types": "./dist/stdio.d.ts",
|
|
59
|
+
"import": "./dist/stdio.js"
|
|
60
|
+
},
|
|
61
|
+
"./prompt": {
|
|
62
|
+
"types": "./dist/prompt.d.ts",
|
|
63
|
+
"import": "./dist/prompt.js"
|
|
64
|
+
}
|
|
65
|
+
},
|
|
66
|
+
"files": [
|
|
67
|
+
"dist",
|
|
68
|
+
"README.md"
|
|
69
|
+
],
|
|
70
|
+
"scripts": {
|
|
71
|
+
"build": "tsc",
|
|
72
|
+
"dev": "tsc --watch",
|
|
73
|
+
"test": "vitest",
|
|
74
|
+
"test:watch": "vitest --watch",
|
|
75
|
+
"lint": "echo 'lint not configured'",
|
|
76
|
+
"clean": "rm -rf dist"
|
|
77
|
+
},
|
|
78
|
+
"keywords": [
|
|
79
|
+
"cli",
|
|
80
|
+
"superfunctions",
|
|
81
|
+
"runner",
|
|
82
|
+
"output",
|
|
83
|
+
"diagnostics",
|
|
84
|
+
"scaffold",
|
|
85
|
+
"credentials",
|
|
86
|
+
"config",
|
|
87
|
+
"stdio"
|
|
88
|
+
],
|
|
89
|
+
"author": "21n",
|
|
90
|
+
"license": "MIT",
|
|
91
|
+
"dependencies": {
|
|
92
|
+
"ini": "^5.0.0",
|
|
93
|
+
"jiti": "^2.4.2"
|
|
94
|
+
},
|
|
95
|
+
"devDependencies": {
|
|
96
|
+
"@types/ini": "^4.1.1",
|
|
97
|
+
"@types/node": "^22.0.0",
|
|
98
|
+
"cac": "^6.7.14",
|
|
99
|
+
"commander": "^12.1.0",
|
|
100
|
+
"typescript": "^5.6.0",
|
|
101
|
+
"vitest": "^3.2.4"
|
|
102
|
+
},
|
|
103
|
+
"repository": {
|
|
104
|
+
"type": "git",
|
|
105
|
+
"url": "git+https://github.com/21nCo/super-functions.git",
|
|
106
|
+
"directory": "clifn/core"
|
|
107
|
+
},
|
|
108
|
+
"superfunctionsPhasePlan": {
|
|
109
|
+
"clifnPhase00": {
|
|
110
|
+
"addedRuntimeDependencies": [
|
|
111
|
+
"ini",
|
|
112
|
+
"jiti"
|
|
113
|
+
],
|
|
114
|
+
"dependencyPolicy": "Prefer repository-shared mechanisms such as the existing packages/cli loader pattern before adding new dependencies in later phases."
|
|
115
|
+
}
|
|
116
|
+
},
|
|
117
|
+
"publishConfig": {
|
|
118
|
+
"access": "public"
|
|
119
|
+
}
|
|
120
|
+
}
|