@artis-lab/cli 0.10.0 → 0.12.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.
@@ -0,0 +1,117 @@
1
+ import {
2
+ usageError
3
+ } from "./chunk-H32QAUSA.js";
4
+
5
+ // packages/cli/src/lib/files.ts
6
+ import { createHash } from "node:crypto";
7
+ import { mkdir, readdir, readFile, writeFile } from "node:fs/promises";
8
+ import path from "node:path";
9
+
10
+ // packages/sdk/src/lib/artifact-types.ts
11
+ var artifactTypeRegistry = {
12
+ slides: {}
13
+ };
14
+ var HTML_TYPE = "html";
15
+ var typePackageNames = Object.keys(artifactTypeRegistry);
16
+ var artifactTypeNames = [
17
+ HTML_TYPE,
18
+ ...typePackageNames
19
+ ];
20
+ var PACKAGE_PATH_PATTERN = /^(?!.*(?:^|\/)\.{1,2}(?:\/|$))[A-Za-z0-9._-]+(?:\/[A-Za-z0-9._-]+)*$/;
21
+ function isPackagePath(value) {
22
+ return typeof value === "string" && value.length <= 256 && PACKAGE_PATH_PATTERN.test(value);
23
+ }
24
+ var artifactFileLimits = {
25
+ /** Files in one revision. */
26
+ files: 1e3,
27
+ /** UTF-8 bytes in one file. */
28
+ fileBytes: 2 * 1024 * 1024,
29
+ /** UTF-8 bytes in all the files one request sends. */
30
+ requestBytes: 16 * 1024 * 1024,
31
+ /** Characters in one path. */
32
+ pathLength: 256
33
+ };
34
+ function isArtifactFilePath(value) {
35
+ return typeof value === "string" && value.length <= artifactFileLimits.pathLength && isPackagePath(value) && !value.split("/").some((segment) => segment.startsWith("."));
36
+ }
37
+ var utf8 = new TextEncoder();
38
+
39
+ // packages/cli/src/lib/files.ts
40
+ function isSkipped(name) {
41
+ return name.startsWith(".") || name === "node_modules";
42
+ }
43
+ async function readFilesDir(root) {
44
+ const files = {};
45
+ async function walk(relative) {
46
+ const entries = await readdir(path.join(root, relative), {
47
+ withFileTypes: true
48
+ }).catch((error) => {
49
+ if (error.code === "ENOENT" || error.code === "ENOTDIR") {
50
+ throw usageError(`--root ${root} is not a directory.`);
51
+ }
52
+ throw error;
53
+ });
54
+ for (const entry of entries) {
55
+ if (isSkipped(entry.name)) continue;
56
+ const filePath = relative ? `${relative}/${entry.name}` : entry.name;
57
+ if (entry.isDirectory()) {
58
+ await walk(filePath);
59
+ } else if (entry.isFile()) {
60
+ if (!isArtifactFilePath(filePath)) {
61
+ throw usageError(
62
+ `${filePath}: not a valid artifact file path (letters, digits, . _ and -).`
63
+ );
64
+ }
65
+ files[filePath] = await readFile(path.join(root, filePath), "utf8");
66
+ }
67
+ }
68
+ }
69
+ await walk("");
70
+ if (Object.keys(files).length === 0) {
71
+ throw usageError(`--root ${root} has no files.`);
72
+ }
73
+ return files;
74
+ }
75
+ function sha256Hex(text) {
76
+ return createHash("sha256").update(text, "utf8").digest("hex");
77
+ }
78
+ function changedFiles(local, remote) {
79
+ const remoteHashes = new Map(
80
+ remote.map((file) => [file.path, file.contentHash])
81
+ );
82
+ const changes = {};
83
+ for (const [filePath, content] of Object.entries(local)) {
84
+ if (remoteHashes.get(filePath) !== sha256Hex(content)) {
85
+ changes[filePath] = content;
86
+ }
87
+ }
88
+ for (const filePath of remoteHashes.keys()) {
89
+ if (!Object.prototype.hasOwnProperty.call(local, filePath)) {
90
+ changes[filePath] = null;
91
+ }
92
+ }
93
+ return changes;
94
+ }
95
+ async function writeFilesDir(root, files) {
96
+ const resolvedRoot = path.resolve(root);
97
+ const targets = files.map((file) => {
98
+ const target = path.resolve(resolvedRoot, file.path);
99
+ if (!isArtifactFilePath(file.path) || !target.startsWith(`${resolvedRoot}${path.sep}`)) {
100
+ throw new Error(
101
+ `Refusing to write ${file.path}: not a valid artifact file path under ${root}.`
102
+ );
103
+ }
104
+ return { target, content: file.content };
105
+ });
106
+ for (const { target, content } of targets) {
107
+ await mkdir(path.dirname(target), { recursive: true });
108
+ await writeFile(target, content);
109
+ }
110
+ }
111
+
112
+ export {
113
+ readFilesDir,
114
+ sha256Hex,
115
+ changedFiles,
116
+ writeFilesDir
117
+ };
@@ -0,0 +1,63 @@
1
+ // packages/cli/src/lib/config.ts
2
+ import { mkdir, readFile, writeFile } from "node:fs/promises";
3
+ import { homedir } from "node:os";
4
+ import { dirname, join } from "node:path";
5
+ var ARTIS_DEFAULT_HOST = "https://artis.xiispace.eu.org";
6
+ function normalizeHost(value) {
7
+ return value.replace(/\/+$/, "");
8
+ }
9
+ function getConfigPath() {
10
+ return join(
11
+ process.env["ARTIS_CONFIG_DIR"] ?? homedir(),
12
+ ".artis",
13
+ "config.json"
14
+ );
15
+ }
16
+ async function readCliConfig() {
17
+ try {
18
+ return JSON.parse(
19
+ await readFile(getConfigPath(), "utf8")
20
+ );
21
+ } catch {
22
+ return void 0;
23
+ }
24
+ }
25
+ async function writeCliConfig(config) {
26
+ const configPath = getConfigPath();
27
+ await mkdir(dirname(configPath), { mode: 448, recursive: true });
28
+ await writeFile(configPath, `${JSON.stringify(config, null, 2)}
29
+ `, {
30
+ mode: 384
31
+ });
32
+ }
33
+ function findProfileForHost(config, host) {
34
+ if (!config) {
35
+ return void 0;
36
+ }
37
+ return Object.values(config.profiles).find(
38
+ (profile) => normalizeHost(profile.host) === normalizeHost(host)
39
+ );
40
+ }
41
+ async function saveDefaultProfile(input) {
42
+ const existing = await readCliConfig();
43
+ const host = normalizeHost(input.host);
44
+ await writeCliConfig({
45
+ defaultHost: host,
46
+ profiles: {
47
+ ...existing?.profiles ?? {},
48
+ default: {
49
+ apiKey: input.apiKey,
50
+ host
51
+ }
52
+ }
53
+ });
54
+ }
55
+
56
+ export {
57
+ ARTIS_DEFAULT_HOST,
58
+ normalizeHost,
59
+ getConfigPath,
60
+ readCliConfig,
61
+ findProfileForHost,
62
+ saveDefaultProfile
63
+ };
@@ -0,0 +1,25 @@
1
+ // packages/cli/src/lib/args.ts
2
+ function parseFlagValue(args, name) {
3
+ const index = args.indexOf(name);
4
+ if (index === -1) {
5
+ return void 0;
6
+ }
7
+ return args[index + 1];
8
+ }
9
+ function hasFlag(args, name) {
10
+ return args.includes(name);
11
+ }
12
+ function collectFlagValues(args, name) {
13
+ return args.reduce((values, arg, index) => {
14
+ if (arg === name && args[index + 1]) {
15
+ values.push(args[index + 1]);
16
+ }
17
+ return values;
18
+ }, []);
19
+ }
20
+
21
+ export {
22
+ parseFlagValue,
23
+ hasFlag,
24
+ collectFlagValues
25
+ };