@domesystems/templates 0.3.2 → 0.3.3
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 +3 -3
- package/package.json +1 -1
- package/src/cli.mjs +21 -8
- package/src/registry.mjs +1 -0
- package/src/template-files.mjs +19 -0
package/README.md
CHANGED
|
@@ -8,7 +8,7 @@ npx @domesystems/templates pull dome-systems/ai-agent-for-documentation --regist
|
|
|
8
8
|
npx @domesystems/templates up --dir ai-agent-for-documentation
|
|
9
9
|
```
|
|
10
10
|
|
|
11
|
-
|
|
11
|
+
Remote commands use `https://templates-registry.domesystems.ai` by default. Pass either `--server <Dome application URL>` to discover a different Registry endpoint through an app server, or `--registry <Registry URL>` to connect directly. A successful login makes that endpoint the default for later remote commands; either flag overrides it. Credentials are isolated per selected endpoint.
|
|
12
12
|
|
|
13
13
|
Use direct Registry endpoints when the app-server discovery document is unavailable:
|
|
14
14
|
|
|
@@ -28,11 +28,11 @@ templates login [--server <URL>|--registry <URL>] [--github]
|
|
|
28
28
|
templates logout [--server <URL>|--registry <URL>]
|
|
29
29
|
templates doctor <pull|up|publish> [--dir <directory>] [--server <URL>|--registry <URL>] [--json]
|
|
30
30
|
templates pull <scope/package[@version]> [--dir <directory>] [--server <URL>|--registry <URL>]
|
|
31
|
-
templates up [--dir <directory>] [--port <host-port>] [--skip-provision|--provision] [--no-start] [--detach]
|
|
31
|
+
templates up [--dir <directory>] [--port <host-port>] [--server <URL>|--registry <URL>] [--import-job <job-id>] [--skip-provision|--provision] [--no-start] [--detach]
|
|
32
32
|
templates publish [--dry-run] [--server <URL>|--registry <URL>]
|
|
33
33
|
```
|
|
34
34
|
|
|
35
|
-
`pull` only accepts scoped package names, verifies the Registry SHA-256 before extraction, accepts only the archive's `template/` root, writes `.dome-template-lock.yaml`, and never overwrites a non-empty directory. `up` is local-only and never downloads or updates a release. `publish` reads the scoped name and SemVer version from `template.yaml`, requires a clean Git checkout, and cannot replace a published version.
|
|
35
|
+
`pull` only accepts scoped package names, verifies the Registry SHA-256 before extraction, accepts only the archive's `template/` root, writes `.dome-template-lock.yaml`, and never overwrites a non-empty directory. `up` is local-only and never downloads or updates a release. When it provisions an unconfigured runtime, it reveals the import's one-time agent token, resolves the workspace default Gateway, and writes only `DOME_TOKEN` and `DOME_GATEWAY_URL` to the ignored `.env` before starting Docker. `publish` reads the scoped name and SemVer version from `template.yaml`, requires a clean Git checkout, and cannot replace a published version.
|
|
36
36
|
|
|
37
37
|
Use `--verbose` with a command to include its diagnostic stack trace when it fails. When publishing requires a GitHub connection, the CLI opens the authorization page automatically; complete it, then run `publish` again.
|
|
38
38
|
|
package/package.json
CHANGED
package/src/cli.mjs
CHANGED
|
@@ -1,31 +1,30 @@
|
|
|
1
1
|
import fs from "node:fs";
|
|
2
2
|
import path from "node:path";
|
|
3
3
|
import { spawn, spawnSync } from "node:child_process";
|
|
4
|
-
import readline from "node:readline/promises";
|
|
5
4
|
import { readManifest, readPublishManifest } from "./manifest.mjs";
|
|
6
5
|
import { imageForRuntime, renderCompose } from "./runtime.mjs";
|
|
7
|
-
import { extractRelease, initializeEnvironment } from "./template-files.mjs";
|
|
6
|
+
import { extractRelease, initializeEnvironment, writeRuntimeEnvironment } from "./template-files.mjs";
|
|
8
7
|
import { createArchive, publishSource } from "./publish-files.mjs";
|
|
9
|
-
import {
|
|
8
|
+
import { DEFAULT_REGISTRY, clearSelectedTarget, deleteProfile, directRegistry, discover, login, normalizeServer, readProfile, readSelectedTarget, registryRequest, writeSelectedTarget } from "./registry.mjs";
|
|
10
9
|
|
|
11
10
|
function ansi(code, value, options) { return options.color && process.stdout.isTTY ? `\u001B[${code}m${value}\u001B[0m` : value; }
|
|
12
11
|
function line(kind, value, options) { const styles = { info: ["•", "36"], success: ["✓", "32"], warning: ["!", "33"], error: ["✗", "31"] }; const [mark, code] = styles[kind]; return `${ansi(code, mark, options)} ${value}`; }
|
|
13
|
-
function usage() { return "Usage:\n templates login [--server <Dome application URL>|--registry <Registry URL>] [--github]\n templates logout [--server <Dome application URL>|--registry <Registry URL>]\n templates doctor <pull|up|publish> [package] [--dir <directory>] [--server <URL>|--registry <URL>] [--json]\n templates pull <scope/package[@version]> [--dir <directory>] [--server <URL>|--registry <URL>]\n templates up [--dir <directory>] [--port <host-port>] [--skip-provision|--provision] [--no-start] [--detach] [--verbose]\n templates publish [--dry-run] [--server <Dome application URL>|--registry <Registry URL>]\n"; }
|
|
12
|
+
function usage() { return "Usage:\n templates login [--server <Dome application URL>|--registry <Registry URL>] [--github]\n templates logout [--server <Dome application URL>|--registry <Registry URL>]\n templates doctor <pull|up|publish> [package] [--dir <directory>] [--server <URL>|--registry <URL>] [--json]\n templates pull <scope/package[@version]> [--dir <directory>] [--server <URL>|--registry <URL>]\n templates up [--dir <directory>] [--port <host-port>] [--server <URL>|--registry <URL>] [--import-job <job-id>] [--skip-provision|--provision] [--no-start] [--detach] [--verbose]\n templates publish [--dry-run] [--server <Dome application URL>|--registry <Registry URL>]\n"; }
|
|
14
13
|
function parse(argv) {
|
|
15
14
|
if (!argv.length || argv.includes("--help") || argv.includes("-h")) return { help: true };
|
|
16
15
|
const [command, ...rest] = argv;
|
|
17
|
-
const options = { server: undefined, directory: undefined, port: 3000, dryRun: false, json: false, github: false, provision: true, forceProvision: false, start: true, detach: false, verbose: false, color: !process.env.NO_COLOR };
|
|
16
|
+
const options = { server: undefined, directory: undefined, port: 3000, dryRun: false, json: false, github: false, importJob: undefined, provision: true, forceProvision: false, start: true, detach: false, verbose: false, color: !process.env.NO_COLOR };
|
|
18
17
|
const values = [];
|
|
19
18
|
for (let i = 0; i < rest.length; i += 1) {
|
|
20
19
|
const arg = rest[i];
|
|
21
|
-
if (arg === "--server" || arg === "--registry" || arg === "--dir" || arg === "--port" || arg === "--color") { const value = rest[++i]; if (!value) throw new Error(`${arg} requires a value.`); if (arg === "--server") options.server = normalizeServer(value); else if (arg === "--registry") options.registry = normalizeServer(value); else if (arg === "--dir") options.directory = value; else if (arg === "--color") { if (!["auto", "always", "never"].includes(value)) throw new Error("--color must be auto, always, or never."); options.color = value === "always" || (value === "auto" && !process.env.NO_COLOR); } else { options.port = Number(value); if (!Number.isInteger(options.port) || options.port < 1 || options.port > 65535) throw new Error("--port requires an integer from 1 to 65535."); } }
|
|
20
|
+
if (arg === "--server" || arg === "--registry" || arg === "--dir" || arg === "--port" || arg === "--color" || arg === "--import-job") { const value = rest[++i]; if (!value) throw new Error(`${arg} requires a value.`); if (arg === "--server") options.server = normalizeServer(value); else if (arg === "--registry") options.registry = normalizeServer(value); else if (arg === "--dir") options.directory = value; else if (arg === "--import-job") options.importJob = value; else if (arg === "--color") { if (!["auto", "always", "never"].includes(value)) throw new Error("--color must be auto, always, or never."); options.color = value === "always" || (value === "auto" && !process.env.NO_COLOR); } else { options.port = Number(value); if (!Number.isInteger(options.port) || options.port < 1 || options.port > 65535) throw new Error("--port requires an integer from 1 to 65535."); } }
|
|
22
21
|
else if (arg === "--github") options.github = true; else if (arg === "--dry-run") options.dryRun = true; else if (arg === "--json") options.json = true; else if (arg === "--skip-provision") options.provision = false; else if (arg === "--provision") options.forceProvision = true; else if (arg === "--no-start") options.start = false; else if (arg === "--detach") options.detach = true; else if (arg === "--verbose") options.verbose = true; else if (arg.startsWith("-")) throw new Error(`Unknown option: ${arg}`); else values.push(arg);
|
|
23
22
|
}
|
|
24
23
|
if (options.server && options.registry) throw new Error("Use either --server or --registry, not both.");
|
|
25
24
|
if (!["login", "logout", "doctor", "pull", "up", "publish"].includes(command)) throw new Error(`Unknown command: ${command}`);
|
|
26
25
|
return { command, values, options };
|
|
27
26
|
}
|
|
28
|
-
function remoteTarget(options) { return options.registry ? directRegistry(options.registry) : options.server ?? readSelectedTarget() ??
|
|
27
|
+
function remoteTarget(options) { return options.registry ? directRegistry(options.registry) : options.server ?? readSelectedTarget() ?? directRegistry(DEFAULT_REGISTRY); }
|
|
29
28
|
function remoteLocation(options) { const target = remoteTarget(options); return typeof target === "string" ? `--server ${target}` : `--registry ${target.registry}`; }
|
|
30
29
|
const githubPublishingAccessMessage = "GitHub publishing access is required.";
|
|
31
30
|
function openBrowser(url) {
|
|
@@ -54,8 +53,22 @@ async function pull(name, options, output, fetchImplementation) {
|
|
|
54
53
|
output(line("success", `Pulled ${release.name}@${release.version}`, options)); output(` Saved to ${target}`); if (created) output(line("success", "Created .env from .env.example", options));
|
|
55
54
|
}
|
|
56
55
|
function run(command, args, cwd) { const result = spawnSync(command, args, { cwd, stdio: "inherit" }); if (result.error || result.status !== 0) throw new Error(`${command} ${args.join(" ")} failed.`); }
|
|
56
|
+
function json(text, command) { try { return JSON.parse(text.slice(text.indexOf("{"))); } catch { throw new Error(`${command} did not return valid JSON.`); } }
|
|
57
|
+
function runJSON(command, args, cwd, { reveal = false } = {}) {
|
|
58
|
+
return new Promise((resolve, reject) => {
|
|
59
|
+
const child = spawn(command, args, { cwd, stdio: ["inherit", "pipe", "inherit"] });
|
|
60
|
+
let stdout = "";
|
|
61
|
+
child.stdout.on("data", (chunk) => { stdout += chunk; if (reveal) process.stdout.write(chunk); });
|
|
62
|
+
child.on("error", () => reject(new Error(`${command} ${args.join(" ")} failed.`)));
|
|
63
|
+
child.on("close", (status) => status === 0 ? resolve(json(stdout, command)) : reject(new Error(`${command} ${args.join(" ")} failed.`)));
|
|
64
|
+
});
|
|
65
|
+
}
|
|
57
66
|
function configured(target) { const env = path.join(target, ".env"); return fs.existsSync(env) && ["DOME_TOKEN", "DOME_GATEWAY_URL"].every((key) => new RegExp(`^${key}=(?!\\s*$|undefined|\"\")`, "m").test(fs.readFileSync(env, "utf8"))); }
|
|
58
|
-
|
|
67
|
+
export function importMadeNoChanges(job) { const summary = job?.plan_summary ?? job?.planSummary; if (!summary) return false; return ["create_count", "update_count", "import_count"].every((key) => Number(summary[key] ?? summary[key.replace(/_([a-z])/g, (_, letter) => letter.toUpperCase())] ?? 0) === 0); }
|
|
68
|
+
export function importHasPendingOutputs(job) { return (job?.outputs_state ?? job?.outputsState) === "pending"; }
|
|
69
|
+
export function importCreatedResources(job) { const summary = job?.plan_summary ?? job?.planSummary; return Number(summary?.create_count ?? summary?.createCount ?? 0) > 0; }
|
|
70
|
+
async function configureRuntime(target, jobID, options, output) { const gateways = await runJSON("dome", ["gateways", "list", "--json"], target); const gatewayURL = gateways?.data?.gateways?.find((candidate) => candidate.is_default)?.endpoints?.gateway_url; if (!gatewayURL) throw new Error("The import did not configure a default Gateway for the runtime."); const revealed = await runJSON("dome", ["import", "outputs", jobID, "--json"], target); const tokens = Object.entries(revealed?.data?.outputs ?? {}).filter(([key, value]) => key.endsWith("_token") && typeof value === "string"); if (tokens.length !== 1) throw new Error("The import did not produce exactly one runtime agent token."); writeRuntimeEnvironment(target, { DOME_TOKEN: tokens[0][1], DOME_GATEWAY_URL: gatewayURL }); output(line("success", "Configured the runtime with the imported agent credentials.", options)); }
|
|
71
|
+
async function up(options, output) { const target = path.resolve(process.cwd(), options.directory ?? "."); const manifest = readManifest(target); const needsCredentials = !configured(target); output(line("info", `Checking ${target}`, options)); fs.writeFileSync(path.join(target, ".dome-compose.yaml"), renderCompose(manifest.app.runtime, options.port)); if (options.importJob) { output(line("info", "Saving credentials from the selected Dome import", options)); await configureRuntime(target, options.importJob, options, output); } else if (options.provision && (needsCredentials || options.forceProvision)) { output(line("info", "Setting up Dome resources", options)); const imported = await runJSON("dome", ["import", "dome.tf", "--json"], target); const job = imported?.data?.job; if (!job?.id) throw new Error("Dome import did not return a job ID."); if (importCreatedResources(job) && importHasPendingOutputs(job)) await configureRuntime(target, job.id, options, output); else if (needsCredentials) throw new Error(`Dome matched existing resources and did not create a new runtime token. Recover a pending import with templates up --dir ${options.directory ?? "."} --import-job <job-id>, or rotate the runtime key and set DOME_TOKEN and DOME_GATEWAY_URL in .env.`); else output(line("success", "Dome resources are already up to date; keeping the existing runtime credentials.", options)); } if (options.start) { output(line("info", `Starting app at http://localhost:${options.port}`, options)); run("docker", ["compose", "-f", ".dome-compose.yaml", "up", ...(options.detach ? ["--detach"] : [])], target); } }
|
|
59
72
|
async function publish(options, output, fetchImplementation) { const target = path.resolve(process.cwd(), options.directory ?? "."); const manifest = readPublishManifest(target); const source = publishSource(target); const name = manifest.metadata.name; const [scope, slug] = name.split("/"); const archive = options.dryRun ? null : createArchive(source); output(line("info", `Checking ${name}@${manifest.metadata.version}`, options)); const payload = { name, version: manifest.metadata.version, title: manifest.metadata.title, summary: manifest.metadata.summary ?? "", sha256: archive?.sha256, size_bytes: archive?.buffer.length, source: { repository: manifest.metadata.source.repository, path: manifest.metadata.source.path, commit: source.commit, remote: source.remote, files: source.files } };
|
|
60
73
|
const remote = remoteTarget(options); let registry; let attempt;
|
|
61
74
|
try { ({ target: registry, body: attempt } = await registryRequest(remote, "/v1/publish-attempts", { method: "POST", body: { ...payload, dry_run: options.dryRun }, authenticated: true, fetchImplementation })); } catch (caught) {
|
package/src/registry.mjs
CHANGED
|
@@ -4,6 +4,7 @@ import os from "node:os";
|
|
|
4
4
|
import path from "node:path";
|
|
5
5
|
|
|
6
6
|
export const DEFAULT_SERVER = "https://app.domesystems.ai";
|
|
7
|
+
export const DEFAULT_REGISTRY = "https://templates-registry.domesystems.ai";
|
|
7
8
|
|
|
8
9
|
function profileKey(server) {
|
|
9
10
|
return crypto.createHash("sha256").update(new URL(server).origin).digest("hex").slice(0, 24);
|
package/src/template-files.mjs
CHANGED
|
@@ -44,6 +44,25 @@ export function initializeEnvironment(templateDirectory) {
|
|
|
44
44
|
return false;
|
|
45
45
|
}
|
|
46
46
|
|
|
47
|
+
export function writeRuntimeEnvironment(templateDirectory, values) {
|
|
48
|
+
const environment = path.join(templateDirectory, ".env");
|
|
49
|
+
const required = ["DOME_TOKEN", "DOME_GATEWAY_URL"];
|
|
50
|
+
for (const key of required) {
|
|
51
|
+
if (typeof values[key] !== "string" || !values[key].trim() || /[\r\n]/.test(values[key])) {
|
|
52
|
+
throw new Error(`Cannot configure runtime: ${key} is missing or invalid.`);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
let text = fs.existsSync(environment) ? fs.readFileSync(environment, "utf8") : "";
|
|
57
|
+
for (const key of required) {
|
|
58
|
+
const assignment = `${key}=${values[key]}`;
|
|
59
|
+
const pattern = new RegExp(`^${key}=.*$`, "m");
|
|
60
|
+
text = pattern.test(text) ? text.replace(pattern, assignment) : `${text}${text && !text.endsWith("\n") ? "\n" : ""}${assignment}\n`;
|
|
61
|
+
}
|
|
62
|
+
fs.writeFileSync(environment, text, { mode: 0o600 });
|
|
63
|
+
fs.chmodSync(environment, 0o600);
|
|
64
|
+
}
|
|
65
|
+
|
|
47
66
|
export function writeLock(destination, release) {
|
|
48
67
|
fs.writeFileSync(path.join(destination, ".dome-template-lock.yaml"), stringify({
|
|
49
68
|
apiVersion: "templates.domesystems.ai/v1", package: release.name, version: release.version, sha256: release.sha256,
|