@darkrymit/crunes-cli 0.8.5 → 0.10.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.
Files changed (95) hide show
  1. package/dist/chunks/{add-CBHWFGCA.js → add-IT3IHBPO.js} +2 -2
  2. package/dist/chunks/add-MSX6XSU3.js +68 -0
  3. package/dist/chunks/{apply-J4YICV6H.js → apply-LOIJIPLR.js} +21 -12
  4. package/dist/chunks/{benchmark-AKKV7G32.js → benchmark-3IHFP3V2.js} +27 -18
  5. package/dist/chunks/{browse-JZFU52ET.js → browse-PHMBDREQ.js} +2 -2
  6. package/dist/chunks/chunk-2DNYVKBF.js +109 -0
  7. package/dist/chunks/chunk-2FI6KN3C.js +283 -0
  8. package/dist/chunks/chunk-2HM5Q52B.js +131 -0
  9. package/dist/chunks/{chunk-6MRZXASS.js → chunk-2Q3KLBE6.js} +16 -0
  10. package/dist/chunks/{chunk-ZJDKCC2Y.js → chunk-4GJMCFR6.js} +6 -3
  11. package/dist/chunks/chunk-55GTJG5I.js +1154 -0
  12. package/dist/chunks/chunk-6IC6ZJ74.js +9525 -0
  13. package/dist/chunks/chunk-7KYCYDAS.js +325 -0
  14. package/dist/chunks/chunk-DTOYFY3Z.js +578 -0
  15. package/dist/chunks/chunk-EO3CSXWK.js +70 -0
  16. package/dist/chunks/chunk-FIAFVIAN.js +79 -0
  17. package/dist/chunks/{chunk-J3GZ3DXD.js → chunk-GVMT55GH.js} +3 -2
  18. package/dist/chunks/chunk-H2OT7D6N.js +303 -0
  19. package/dist/chunks/{chunk-JOAL437H.js → chunk-I6MHDUCW.js} +16 -1
  20. package/dist/chunks/chunk-KSQDFE5N.js +85 -0
  21. package/dist/chunks/{chunk-BTIPUR5S.js → chunk-KUEMDPNG.js} +108 -91
  22. package/dist/chunks/{chunk-6XLT25GS.js → chunk-MIKWIFYR.js} +47 -0
  23. package/dist/chunks/{chunk-GTCPZ22N.js → chunk-MTJ2KW3V.js} +3 -1
  24. package/dist/chunks/chunk-MVCBPI4J.js +2354 -0
  25. package/dist/chunks/{chunk-T53L5CJW.js → chunk-PBGUKXT6.js} +2887 -1874
  26. package/dist/chunks/{chunk-KII6KW5Z.js → chunk-RVBAAXRY.js} +64 -44
  27. package/dist/chunks/chunk-SUCYDYMU.js +44 -0
  28. package/dist/chunks/chunk-UBCAZGLE.js +15 -0
  29. package/dist/chunks/{chunk-63H5VXZY.js → chunk-UEUUOAJY.js} +4 -1
  30. package/dist/chunks/chunk-VP7YLCQP.js +244 -0
  31. package/dist/chunks/{chunk-NDEYEYSU.js → chunk-WJII3LZK.js} +4 -3
  32. package/dist/chunks/chunk-XDMMSZIL.js +71 -0
  33. package/dist/chunks/chunk-YKIKJHYO.js +94 -0
  34. package/dist/chunks/{chunk-NYLRTAAA.js → chunk-YVN3VDRE.js} +1 -1
  35. package/dist/chunks/{clear-YBDIPEEH.js → clear-AT7HNUV4.js} +2 -2
  36. package/dist/chunks/{create-L6DJN4ME.js → create-DXW4YYLJ.js} +8 -8
  37. package/dist/chunks/create-LPLNCOW4.js +268 -0
  38. package/dist/chunks/{create-GPDE2YKL.js → create-Y4LHLWGL.js} +26 -18
  39. package/dist/chunks/{delete-LNOYM5KG.js → delete-2EXPFBFC.js} +2 -2
  40. package/dist/chunks/{delete-E4TEBRPX.js → delete-GKYA2W5Z.js} +4 -2
  41. package/dist/chunks/{delete-C4UJ7X5K.js → delete-MJAECXXV.js} +2 -2
  42. package/dist/chunks/disable-TITOSMXB.js +43 -0
  43. package/dist/chunks/doctor-R2Y6UAY4.js +208 -0
  44. package/dist/chunks/enable-LGTPDWPV.js +35 -0
  45. package/dist/chunks/{globals-H2WF344Y.js → globals-SNQFCHMP.js} +1 -1
  46. package/dist/chunks/{init-HU5OMHEH.js → init-KO2HLED5.js} +18 -8
  47. package/dist/chunks/install-7IRUAGSF.js +64 -0
  48. package/dist/chunks/{intro-36OLGNYN.js → intro-JNJ6A75J.js} +89 -21
  49. package/dist/chunks/{kill-2UG7PHUV.js → kill-TX3PC73P.js} +2 -1
  50. package/dist/chunks/{list-2JYMBMZC.js → list-BCCM3TLP.js} +12 -7
  51. package/dist/chunks/{list-4X3JFQGS.js → list-EHREZR6M.js} +2 -1
  52. package/dist/chunks/{list-U7PAYNAH.js → list-LC6HB3H3.js} +2 -2
  53. package/dist/chunks/list-MLKZE36K.js +92 -0
  54. package/dist/chunks/list-QVZKR3IE.js +62 -0
  55. package/dist/chunks/{list-7VHJIUDX.js → list-U4EMHQZT.js} +2 -2
  56. package/dist/chunks/{list-PTAWQYWG.js → list-V7VVD5AG.js} +2 -1
  57. package/dist/chunks/{list-A5WVDBWF.js → list-VA73WPK6.js} +2 -2
  58. package/dist/chunks/{manifest-7RVNKBOH.js → manifest-76MIEL2F.js} +3 -1
  59. package/dist/chunks/migrate-Y3EKMVP5.js +87 -0
  60. package/dist/chunks/{output-RJ6JSNY7.js → output-QN5CVJ62.js} +4 -1
  61. package/dist/chunks/{query-AW7WYILM.js → query-2US3CPYT.js} +2 -2
  62. package/dist/chunks/{registry-RG34DPD7.js → registry-IWP7JMNI.js} +2 -2
  63. package/dist/chunks/{remove-QUH2D3S5.js → remove-BCFTF23J.js} +2 -2
  64. package/dist/chunks/{repl-STBZC4P2.js → repl-2YWTHUWT.js} +4 -0
  65. package/dist/chunks/{repl-BPEWARN7.js → repl-5GDN3RAO.js} +99 -31
  66. package/dist/chunks/resolver-CA2A32AC.js +36 -0
  67. package/dist/chunks/run-WL3IZBXF.js +35 -0
  68. package/dist/chunks/rune-YA4TFHTB.js +283 -0
  69. package/dist/chunks/runner-QNZMBHQY.js +34 -0
  70. package/dist/chunks/{search-MSSDGJTF.js → search-B7IM7JRJ.js} +2 -2
  71. package/dist/chunks/shell-explain-LKBBP2BG.js +77 -0
  72. package/dist/chunks/trust-BFIDANXD.js +109 -0
  73. package/dist/chunks/uninstall-3MWZIZVA.js +58 -0
  74. package/dist/chunks/{unset-YASSMSE5.js → unset-P6IUF7SS.js} +2 -2
  75. package/dist/chunks/{update-KSC6VRMU.js → update-W4MJUTTW.js} +25 -19
  76. package/dist/chunks/{update-EWWDROKW.js → update-WXT7CHYA.js} +2 -2
  77. package/dist/chunks/{utils-3SJFSUSV.js → utils-B2QULX5J.js} +8 -4
  78. package/dist/chunks/{version-YG2M7HER.js → version-FLHI5KFS.js} +3 -3
  79. package/dist/cli.js +115 -70
  80. package/package.json +2 -2
  81. package/dist/chunks/chunk-ASZKAC24.js +0 -103
  82. package/dist/chunks/chunk-G3U4V32O.js +0 -46
  83. package/dist/chunks/chunk-GSA432UC.js +0 -90
  84. package/dist/chunks/chunk-WJRZN7BR.js +0 -77492
  85. package/dist/chunks/create-VYZRWRGQ.js +0 -170
  86. package/dist/chunks/disable-FGJ7VMPO.js +0 -47
  87. package/dist/chunks/doctor-7NWBJAH7.js +0 -53
  88. package/dist/chunks/enable-ZL6QOC2I.js +0 -47
  89. package/dist/chunks/install-FIYRMVLR.js +0 -57
  90. package/dist/chunks/list-AIYNOPNZ.js +0 -28
  91. package/dist/chunks/list-NZ5DHXJF.js +0 -111
  92. package/dist/chunks/resolver-36V72VKR.js +0 -28
  93. package/dist/chunks/run-IVL23SMW.js +0 -26
  94. package/dist/chunks/rune-XRREUL56.js +0 -186
  95. package/dist/chunks/uninstall-G56DZRFG.js +0 -49
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  addMarketplace
3
- } from "./chunk-NYLRTAAA.js";
4
- import "./chunk-6MRZXASS.js";
3
+ } from "./chunk-YVN3VDRE.js";
4
+ import "./chunk-2Q3KLBE6.js";
5
5
  import {
6
6
  init_require_shim
7
7
  } from "./chunk-TPK5AGRA.js";
@@ -0,0 +1,68 @@
1
+ import {
2
+ getConfigPath,
3
+ readRawConfig,
4
+ writeRawConfig
5
+ } from "./chunk-SUCYDYMU.js";
6
+ import {
7
+ output
8
+ } from "./chunk-MIKWIFYR.js";
9
+ import "./chunk-CGZL5SSH.js";
10
+ import "./chunk-WCODTMDN.js";
11
+ import {
12
+ runtimeOf
13
+ } from "./chunk-2DNYVKBF.js";
14
+ import "./chunk-2FI6KN3C.js";
15
+ import {
16
+ isPathCommand
17
+ } from "./chunk-UBCAZGLE.js";
18
+ import "./chunk-VP7YLCQP.js";
19
+ import "./chunk-YKIKJHYO.js";
20
+ import "./chunk-2HM5Q52B.js";
21
+ import {
22
+ getStorePath
23
+ } from "./chunk-2Q3KLBE6.js";
24
+ import "./chunk-55GTJG5I.js";
25
+ import {
26
+ init_require_shim
27
+ } from "./chunk-TPK5AGRA.js";
28
+
29
+ // src/rune/commands/add.js
30
+ init_require_shim();
31
+ import { existsSync } from "node:fs";
32
+ import { join, isAbsolute } from "node:path";
33
+ async function handler({ key, command, argv = [], name, description, configRoot = process.cwd(), global = false }) {
34
+ const configPath = getConfigPath({ configRoot, global });
35
+ const config = readRawConfig(configPath);
36
+ if (!config) {
37
+ output.error(`No config at ${configPath}. Run \`crunes init${global ? " -g" : ""}\` first.`);
38
+ process.exit(1);
39
+ }
40
+ if (config.runes?.[key] != null) {
41
+ output.error(`Rune "${key}" already exists in ${configPath}.`);
42
+ process.exit(1);
43
+ }
44
+ if (isPathCommand(command)) {
45
+ const base = global ? getStorePath() : configRoot;
46
+ const file = isAbsolute(command) ? command : join(base, command);
47
+ if (!existsSync(file)) {
48
+ output.error(`${command} does not exist (looked for ${file}).`);
49
+ process.exit(1);
50
+ }
51
+ }
52
+ const run = argv.length > 0 ? { command, argv } : command;
53
+ config.runes = {
54
+ ...config.runes ?? {},
55
+ [key]: {
56
+ ...name && { name },
57
+ ...description && { description },
58
+ run
59
+ }
60
+ };
61
+ await writeRawConfig(configPath, config);
62
+ const runtime = runtimeOf(command);
63
+ output.success(`Added "${key}" \u2192 ${[command, ...argv].join(" ")} (${runtime === "isolate" ? "isolate" : `${runtime}, native`})`);
64
+ output.info(`Run: crunes run ${key}`);
65
+ }
66
+ export {
67
+ handler
68
+ };
@@ -1,24 +1,30 @@
1
+ import {
2
+ output
3
+ } from "./chunk-MIKWIFYR.js";
1
4
  import {
2
5
  fe,
3
6
  ge,
4
7
  le,
5
8
  ye
6
9
  } from "./chunk-CGZL5SSH.js";
10
+ import "./chunk-WCODTMDN.js";
7
11
  import {
8
12
  loadConfig
9
- } from "./chunk-GSA432UC.js";
10
- import {
11
- output
12
- } from "./chunk-6XLT25GS.js";
13
- import "./chunk-WCODTMDN.js";
13
+ } from "./chunk-2FI6KN3C.js";
14
+ import "./chunk-UBCAZGLE.js";
14
15
  import {
15
16
  loadRegistry,
16
17
  resolvePluginKey
17
- } from "./chunk-GTCPZ22N.js";
18
- import "./chunk-6MRZXASS.js";
18
+ } from "./chunk-MTJ2KW3V.js";
19
19
  import {
20
20
  loadPluginJson
21
- } from "./chunk-G3U4V32O.js";
21
+ } from "./chunk-FIAFVIAN.js";
22
+ import "./chunk-VP7YLCQP.js";
23
+ import {
24
+ migrateEntry
25
+ } from "./chunk-YKIKJHYO.js";
26
+ import "./chunk-2Q3KLBE6.js";
27
+ import "./chunk-55GTJG5I.js";
22
28
  import {
23
29
  init_require_shim
24
30
  } from "./chunk-TPK5AGRA.js";
@@ -99,7 +105,11 @@ async function handler({
99
105
  process.exit(1);
100
106
  }
101
107
  const outputKey = key ?? templateName;
102
- runeRelPath = runeRelPath ?? `.crunes/runes/${outputKey}.js`;
108
+ runeRelPath = runeRelPath ?? `.crunes/runes/${outputKey}.rune.js`;
109
+ if (!/\.rune\.m?js$/i.test(runeRelPath)) {
110
+ output.error(`${runeRelPath}: a sandboxed rune's file must be named *.rune.js \u2014 the name is what runs it in the isolate.`);
111
+ process.exit(1);
112
+ }
103
113
  const runeAbsPath = path.join(projectRoot, runeRelPath);
104
114
  if (!isNonInteractive) ye("crunes template apply");
105
115
  try {
@@ -117,7 +127,7 @@ async function handler({
117
127
  let templateMeta = {};
118
128
  if (resolved.type === "local") {
119
129
  const localPath = typeof resolved.entry === "string" ? resolved.entry : resolved.entry.path;
120
- await fs.copyFile(path.join(projectRoot, localPath), runeAbsPath);
130
+ await fs.copyFile(path.resolve(projectRoot, localPath), runeAbsPath);
121
131
  templateMeta = typeof resolved.entry === "string" ? {} : resolved.entry;
122
132
  } else if (resolved.type === "shortcut") {
123
133
  const pluginRef = resolved.entry.plugin;
@@ -162,10 +172,9 @@ async function handler({
162
172
  const resolvedName = name ?? templateMeta.name;
163
173
  const resolvedDesc = description ?? templateMeta.description;
164
174
  const configEntry = {
165
- path: runeRelPath,
166
175
  ...resolvedName && { name: resolvedName },
167
176
  ...resolvedDesc && { description: resolvedDesc },
168
- ...templateMeta.permissions && { permissions: templateMeta.permissions }
177
+ ...migrateEntry({ path: runeRelPath, permissions: templateMeta.permissions }, { base: projectRoot }).entry
169
178
  };
170
179
  config.runes = { ...config.runes ?? {}, [outputKey]: configEntry };
171
180
  const tmp = configPath + ".tmp";
@@ -1,27 +1,35 @@
1
1
  import {
2
+ joinBracketTokens,
2
3
  parseBracketKey
3
- } from "./chunk-BTIPUR5S.js";
4
- import {
5
- loadConfig
6
- } from "./chunk-GSA432UC.js";
7
- import {
8
- runRune
9
- } from "./chunk-WJRZN7BR.js";
10
- import "./chunk-J3GZ3DXD.js";
11
- import "./chunk-ZJDKCC2Y.js";
12
- import "./chunk-NDEYEYSU.js";
13
- import "./chunk-G4ZAUHHA.js";
14
- import "./chunk-63H5VXZY.js";
15
- import "./chunk-ASZKAC24.js";
4
+ } from "./chunk-KUEMDPNG.js";
16
5
  import {
17
6
  output
18
- } from "./chunk-6XLT25GS.js";
7
+ } from "./chunk-MIKWIFYR.js";
8
+ import "./chunk-CGZL5SSH.js";
19
9
  import {
20
10
  source_default
21
11
  } from "./chunk-WCODTMDN.js";
22
- import "./chunk-GTCPZ22N.js";
23
- import "./chunk-6MRZXASS.js";
24
- import "./chunk-G3U4V32O.js";
12
+ import {
13
+ runRune
14
+ } from "./chunk-H2OT7D6N.js";
15
+ import "./chunk-DTOYFY3Z.js";
16
+ import "./chunk-2DNYVKBF.js";
17
+ import "./chunk-XDMMSZIL.js";
18
+ import {
19
+ loadConfig
20
+ } from "./chunk-2FI6KN3C.js";
21
+ import "./chunk-UBCAZGLE.js";
22
+ import "./chunk-MTJ2KW3V.js";
23
+ import "./chunk-FIAFVIAN.js";
24
+ import "./chunk-VP7YLCQP.js";
25
+ import "./chunk-YKIKJHYO.js";
26
+ import "./chunk-MVCBPI4J.js";
27
+ import "./chunk-GVMT55GH.js";
28
+ import "./chunk-2HM5Q52B.js";
29
+ import "./chunk-WJII3LZK.js";
30
+ import "./chunk-G4ZAUHHA.js";
31
+ import "./chunk-2Q3KLBE6.js";
32
+ import "./chunk-55GTJG5I.js";
25
33
  import {
26
34
  init_require_shim
27
35
  } from "./chunk-TPK5AGRA.js";
@@ -98,7 +106,8 @@ function parseBenchArgs(argv) {
98
106
  }
99
107
  }
100
108
  rawSegments.push(current);
101
- const segments = rawSegments.map((seg) => {
109
+ const segments = rawSegments.map((rawSeg) => {
110
+ const seg = joinBracketTokens(rawSeg);
102
111
  const { key, bracketArgs } = parseBracketKey(seg[0] ?? "");
103
112
  let runs = globalRuns;
104
113
  let warmup = globalWarmup;
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  searchMarketplaces
3
- } from "./chunk-NYLRTAAA.js";
4
- import "./chunk-6MRZXASS.js";
3
+ } from "./chunk-YVN3VDRE.js";
4
+ import "./chunk-2Q3KLBE6.js";
5
5
  import {
6
6
  init_require_shim
7
7
  } from "./chunk-TPK5AGRA.js";
@@ -0,0 +1,109 @@
1
+ import {
2
+ isPathCommand
3
+ } from "./chunk-UBCAZGLE.js";
4
+ import {
5
+ defaultDeps,
6
+ resolveShell
7
+ } from "./chunk-2HM5Q52B.js";
8
+ import {
9
+ init_require_shim
10
+ } from "./chunk-TPK5AGRA.js";
11
+
12
+ // src/rune/native/runtime.js
13
+ init_require_shim();
14
+ import { readFileSync } from "node:fs";
15
+ import path from "node:path";
16
+ var ISOLATE_FILE = /\.rune\.m?js$/i;
17
+ var RUNTIME_BY_EXTENSION = {
18
+ ".js": "node",
19
+ ".mjs": "node",
20
+ ".cjs": "node",
21
+ ".py": "python",
22
+ ".sh": "bash",
23
+ ".cmd": "cmd",
24
+ ".bat": "cmd"
25
+ };
26
+ function runtimeOf(command) {
27
+ if (!isPathCommand(command)) return "program";
28
+ if (ISOLATE_FILE.test(command)) return "isolate";
29
+ return RUNTIME_BY_EXTENSION[path.extname(command).toLowerCase()] ?? "file";
30
+ }
31
+ var InterpreterNotFoundError = class extends Error {
32
+ constructor(runtime, tried, hint) {
33
+ super(`No ${runtime} interpreter found. Tried:
34
+ ${tried.join("\n ")}
35
+ ${hint}`);
36
+ this.name = "InterpreterNotFoundError";
37
+ this.tried = tried;
38
+ }
39
+ };
40
+ var pathFor = (deps) => deps.platform === "win32" ? path.win32 : path.posix;
41
+ function onPath(name, deps = defaultDeps()) {
42
+ const names = deps.platform === "win32" ? [`${name}.exe`, name] : [name];
43
+ for (const dir of deps.pathDirs) {
44
+ for (const n of names) {
45
+ const candidate = pathFor(deps).join(dir, n);
46
+ if (deps.exists(candidate)) return candidate;
47
+ }
48
+ }
49
+ return null;
50
+ }
51
+ function findPython(projectDir, deps = defaultDeps()) {
52
+ const win = deps.platform === "win32";
53
+ const p = pathFor(deps);
54
+ const venvPython = (dir) => win ? p.join(dir, "Scripts", "python.exe") : p.join(dir, "bin", "python");
55
+ const tried = [];
56
+ const attempt = (label, candidate, args = [], missing = "(unset)") => {
57
+ tried.push(candidate ? `${label}: ${candidate}` : `${label}: ${missing}`);
58
+ return candidate && deps.exists(candidate) ? { command: candidate, args } : null;
59
+ };
60
+ const found = attempt("CRUNES_PYTHON", deps.env.CRUNES_PYTHON) ?? attempt("VIRTUAL_ENV", deps.env.VIRTUAL_ENV && venvPython(deps.env.VIRTUAL_ENV)) ?? attempt(".venv", venvPython(p.join(projectDir, ".venv"))) ?? attempt("venv", venvPython(p.join(projectDir, "venv"))) ?? attempt("python3", onPath("python3", deps), [], "(not on PATH)") ?? attempt("python", onPath("python", deps), [], "(not on PATH)") ?? (win ? attempt("py -3", onPath("py", deps), ["-3"], "(not on PATH)") : null);
61
+ if (found) return found;
62
+ throw new InterpreterNotFoundError("Python", tried, "Install Python, or set CRUNES_PYTHON to the interpreter to use.");
63
+ }
64
+ var pythonCache = /* @__PURE__ */ new Map();
65
+ function resolveInterpreter(runtime, projectDir) {
66
+ switch (runtime) {
67
+ case "node":
68
+ return { command: process.execPath, args: [] };
69
+ case "python": {
70
+ if (!pythonCache.has(projectDir)) pythonCache.set(projectDir, findPython(projectDir));
71
+ return pythonCache.get(projectDir);
72
+ }
73
+ case "bash":
74
+ return { command: resolveShell("bash").path, args: [] };
75
+ case "cmd":
76
+ if (process.platform !== "win32") {
77
+ throw new Error("Windows batch scripts (.cmd/.bat) can only run on Windows.");
78
+ }
79
+ return { command: process.env.COMSPEC || "cmd.exe", args: ["/d", "/s", "/c"] };
80
+ default:
81
+ return null;
82
+ }
83
+ }
84
+ var UTILS_IMPORT = /(?:\bfrom\s*|\bimport\s*\(?\s*)['"]@utils['"]/;
85
+ function assertNotMissedRename(file, runtime, projectDir = null) {
86
+ if (runtime !== "node") return;
87
+ let source;
88
+ try {
89
+ source = readFileSync(file, "utf8");
90
+ } catch {
91
+ return;
92
+ }
93
+ if (!UTILS_IMPORT.test(source)) return;
94
+ const shown = projectDir ? path.relative(projectDir, file).replace(/\\/g, "/") : file;
95
+ const renamed = shown.replace(/\.(m?js|cjs)$/i, ".rune.$1");
96
+ throw new Error(
97
+ `${shown} imports @utils but is named as a native Node script.
98
+ A run block runs plain .js files with Node, outside the sandbox. To keep it in the isolate, rename it:
99
+ mv ${shown} ${renamed}
100
+ and point run.command at the new name.`
101
+ );
102
+ }
103
+
104
+ export {
105
+ runtimeOf,
106
+ onPath,
107
+ resolveInterpreter,
108
+ assertNotMissedRename
109
+ };
@@ -0,0 +1,283 @@
1
+ import {
2
+ isPathCommand
3
+ } from "./chunk-UBCAZGLE.js";
4
+ import {
5
+ isInlineSchema,
6
+ normaliseJsonSchema
7
+ } from "./chunk-VP7YLCQP.js";
8
+ import {
9
+ migrateEntry,
10
+ v1EntryError
11
+ } from "./chunk-YKIKJHYO.js";
12
+ import {
13
+ getStorePath
14
+ } from "./chunk-2Q3KLBE6.js";
15
+ import {
16
+ init_require_shim
17
+ } from "./chunk-TPK5AGRA.js";
18
+
19
+ // src/core/config.js
20
+ init_require_shim();
21
+ import { readFileSync, existsSync } from "node:fs";
22
+ import { join, resolve } from "node:path";
23
+ function isObject(item) {
24
+ return item && typeof item === "object" && !Array.isArray(item);
25
+ }
26
+ var LAYER = /* @__PURE__ */ Symbol.for("crunes.layer");
27
+ var BLOCKS = ["run", "args", "repl"];
28
+ function absolutizeSection(section, kind, baseDir, layer) {
29
+ const out = {};
30
+ for (const [key, entry] of Object.entries(section)) {
31
+ if (!isObject(entry)) {
32
+ out[key] = entry;
33
+ continue;
34
+ }
35
+ const copy = { ...entry, [LAYER]: layer };
36
+ if (kind === "templates") {
37
+ const isPluginRef = Boolean(entry.plugin) || key.includes(":");
38
+ if (entry.path) copy.path = resolve(baseDir, entry.path);
39
+ else if (layer === "global" && !isPluginRef) copy.path = resolve(baseDir, `templates/${key}.js`);
40
+ out[key] = copy;
41
+ continue;
42
+ }
43
+ for (const block of BLOCKS) {
44
+ if (entry[block] == null || block === "args" && isInlineSchema(entry[block])) continue;
45
+ const b = typeof entry[block] === "string" ? { command: entry[block] } : { ...entry[block] };
46
+ if (typeof b.command === "string" && (block !== "run" || isPathCommand(b.command))) b.command = resolve(baseDir, b.command);
47
+ copy[block] = b;
48
+ }
49
+ out[key] = copy;
50
+ }
51
+ return out;
52
+ }
53
+ function absolutizeEntryPaths(config, baseDir, layer) {
54
+ const out = { ...config };
55
+ if (isObject(config.runes)) out.runes = absolutizeSection(config.runes, "runes", baseDir, layer);
56
+ if (isObject(config.templates)) out.templates = absolutizeSection(config.templates, "templates", baseDir, layer);
57
+ return out;
58
+ }
59
+ function coercePlugins(plugins) {
60
+ if (Array.isArray(plugins)) return Object.fromEntries(plugins.map((k) => [k, true]));
61
+ return isObject(plugins) ? { ...plugins } : {};
62
+ }
63
+ function enabledPluginKeys(config) {
64
+ return Object.entries(coercePlugins(config?.plugins)).filter(([, enabled]) => enabled === true).map(([key]) => key);
65
+ }
66
+ function mergeConfigs(shared, local) {
67
+ const merged = { ...shared };
68
+ for (const [key, value] of Object.entries(local)) {
69
+ if (key !== "runes" && key !== "templates" && key !== "plugins") {
70
+ merged[key] = value;
71
+ }
72
+ }
73
+ if (local.runes) {
74
+ merged.runes = { ...shared.runes };
75
+ for (const [key, localEntry] of Object.entries(local.runes)) {
76
+ const sharedEntry = shared.runes?.[key];
77
+ if (sharedEntry && isObject(sharedEntry) && isObject(localEntry)) {
78
+ merged.runes[key] = {
79
+ ...sharedEntry,
80
+ ...localEntry,
81
+ vars: { ...sharedEntry.vars, ...localEntry.vars }
82
+ };
83
+ for (const block of BLOCKS) {
84
+ if (isObject(sharedEntry[block]) && isObject(localEntry[block]) && !isInlineSchema(sharedEntry[block]) && !isInlineSchema(localEntry[block])) {
85
+ merged.runes[key][block] = { ...sharedEntry[block], ...localEntry[block] };
86
+ }
87
+ }
88
+ } else {
89
+ merged.runes[key] = localEntry;
90
+ }
91
+ }
92
+ }
93
+ if (local.templates) {
94
+ merged.templates = { ...shared.templates };
95
+ for (const [key, localEntry] of Object.entries(local.templates)) {
96
+ const sharedEntry = shared.templates?.[key];
97
+ merged.templates[key] = isObject(sharedEntry) && isObject(localEntry) ? { ...sharedEntry, ...localEntry } : localEntry;
98
+ }
99
+ }
100
+ if (shared.plugins || local.plugins) {
101
+ merged.plugins = { ...coercePlugins(shared.plugins), ...coercePlugins(local.plugins) };
102
+ }
103
+ return merged;
104
+ }
105
+ var ISOLATE_MODULE = /\.rune\.m?js$/i;
106
+ var RUN_BLOCK_KEYS = ["command", "argv", "permissions", "output", "pass", "timeout"];
107
+ var ARGS_FILE = /\.(rune\.m?js|json)$/i;
108
+ var ARGS_BLOCK_KEYS = ["command", "permissions"];
109
+ var REPL_BLOCK_KEYS = ["command", "permissions"];
110
+ function validateBlockShape(where, block, allowedKeys, fileName) {
111
+ if (typeof block === "string") {
112
+ if (block === "") throw new Error(`${fileName}: ${where} must not be empty`);
113
+ return { command: block };
114
+ }
115
+ if (!isObject(block)) throw new Error(`${fileName}: ${where} must be a command string or an object with "command"`);
116
+ const unknown = Object.keys(block).filter((k) => !allowedKeys.includes(k));
117
+ if (unknown.length) {
118
+ throw new Error(`${fileName}: ${where} has unknown field${unknown.length > 1 ? "s" : ""} ${unknown.map((k) => `"${k}"`).join(", ")} \u2014 allowed: ${allowedKeys.join(", ")}`);
119
+ }
120
+ if (block.command !== void 0 && (typeof block.command !== "string" || block.command === "")) {
121
+ throw new Error(`${fileName}: ${where}.command must be a non-empty string`);
122
+ }
123
+ if (block.permissions != null) {
124
+ const p = block.permissions;
125
+ if (!isObject(p) || Object.keys(p).some((k) => k !== "allow" && k !== "deny")) {
126
+ throw new Error(`${fileName}: ${where}.permissions must be { "allow": [...], "deny": [...] } \u2014 grants in a block are not scoped by lifecycle`);
127
+ }
128
+ }
129
+ return block;
130
+ }
131
+ function isPluginReference(runeKey, entry) {
132
+ const runCommand = typeof entry.run === "string" ? entry.run : entry.run?.command;
133
+ return entry.plugin != null || runeKey.includes(":") && runCommand === void 0;
134
+ }
135
+ function validateEntry(runeKey, entry, fileName, partial) {
136
+ const at = `runes["${runeKey}"]`;
137
+ if (isPluginReference(runeKey, entry)) {
138
+ if (entry.plugin == null) {
139
+ const pluginPart = runeKey.slice(0, runeKey.indexOf(":"));
140
+ if (!pluginPart.includes("@")) {
141
+ throw new Error(
142
+ `${fileName}: ${at} is a plugin-rune override \u2014 but "${pluginPart}" is missing the marketplace prefix. Use the full "marketplace@plugin:${runeKey.slice(runeKey.indexOf(":") + 1)}" form.`
143
+ );
144
+ }
145
+ }
146
+ for (const name of ["run", "repl"]) {
147
+ if (entry[name] == null) continue;
148
+ const b = validateBlockShape(`${at}.${name}`, entry[name], ["command", "permissions"], fileName);
149
+ if (b.command !== void 0) throw new Error(`${fileName}: ${at}.${name} \u2014 a plugin rune's handler comes from the plugin; this entry may only grant permissions`);
150
+ }
151
+ if (entry.args != null) throw new Error(`${fileName}: ${at}.args \u2014 a plugin rune's schema comes from the plugin`);
152
+ return;
153
+ }
154
+ const run = entry.run == null ? null : validateBlockShape(`${at}.run`, entry.run, RUN_BLOCK_KEYS, fileName);
155
+ if (!partial && (run == null || run.command === void 0)) {
156
+ throw new Error(
157
+ `${fileName}: ${at} has no run block naming its command in any config layer \u2014 a rune runs only what its entry declares. An entry written for the old .crunes/runes/<key>.js convention is rewritten by \`crunes migrate\`.`
158
+ );
159
+ }
160
+ if (run?.argv != null && (!Array.isArray(run.argv) || run.argv.some((a) => typeof a !== "string"))) {
161
+ throw new Error(`${fileName}: ${at}.run.argv must be an array of strings`);
162
+ }
163
+ const runIsIsolate = run?.command !== void 0 && ISOLATE_MODULE.test(run.command);
164
+ if (run?.permissions != null && run.command !== void 0 && !runIsIsolate) {
165
+ throw new Error(`${fileName}: ${at}.run.permissions \u2014 "${run.command}" runs natively with full access, so grants would not apply to it. Remove the block, or use a *.rune.js module to run in the sandbox`);
166
+ }
167
+ if (run?.output !== void 0) {
168
+ if (run.output !== "text" && run.output !== "sections") throw new Error(`${fileName}: ${at}.run.output must be "text" or "sections"`);
169
+ if (runIsIsolate) throw new Error(`${fileName}: ${at}.run.output \u2014 an isolate module returns sections itself; output applies to native handlers`);
170
+ }
171
+ if (run?.timeout !== void 0 && !(typeof run.timeout === "number" && Number.isFinite(run.timeout) && run.timeout > 0)) {
172
+ throw new Error(`${fileName}: ${at}.run.timeout must be a number of seconds greater than 0`);
173
+ }
174
+ if (run?.pass !== void 0) {
175
+ if (run.pass !== "raw" && run.pass !== "mapped") throw new Error(`${fileName}: ${at}.run.pass must be "raw" or "mapped"`);
176
+ if (runIsIsolate) throw new Error(`${fileName}: ${at}.run.pass \u2014 an isolate module receives parsed arguments itself; pass applies to native handlers`);
177
+ if (run.pass === "mapped" && entry.args == null && !partial) {
178
+ throw new Error(`${fileName}: ${at}.run.pass is "mapped" but the rune has no args block \u2014 mapped arguments are built from the schema`);
179
+ }
180
+ }
181
+ if (entry.args != null && isInlineSchema(entry.args)) {
182
+ try {
183
+ normaliseJsonSchema(entry.args, `${at}.args`);
184
+ } catch (err) {
185
+ throw new Error(`${fileName}: ${err.message}`);
186
+ }
187
+ if (runIsIsolate) {
188
+ throw new Error(`${fileName}: ${at}.args \u2014 an isolate run module declares its schema with its own args export; an args block is for native handlers`);
189
+ }
190
+ } else if (entry.args != null) {
191
+ const args = validateBlockShape(`${at}.args`, entry.args, ARGS_BLOCK_KEYS, fileName);
192
+ if (args.command === void 0 && !partial) throw new Error(`${fileName}: ${at}.args has no command in any config layer`);
193
+ if (args.command !== void 0 && !ARGS_FILE.test(args.command)) {
194
+ throw new Error(`${fileName}: ${at}.args.command must be a *.rune.js module exporting args(builder), or a .json schema`);
195
+ }
196
+ if (runIsIsolate) {
197
+ throw new Error(`${fileName}: ${at}.args \u2014 an isolate run module declares its schema with its own args export; an args block is for native handlers`);
198
+ }
199
+ }
200
+ if (entry.repl != null) {
201
+ const repl = validateBlockShape(`${at}.repl`, entry.repl, REPL_BLOCK_KEYS, fileName);
202
+ if (repl.command === void 0 && !partial) throw new Error(`${fileName}: ${at}.repl has no command in any config layer`);
203
+ if (repl.command !== void 0 && !ISOLATE_MODULE.test(repl.command)) {
204
+ throw new Error(`${fileName}: ${at}.repl.command must be a *.rune.js module \u2014 a REPL runs in the isolate`);
205
+ }
206
+ }
207
+ }
208
+ function validateMergedRunBlocks(config, label = "config") {
209
+ for (const [runeKey, entry] of Object.entries(config.runes ?? {})) {
210
+ if (isObject(entry)) validateEntry(runeKey, entry, label, false);
211
+ }
212
+ }
213
+ function validateConfig(config, fileName = "config.json", { base = null, layer = "project" } = {}) {
214
+ if (config.runes && typeof config.runes === "object") {
215
+ for (const [runeKey, entry] of Object.entries(config.runes)) {
216
+ if (!isObject(entry)) continue;
217
+ const rewritten = entry.path !== void 0 || entry.permissions !== void 0 ? migrateEntry(entry, { base: base ?? ".", defaultFile: base ? defaultRuneFile(runeKey, layer) : null }) : null;
218
+ if (rewritten) throw v1EntryError(fileName, `runes["${runeKey}"]`, rewritten);
219
+ validateEntry(runeKey, entry, fileName, true);
220
+ }
221
+ }
222
+ if (config.plugins && !Array.isArray(config.plugins) && typeof config.plugins === "object") {
223
+ for (const [key, value] of Object.entries(config.plugins)) {
224
+ if (typeof value !== "boolean") {
225
+ throw new Error(`${fileName}: plugins["${key}"] must be true or false, got ${JSON.stringify(value)}`);
226
+ }
227
+ }
228
+ }
229
+ }
230
+ function defaultRuneFile(runeKey, layer) {
231
+ return runeKey.includes(":") ? null : layer === "global" ? `runes/${runeKey}.js` : `.crunes/runes/${runeKey}.js`;
232
+ }
233
+ function normaliseRune(entry) {
234
+ return entry;
235
+ }
236
+ function getRune(config, key) {
237
+ const raw = (config.runes ?? {})[key];
238
+ if (raw == null) return null;
239
+ return normaliseRune(raw);
240
+ }
241
+ var ROOTLESS = /* @__PURE__ */ Symbol.for("crunes.rootless");
242
+ function readLayer(filePath, label, base, layer) {
243
+ if (!existsSync(filePath)) return null;
244
+ let parsed;
245
+ try {
246
+ parsed = JSON.parse(readFileSync(filePath, "utf8"));
247
+ } catch (err) {
248
+ throw new Error(`${label} is invalid JSON: ${err.message}`);
249
+ }
250
+ validateConfig(parsed, label, { base, layer });
251
+ return parsed;
252
+ }
253
+ function loadConfig(dir) {
254
+ const layers = [];
255
+ if (process.env.CRUNES_NO_GLOBAL !== "1") {
256
+ const storeRoot = getStorePath();
257
+ const globalPath = join(storeRoot, "config.json");
258
+ const globalRaw = readLayer(globalPath, globalPath, storeRoot, "global");
259
+ if (globalRaw) layers.push(absolutizeEntryPaths(globalRaw, storeRoot, "global"));
260
+ }
261
+ const sharedRaw = readLayer(join(dir, ".crunes", "config.json"), "config.json", dir, "project");
262
+ const rootless = sharedRaw === null;
263
+ if (sharedRaw) {
264
+ layers.push(absolutizeEntryPaths(sharedRaw, dir, "project"));
265
+ const localRaw = readLayer(join(dir, ".crunes", "config.local.json"), "config.local.json", dir, "local");
266
+ if (localRaw) layers.push(absolutizeEntryPaths(localRaw, dir, "local"));
267
+ }
268
+ const merged = layers.reduce((acc, layer) => mergeConfigs(acc, layer), {});
269
+ validateMergedRunBlocks(merged, "config (merged layers)");
270
+ merged[ROOTLESS] = rootless;
271
+ return merged;
272
+ }
273
+
274
+ export {
275
+ LAYER,
276
+ coercePlugins,
277
+ enabledPluginKeys,
278
+ defaultRuneFile,
279
+ normaliseRune,
280
+ getRune,
281
+ ROOTLESS,
282
+ loadConfig
283
+ };