spatial-views 0.1.0 → 0.2.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.
- package/README.md +201 -19
- package/dist/app/assets/{abnfDiagram-S7VZAUFO-BVgtb0Hw.js → abnfDiagram-S7VZAUFO-CFbb_Sz2.js} +1 -1
- package/dist/app/assets/{architecture-WOLXFQ4H-DxylGHqT.js → architecture-WOLXFQ4H-CsBD__5a.js} +1 -1
- package/dist/app/assets/architectureDiagram-YGSQEFNW-C0ic4ib1.js +36 -0
- package/dist/app/assets/{blockDiagram-APKKGZFW-C7fb3MCE.js → blockDiagram-APKKGZFW-BZDPbzAG.js} +1 -1
- package/dist/app/assets/{c4Diagram-KJ2TOQU5-Bkb_0fbd.js → c4Diagram-KJ2TOQU5-BilBWuq5.js} +1 -1
- package/dist/app/assets/{chunk-6MUBBAK2-DU4vZ03M.js → chunk-6MUBBAK2-BBl-4tQr.js} +1 -1
- package/dist/app/assets/{chunk-AXFCVDMN-De96BVTB.js → chunk-AXFCVDMN-BhsttQ-k.js} +1 -1
- package/dist/app/assets/{chunk-BG4553SI-J33jsRLV.js → chunk-BG4553SI-mWcPcFZr.js} +2 -2
- package/dist/app/assets/{chunk-DQASFODM-Bh85SSGO.js → chunk-DQASFODM-7FKe2gLt.js} +1 -1
- package/dist/app/assets/{chunk-HFQBQIA3-CCklgplR.js → chunk-HFQBQIA3-nXrQ08BL.js} +1 -1
- package/dist/app/assets/{chunk-KQLB2F66-B04qR8ED.js → chunk-KQLB2F66-swukhFmZ.js} +1 -1
- package/dist/app/assets/{chunk-KS47KZWR-BsqDyyAt.js → chunk-KS47KZWR-DkDkwO82.js} +1 -1
- package/dist/app/assets/{chunk-LEVMRMFU-DwxrBz2u.js → chunk-LEVMRMFU-DXH6iXv3.js} +1 -1
- package/dist/app/assets/{chunk-M57EVK23-D9YIHjps.js → chunk-M57EVK23-B-kEyOJF.js} +1 -1
- package/dist/app/assets/{chunk-OY52VR5R-WB63DeTA.js → chunk-OY52VR5R-DweG7qYw.js} +1 -1
- package/dist/app/assets/{chunk-QB3YPZ4I-cHzzKdtb.js → chunk-QB3YPZ4I-DyozK3lV.js} +1 -1
- package/dist/app/assets/{chunk-R3KSYZAL-C7rDagaa.js → chunk-R3KSYZAL-BEXCUN6J.js} +1 -1
- package/dist/app/assets/{chunk-STOXU5M4-CG6pnJtH.js → chunk-STOXU5M4-BaTlty4O.js} +1 -1
- package/dist/app/assets/{chunk-TU62E6Q5-BbcGYfy_.js → chunk-TU62E6Q5-_rcUP1Fh.js} +1 -1
- package/dist/app/assets/chunk-X3CZISLH-NOpCnvgE.js +1 -0
- package/dist/app/assets/{chunk-X4PVCEUW-BUI1Oi3q.js → chunk-X4PVCEUW-DGxTL3kB.js} +1 -1
- package/dist/app/assets/{chunk-XXDRQBXY-DYhK5YW-.js → chunk-XXDRQBXY-C02Gpej2.js} +1 -1
- package/dist/app/assets/{classDiagram-v2-L25IHNXF-K-k5GTUn.js → classDiagram-v2-L25IHNXF-DLEBwpvk.js} +1 -1
- package/dist/app/assets/cose-bilkent-JH36ORCC-kNCXOm8k.js +1 -0
- package/dist/app/assets/cynefin-EF2NZ3EQ-6MWNLbuF.js +1 -0
- package/dist/app/assets/{cynefinDiagram-L36ESFBY-mkV33_Xx.js → cynefinDiagram-L36ESFBY-CLFstukD.js} +1 -1
- package/dist/app/assets/{d3-scale-ygdFCUH8.js → d3-scale-CaJQQYkF.js} +1 -1
- package/dist/app/assets/{dagre-SPEB5RXH-BNIymLsu.js → dagre-SPEB5RXH-BaSAny1T.js} +1 -1
- package/dist/app/assets/{diagram-3B7GL73J-CGOiolFW.js → diagram-3B7GL73J-DJNlt5Sg.js} +1 -1
- package/dist/app/assets/{diagram-DNEMPZML-DZFkHNly.js → diagram-DNEMPZML-DEHeuILB.js} +1 -1
- package/dist/app/assets/{diagram-L3MUGCDN-B42GAYLq.js → diagram-L3MUGCDN-ob5Ka2Xc.js} +1 -1
- package/dist/app/assets/{diagram-LA4NIPWU-BYuLRYId.js → diagram-LA4NIPWU-DNESzFAc.js} +1 -1
- package/dist/app/assets/{diagram-MHCOC4E2-DGzI6TJG.js → diagram-MHCOC4E2-CVi9zKjG.js} +1 -1
- package/dist/app/assets/{diagram-XPFHEONP-CTBEgDQ8.js → diagram-XPFHEONP-DInT4C7e.js} +1 -1
- package/dist/app/assets/dist-CjvHN74u.js +1 -0
- package/dist/app/assets/{ebnfDiagram-UZSYDL2S-GuMdOCvL.js → ebnfDiagram-UZSYDL2S-CRCZNsji.js} +1 -1
- package/dist/app/assets/{elk-XV6XMXQE-BpY3loT5.js → elk-XV6XMXQE-D4fsWI2l.js} +1 -1
- package/dist/app/assets/{erDiagram-EYBAPDEH-nDZ2PMGl.js → erDiagram-EYBAPDEH-YQNM5StV.js} +1 -1
- package/dist/app/assets/{eventmodeling-K75KTNOO-BN42Mgwh.js → eventmodeling-K75KTNOO-Bev08J2F.js} +1 -1
- package/dist/app/assets/flowDiagram-MALGAFMO-BhJ554DH.js +1 -0
- package/dist/app/assets/ganttDiagram-23REXITY-CkaBMYM4.js +292 -0
- package/dist/app/assets/gitGraph-VSP46ZUC-CtbZ0sIS.js +1 -0
- package/dist/app/assets/{gitGraphDiagram-STJD7YCW-CDkKLrvO.js → gitGraphDiagram-STJD7YCW-D15ZdQ-Z.js} +1 -1
- package/dist/app/assets/index-Bn6_wHEp.js +98 -0
- package/dist/app/assets/index-H-Z7MFhZ.css +1 -0
- package/dist/app/assets/info-OHQRW6UA-CfUW1XFR.js +1 -0
- package/dist/app/assets/{infoDiagram-KBXO4MBG-B92P8pjY.js → infoDiagram-KBXO4MBG-DT7LbBRQ.js} +1 -1
- package/dist/app/assets/{ishikawaDiagram-M35NCXHK-BRZsbCvK.js → ishikawaDiagram-M35NCXHK-CF51lhjB.js} +1 -1
- package/dist/app/assets/{journeyDiagram-OPLOXDXO-Bppwb1oF.js → journeyDiagram-OPLOXDXO-_36FonKk.js} +1 -1
- package/dist/app/assets/{kanban-definition-BZKZHSQY-ByD_x5cm.js → kanban-definition-BZKZHSQY-tLk_OlQ-.js} +1 -1
- package/dist/app/assets/{mermaid-parser.core-CKmnV0Md.js → mermaid-parser.core-_3Vo8x6b.js} +3 -3
- package/dist/app/assets/mermaid.core-y-x1CYxC.js +44 -0
- package/dist/app/assets/{mindmap-definition-I2QAS2VK-fRbUh23C.js → mindmap-definition-I2QAS2VK-BCI4HRX3.js} +1 -1
- package/dist/app/assets/packet-JDAUHWVQ-Cco3UoaI.js +1 -0
- package/dist/app/assets/{pegDiagram-FD4XPPHP-qIABf6H8.js → pegDiagram-FD4XPPHP-BOzSoNji.js} +1 -1
- package/dist/app/assets/pie-XZMESJXO-reOB2GXn.js +1 -0
- package/dist/app/assets/{pieDiagram-J4AKIIQI-Bb4RBdrc.js → pieDiagram-J4AKIIQI-PlHpwT_c.js} +1 -1
- package/dist/app/assets/{quadrantDiagram-XTYBLID2-B0CpcVWE.js → quadrantDiagram-XTYBLID2-CdxZ0ael.js} +1 -1
- package/dist/app/assets/radar-ABXABTNO-DfCl8uV2.js +1 -0
- package/dist/app/assets/railroad-I3PHUGI6-4s14UyW1.js +1 -0
- package/dist/app/assets/{railroad-abnf-LNEFI6M7-BUhw7Iuo.js → railroad-abnf-LNEFI6M7-C49jJE4Y.js} +1 -1
- package/dist/app/assets/{railroad-ebnf-DWYD2UWJ-nQNF56LJ.js → railroad-ebnf-DWYD2UWJ-Bx-pcPc7.js} +1 -1
- package/dist/app/assets/{railroad-peg-JR7BVNR7-CwvbVIo6.js → railroad-peg-JR7BVNR7-DjBPSCKV.js} +1 -1
- package/dist/app/assets/{railroadDiagram-MRQ6NCDL-Dwr-mPZL.js → railroadDiagram-MRQ6NCDL-wGcmIMqq.js} +1 -1
- package/dist/app/assets/{requirementDiagram-Y7LWUY3S-Ctv2DKJM.js → requirementDiagram-Y7LWUY3S-DprFI03z.js} +1 -1
- package/dist/app/assets/rolldown-runtime-XKyaHtl9.js +1 -0
- package/dist/app/assets/{sankeyDiagram-WGJFYUXA-BvKX0eIe.js → sankeyDiagram-WGJFYUXA-DygYWLTR.js} +1 -1
- package/dist/app/assets/{sequenceDiagram-5NDRBG5M-Y4YqU2Xt.js → sequenceDiagram-5NDRBG5M-BzIr2DBO.js} +1 -1
- package/dist/app/assets/{stateDiagram-v2-FJ4FJNEA-DmjIjn4u.js → stateDiagram-v2-FJ4FJNEA-MGCqKnm_.js} +1 -1
- package/dist/app/assets/{swimlanes-UMQ2AFYF-B2O5JCjs.js → swimlanes-UMQ2AFYF-CIEH82Zt.js} +1 -1
- package/dist/app/assets/swimlanesDiagram-OMPLPG4O-YWNJjl0g.js +8 -0
- package/dist/app/assets/{timeline-definition-SEUHKTQE-DJJdixAI.js → timeline-definition-SEUHKTQE-BGYSqup2.js} +1 -1
- package/dist/app/assets/treeView-D4JQ5SDB-B39o1sJo.js +1 -0
- package/dist/app/assets/treemap-SAJKECNS-BoGxrUH4.js +1 -0
- package/dist/app/assets/{usecaseDiagram-AFA6YXN4-Bp8pXgvR.js → usecaseDiagram-AFA6YXN4-dy6oVOke.js} +1 -1
- package/dist/app/assets/{vennDiagram-NNMJR3RB-DYSfZMWT.js → vennDiagram-NNMJR3RB-CUyuhcAk.js} +1 -1
- package/dist/app/assets/wardley-7MLQ67FV-sYPAvQ06.js +1 -0
- package/dist/app/assets/{wardleyDiagram-S7YFUSEH-CGJ8qg76.js → wardleyDiagram-S7YFUSEH-DYtxYUHe.js} +1 -1
- package/dist/app/assets/{xychartDiagram-GGNKY7IX-CRSvraFc.js → xychartDiagram-GGNKY7IX-ClR06AlB.js} +1 -1
- package/dist/app/index.html +3 -2
- package/dist/cli/index.js +281 -53
- package/dist/core/api.js +262 -51
- package/dist/core/approvals.js +426 -0
- package/dist/core/context.js +68 -3
- package/dist/core/diff.js +156 -0
- package/dist/core/docs.js +9 -3
- package/dist/core/doctor.js +152 -0
- package/dist/core/doctypes.js +79 -0
- package/dist/core/edit.js +25 -0
- package/dist/core/flags.js +13 -0
- package/dist/core/gaps.js +7 -2
- package/dist/core/heading.js +4 -0
- package/dist/core/history.js +36 -5
- package/dist/core/home.js +345 -0
- package/dist/core/lanes.js +134 -0
- package/dist/core/launch.js +34 -0
- package/dist/core/layout.js +362 -0
- package/dist/core/newdoc.js +66 -0
- package/dist/core/open-folder.js +30 -0
- package/dist/core/openspec.js +6 -6
- package/dist/core/paths.js +71 -4
- package/dist/core/project.js +392 -0
- package/dist/core/route.js +420 -0
- package/dist/core/scan.js +514 -0
- package/dist/core/schema.js +170 -1
- package/dist/core/scope.js +126 -0
- package/dist/core/setup.js +134 -0
- package/dist/core/skills.js +92 -14
- package/dist/core/storage/config.js +74 -0
- package/dist/core/storage/events.js +54 -0
- package/dist/core/storage/folder-watch.js +147 -0
- package/dist/core/storage/folder.js +469 -0
- package/dist/core/storage/index.js +8 -0
- package/dist/core/storage/local.js +22 -0
- package/dist/core/storage/migrate.js +48 -0
- package/dist/core/storage/postgres.js +290 -0
- package/dist/core/storage/types.js +101 -0
- package/dist/core/storage.js +483 -77
- package/dist/core/suggest.js +283 -0
- package/dist/core/tree.js +254 -0
- package/dist/core/types.js +583 -0
- package/dist/core/version.js +1 -1
- package/dist/core/versions.js +245 -0
- package/dist/core/watch.js +2 -136
- package/dist/core/xml.js +23 -5
- package/dist/demo/args.js +58 -0
- package/dist/demo/browser.js +301 -0
- package/dist/demo/platform.js +147 -0
- package/dist/demo/run.js +198 -0
- package/dist/demo/steps.js +328 -0
- package/dist/mcp/project-tools.js +193 -0
- package/dist/mcp/server.js +493 -65
- package/examples/platform/README.md +33 -0
- package/examples/platform/api/package.json +8 -0
- package/examples/platform/api/src/auth-middleware.js +16 -0
- package/examples/platform/api/src/proxy.js +7 -0
- package/examples/platform/api/src/rate-limit.js +13 -0
- package/examples/platform/api/src/server.js +40 -0
- package/examples/platform/auth/package.json +9 -0
- package/examples/platform/auth/src/server.js +24 -0
- package/examples/platform/auth/src/tokens.js +19 -0
- package/examples/platform/auth/src/users.js +16 -0
- package/examples/platform/docs/api/PRD.md +10 -0
- package/examples/platform/docs/api/SDD.md +15 -0
- package/examples/platform/docs/auth/PRD.md +10 -0
- package/examples/platform/docs/auth/SDD.md +8 -0
- package/examples/platform/docs/frontend/PRD.md +18 -0
- package/examples/platform/docs/frontend/SDD.md +13 -0
- package/examples/platform/docs/pricing/PRD.md +9 -0
- package/examples/platform/docs/pricing/SDD.md +8 -0
- package/examples/platform/docs/products/PRD.md +9 -0
- package/examples/platform/docs/products/SDD.md +8 -0
- package/examples/platform/frontend/package.json +8 -0
- package/examples/platform/frontend/src/api-client.js +21 -0
- package/examples/platform/frontend/src/main.js +21 -0
- package/examples/platform/frontend/src/pages/products.js +12 -0
- package/examples/platform/frontend/src/pages/sign-in.js +14 -0
- package/examples/platform/frontend/src/session.js +11 -0
- package/examples/platform/pricing/package.json +9 -0
- package/examples/platform/pricing/src/cache.js +19 -0
- package/examples/platform/pricing/src/product-events.js +17 -0
- package/examples/platform/pricing/src/rules.js +19 -0
- package/examples/platform/pricing/src/server.js +32 -0
- package/examples/platform/products/package.json +9 -0
- package/examples/platform/products/src/catalog.js +20 -0
- package/examples/platform/products/src/events.js +11 -0
- package/examples/platform/products/src/search.js +9 -0
- package/examples/platform/products/src/server.js +31 -0
- package/migrations/postgres/001_init.sql +55 -0
- package/migrations/postgres/002_view_versions.sql +18 -0
- package/package.json +18 -2
- package/schema/approval.schema.json +173 -0
- package/schema/gap.schema.json +3 -0
- package/schema/project.schema.json +93 -0
- package/schema/settings.schema.json +86 -0
- package/schema/toolbox.schema.json +206 -0
- package/schema/type.schema.json +121 -0
- package/schema/view.schema.json +106 -0
- package/skills/claude/build-gap/SKILL.md +7 -0
- package/skills/claude/check-ui/SKILL.md +40 -0
- package/skills/claude/draw-system/SKILL.md +24 -9
- package/skills/claude/gap-from-view/SKILL.md +7 -0
- package/skills/claude/post-audit/SKILL.md +58 -0
- package/skills/claude/pre-audit/SKILL.md +82 -0
- package/skills/claude/update-state/SKILL.md +56 -0
- package/skills/cursor/check-ui.mdc +40 -0
- package/skills/cursor/draw-system.mdc +24 -9
- package/skills/cursor/post-audit.mdc +44 -0
- package/skills/cursor/pre-audit.mdc +49 -0
- package/skills/cursor/update-state.mdc +45 -0
- package/skills/windsurf/spatial-views.md +182 -36
- package/toolboxes/aws.yaml +43 -0
- package/toolboxes/azure.yaml +41 -0
- package/toolboxes/cloud.yaml +95 -0
- package/toolboxes/docs.yaml +111 -0
- package/toolboxes/software.yaml +114 -0
- package/toolboxes/types/docs/gap.yaml +15 -0
- package/toolboxes/types/docs/mdd.yaml +19 -0
- package/toolboxes/types/docs/plain.yaml +7 -0
- package/toolboxes/types/docs/prd.yaml +17 -0
- package/dist/app/assets/architectureDiagram-YGSQEFNW-BrBCHqUH.js +0 -36
- package/dist/app/assets/chunk-X3CZISLH-FMl4PpWt.js +0 -1
- package/dist/app/assets/cose-bilkent-JH36ORCC-BJmLLpaN.js +0 -1
- package/dist/app/assets/cynefin-EF2NZ3EQ-DrzwMKdm.js +0 -1
- package/dist/app/assets/dist-1Q8ng3tu.js +0 -1
- package/dist/app/assets/flowDiagram-MALGAFMO-BCwwiLzb.js +0 -1
- package/dist/app/assets/ganttDiagram-23REXITY-xMRHaU3y.js +0 -292
- package/dist/app/assets/gitGraph-VSP46ZUC-DyGq5mdd.js +0 -1
- package/dist/app/assets/index-BY66fc8Q.js +0 -95
- package/dist/app/assets/index-ByetEVbs.css +0 -1
- package/dist/app/assets/info-OHQRW6UA-DOBf-fQH.js +0 -1
- package/dist/app/assets/mermaid.core-BoRvk1Q-.js +0 -44
- package/dist/app/assets/packet-JDAUHWVQ-CgHq03Zj.js +0 -1
- package/dist/app/assets/pie-XZMESJXO-DtNh3cDE.js +0 -1
- package/dist/app/assets/radar-ABXABTNO-BS0FcsIX.js +0 -1
- package/dist/app/assets/railroad-I3PHUGI6-lO8vfGdI.js +0 -1
- package/dist/app/assets/swimlanesDiagram-OMPLPG4O-DhCl-Cjg.js +0 -8
- package/dist/app/assets/treeView-D4JQ5SDB-Dd1no4eE.js +0 -1
- package/dist/app/assets/treemap-SAJKECNS-t6wtsaki.js +0 -1
- package/dist/app/assets/wardley-7MLQ67FV-Bx60QTuu.js +0 -1
- package/skills/cursor/build-gap.mdc +0 -21
- package/skills/cursor/gap-from-view.mdc +0 -22
|
@@ -0,0 +1,283 @@
|
|
|
1
|
+
import { lstat, readdir, readFile } from "node:fs/promises";
|
|
2
|
+
import { basename, dirname, join, resolve } from "node:path";
|
|
3
|
+
import { UserError } from "./errors.js";
|
|
4
|
+
export const SUGGEST_LIMITS = {
|
|
5
|
+
maxFiles: 5000,
|
|
6
|
+
maxFileBytes: 256 * 1024,
|
|
7
|
+
maxTotalBytes: 16 * 1024 * 1024,
|
|
8
|
+
maxDepth: 12,
|
|
9
|
+
maxEvidence: 10,
|
|
10
|
+
};
|
|
11
|
+
const SKIP_DIRS = new Set([
|
|
12
|
+
".git", "node_modules", "dist", "build", "out", ".next", ".nuxt", "coverage", "vendor", "target",
|
|
13
|
+
"__pycache__", ".venv", "venv", ".tox", ".idea", ".vscode", "bin", "obj",
|
|
14
|
+
]);
|
|
15
|
+
const CODE = /\.(?:[cm]?[jt]sx?|py|go|rs|java|kt|cs|rb|php|scala|swift|sh|ps1|env|ini|toml|properties|conf|cfg)$/i;
|
|
16
|
+
const YAML = /\.ya?ml$/i;
|
|
17
|
+
const MD = /\.(?:md|mdx|markdown|txt|rst|adoc)$/i;
|
|
18
|
+
const MANIFEST = new Set(["package.json", "go.mod", "Cargo.toml", "pyproject.toml", "requirements.txt", "composer.json", "pom.xml", "build.gradle", "build.gradle.kts", "Gemfile"]);
|
|
19
|
+
/** Names that are infrastructure or the web, not another repo of ours. */
|
|
20
|
+
const NOT_A_REPO = new Set([
|
|
21
|
+
"localhost", "www", "http", "https", "database", "db", "amqp", "rabbit", "rabbitmq", "redis", "postgres", "postgresql",
|
|
22
|
+
"pg", "mysql", "mongo", "mongodb", "kafka", "nats", "sqs", "sns", "s3", "smtp", "sentry", "elastic", "elasticsearch",
|
|
23
|
+
"memcached", "minio", "public", "base", "env", "node", "npm", "github", "gitlab", "bitbucket", "example", "test", "tests",
|
|
24
|
+
"src", "lib", "app", "main", "master", "origin", "upstream", "default", "latest", "proxy", "host", "port", "url",
|
|
25
|
+
]);
|
|
26
|
+
const PUBLIC_HOST = /(^|\.)(github\.com|gitlab\.com|bitbucket\.org|npmjs\.(?:com|org)|registry\.\w+|w3\.org|json-schema\.org|example\.(?:com|org|net)|googleapis\.com|amazonaws\.com|microsoft\.com|mozilla\.org|schema\.org|cloudflare\.com|stripe\.com|apple\.com|google\.com|spatialthinking\.ai|reactflow\.dev|mermaid\.js\.org)$/i;
|
|
27
|
+
const IP = /^\d+\.\d+\.\d+\.\d+$|^\[/;
|
|
28
|
+
/** Lower case, words split on anything that is not a letter or a digit. */
|
|
29
|
+
const words = (s) => s.toLowerCase().split(/[^a-z0-9]+/).filter(Boolean);
|
|
30
|
+
const norm = (s) => words(s).join("-");
|
|
31
|
+
/**
|
|
32
|
+
* A folder matches a name when one's words end the other's: `worker` matches
|
|
33
|
+
* `shop-worker`, `@acme/billing` matches `billing`. A name of one short word
|
|
34
|
+
* must match exactly.
|
|
35
|
+
*/
|
|
36
|
+
export function nameMatches(name, folder) {
|
|
37
|
+
const a = words(name);
|
|
38
|
+
const b = words(folder);
|
|
39
|
+
if (!a.length || !b.length)
|
|
40
|
+
return false;
|
|
41
|
+
if (a.join("-") === b.join("-"))
|
|
42
|
+
return true;
|
|
43
|
+
const [short, long] = a.length <= b.length ? [a, b] : [b, a];
|
|
44
|
+
if (short.join("").length < 4)
|
|
45
|
+
return false;
|
|
46
|
+
return long.slice(long.length - short.length).join("-") === short.join("-") || long.slice(0, short.length).join("-") === short.join("-");
|
|
47
|
+
}
|
|
48
|
+
async function walk(root, lim) {
|
|
49
|
+
const files = [];
|
|
50
|
+
let queue = [{ rel: "", depth: 0 }];
|
|
51
|
+
while (queue.length && files.length < lim.maxFiles) {
|
|
52
|
+
const next = [];
|
|
53
|
+
for (const dir of queue) {
|
|
54
|
+
const names = (await readdir(join(root, dir.rel)).catch(() => [])).sort();
|
|
55
|
+
for (const name of names) {
|
|
56
|
+
const rel = dir.rel ? `${dir.rel}/${name}` : name;
|
|
57
|
+
const st = await lstat(join(root, rel)).catch(() => null);
|
|
58
|
+
if (!st || st.isSymbolicLink())
|
|
59
|
+
continue;
|
|
60
|
+
if (st.isDirectory()) {
|
|
61
|
+
if (!SKIP_DIRS.has(name) && dir.depth + 1 <= lim.maxDepth)
|
|
62
|
+
next.push({ rel, depth: dir.depth + 1 });
|
|
63
|
+
}
|
|
64
|
+
else if (st.isFile() && files.length < lim.maxFiles)
|
|
65
|
+
files.push({ rel, abs: join(root, rel), size: st.size });
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
queue = next;
|
|
69
|
+
}
|
|
70
|
+
return files;
|
|
71
|
+
}
|
|
72
|
+
/** The sub-folders of each folder, by listing only (symlinks and dot-folders left out). */
|
|
73
|
+
export async function siblingFolders(parents) {
|
|
74
|
+
const out = new Set();
|
|
75
|
+
for (const p of new Set(parents)) {
|
|
76
|
+
const entries = await readdir(p, { withFileTypes: true }).catch(() => []);
|
|
77
|
+
for (const e of entries)
|
|
78
|
+
if (e.isDirectory() && !e.name.startsWith("."))
|
|
79
|
+
out.add(join(p, e.name));
|
|
80
|
+
}
|
|
81
|
+
return [...out].sort();
|
|
82
|
+
}
|
|
83
|
+
class Found {
|
|
84
|
+
max;
|
|
85
|
+
by = new Map();
|
|
86
|
+
constructor(max) {
|
|
87
|
+
this.max = max;
|
|
88
|
+
}
|
|
89
|
+
add(name, file, line, text) {
|
|
90
|
+
const n = norm(name);
|
|
91
|
+
if (!n || n.length < 2 || NOT_A_REPO.has(n))
|
|
92
|
+
return;
|
|
93
|
+
const list = this.by.get(n) ?? [];
|
|
94
|
+
if (!list.some((e) => e.file === file && e.line === line) && list.length < this.max)
|
|
95
|
+
list.push({ file, line, text: text.trim().slice(0, 200) });
|
|
96
|
+
this.by.set(n, list);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
const lineAt = (text, index) => text.slice(0, index).split("\n").length;
|
|
100
|
+
const lineText = (lines, n) => lines[n - 1] ?? "";
|
|
101
|
+
/** The repo name in a git URL: git@github.com:acme/billing.git, https://host/acme/billing. */
|
|
102
|
+
export function repoFromUrl(url) {
|
|
103
|
+
const m = /[/:]([^/:]+?)(?:\.git)?\/?$/.exec(url.trim());
|
|
104
|
+
return m?.[1] && !/^[\d.]+$/.test(m[1]) ? m[1] : undefined;
|
|
105
|
+
}
|
|
106
|
+
/** The service a host names: `web` in http://web:8080, `billing` in billing.internal.acme.io. */
|
|
107
|
+
function hostName(host) {
|
|
108
|
+
if (IP.test(host) || PUBLIC_HOST.test(host) || /^(localhost|0\.0\.0\.0)$/i.test(host))
|
|
109
|
+
return undefined;
|
|
110
|
+
return host.split(".")[0];
|
|
111
|
+
}
|
|
112
|
+
const ENV_SERVICE = /\b([A-Z][A-Z0-9]*(?:_[A-Z0-9]+)*?)_(?:BASE_URL|API_URL|SERVICE_URL|URL|HOST|ENDPOINT|ADDR|SERVICE)\b/g;
|
|
113
|
+
const URL_LIT = /\b(?:https?|wss?|grpc):\/\/([A-Za-z0-9_.-]+)(?::\d+)?/g;
|
|
114
|
+
const QUEUE_CALL = /\b(?:publish|sendToQueue|enqueue|produce|consume|subscribe|sendMessage|assertQueue|assertExchange|bindQueue|createQueue|queue_declare|declare_queue|basic_publish|basic_consume|xadd|lpush|rpush|brpop|blpop)\s*\(\s*(?:[^,()]*?,\s*)?["'`]([\w.:/-]+)["'`]/g;
|
|
115
|
+
const QUEUE_KEY = /\b(?:queue|queueName|queue_name|topic|topicName|topic_name|exchange|routingKey|routing_key|QueueUrl|TopicArn|channel)\s*[:=]\s*["'`]([\w.:/-]+)["'`]/g;
|
|
116
|
+
const NEW_QUEUE = /new\s+(?:Queue|Worker|Topic|Exchange|Consumer|Producer)\s*\(\s*["'`]([\w.:/-]+)["'`]/g;
|
|
117
|
+
/** Read evidence of other repos in one source. `self` are names that mean this project's own sources. */
|
|
118
|
+
export async function findRelated(root, opts) {
|
|
119
|
+
const lim = { ...SUGGEST_LIMITS, ...opts.limits };
|
|
120
|
+
if (!(await lstat(root).catch(() => null))?.isDirectory())
|
|
121
|
+
throw new UserError(`${root} is not a folder (moved or not mounted?)`);
|
|
122
|
+
const found = new Found(lim.maxEvidence);
|
|
123
|
+
/** Queue and topic names: only kept when a word in one is a sibling folder (order.created alone says nothing). */
|
|
124
|
+
const queues = [];
|
|
125
|
+
const siblingNames = opts.siblings.map((p) => basename(p));
|
|
126
|
+
const files = await walk(root, lim);
|
|
127
|
+
let total = 0;
|
|
128
|
+
const read = async (f) => {
|
|
129
|
+
if (f.size > lim.maxFileBytes || total + f.size > lim.maxTotalBytes)
|
|
130
|
+
return undefined;
|
|
131
|
+
const t = await readFile(f.abs, "utf8").catch(() => undefined);
|
|
132
|
+
if (t !== undefined)
|
|
133
|
+
total += f.size;
|
|
134
|
+
return t;
|
|
135
|
+
};
|
|
136
|
+
// git: remotes and submodules. A remote naming another repo (a fork's upstream) counts; this repo's own does not.
|
|
137
|
+
const gitConfig = await lstat(join(root, ".git", "config")).catch(() => null);
|
|
138
|
+
if (gitConfig?.isFile() && gitConfig.size <= lim.maxFileBytes) {
|
|
139
|
+
const t = await readFile(join(root, ".git", "config"), "utf8").catch(() => "");
|
|
140
|
+
const lines = t.split("\n");
|
|
141
|
+
for (const m of t.matchAll(/^\s*url\s*=\s*(\S+)/gm)) {
|
|
142
|
+
const repo = repoFromUrl(m[1]);
|
|
143
|
+
const line = lineAt(t, m.index);
|
|
144
|
+
const section = lines.slice(0, line).reverse().find((l) => /^\s*\[/.test(l)) ?? "";
|
|
145
|
+
if (repo && /submodule/.test(section))
|
|
146
|
+
found.add(repo, ".git/config", line, lineText(lines, line));
|
|
147
|
+
else if (repo && norm(repo) !== norm(basename(root)))
|
|
148
|
+
found.add(repo, ".git/config", line, lineText(lines, line));
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
for (const f of files) {
|
|
152
|
+
const name = basename(f.rel);
|
|
153
|
+
const isManifest = MANIFEST.has(name);
|
|
154
|
+
const isCompose = /^(docker-)?compose(\.[\w-]+)?\.ya?ml$/i.test(name);
|
|
155
|
+
const isYaml = YAML.test(name);
|
|
156
|
+
const isDoc = MD.test(name);
|
|
157
|
+
const isCode = CODE.test(name) || name === "Dockerfile" || name.startsWith(".env.example");
|
|
158
|
+
if (name === ".gitmodules" || isManifest || isYaml || isDoc || isCode) {
|
|
159
|
+
if (/^\.env($|\.(?!example))/.test(name))
|
|
160
|
+
continue; // never secrets
|
|
161
|
+
const t = await read(f);
|
|
162
|
+
if (t === undefined)
|
|
163
|
+
continue;
|
|
164
|
+
const lines = t.split("\n");
|
|
165
|
+
const at = (i) => {
|
|
166
|
+
const line = lineAt(t, i);
|
|
167
|
+
return [line, lineText(lines, line)];
|
|
168
|
+
};
|
|
169
|
+
if (name === ".gitmodules") {
|
|
170
|
+
for (const m of t.matchAll(/^\s*(?:path|url)\s*=\s*(\S+)/gm)) {
|
|
171
|
+
const repo = /url/.test(m[0]) ? repoFromUrl(m[1]) : basename(m[1]);
|
|
172
|
+
if (repo)
|
|
173
|
+
found.add(repo, f.rel, ...at(m.index));
|
|
174
|
+
}
|
|
175
|
+
continue;
|
|
176
|
+
}
|
|
177
|
+
if (name === "package.json" || name === "composer.json") {
|
|
178
|
+
try {
|
|
179
|
+
const j = JSON.parse(t);
|
|
180
|
+
const own = j.name ?? "";
|
|
181
|
+
const scope = /^(@[^/]+)\//.exec(own)?.[1];
|
|
182
|
+
const ownPrefix = words(own.replace(/^@[^/]+\//, ""))[0];
|
|
183
|
+
const deps = { ...j.dependencies, ...j.devDependencies, ...j.peerDependencies, ...j.require };
|
|
184
|
+
for (const [dep, version] of Object.entries(deps)) {
|
|
185
|
+
const local = /^(file|link|workspace|portal):/.test(version) || /^\.\.?\//.test(version);
|
|
186
|
+
const sameScope = !!scope && dep.startsWith(`${scope}/`);
|
|
187
|
+
const samePrefix = !!ownPrefix && ownPrefix.length >= 3 && words(dep.replace(/^@[^/]+\//, ""))[0] === ownPrefix && words(dep).length > 1;
|
|
188
|
+
const sibling = siblingNames.some((s) => nameMatches(dep.replace(/^@[^/]+\//, ""), s));
|
|
189
|
+
if (!(local || sameScope || samePrefix || sibling))
|
|
190
|
+
continue;
|
|
191
|
+
const i = t.indexOf(`"${dep}"`);
|
|
192
|
+
const short = local && /\//.test(version) ? basename(version.replace(/^\w+:/, "")) : dep.replace(/^@[^/]+\//, "");
|
|
193
|
+
found.add(short, f.rel, ...at(Math.max(0, i)));
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
catch {
|
|
197
|
+
// a broken manifest says nothing
|
|
198
|
+
}
|
|
199
|
+
continue;
|
|
200
|
+
}
|
|
201
|
+
if (name === "go.mod") {
|
|
202
|
+
for (const m of t.matchAll(/^\s*replace\s+\S+\s*=>\s*(\.\.?\/\S+)/gm))
|
|
203
|
+
found.add(basename(m[1]), f.rel, ...at(m.index));
|
|
204
|
+
continue;
|
|
205
|
+
}
|
|
206
|
+
if (isCompose) {
|
|
207
|
+
const services = /^services:\s*$/m.exec(t);
|
|
208
|
+
if (services) {
|
|
209
|
+
const body = t.slice(services.index);
|
|
210
|
+
for (const m of body.matchAll(/^ {2}([\w.-]+):\s*$/gm))
|
|
211
|
+
found.add(m[1], f.rel, ...at(services.index + m.index));
|
|
212
|
+
}
|
|
213
|
+
for (const m of t.matchAll(/^\s*(?:build|context):\s*["']?(\.\.\/[^\s"']+)/gm))
|
|
214
|
+
found.add(basename(m[1]), f.rel, ...at(m.index));
|
|
215
|
+
}
|
|
216
|
+
else if (isYaml && /^kind:\s*(Service|Deployment|StatefulSet|Ingress|CronJob|Job)\b/m.test(t)) {
|
|
217
|
+
// k8s: the metadata name of each object.
|
|
218
|
+
for (const m of t.matchAll(/^metadata:\s*\n(?:[ \t]+.*\n)*?[ \t]+name:\s*["']?([\w.-]+)/gm)) {
|
|
219
|
+
const i = m.index + m[0].lastIndexOf(m[1]);
|
|
220
|
+
found.add(m[1], f.rel, ...at(i));
|
|
221
|
+
}
|
|
222
|
+
for (const m of t.matchAll(/^\s*(?:serviceName|service):\s*["']?([\w.-]+)/gm))
|
|
223
|
+
found.add(m[1], f.rel, ...at(m.index));
|
|
224
|
+
}
|
|
225
|
+
if (isDoc) {
|
|
226
|
+
// Docs that mention another repo: a repo URL, or a sibling folder named as a repo, service, app or package.
|
|
227
|
+
for (const m of t.matchAll(/\b(?:github\.com|gitlab\.com|bitbucket\.org|dev\.azure\.com)[/:][\w.-]+\/(?:_git\/)?([\w.-]+)/g)) {
|
|
228
|
+
const repo = m[1].replace(/\.git$/, "");
|
|
229
|
+
if (norm(repo) !== norm(basename(root)))
|
|
230
|
+
found.add(repo, f.rel, ...at(m.index));
|
|
231
|
+
}
|
|
232
|
+
for (const s of siblingNames) {
|
|
233
|
+
if (s.length < 3 || opts.self.some((x) => norm(x) === norm(s)))
|
|
234
|
+
continue;
|
|
235
|
+
const e = s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
236
|
+
const re = new RegExp(`\`${e}\`|\\[${e}\\]\\(|\\b${e}\\s+(?:repo|repository|service|app|package|module|project)\\b|\\b(?:repo|repository|service)\\s+\`?${e}\\b|\\.\\./${e}\\b`, "gi");
|
|
237
|
+
for (const m of t.matchAll(re))
|
|
238
|
+
found.add(s, f.rel, ...at(m.index));
|
|
239
|
+
}
|
|
240
|
+
continue;
|
|
241
|
+
}
|
|
242
|
+
// Code and config: base URLs, hostnames, env names, queues, and paths up into a sibling.
|
|
243
|
+
for (const m of t.matchAll(URL_LIT)) {
|
|
244
|
+
const n = hostName(m[1]);
|
|
245
|
+
if (n)
|
|
246
|
+
found.add(n, f.rel, ...at(m.index));
|
|
247
|
+
}
|
|
248
|
+
for (const m of t.matchAll(ENV_SERVICE))
|
|
249
|
+
found.add(m[1], f.rel, ...at(m.index));
|
|
250
|
+
for (const re of [QUEUE_CALL, QUEUE_KEY, NEW_QUEUE])
|
|
251
|
+
for (const m of t.matchAll(re)) {
|
|
252
|
+
const [line, text] = at(m.index);
|
|
253
|
+
queues.push({ name: m[1], file: f.rel, line, text });
|
|
254
|
+
}
|
|
255
|
+
for (const m of t.matchAll(/["'`](?:\.\.\/)+([\w.-]+)\//g)) {
|
|
256
|
+
const up = (m[0].match(/\.\.\//g) ?? []).length;
|
|
257
|
+
const depth = f.rel.split("/").length - 1;
|
|
258
|
+
if (up > depth)
|
|
259
|
+
found.add(m[1], f.rel, ...at(m.index));
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
// A queue or topic name counts only when a word in it is a sibling folder: worker.orders -> worker.
|
|
264
|
+
for (const q of queues)
|
|
265
|
+
for (const s of siblingNames) {
|
|
266
|
+
const sn = norm(s);
|
|
267
|
+
if (sn.length >= 3 && `-${norm(q.name)}-`.includes(`-${sn}-`))
|
|
268
|
+
found.add(s, q.file, q.line, q.text);
|
|
269
|
+
}
|
|
270
|
+
const out = [];
|
|
271
|
+
for (const [name, evidence] of found.by) {
|
|
272
|
+
if (!evidence.length)
|
|
273
|
+
continue;
|
|
274
|
+
if (opts.self.some((s) => norm(s) === name))
|
|
275
|
+
continue;
|
|
276
|
+
const candidate_paths = opts.siblings.filter((p) => nameMatches(name, basename(p)));
|
|
277
|
+
out.push({ name, evidence, candidate_paths });
|
|
278
|
+
}
|
|
279
|
+
// Names with a folder first, then by how much points at them.
|
|
280
|
+
return out.sort((a, b) => Number(!a.candidate_paths.length) - Number(!b.candidate_paths.length) || b.evidence.length - a.evidence.length || a.name.localeCompare(b.name));
|
|
281
|
+
}
|
|
282
|
+
/** The folders that hold the project's sources. */
|
|
283
|
+
export const parentsOf = (paths) => [...new Set(paths.map((p) => dirname(resolve(p))))];
|
|
@@ -0,0 +1,254 @@
|
|
|
1
|
+
import { readSettings, writeSettings } from "./approvals.js";
|
|
2
|
+
import { fillTemplate, findDocType, loadDocTypes, slugOf } from "./doctypes.js";
|
|
3
|
+
import { UserError } from "./errors.js";
|
|
4
|
+
import { withTitle } from "./heading.js";
|
|
5
|
+
import { createChange, findChange } from "./openspec.js";
|
|
6
|
+
import { NotFoundError } from "./storage.js";
|
|
7
|
+
export const DEFAULT_FOLDERS = [
|
|
8
|
+
{ id: "specs", name: "Specs" },
|
|
9
|
+
{ id: "docs", name: "Docs" },
|
|
10
|
+
{ id: "diagrams", name: "Diagrams" },
|
|
11
|
+
{ id: "logic", name: "Logic" },
|
|
12
|
+
{ id: "software", name: "Software" },
|
|
13
|
+
{ id: "cloud", name: "Cloud" },
|
|
14
|
+
];
|
|
15
|
+
/** In the OpenSpec format, the Project panel's first two folders: openspec/specs and openspec/changes. */
|
|
16
|
+
export const OPENSPEC_FOLDERS = [
|
|
17
|
+
{ id: "openspec-specs", name: "Specs", fixed: true },
|
|
18
|
+
{ id: "openspec-changes", name: "Changes", fixed: true },
|
|
19
|
+
];
|
|
20
|
+
const isFixed = (id) => OPENSPEC_FOLDERS.some((f) => f.id === id);
|
|
21
|
+
/** The OpenSpec format's one doc type: a change (proposal, tasks and a delta spec). */
|
|
22
|
+
export const CHANGE_TYPE = { id: "change", name: "Change", description: "An OpenSpec change: proposal, tasks and a delta spec.", icon: "git-pull-request" };
|
|
23
|
+
/** Where an openspec/ file goes in the OpenSpec format, and what it is called (null: not a spec or a change file). */
|
|
24
|
+
export function openspecPlace(path) {
|
|
25
|
+
const spec = /^openspec\/specs\/([^/]+)\/spec\.md$/.exec(path);
|
|
26
|
+
if (spec)
|
|
27
|
+
return { folder: "openspec-specs", name: spec[1] };
|
|
28
|
+
const ch = /^openspec\/changes\/(?!archive\/)([^/]+)\/(.+)\.md$/.exec(path);
|
|
29
|
+
if (!ch)
|
|
30
|
+
return null;
|
|
31
|
+
const file = ch[2];
|
|
32
|
+
const delta = /^specs\/([^/]+)\/spec$/.exec(file);
|
|
33
|
+
return { folder: "openspec-changes", group: ch[1], name: delta ? `spec: ${delta[1]}` : file };
|
|
34
|
+
}
|
|
35
|
+
export const itemKey = (kind, ref) => `${kind}:${ref}`;
|
|
36
|
+
const foldersOf = (s) => (s.tree?.folders?.length ? s.tree.folders : [...DEFAULT_FOLDERS]);
|
|
37
|
+
/** A doc's title: its first `# heading`, else its file name. */
|
|
38
|
+
export function docTitle(path, text) {
|
|
39
|
+
const h = text ? /^#\s+(.+?)\s*#*\s*$/m.exec(text) : null;
|
|
40
|
+
return h?.[1]?.trim() || path.split("/").pop().replace(/\.md$/i, "");
|
|
41
|
+
}
|
|
42
|
+
/** The doc type whose folder a path is in (the deepest match). */
|
|
43
|
+
export function docTypeOf(path, types) {
|
|
44
|
+
return [...types].sort((a, b) => b.folder.length - a.folder.length).find((t) => path.startsWith(`${t.folder}/`) && t.id !== "plain");
|
|
45
|
+
}
|
|
46
|
+
function defaultFolder(item, view, folders) {
|
|
47
|
+
const has = (id) => folders.some((f) => f.id === id);
|
|
48
|
+
const pick = (...ids) => ids.find(has) ?? folders[0]?.id ?? "docs";
|
|
49
|
+
if (item.kind === "view")
|
|
50
|
+
return view?.kind === "sequence" ? pick("logic", "diagrams") : pick("diagrams");
|
|
51
|
+
if (item.doc_type || item.ref.startsWith("specs/") || item.ref.startsWith("openspec/specs/") || item.ref.startsWith("openspec/changes/"))
|
|
52
|
+
return pick("specs", "docs");
|
|
53
|
+
return pick("docs");
|
|
54
|
+
}
|
|
55
|
+
/** Docs the Project panel lists: the project's own .md files (not its reports). */
|
|
56
|
+
const listed = (path) => !path.startsWith("reports/") && !path.startsWith(".spatial/");
|
|
57
|
+
/** Is a doc one the project's spec format lists? OpenSpec specs and changes only in OpenSpec; PRDs, MDDs and Gaps only in Spatial Views Spec. */
|
|
58
|
+
export function inFormat(path, format, types) {
|
|
59
|
+
const openspec = /^openspec\/(specs|changes)\//.test(path);
|
|
60
|
+
if (format === "openspec")
|
|
61
|
+
return openspec || !["prd", "mdd", "gap"].includes(docTypeOf(path, types)?.id ?? "");
|
|
62
|
+
return !openspec;
|
|
63
|
+
}
|
|
64
|
+
export async function projectTree(spatial) {
|
|
65
|
+
const settings = await readSettings(spatial);
|
|
66
|
+
const format = settings.spec_format;
|
|
67
|
+
const own = foldersOf(settings);
|
|
68
|
+
// OpenSpec: its Specs and Changes come first and replace the Specs folder.
|
|
69
|
+
const folders = format === "openspec" ? [...OPENSPEC_FOLDERS, ...own.filter((f) => f.id !== "specs")] : own;
|
|
70
|
+
const place = settings.tree?.place ?? {};
|
|
71
|
+
const types = await loadDocTypes(spatial.dataDir);
|
|
72
|
+
const where = (item, view) => {
|
|
73
|
+
const f = place[itemKey(item.kind, item.ref)];
|
|
74
|
+
return f && folders.some((x) => x.id === f) ? f : defaultFolder(item, view, folders);
|
|
75
|
+
};
|
|
76
|
+
const items = [];
|
|
77
|
+
const docs = (await spatial.storage.listIds("doc", spatial.ref.project)).filter(listed);
|
|
78
|
+
for (const path of docs) {
|
|
79
|
+
// The spec format filters the specs: Spatial Views Spec lists PRDs, MDDs and Gaps, OpenSpec its Specs and Changes.
|
|
80
|
+
if (!inFormat(path, format, types))
|
|
81
|
+
continue;
|
|
82
|
+
// OpenSpec files: specs by capability, a change's files under the change; archived changes are left out.
|
|
83
|
+
if (path.startsWith("openspec/changes/archive/"))
|
|
84
|
+
continue;
|
|
85
|
+
const at = path.startsWith("openspec/") ? openspecPlace(path) : null;
|
|
86
|
+
if (at && format === "openspec") {
|
|
87
|
+
items.push({ kind: "doc", ref: path, ...at });
|
|
88
|
+
continue;
|
|
89
|
+
}
|
|
90
|
+
if (at) {
|
|
91
|
+
const base = { kind: "doc", ref: path, name: at.name, ...(at.group ? { group: at.group } : {}) };
|
|
92
|
+
items.push({ ...base, folder: where(base) });
|
|
93
|
+
continue;
|
|
94
|
+
}
|
|
95
|
+
// OpenSpec: PRDs, MDDs and Gap docs are plain docs there.
|
|
96
|
+
const t = format === "openspec" ? undefined : docTypeOf(path, types);
|
|
97
|
+
const base = { kind: "doc", ref: path, name: docTitle(path, await spatial.docs.liveRead(path).catch(() => null)), ...(t ? { doc_type: t.id } : {}) };
|
|
98
|
+
items.push({ ...base, folder: where(base) });
|
|
99
|
+
}
|
|
100
|
+
const views = await spatial.views.listViews();
|
|
101
|
+
for (const v of views) {
|
|
102
|
+
const links = [...new Set(v.modules.flatMap((m) => (m.linked_view_id && m.linked_view_id !== v.id ? [m.linked_view_id] : [])))];
|
|
103
|
+
const base = { kind: "view", ref: v.id, name: v.name, view_type: v.type, ...(links.length ? { links } : {}) };
|
|
104
|
+
items.push({ ...base, folder: where(base, v) });
|
|
105
|
+
}
|
|
106
|
+
// Deleted: has versions, isn't live.
|
|
107
|
+
const deleted = [];
|
|
108
|
+
const liveDocs = new Set(docs);
|
|
109
|
+
for (const path of await spatial.docs.history.objects()) {
|
|
110
|
+
if (liveDocs.has(path) || !listed(path) || !inFormat(path, format, types))
|
|
111
|
+
continue;
|
|
112
|
+
const last = await spatial.docs.history.latest(path);
|
|
113
|
+
const base = { kind: "doc", ref: path, name: docTitle(path, last?.data ?? null) };
|
|
114
|
+
deleted.push({ ...base, folder: where(base) });
|
|
115
|
+
}
|
|
116
|
+
const liveViews = new Set(views.map((v) => v.id));
|
|
117
|
+
for (const id of await spatial.views.versions.objects()) {
|
|
118
|
+
if (liveViews.has(id))
|
|
119
|
+
continue;
|
|
120
|
+
const last = await spatial.views.versions.latest(id);
|
|
121
|
+
const base = { kind: "view", ref: id, name: last?.data.name ?? id };
|
|
122
|
+
deleted.push({ ...base, folder: where(base, last?.data) });
|
|
123
|
+
}
|
|
124
|
+
// A change's files stay together under it.
|
|
125
|
+
const byName = (a, b) => (a.group ?? "").localeCompare(b.group ?? "") || a.name.localeCompare(b.name);
|
|
126
|
+
return {
|
|
127
|
+
spec_format: format,
|
|
128
|
+
folders,
|
|
129
|
+
items: items.sort(byName),
|
|
130
|
+
deleted: deleted.sort(byName),
|
|
131
|
+
doc_types: format === "openspec" ? [CHANGE_TYPE] : types.map(({ id, name, description, icon }) => ({ id, name, icon, ...(description ? { description } : {}) })),
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
/** Add or rename folders (the whole list, in order). A folder that still holds items can't be dropped. */
|
|
135
|
+
export async function saveFolders(spatial, all) {
|
|
136
|
+
// OpenSpec's Specs and Changes are not the project's own folders.
|
|
137
|
+
const next = all.filter((f) => !isFixed(f.id));
|
|
138
|
+
const ids = new Set();
|
|
139
|
+
for (const f of next) {
|
|
140
|
+
if (!f.name.trim())
|
|
141
|
+
throw new UserError("a folder needs a name");
|
|
142
|
+
if (ids.has(f.id))
|
|
143
|
+
throw new UserError(`two folders with the id ${f.id}`);
|
|
144
|
+
ids.add(f.id);
|
|
145
|
+
}
|
|
146
|
+
const byId = new Map(next.map((f) => [f.id, f]));
|
|
147
|
+
for (const f of next) {
|
|
148
|
+
if (!f.parent)
|
|
149
|
+
continue;
|
|
150
|
+
if (isFixed(f.parent))
|
|
151
|
+
throw new UserError("Specs and Changes follow openspec/: they can't hold folders");
|
|
152
|
+
if (!byId.has(f.parent))
|
|
153
|
+
throw new UserError(`${f.name} is in a folder that isn't there: ${f.parent}`);
|
|
154
|
+
// Never inside itself, however deep.
|
|
155
|
+
for (let at = f.parent, n = 0; at; at = byId.get(at)?.parent, n++)
|
|
156
|
+
if (at === f.id || n > next.length)
|
|
157
|
+
throw new UserError(`${f.name} can't go inside itself`);
|
|
158
|
+
}
|
|
159
|
+
const tree = await projectTree(spatial);
|
|
160
|
+
const dropped = tree.folders.filter((f) => !f.fixed && !ids.has(f.id));
|
|
161
|
+
for (const f of dropped)
|
|
162
|
+
if (tree.items.some((i) => i.folder === f.id))
|
|
163
|
+
throw new UserError(`${f.name} isn't empty: move or delete what's in it first`);
|
|
164
|
+
const s = await readSettings(spatial);
|
|
165
|
+
const folders = next.map((f) => ({ id: f.id, name: f.name.trim(), ...(f.parent ? { parent: f.parent } : {}) }));
|
|
166
|
+
await writeSettings(spatial, { tree: { ...s.tree, folders } });
|
|
167
|
+
return folders;
|
|
168
|
+
}
|
|
169
|
+
/** A new folder's id from its name (unique among the folders). */
|
|
170
|
+
export function folderId(name, folders) {
|
|
171
|
+
const base = slugOf(name);
|
|
172
|
+
let id = base;
|
|
173
|
+
for (let n = 2; folders.some((f) => f.id === id); n++)
|
|
174
|
+
id = `${base}-${n}`;
|
|
175
|
+
return id;
|
|
176
|
+
}
|
|
177
|
+
/** Put an item in a folder. */
|
|
178
|
+
export async function placeItem(spatial, kind, ref, folder) {
|
|
179
|
+
const s = await readSettings(spatial);
|
|
180
|
+
if (isFixed(folder))
|
|
181
|
+
throw new UserError("Specs and Changes follow openspec/: add a change instead");
|
|
182
|
+
if (!foldersOf(s).some((f) => f.id === folder))
|
|
183
|
+
throw new NotFoundError(`no folder ${folder}`);
|
|
184
|
+
await writeSettings(spatial, { tree: { ...s.tree, place: { ...(s.tree?.place ?? {}), [itemKey(kind, ref)]: folder } } });
|
|
185
|
+
}
|
|
186
|
+
/** A path not taken yet: `<base>.md`, else `<base>-2.md`, `-3`... */
|
|
187
|
+
async function freePath(spatial, base) {
|
|
188
|
+
let rel = `${base}.md`;
|
|
189
|
+
for (let n = 2; (await spatial.docs.liveRead(rel)) !== null; n++)
|
|
190
|
+
rel = `${base}-${n}.md`;
|
|
191
|
+
return rel;
|
|
192
|
+
}
|
|
193
|
+
/** A new doc from a doc type's template, in the type's folder; optionally placed in a Project folder. */
|
|
194
|
+
export async function newDoc(spatial, input) {
|
|
195
|
+
if (input.type === CHANGE_TYPE.id || (await readSettings(spatial)).spec_format === "openspec")
|
|
196
|
+
return newChange(spatial, input.title);
|
|
197
|
+
const type = findDocType(await loadDocTypes(spatial.dataDir), input.type);
|
|
198
|
+
const title = input.title?.trim() || type.title;
|
|
199
|
+
const path = await freePath(spatial, `${type.folder}/${slugOf(title)}`);
|
|
200
|
+
const text = fillTemplate(type.template, { title });
|
|
201
|
+
await spatial.docs.write(path, text.endsWith("\n") ? text : `${text}\n`);
|
|
202
|
+
if (input.folder)
|
|
203
|
+
await placeItem(spatial, "doc", path, input.folder);
|
|
204
|
+
return { kind: "doc", ref: path, name: title, folder: input.folder ?? "", ...(type.id !== "plain" ? { doc_type: type.id } : {}) };
|
|
205
|
+
}
|
|
206
|
+
/**
|
|
207
|
+
* A new OpenSpec change, in the layout OpenSpec's own commands read:
|
|
208
|
+
* openspec/changes/<id>/proposal.md, tasks.md and specs/<id>/spec.md (a delta
|
|
209
|
+
* spec with an ADDED requirement to fill in). Returns its proposal.
|
|
210
|
+
*/
|
|
211
|
+
export async function newChange(spatial, title) {
|
|
212
|
+
const name = title?.trim() || "New change";
|
|
213
|
+
const base = slugOf(name);
|
|
214
|
+
let id = base;
|
|
215
|
+
for (let n = 2; (await findChange(spatial.root, id)) || (await spatial.docs.liveRead(`openspec/changes/${id}/proposal.md`)) !== null; n++)
|
|
216
|
+
id = `${base}-${n}`;
|
|
217
|
+
const write = (rel, text) => spatial.docs.write(rel, text);
|
|
218
|
+
const rel = await createChange(spatial.root, { id, title: name, intent: "", view: "", modules: [] }, write);
|
|
219
|
+
await write(`${rel}/specs/${id}/spec.md`, `## ADDED Requirements\n\n### Requirement: ${name}\nThe system SHALL ...\n\n#### Scenario: ...\n- **WHEN** ...\n- **THEN** ...\n`);
|
|
220
|
+
return { kind: "doc", ref: `${rel}/proposal.md`, name: "proposal", folder: "openspec-changes", group: id };
|
|
221
|
+
}
|
|
222
|
+
/** A new, empty flow View (a diagram); optionally placed in a Project folder. */
|
|
223
|
+
export async function newView(spatial, input) {
|
|
224
|
+
const name = input.name?.trim() || "New diagram";
|
|
225
|
+
const base = slugOf(name);
|
|
226
|
+
let id = base;
|
|
227
|
+
for (let n = 2; await spatial.views.has(id); n++)
|
|
228
|
+
id = `${base}-${n}`;
|
|
229
|
+
const view = await spatial.views.save({ id, name, type: "sub-view", modules: [], connections: [] }, { note: "created" });
|
|
230
|
+
if (input.folder)
|
|
231
|
+
await placeItem(spatial, "view", id, input.folder);
|
|
232
|
+
return view;
|
|
233
|
+
}
|
|
234
|
+
/** Rename a doc: its first `# heading` (added when it has none). The path stays, so pins still hold. */
|
|
235
|
+
export async function renameDoc(spatial, path, title) {
|
|
236
|
+
const name = title.trim();
|
|
237
|
+
if (!name)
|
|
238
|
+
throw new UserError("a doc needs a name");
|
|
239
|
+
const text = await spatial.docs.liveRead(path);
|
|
240
|
+
if (text === null)
|
|
241
|
+
throw new NotFoundError(`no doc ${path}`);
|
|
242
|
+
return spatial.docs.write(path, withTitle(text, name));
|
|
243
|
+
}
|
|
244
|
+
/** Restore a deleted doc or View from its newest version. */
|
|
245
|
+
export async function restoreItem(spatial, kind, ref) {
|
|
246
|
+
const versions = kind === "doc" ? spatial.docs.history : spatial.views.versions;
|
|
247
|
+
const last = await versions.latest(kind === "doc" ? spatial.docs.rel(ref) : ref);
|
|
248
|
+
if (!last)
|
|
249
|
+
throw new NotFoundError(`no versions of ${ref}`);
|
|
250
|
+
if (kind === "doc")
|
|
251
|
+
await spatial.docs.restore(ref, last.n);
|
|
252
|
+
else
|
|
253
|
+
await spatial.views.restore(ref, last.n);
|
|
254
|
+
}
|