@theholocron/holocron-plugin-bruno 5.0.0-alpha.461 → 5.0.0-alpha.463

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 (3) hide show
  1. package/README.md +6 -2
  2. package/dist/index.mjs +162 -12
  3. package/package.json +4 -4
package/README.md CHANGED
@@ -44,13 +44,17 @@ providers: {
44
44
  Converter issues are returned, not thrown. A dry run converts and writes nothing.
45
45
  - `workspace` keeps the committed `.bruno/` workspace and the request files in step, both ways (`holocron sync requests`), with Bruno's own
46
46
  file writer (`@usebruno/filestore`) and reader (`parseRequest`, `parseFolder`, `parseCollection`, `parseEnvironment`):
47
- `workspace.yml`, and one folder per collection under `collections/` holding `opencollection.yml`, a `folder.yml` per folder,
47
+ `workspace.yml` (made when missing; when it lacks a collection only that entry is added to its `collections:` list, so the
48
+ `docs`, `specs` and the other settings Bruno keeps there stay as they are), and one folder per collection under `collections/` holding `opencollection.yml`, a `folder.yml` per folder,
48
49
  a YAML file per request and an `environments/` folder. A secret variable is written without its value. `.bruno/.sync.json`
49
50
  records a hash of both sides of each pair at the last sync, so a run knows which side changed: the request file only
50
51
  regenerates the workspace, the workspace only regenerates the request file (through `brunoToPostman`, with the request
51
52
  file's untouched entries kept as they were), both is a `conflict` that writes nothing unless `prefer` (`source` or
52
53
  `workspace`) settles it. A setting only Bruno has stays in the workspace and is reported as an issue. Each file is `new`,
53
- `changed`, `unchanged`, `removed` or `conflict`; a dry run writes and deletes nothing.
54
+ `changed`, `unchanged`, `removed` or `conflict`; a dry run writes and deletes nothing. The workspace's docs are one more
55
+ pair (kind `docs`): `.bruno/README.md` (`source`) and the `docs` value of `workspace.yml` (`workspace`), compared as text
56
+ with line endings and the trailing newline ignored, synced by the same rules, and only that value of `workspace.yml` is
57
+ written.
54
58
  - `sync` does nothing and succeeds: Bruno keeps no state, and the files are written by `export` and `workspace`.
55
59
  - `doctor` checks `collectionFiles` is set and every file exists and parses as JSON.
56
60
 
package/dist/index.mjs CHANGED
@@ -108,7 +108,7 @@ function parseBaseline(value) {
108
108
  return {
109
109
  version: 1,
110
110
  pairs: value.pairs.map((pair, index) => {
111
- if (!(isObject$1(pair) && (pair.kind === "collection" || pair.kind === "environment") && [
111
+ if (!(isObject$1(pair) && (pair.kind === "collection" || pair.kind === "environment" || pair.kind === "docs") && [
112
112
  pair.source,
113
113
  pair.workspace,
114
114
  pair.sourceHash,
@@ -209,6 +209,89 @@ async function collectionFiles(collection, environments) {
209
209
  issues: (read.issues ?? []).map(describeIssue)
210
210
  };
211
211
  }
212
+ // src/workspace-docs.ts
213
+ /** The workspace's README, relative to the repo root: the markdown side of the `docs` pair. */
214
+ const DOCS_FILE = ".bruno/README.md";
215
+ /** The text compared and hashed: line endings as `\n`, no trailing whitespace, so `|-` and a final newline agree. */
216
+ const normalizeDocs = (text) => text.replace(/\r\n?/g, "\n").trimEnd();
217
+ /**
218
+ * The text two docs are compared and hashed by: {@link normalizeDocs}, and a markdown table written without its padding
219
+ * (each cell trimmed, a separator row as `---`), so a formatter that pads the table's columns, as Prettier does, and
220
+ * Bruno's editor, which does not, give the same text. Only the comparison uses it; a write keeps the text it is given.
221
+ */
222
+ function comparableDocs(text) {
223
+ return normalizeDocs(text).split("\n").map((line) => {
224
+ const row = line.trim();
225
+ if (!row.startsWith("|") || !row.endsWith("|")) return line;
226
+ return `| ${row.slice(1, -1).split("|").map((cell) => cell.trim()).map((cell) => /^:?-+:?$/.test(cell) ? `${cell.startsWith(":") ? ":" : ""}---${cell.endsWith(":") ? ":" : ""}` : cell).join(" | ")} |`;
227
+ }).join("\n");
228
+ }
229
+ /** Where the `docs:` entry sits in a workspace file: its first line and the last line it covers. */
230
+ function docsRange(lines) {
231
+ const start = lines.findIndex((line) => /^docs:(\s|$)/.test(line));
232
+ if (start === -1) return void 0;
233
+ let end = start;
234
+ for (let i = start + 1; i < lines.length; i += 1) {
235
+ const line = lines[i];
236
+ if (line.trim() === "") continue;
237
+ if (/^\s/.test(line)) end = i;
238
+ else break;
239
+ }
240
+ return {
241
+ start,
242
+ end
243
+ };
244
+ }
245
+ /**
246
+ * The `docs` text of a workspace file (normalized), or `undefined` when the file has no `docs:` key. A `|` or `|-`
247
+ * block and a one-line plain or quoted value are read; a folded (`>`) block is an error, because the sync cannot
248
+ * write it back the way it found it.
249
+ */
250
+ function readDocs(text) {
251
+ const lines = text.replace(/\r\n?/g, "\n").split("\n");
252
+ const range = docsRange(lines);
253
+ if (!range) return void 0;
254
+ const value = lines[range.start].slice(5).trim();
255
+ const block = /^([|>])(\d?)[+-]?(\s+#.*)?$/.exec(value);
256
+ if (block) {
257
+ if (block[1] === ">") throw new Error(".bruno/workspace.yml: docs is a folded (>) block; use a literal (|) block so the sync can read it");
258
+ const body = lines.slice(range.start + 1, range.end + 1);
259
+ const indent = block[2] ? Number(block[2]) : Math.min(...body.filter((l) => l.trim() !== "").map((l) => /^ */.exec(l)[0].length));
260
+ if (!Number.isFinite(indent)) return "";
261
+ return normalizeDocs(body.map((l) => l.slice(indent)).join("\n"));
262
+ }
263
+ if (value.startsWith("\"")) return normalizeDocs(JSON.parse(value));
264
+ if (value.startsWith("'")) return normalizeDocs(value.slice(1, -1).replaceAll("''", "'"));
265
+ if (value === "" || value === "null" || value === "~") return "";
266
+ return normalizeDocs(value);
267
+ }
268
+ /** `docs: |-` and the text indented, as Bruno writes it; an empty text is `docs: ''`. */
269
+ function docsBlock(docs) {
270
+ const text = normalizeDocs(docs);
271
+ if (text === "") return ["docs: ''"];
272
+ const body = text.split("\n");
273
+ const first = body.find((l) => l.trim() !== "");
274
+ return [`docs: |${first !== void 0 && /^\s/.test(first) ? "2" : ""}-`, ...body.map((l) => l === "" ? "" : ` ${l}`)];
275
+ }
276
+ /**
277
+ * The workspace file with only its `docs` value replaced, every other byte kept. A file with no `docs:` key gets one
278
+ * at the end, after a blank line.
279
+ */
280
+ function withDocs(text, docs) {
281
+ const lines = text.split("\n");
282
+ const range = docsRange(lines);
283
+ const block = docsBlock(docs);
284
+ if (range) {
285
+ lines.splice(range.start, range.end - range.start + 1, ...block);
286
+ return lines.join("\n");
287
+ }
288
+ const body = text.endsWith("\n") || text === "" ? lines.slice(0, -1) : lines;
289
+ return [
290
+ ...body.length > 0 && body[body.length - 1] !== "" ? [...body, ""] : body,
291
+ ...block,
292
+ ""
293
+ ].join("\n");
294
+ }
212
295
  // src/workspace-to-source.ts
213
296
  /** A value in a stable text: object keys sorted, so two equal documents give the same text whatever their key order. */
214
297
  function canonicalJson(value) {
@@ -496,6 +579,32 @@ function workspaceYml(name, collections) {
496
579
  ""
497
580
  ].join("\n");
498
581
  }
582
+ /**
583
+ * The workspace file with `collections` added to its `collections:` list and every other byte kept: the entries are
584
+ * inserted after the last one, or in place of a `collections: []`. Returns `undefined` when the file has no
585
+ * `collections:` key, so the caller makes the file whole.
586
+ */
587
+ function addCollections(text, collections) {
588
+ const entries = collections.map((c) => ` - name: ${JSON.stringify(c.name)}\n path: ${JSON.stringify(c.path)}`);
589
+ const lines = text.split("\n");
590
+ const at = lines.findIndex((line) => /^collections:(\s|$)/.test(line));
591
+ if (at === -1) return void 0;
592
+ const value = lines[at].slice(12).replace(/\s+#.*$/, "").trim();
593
+ if (value === "[]" || value === "null" || value === "~") {
594
+ lines.splice(at, 1, "collections:", ...entries);
595
+ return lines.join("\n");
596
+ }
597
+ if (value !== "") return void 0;
598
+ let last = at;
599
+ for (let i = at + 1; i < lines.length; i += 1) {
600
+ const line = lines[i];
601
+ if (line.trim() === "") continue;
602
+ if (/^\s/.test(line) || line.startsWith("- ")) last = i;
603
+ else break;
604
+ }
605
+ lines.splice(last + 1, 0, ...entries);
606
+ return lines.join("\n");
607
+ }
499
608
  /** The text of a file, or `undefined` when it is not there. */
500
609
  async function readIfThere(path) {
501
610
  try {
@@ -613,8 +722,9 @@ function conflictNote(what) {
613
722
  * (see {@link decide}). The request file's hash is the hash of what the workspace can carry of it, so a setting that
614
723
  * only the workspace holds (a request's timeout) never looks like a change to the request file; the workspace's hash is
615
724
  * that of its own files, so such an edit is still seen as a change to the workspace and is never overwritten by a
616
- * request-file change without a conflict. `workspace.yml` is written when it is missing or does not list a collection,
617
- * and left alone otherwise, because Bruno adds settings of its own to it, such as the active environment.
725
+ * request-file change without a conflict. `workspace.yml` is made when it is missing, left alone while it lists every
726
+ * collection, and otherwise only gets the missing entries ({@link addCollections}), because Bruno keeps settings of
727
+ * its own in it: the active environment, the `docs` and the `specs`.
618
728
  *
619
729
  * A file is `new`, `changed` or `unchanged` by its text; a workspace file that is no longer generated is `removed`;
620
730
  * a pair that both sides changed is a `conflict`, and with a conflict (and no `prefer`) nothing at all is written.
@@ -819,17 +929,57 @@ async function syncWorkspace(input) {
819
929
  }
820
930
  const workspacePath = `${WORKSPACE_DIR}/workspace.yml`;
821
931
  const workspaceNow = await readIfThere(join(repoRoot, workspacePath));
822
- if (workspaceNow !== void 0 && listed.every((c) => workspaceNow.includes(JSON.stringify(c.path)))) plan.files.push({
823
- path: workspacePath,
824
- state: "unchanged"
825
- });
826
- else {
827
- plan.files.push({
828
- path: workspacePath,
829
- state: workspaceNow === void 0 ? "new" : "changed"
932
+ const listsAll = workspaceNow !== void 0 && listed.every((c) => workspaceNow.includes(JSON.stringify(c.path)));
933
+ let workspaceText = workspaceNow ?? "";
934
+ if (!listsAll) {
935
+ const missing = listed.filter((c) => workspaceNow === void 0 || !workspaceNow.includes(JSON.stringify(c.path)));
936
+ workspaceText = (workspaceNow === void 0 ? void 0 : addCollections(workspaceNow, missing)) ?? workspaceYml(input.workspaceName, listed);
937
+ }
938
+ const readmeNow = await readIfThere(join(repoRoot, DOCS_FILE));
939
+ const docsNow = workspaceNow === void 0 ? void 0 : readDocs(workspaceNow);
940
+ const readmeDocs = readmeNow === void 0 ? void 0 : normalizeDocs(readmeNow);
941
+ const workspaceDocs = docsNow === void 0 || docsNow === "" ? void 0 : docsNow;
942
+ const knownDocs = priorPair(prior?.baseline, "docs", DOCS_FILE);
943
+ if (readmeDocs !== void 0 || workspaceDocs !== void 0) {
944
+ const direction = decide({
945
+ sourcePresent: readmeDocs !== void 0,
946
+ workspacePresent: workspaceDocs !== void 0,
947
+ prior: knownDocs,
948
+ sourceHash: readmeDocs === void 0 ? "" : hashOf(comparableDocs(readmeDocs)),
949
+ workspaceHash: workspaceDocs === void 0 ? "" : hashOf(comparableDocs(workspaceDocs)),
950
+ agree: () => comparableDocs(readmeDocs) === comparableDocs(workspaceDocs),
951
+ prefer
830
952
  });
831
- plan.write(workspacePath, workspaceYml(input.workspaceName, listed));
953
+ if (direction === "conflict") {
954
+ plan.conflicts += 1;
955
+ plan.files.push({
956
+ path: DOCS_FILE,
957
+ state: "conflict",
958
+ note: conflictNote("The docs")
959
+ });
960
+ if (knownDocs) plan.pairs.push(knownDocs);
961
+ } else {
962
+ const finalDocs = direction === "toSource" ? workspaceDocs : readmeDocs;
963
+ if (direction === "toWorkspace") workspaceText = withDocs(workspaceText, finalDocs);
964
+ if (direction === "toSource") plan.write(DOCS_FILE, `${finalDocs}\n`);
965
+ plan.files.push({
966
+ path: DOCS_FILE,
967
+ state: direction !== "toSource" ? "unchanged" : readmeNow === void 0 ? "new" : "changed"
968
+ });
969
+ plan.pairs.push({
970
+ kind: "docs",
971
+ source: DOCS_FILE,
972
+ workspace: "workspace.yml",
973
+ sourceHash: hashOf(comparableDocs(finalDocs)),
974
+ workspaceHash: hashOf(comparableDocs(finalDocs))
975
+ });
976
+ }
832
977
  }
978
+ plan.files.push({
979
+ path: workspacePath,
980
+ state: workspaceNow === void 0 ? "new" : workspaceNow === workspaceText ? "unchanged" : "changed"
981
+ });
982
+ if (workspaceNow !== workspaceText) plan.write(workspacePath, workspaceText);
833
983
  const baselineText = serializeBaseline(plan.pairs);
834
984
  const baselineNow = prior?.text;
835
985
  plan.files.push({
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@theholocron/holocron-plugin-bruno",
3
- "version": "5.0.0-alpha.461",
3
+ "version": "5.0.0-alpha.463",
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.461",
48
- "@theholocron/tsconfig": "^8.9.0-alpha.18",
47
+ "@theholocron/cli": "5.0.0-alpha.463",
49
48
  "@theholocron/eslint-config": "^8.9.0-alpha.18",
49
+ "@theholocron/tsconfig": "^8.9.0-alpha.18",
50
50
  "@theholocron/tsdown-config": "^8.9.0-alpha.18",
51
51
  "@theholocron/vitest-config": "^8.9.0-alpha.18"
52
52
  },
53
53
  "peerDependencies": {
54
- "@theholocron/cli": "5.0.0-alpha.461"
54
+ "@theholocron/cli": "5.0.0-alpha.463"
55
55
  },
56
56
  "engines": {
57
57
  "node": ">=22"