@theholocron/holocron-plugin-bruno 5.0.0-alpha.481 → 5.0.0-alpha.483

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/dist/index.d.mts CHANGED
@@ -5,7 +5,7 @@
5
5
  * collections as Bruno JSON files, which Bruno imports. Bruno has no HTTP API and no account to sync
6
6
  * to, so the plugin has no token and makes no network call, and so no `auth.ts`.
7
7
  */
8
- import { Tooling, ToolingDoctorReport, ToolingExportInput, ToolingExportResult, ToolingWorkspaceInput, ToolingWorkspaceResult } from "@theholocron/cli";
8
+ import { Tooling, ToolingDoctorReport, ToolingExportInput, ToolingExportResult, ToolingOpenInput, ToolingOpenPlan, ToolingWorkspaceInput, ToolingWorkspaceResult } from "@theholocron/cli";
9
9
  /** Options for the Bruno `tooling` provider: the same two file lists the other tooling providers take. */
10
10
  interface BrunoToolingOptions {
11
11
  /** Collection JSON files (v2.1), relative to `repoRoot`. */
@@ -16,6 +16,15 @@ interface BrunoToolingOptions {
16
16
  workspaceName?: string;
17
17
  /** The `info.version` of each collection `workspace()` writes. Defaults to the version of the root `package.json`. */
18
18
  version?: string;
19
+ /**
20
+ * Secret variable name to the name of the entry that holds its value, e.g. `{ webhookSecret: "SENTINEL_WEBHOOK_SECRET" }`.
21
+ * `workspace()` writes each such secret into the `.bruno/` environments as a plain variable that refers to the entry
22
+ * (`{{process.env.SENTINEL_WEBHOOK_SECRET}}`), which `holocron open requests` fills from the layer's vault
23
+ * Environment. The request files keep the secret blank.
24
+ */
25
+ secretRefs?: Record<string, string>;
26
+ /** The program that starts Bruno for `open()`. Defaults to the macOS app's; set it on any other platform. */
27
+ app?: string;
19
28
  /** Working repo root. Defaults to `process.cwd()`. */
20
29
  repoRoot?: string;
21
30
  }
@@ -32,6 +41,7 @@ export declare class BrunoTooling implements Tooling {
32
41
  sync(): Promise<void>;
33
42
  doctor(): Promise<ToolingDoctorReport>;
34
43
  export(input: ToolingExportInput): Promise<ToolingExportResult>;
44
+ open(input: ToolingOpenInput): Promise<ToolingOpenPlan>;
35
45
  workspace(input: ToolingWorkspaceInput): Promise<ToolingWorkspaceResult>;
36
46
  }
37
47
  /** What {@link convertCollection} produced. */
package/dist/index.mjs CHANGED
@@ -96,24 +96,6 @@ async function convertCollection(collection, environments) {
96
96
  issues: (read.issues ?? []).map(describeIssue)
97
97
  };
98
98
  }
99
- // src/output-names.ts
100
- /** A file's path with its extension and a trailing `.collection` taken off (`api/webhook.collection.json` is `api/webhook`). */
101
- const withoutSuffix = (file) => file.slice(0, file.length - extname(file).length).replace(/\.collection$/, "");
102
- /**
103
- * The output file name for each collection file, in order: its own name with `extension` in place of `.json`, and
104
- * without a `.collection` part (`api/webhook.collection.json` is `webhook.bruno.json`; `collection.json` is
105
- * `collection.bruno.json`). Two files with the same name would overwrite each other, so those use the whole path instead, its
106
- * folders joined with `-` (`apps-a-collection.bruno.json`).
107
- */
108
- function outputNames(files, extension = ".bruno.json") {
109
- const stem = (file) => basename(withoutSuffix(file));
110
- const counts = /* @__PURE__ */ new Map();
111
- for (const file of files) counts.set(stem(file), (counts.get(stem(file)) ?? 0) + 1);
112
- return files.map((file) => {
113
- if (counts.get(stem(file)) === 1) return `${stem(file)}${extension}`;
114
- return `${withoutSuffix(file).split("/").filter(Boolean).join("-")}${extension}`;
115
- });
116
- }
117
99
  // src/format-json.ts
118
100
  /** Prettier's default for a JSON file under this repo's config (`printWidth: 120`, 2-space indent). */
119
101
  const PRINT_WIDTH = 120;
@@ -196,6 +178,125 @@ async function readBaseline(repoRoot) {
196
178
  throw new Error(`${BASELINE_FILE}: ${err.message}; delete it to start again`, { cause: err });
197
179
  }
198
180
  }
181
+ // src/secret-references.ts
182
+ /**
183
+ * The reference a Bruno environment uses to read a value from the collection's `.env` file: `{{process.env.NAME}}`.
184
+ *
185
+ * Bruno's file format keeps no value for a secret variable (its own store holds that), so a reference cannot sit on one.
186
+ * The workspace therefore writes a configured secret as a plain variable whose value is the reference, and reads such a
187
+ * variable back as the secret it came from, so the request files (shared with other tools) keep a blank secret.
188
+ */
189
+ const REFERENCE = /^\{\{\s*process\.env\.([A-Za-z_][A-Za-z0-9_]*)\s*\}\}$/;
190
+ /** The reference to the entry `name`. */
191
+ const referenceTo = (name) => `{{process.env.${name}}}`;
192
+ /** The entry name a value refers to, or `undefined` when the value is not exactly one reference. */
193
+ function referencedName(value) {
194
+ return value === void 0 ? void 0 : REFERENCE.exec(value.trim())?.[1];
195
+ }
196
+ /**
197
+ * The environment with each secret variable named in `refs` (variable name to entry name) made a plain variable whose
198
+ * value is the reference to its entry. Everything else is left as it was.
199
+ */
200
+ function withSecretReferences(environment, refs) {
201
+ if (!refs || !environment.variables) return environment;
202
+ return {
203
+ ...environment,
204
+ variables: environment.variables.map((variable) => {
205
+ const entry = variable.secret ? refs[variable.name] : void 0;
206
+ return entry === void 0 ? variable : {
207
+ ...variable,
208
+ secret: false,
209
+ value: referenceTo(entry)
210
+ };
211
+ })
212
+ };
213
+ }
214
+ /**
215
+ * The text of a workspace environment file with each secret variable named in `refs` made a reference, as
216
+ * {@link withSecretReferences} does for a generated one: for a file that is already written, whose secrets
217
+ * `sync requests` would otherwise find in step and leave blank. A file with nothing to change comes back as it is,
218
+ * byte for byte, so Bruno's own layout is never rewritten for nothing.
219
+ */
220
+ function healReferences(text, refs) {
221
+ if (!refs) return text;
222
+ const environment = parseEnvironment(text, { format: "yml" });
223
+ if (!environment.variables.some((variable) => variable.secret && refs[variable.name] !== void 0)) return text;
224
+ return stringifyEnvironment(withSecretReferences(environment, refs), { format: "yml" });
225
+ }
226
+ // src/open-plan.ts
227
+ /** The folder at the repo root that holds the workspace Bruno opens. */
228
+ const WORKSPACE = ".bruno";
229
+ /** The `.yml` files of a folder, sorted; none when the folder is not there. */
230
+ async function environmentFiles(dir) {
231
+ try {
232
+ return (await readdir(dir)).filter((name) => name.endsWith(".yml")).sort();
233
+ } catch (err) {
234
+ if (err.code === "ENOENT") return [];
235
+ throw err;
236
+ }
237
+ }
238
+ /**
239
+ * What it takes to open the committed `.bruno/` workspace with its secrets filled in: for each collection the baseline
240
+ * lists, the secret variables its environments hold. A variable that is a reference to an entry (`{{process.env.NAME}}`,
241
+ * written by `sync requests` for each name in the `secretRefs` option) is a value to fill; a secret variable with no
242
+ * reference is reported as unfilled. Reads files only and returns names and paths, never a value.
243
+ *
244
+ * @throws {Error} When there is no workspace to open, or the platform has no known program for Bruno and `app` is not set.
245
+ */
246
+ async function openPlan(input, options = {}) {
247
+ const { repoRoot, collectionFiles } = input;
248
+ const folders = ((await readBaseline(repoRoot))?.baseline.pairs ?? []).filter((pair) => pair.kind === "collection" && collectionFiles.includes(pair.source)).map((pair) => pair.workspace);
249
+ if (folders.length === 0) throw new Error(`no ${WORKSPACE}/ workspace to open for these collections; run \`holocron sync requests\` first`);
250
+ const command = options.app ?? ((options.platform ?? process.platform) === "darwin" ? "/Applications/Bruno.app/Contents/MacOS/Bruno" : void 0);
251
+ if (command === void 0) throw new Error("this platform has no known program for Bruno; set the `app` option to the path of its executable");
252
+ const wants = [];
253
+ const unfilled = /* @__PURE__ */ new Set();
254
+ for (const folder of folders) {
255
+ const dir = join(repoRoot, WORKSPACE, folder, "environments");
256
+ for (const file of await environmentFiles(dir)) {
257
+ const environment = parseEnvironment(await readFile(join(dir, file), "utf8"), { format: "yml" });
258
+ for (const variable of environment.variables) {
259
+ const entry = referencedName(variable.value);
260
+ if (entry !== void 0) {
261
+ if (!wants.some((w) => w.collection === folder && w.variable === variable.name)) wants.push({
262
+ collection: folder,
263
+ variable: variable.name,
264
+ entry
265
+ });
266
+ } else if (variable.secret) unfilled.add(`${folder}: ${variable.name}`);
267
+ }
268
+ }
269
+ }
270
+ return {
271
+ app: "Bruno",
272
+ wants,
273
+ unfilled: [...unfilled].filter((label) => !wants.some((w) => `${w.collection}: ${w.variable}` === label)),
274
+ targets: folders.map((folder) => join(repoRoot, WORKSPACE, folder, ".env")),
275
+ launch: {
276
+ command,
277
+ args: [join(repoRoot, WORKSPACE)]
278
+ },
279
+ running: command
280
+ };
281
+ }
282
+ // src/output-names.ts
283
+ /** A file's path with its extension and a trailing `.collection` taken off (`api/webhook.collection.json` is `api/webhook`). */
284
+ const withoutSuffix = (file) => file.slice(0, file.length - extname(file).length).replace(/\.collection$/, "");
285
+ /**
286
+ * The output file name for each collection file, in order: its own name with `extension` in place of `.json`, and
287
+ * without a `.collection` part (`api/webhook.collection.json` is `webhook.bruno.json`; `collection.json` is
288
+ * `collection.bruno.json`). Two files with the same name would overwrite each other, so those use the whole path instead, its
289
+ * folders joined with `-` (`apps-a-collection.bruno.json`).
290
+ */
291
+ function outputNames(files, extension = ".bruno.json") {
292
+ const stem = (file) => basename(withoutSuffix(file));
293
+ const counts = /* @__PURE__ */ new Map();
294
+ for (const file of files) counts.set(stem(file), (counts.get(stem(file)) ?? 0) + 1);
295
+ return files.map((file) => {
296
+ if (counts.get(stem(file)) === 1) return `${stem(file)}${extension}`;
297
+ return `${withoutSuffix(file).split("/").filter(Boolean).join("-")}${extension}`;
298
+ });
299
+ }
199
300
  // src/collection-files.ts
200
301
  /** A name safe to use as a file or folder name on any platform, as Bruno itself makes one from a request's name. */
201
302
  function safeName(name) {
@@ -233,9 +334,10 @@ function writeItems(items, dir, files) {
233
334
  * collection folder: `opencollection.yml`, a `folder.yml` for each folder, one YAML file for each request and one
234
335
  * for each environment under `environments/`. Bruno's reader turns the collection into Bruno's own model and
235
336
  * rewrites its `pm.*` scripts to `bru.*` / `req.*`; Bruno's file writer then lays that model out, so the files are
236
- * what the app would have saved. A secret-type variable is written without its value.
337
+ * what the app would have saved. A secret-type variable is written without its value, unless `secretRefs` names it:
338
+ * it is then a plain variable that refers to its entry in the collection's `.env` (see `secret-references.ts`).
237
339
  */
238
- async function collectionFiles(collection, environments) {
340
+ async function collectionFiles(collection, environments, secretRefs) {
239
341
  const read = await postmanToBruno(collection, { preserveScripts: false });
240
342
  const bruno = read.collection;
241
343
  repairSigningBody(bruno);
@@ -250,7 +352,7 @@ async function collectionFiles(collection, environments) {
250
352
  const taken = /* @__PURE__ */ new Set();
251
353
  const environmentFiles = [];
252
354
  for (const source of environments) {
253
- const environment = await postmanToBrunoEnvironment(source);
355
+ const environment = withSecretReferences(await postmanToBrunoEnvironment(source), secretRefs);
254
356
  const file = `environments/${uniqueName(safeName(environment.name), taken)}.yml`;
255
357
  environmentFiles.push(file);
256
358
  files.set(file, stringifyEnvironment(environment, { format: "yml" }));
@@ -602,12 +704,15 @@ function environmentToSource(text, path) {
602
704
  const environment = reading(path, () => parseEnvironment(text, { format: "yml" }));
603
705
  return {
604
706
  name: environment.name,
605
- values: (environment.variables ?? []).map((variable) => ({
606
- key: variable.name,
607
- value: variable.secret ? "" : variable.value ?? "",
608
- type: variable.secret ? "secret" : "default",
609
- enabled: variable.enabled ?? true
610
- }))
707
+ values: (environment.variables ?? []).map((variable) => {
708
+ const hidden = variable.secret || referencedName(variable.value) !== void 0;
709
+ return {
710
+ key: variable.name,
711
+ value: hidden ? "" : variable.value,
712
+ type: hidden ? "secret" : "default",
713
+ enabled: variable.enabled ?? true
714
+ };
715
+ })
611
716
  };
612
717
  }
613
718
  // src/sync-workspace.ts
@@ -740,8 +845,8 @@ function requestFiles(files) {
740
845
  return new Map([...files].filter(([path]) => !path.startsWith("environments/")));
741
846
  }
742
847
  /** An environment's file text and the `{ name, values }` it carries, from the request file's side. */
743
- async function carriedEnvironment(json) {
744
- const text = stringifyEnvironment(await postmanToBrunoEnvironment(json), { format: "yml" });
848
+ async function carriedEnvironment(json, secretRefs) {
849
+ const text = stringifyEnvironment(withSecretReferences(await postmanToBrunoEnvironment(json), secretRefs), { format: "yml" });
745
850
  return {
746
851
  text,
747
852
  canon: canonicalJson(environmentToSource(text, "the environment"))
@@ -805,7 +910,7 @@ async function syncWorkspace(input) {
805
910
  const listed = [];
806
911
  for (const c of input.collections) {
807
912
  const environmentsPresent = c.environments.filter((env) => env.json !== void 0);
808
- const generated = c.json === void 0 ? void 0 : await collectionFiles(c.json, environmentsPresent.map((env) => env.json)).catch((err) => {
913
+ const generated = c.json === void 0 ? void 0 : await collectionFiles(c.json, environmentsPresent.map((env) => env.json), input.secretRefs).catch((err) => {
809
914
  throw new Error(`${c.source}: ${err.message}`, { cause: err });
810
915
  });
811
916
  const known = priorPair(prior?.baseline, "collection", c.source);
@@ -917,7 +1022,7 @@ async function syncWorkspace(input) {
917
1022
  sourceHash: finalSourceHash,
918
1023
  workspaceHash: folderHash(finalWorkspace)
919
1024
  });
920
- const envFiles = generated ?? await collectionFiles(EMPTY_COLLECTION, environmentsPresent.map((env) => env.json));
1025
+ const envFiles = generated ?? await collectionFiles(EMPTY_COLLECTION, environmentsPresent.map((env) => env.json), input.secretRefs);
921
1026
  const mapped = /* @__PURE__ */ new Set();
922
1027
  let index = 0;
923
1028
  for (const env of c.environments) {
@@ -927,7 +1032,7 @@ async function syncWorkspace(input) {
927
1032
  mapped.add(file);
928
1033
  const workspacePath = `${folder}/${file}`;
929
1034
  const now = current.get(file);
930
- const carried = env.json === void 0 ? void 0 : await carriedEnvironment(env.json);
1035
+ const carried = env.json === void 0 ? void 0 : await carriedEnvironment(env.json, input.secretRefs);
931
1036
  const envSourceHash = carried ? hashOf(carried.canon) : "";
932
1037
  const envDirection = decide({
933
1038
  sourcePresent: carried !== void 0,
@@ -957,10 +1062,15 @@ async function syncWorkspace(input) {
957
1062
  note: conflictNote("The environment")
958
1063
  });
959
1064
  } else {
1065
+ const healed = healReferences(now, input.secretRefs);
960
1066
  plan.files.push({
961
1067
  path,
962
- state: "unchanged"
1068
+ state: healed === now ? "unchanged" : "changed"
963
1069
  });
1070
+ if (healed !== now) {
1071
+ plan.write(path, healed);
1072
+ finalText = healed;
1073
+ }
964
1074
  if (envDirection === "toSource") {
965
1075
  const fromWorkspace = environmentToSource(now, workspacePath);
966
1076
  if (carried === void 0 || canonicalJson(fromWorkspace) !== carried.canon) {
@@ -973,7 +1083,7 @@ async function syncWorkspace(input) {
973
1083
  state: env.json === void 0 ? "new" : "changed"
974
1084
  });
975
1085
  plan.write(env.source, text);
976
- const rewritten = await carriedEnvironment(JSON.parse(text));
1086
+ const rewritten = await carriedEnvironment(JSON.parse(text), input.secretRefs);
977
1087
  finalEnvHash = hashOf(rewritten.canon);
978
1088
  if (rewritten.text !== now) plan.issues.push({
979
1089
  file: env.source,
@@ -1086,6 +1196,7 @@ function priorPair(baseline, kind, source) {
1086
1196
  * Bruno has no HTTP API and no account to push to: it reads a folder of files. So this provider works on files only,
1087
1197
  * with no token and no network. `export()` writes each of the repo's collection files as a Bruno JSON file with the
1088
1198
  * environments inside it, for `holocron export requests`.
1199
+ * `open()` says what `holocron open requests` needs to start Bruno with the secrets filled in from outside.
1089
1200
  * `workspace()` keeps the committed `.bruno/` workspace (Bruno's own files, one folder per collection) equal to the
1090
1201
  * repo's request files, for `holocron sync requests`.
1091
1202
  *
@@ -1174,6 +1285,9 @@ var BrunoTooling = class {
1174
1285
  issues
1175
1286
  };
1176
1287
  }
1288
+ open(input) {
1289
+ return openPlan(input, { app: this.options.app });
1290
+ }
1177
1291
  async workspace(input) {
1178
1292
  const { collectionFiles, envFiles, repoRoot } = input;
1179
1293
  const collections = [];
@@ -1194,6 +1308,7 @@ var BrunoTooling = class {
1194
1308
  collections,
1195
1309
  repoRoot,
1196
1310
  ...version === void 0 ? {} : { version },
1311
+ ...this.options.secretRefs ? { secretRefs: this.options.secretRefs } : {},
1197
1312
  ...input.dryRun ? { dryRun: true } : {},
1198
1313
  ...input.prefer ? { prefer: input.prefer } : {}
1199
1314
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@theholocron/holocron-plugin-bruno",
3
- "version": "5.0.0-alpha.481",
3
+ "version": "5.0.0-alpha.483",
4
4
  "description": "Holocron plugin for Bruno. Implements the tooling capability over files: it exports the repo's request collections as Bruno JSON files.",
5
5
  "keywords": [
6
6
  "bruno",
@@ -44,14 +44,14 @@
44
44
  "tsx": "4.23.15",
45
45
  "typescript": "^5.9.3",
46
46
  "vitest": "^5.1.0",
47
- "@theholocron/cli": "5.0.0-alpha.481",
48
- "@theholocron/tsdown-config": "^8.9.0-alpha.21",
47
+ "@theholocron/cli": "5.0.0-alpha.483",
49
48
  "@theholocron/eslint-config": "^8.9.0-alpha.21",
49
+ "@theholocron/tsdown-config": "^8.9.0-alpha.21",
50
50
  "@theholocron/tsconfig": "^8.9.0-alpha.21",
51
51
  "@theholocron/vitest-config": "^8.9.0-alpha.21"
52
52
  },
53
53
  "peerDependencies": {
54
- "@theholocron/cli": "5.0.0-alpha.481"
54
+ "@theholocron/cli": "5.0.0-alpha.483"
55
55
  },
56
56
  "engines": {
57
57
  "node": ">=22"