@trops/dash-check 0.0.0-stage → 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.
Files changed (104) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +355 -2
  3. package/cli/bin.js +12 -0
  4. package/cli/index.d.ts +7 -0
  5. package/cli/index.js +566 -0
  6. package/examples/changes-match-plan.bad.json +4 -0
  7. package/examples/changes-match-plan.check.js +18 -0
  8. package/examples/changes-match-plan.sample.json +4 -0
  9. package/examples/commits-scoped.bad.json +5 -0
  10. package/examples/commits-scoped.check.json +6 -0
  11. package/examples/commits-scoped.sample.json +5 -0
  12. package/examples/every-acceptance-has-a-test.bad.json +3 -0
  13. package/examples/every-acceptance-has-a-test.check.js +19 -0
  14. package/examples/every-acceptance-has-a-test.sample.json +3 -0
  15. package/examples/helpers/changelog.js +23 -0
  16. package/examples/jira-comment-from-job.bad.json +5 -0
  17. package/examples/jira-comment-from-job.check.js +10 -0
  18. package/examples/jira-comment-from-job.sample.json +5 -0
  19. package/examples/lockfile-explained.bad.json +11 -0
  20. package/examples/lockfile-explained.check.json +6 -0
  21. package/examples/lockfile-explained.sample.json +11 -0
  22. package/examples/max-growth.bad.json +8 -0
  23. package/examples/max-growth.check.json +6 -0
  24. package/examples/max-growth.sample.json +8 -0
  25. package/examples/merge-outcome.bad.json +4 -0
  26. package/examples/merge-outcome.check.js +9 -0
  27. package/examples/merge-outcome.sample.json +4 -0
  28. package/examples/new-tests-are-run.bad.json +4 -0
  29. package/examples/new-tests-are-run.check.json +7 -0
  30. package/examples/new-tests-are-run.sample.json +4 -0
  31. package/examples/no-new-debt.bad.json +8 -0
  32. package/examples/no-new-debt.check.json +14 -0
  33. package/examples/no-new-debt.sample.json +8 -0
  34. package/examples/on-branch.bad.json +6 -0
  35. package/examples/on-branch.check.js +12 -0
  36. package/examples/on-branch.sample.json +6 -0
  37. package/examples/plan-complete.bad.json +5 -0
  38. package/examples/plan-complete.check.js +14 -0
  39. package/examples/plan-complete.sample.json +5 -0
  40. package/examples/plan-scope.bad.json +4 -0
  41. package/examples/plan-scope.check.json +6 -0
  42. package/examples/plan-scope.sample.json +4 -0
  43. package/examples/pr-ci.bad.json +4 -0
  44. package/examples/pr-ci.check.js +12 -0
  45. package/examples/pr-ci.sample.json +4 -0
  46. package/examples/ready-for-review.bad.json +6 -0
  47. package/examples/ready-for-review.check.json +12 -0
  48. package/examples/ready-for-review.sample.json +6 -0
  49. package/examples/setup.bad.json +6 -0
  50. package/examples/setup.check.js +8 -0
  51. package/examples/setup.sample.json +6 -0
  52. package/examples/source-has-tests.bad.json +4 -0
  53. package/examples/source-has-tests.check.json +6 -0
  54. package/examples/source-has-tests.sample.json +4 -0
  55. package/examples/test-script-is-real.bad.json +6 -0
  56. package/examples/test-script-is-real.check.json +6 -0
  57. package/examples/test-script-is-real.sample.json +6 -0
  58. package/examples/ticket-has-acceptance.bad.json +4 -0
  59. package/examples/ticket-has-acceptance.check.js +10 -0
  60. package/examples/ticket-has-acceptance.sample.json +4 -0
  61. package/examples/validate-leaves-tree-clean.bad.json +6 -0
  62. package/examples/validate-leaves-tree-clean.check.json +7 -0
  63. package/examples/validate-leaves-tree-clean.sample.json +6 -0
  64. package/examples/validate.bad.json +6 -0
  65. package/examples/validate.check.js +14 -0
  66. package/examples/validate.sample.json +6 -0
  67. package/examples/work-committed.bad.json +13 -0
  68. package/examples/work-committed.check.js +13 -0
  69. package/examples/work-committed.sample.json +5 -0
  70. package/examples/workspace-cleaned.bad.json +4 -0
  71. package/examples/workspace-cleaned.check.js +7 -0
  72. package/examples/workspace-cleaned.sample.json +4 -0
  73. package/helpers/commands.js +62 -0
  74. package/helpers/files.js +124 -0
  75. package/helpers/git.js +379 -0
  76. package/helpers/github.js +77 -0
  77. package/helpers/handoff.js +80 -0
  78. package/helpers/index.d.ts +103 -0
  79. package/helpers/index.js +36 -0
  80. package/helpers/node.js +296 -0
  81. package/helpers/plan.js +55 -0
  82. package/helpers/tracker.js +60 -0
  83. package/index.d.ts +347 -0
  84. package/index.js +86 -0
  85. package/lib/adapters/fs.js +85 -0
  86. package/lib/adapters/git.js +163 -0
  87. package/lib/adapters/github.js +151 -0
  88. package/lib/adapters/run.js +76 -0
  89. package/lib/adapters/tool.js +64 -0
  90. package/lib/context.js +102 -0
  91. package/lib/discover.js +102 -0
  92. package/lib/expect.js +238 -0
  93. package/lib/glob.js +89 -0
  94. package/lib/handoff.js +68 -0
  95. package/lib/helper.js +236 -0
  96. package/lib/register.js +35 -0
  97. package/lib/runner.js +214 -0
  98. package/lib/scope.js +107 -0
  99. package/lib/util.js +122 -0
  100. package/lib/values.js +138 -0
  101. package/package.json +59 -4
  102. package/recipe/index.d.ts +70 -0
  103. package/recipe/index.js +381 -0
  104. package/recipe/schema.json +55 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 trops
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,356 @@
1
- # Temporary Holding Version
1
+ # @trops/dash-check
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Write checks that prove a bot (or a person) did the job: tests pass, the work is committed, every acceptance item has a test, the PR's CI is green. If you know Jest, you already know most of it.
4
+
5
+ - **Deterministic.** No AI. The same input gives the same result, and a failure says exactly what to fix.
6
+ - **Made of building blocks.** Pick a ready-made helper, compose your own check in a file or as JSON, or write your own helper once and reuse it.
7
+ - **Runs anywhere Node runs:** in [Dash](https://github.com/trops/dash-electron), in CI and on your laptop.
8
+ - **No dependencies.** Node 18 or later.
9
+
10
+ ## Install
11
+
12
+ ```sh
13
+ npm install --save-dev @trops/dash-check
14
+ ```
15
+
16
+ ## Your first check in 2 minutes
17
+
18
+ **1. Make one.** In your repo:
19
+
20
+ ```sh
21
+ npx dash-check new ready
22
+ ```
23
+
24
+ That writes two files:
25
+
26
+ - `.dash/checks/ready.check.js`, the check;
27
+ - `.dash/checks/ready.sample.json`, a sample input to try it on.
28
+
29
+ **2. Read it.** It looks like a Jest test:
30
+
31
+ ```js
32
+ const { check, expect } = require("@trops/dash-check");
33
+ const { handoff } = require("@trops/dash-check/helpers");
34
+
35
+ check("ready", async (ctx) => {
36
+ await handoff.has(ctx, { keys: ["summary"] });
37
+ expect(ctx.handoff.files, "handoff.files").toHaveLengthAtLeast(1, "The handoff lists no changed files");
38
+ });
39
+ ```
40
+
41
+ `ctx` holds everything a check can look at (the bot's handoff, the work item, git, the files…). The last argument of a matcher, `"The handoff lists no changed files"`, is what the failure says. Write it as the fix.
42
+
43
+ **3. Run it.**
44
+
45
+ ```sh
46
+ npx dash-check ready
47
+ # ✓ ready: The handoff has summary
48
+ ```
49
+
50
+ **4. Make it fail.** Edit the sample so `"files": []` and run it again:
51
+
52
+ ```
53
+ ✗ ready
54
+ ● The handoff lists no changed files
55
+ ✗ ready: The handoff lists no changed files
56
+ ```
57
+
58
+ A good check passes on good input **and** fails on bad input with a reason that says what to fix. Always try both.
59
+
60
+ ## How a check runs
61
+
62
+ A check gets an **input**: Dash's check input, or a sample file. The input is JSON with the bot's answer, the work item, the workspace and the team's settings:
63
+
64
+ ```json
65
+ {
66
+ "input": { "answer": "Done.\n\n```json\n{ \"files\": [\"src/index.js\"] }\n```" },
67
+ "item": { "id": "DASH-97", "title": "Title-case board names" },
68
+ "workspace": { "dir": ".", "branch": "feat/DASH-97-titlecase" },
69
+ "row": { "repo": { "baseBranch": "main" } },
70
+ "settings": { "mergeWhenGreen": true }
71
+ }
72
+ ```
73
+
74
+ It gives a **result**:
75
+
76
+ ```js
77
+ { pass: false,
78
+ reason: "The handoff lists no changed files",
79
+ checks: [{ name: "ready", pass: false, reason: "…", failures: [{ message, at?, expected, received }] }] }
80
+ ```
81
+
82
+ Run as a file, it prints a report, then one last line of JSON, `{"reason": "…", "details": {…}}`. That's Dash's check contract: exit 0 passes, and the last line says why.
83
+
84
+ Every failing `expect` is **collected**, and the check goes on, so a check lists *all* the gaps, not just the first. A matcher returns `true` or `false`, so you can guard code that needs the value:
85
+
86
+ ```js
87
+ if (expect(handoff.tests, "handoff.tests").toBeArray()) {
88
+ for (const t of handoff.tests) expect(t.file, `${t.acceptance}'s file`).toBeNonEmptyString(`No file for: ${t.acceptance}`);
89
+ }
90
+ ```
91
+
92
+ ## The layers
93
+
94
+ dash-check has three layers. Each is built only from the one below it, so anything a helper or a recipe does, you can do in code.
95
+
96
+ ### Layer 0: primitives (`@trops/dash-check`)
97
+
98
+ **The runner.** `check(name, fn)`, `check.skip`, `check.only` and `describe(name, fn)`. `fn` gets `ctx` and may be async.
99
+
100
+ **`expect(value, label?)`.** `label` names the value in a failure. Every matcher takes an optional last argument, `message`, which is the fix shown on failure:
101
+
102
+ | Kind | Matchers |
103
+ |---|---|
104
+ | Equality and type | `toBe`, `toEqual`, `toBeDefined`, `toBeTruthy`, `toBeArray`, `toBeString`, `toBeNonEmptyString`, `toBeNumber` |
105
+ | Size | `toBeEmpty`, `toHaveLength(n)`, `toHaveLengthAtLeast(n)` |
106
+ | Membership | `toContain(item)`, `toBeOneOf(list)`, `toHaveKeys(keys)` |
107
+ | Text and numbers | `toMatch(regex or string)`, `toBeGreaterThan(n)`, `toBeLessThan(n)` |
108
+ | Modifiers | `.not`, `.resolves`, `.rejects` (`await expect(p).resolves.toBe(1)`) |
109
+
110
+ You can also call `fail(message, { at })` to fail with a message and a location, and `note(text)` to say how a check passed.
111
+
112
+ **The context (`ctx`).**
113
+
114
+ | Reader | What it gives |
115
+ |---|---|
116
+ | `ctx.input` | The raw check input (Dash's `$DASH_INPUT`) |
117
+ | `ctx.answer` | The bot's whole answer |
118
+ | `ctx.handoff` | The bot's handoff: its answer's last ```` ```json ```` block, parsed. `{}` when there's none |
119
+ | `ctx.item`, `ctx.picks`, `ctx.row`, `ctx.settings`, `ctx.step`, `ctx.job` | Dash's job data. Each is `{}` outside Dash |
120
+ | `ctx.workspace` | `{ dir, branch, exists(p), read(p), file(p), dirExists() }` |
121
+ | `ctx.env` | The environment, `DASH_*` included |
122
+ | `ctx.fs` | `exists(p)`, `read(p)`, `readJson(p)`, `list(glob)`, `dirExists(p)`, relative to the workspace |
123
+ | `ctx.run(cmd, { timeoutSec })` | Runs a command in the workspace: `{ code, stdout, stderr, timedOut }` |
124
+ | `ctx.git` | `branch()`, `isClean()`, `uncommitted()`, `commitsSince(ref)`, `changedFiles(ref)`, `addedLines(ref)`, `commits(ref)`, `lastCommitMessage()`, `remoteHas(branch)`, `defaultBranch()`, `fileAt(ref, path)` |
125
+ | `ctx.github` | `pr(urlOrNumber)`, `checksFor(sha)`, `waitForChecks(sha, { timeoutSec, noChecksAfterSec })`, using `GH_TOKEN`. A failure says the HTTP code and what to do |
126
+ | `ctx.tool(provider, name, args)` | A read-only call to one of the bot's providers (Jira, …). Only Dash can answer it, so outside Dash it fails and says so |
127
+ | `ctx.get(path)`, `ctx.fill(text)`, `ctx.when(expr)` | Read a value by path (`"handoff.files"`), fill `"{placeholders}"`, test a `when` |
128
+
129
+ `changedFiles(ref)` and `addedLines(ref)` see the same work: what's committed since where the branch left `ref`, plus uncommitted and untracked files. `addedLines` gives `[{ file, line, text }]`, numbered as the file is now (an untracked file counts whole; binary files are left out), so a check can judge only what the work added. `commits(ref)` gives `[{ sha, subject, message, parents, files }]` for `ref..HEAD`, oldest first.
130
+
131
+ In a test, give the context what it can't have outside Dash:
132
+
133
+ ```js
134
+ const { createContext } = require("@trops/dash-check");
135
+ const ctx = createContext(sampleInput, { cwd: repoDir, fetch: fakeFetch, tool: fakeTool });
136
+ ```
137
+
138
+ ### Layer 1: helpers (`@trops/dash-check/helpers`)
139
+
140
+ A helper is a few lines of Layer 0 plus a description: a label, a sentence and typed params. Call one with `ctx` and its params:
141
+
142
+ ```js
143
+ const { check } = require("@trops/dash-check");
144
+ const { git, node, files } = require("@trops/dash-check/helpers");
145
+
146
+ check("the work is ready for review", async (ctx) => {
147
+ await git.committedSince(ctx, { base: "{repo.baseBranch}" });
148
+ await node.testsPass(ctx);
149
+ await files.noneMatch(ctx, { glob: "src/**/*.js", pattern: "console\\.log", message: "Remove console.log from src" });
150
+ });
151
+ ```
152
+
153
+ | Group | Helpers |
154
+ |---|---|
155
+ | handoff | `has({ keys })`, `status({ is })`, `eachHas({ list, keys })` |
156
+ | plan | `hasSections({ key, sections })`, `acceptanceEachHasTest({ acceptance, tests })` |
157
+ | git | `onBranch({ branch })`, `committedSince({ base, min })`, `clean()`, `changedOnly({ globs, base })`, `noChangesTo({ globs, base })`, and the factory checks below |
158
+ | files | `exist({ paths })`, `contain({ path, text })`, `noneMatch({ glob, pattern })`, `maxGrowth(…)` |
159
+ | commands | `passes({ command, timeoutSec })`, `outputMatches({ command, pattern })` |
160
+ | node | `testsPass({ command })`, `lintPasses({ command })`, `noNewDependencies({ base, allow })`, `lockfileExplained(…)`, `testScriptIsReal(…)`, `newTestsAreRun(…)` |
161
+ | github | `prOpen({ prKey })`, `ciPassed({ prKey, timeoutSec, noChecksAfterSec, ifNoChecks })` |
162
+ | tracker | `ticketExists({ provider, tool, idArg, id })`, `ticketHasLabel({ label, … })` (these need Dash's `ctx.tool`) |
163
+
164
+ `npx dash-check --list-helpers` lists each one with its params and defaults.
165
+
166
+ A few things every helper does for you:
167
+
168
+ - **Placeholders.** A param can hold `{placeholders}`: `{repo.baseBranch}` is the picked row's `baseBranch`, `{workRoot}` is a team setting, and `{item.id}` is the work item's id. A default whose placeholder has no value counts as empty: with no `repo` row, `git.committedSince` uses the repo's default branch.
169
+ - **`message`.** Every helper takes `message`, which replaces its failure text. Where the failure was (`src/index.js:12`) is kept.
170
+ - **Errors become failures.** If a helper throws (the folder isn't a git repo, GitHub says 404), the failure says why, and the check goes on to the next step.
171
+
172
+ ### Factory checks: did the bot do only what it planned?
173
+
174
+ Checks for a code factory, where a planning bot names the files and a coding bot changes them. Each works on any repo: all it needs is git and, for the plan checks, a handoff that lists files. Every one compares with `base` (default `{repo.baseBranch}`; empty means the repo's default branch, through `origin/` when the remote has it), and every one judges only **what the work changed**, so existing debt in the repo doesn't fail it.
175
+
176
+ | Helper | Fails when | Params (defaults) |
177
+ |---|---|---|
178
+ | `git.changesMatchPlan` | A changed file isn't in the plan. A lockfile outside the plan passes only when `node.lockfileExplained` would | `planKeys` (`files, newFiles, tests`), `alsoAllow`, `lockfiles` (`package-lock.json, npm-shrinkwrap.json, yarn.lock, pnpm-lock.yaml`), `base` |
179
+ | `git.commitsScoped` | A commit (not just the total diff) touches files outside the plan, or its message doesn't mention the item. Merge commits are skipped | the same, plus `mentions` (`{item.id}`). `planKeys: ""` or `mentions: ""` turns that half off |
180
+ | `node.lockfileExplained` | The lockfile changed, but no dependency section (`dependencies`, `devDependencies`, `optionalDependencies`, `peerDependencies`) of `package.json` did. Changing only the root `version` fields (a release) is fine. `yarn.lock` and `pnpm-lock.yaml` need a dependency change for any change | `lockfile` (`package-lock.json`), `manifest` (the `package.json` next to it, plus any changed `package.json` below it, for workspaces), `base` |
181
+ | `git.sourceHasTests` | A changed source file has no changed test: its own (`paired`), or any (`any`) | `sources` (`src/**` and `lib/**` JS/TS, not tests, stories or `.d.ts`), `tests`, `testPattern` (`<dir>/<name>.{test,spec}.*`, `<dir>/__tests__/<name>.*`, `{test,tests}/**/<name>.{test,spec}.*`, `{test,tests}/**/<name>.*`), `mode` (`paired`), `exempt` |
182
+ | `git.noNewMatches` | An **added** line matches a pattern. The diff-aware `files.noneMatch`: the 600 `console.log`s already there don't count, a new one does | `glob` (a list; `!glob` leaves out), `pattern`, `base` |
183
+ | `files.maxGrowth` | A changed file is new and over `maxLines`, crosses `maxLines`, or was already over it and grew by more than `maxAdded` | `glob` (JS/TS), `maxLines` (800), `maxAdded` (50), `exempt`, `base` |
184
+ | `git.validateLeavesTreeClean` | The tree isn't clean before validating, the command fails, or it leaves files changed (a formatter's `--write`, `npm install --save`) | `command` (`npm test`), `timeoutSec` (1800), `allowDirtyBefore` (false) |
185
+ | `node.testScriptIsReal` | The `test` script is missing, empty, a no-op (`exit 0`, `true`, `echo …`, npm's placeholder) or hides its failures (`… \|\| true`). With `run`, also when it reports fewer than `minTests` tests | `script` (`test`), `run` (false), `minTests` (1), `timeoutSec` (900) |
186
+ | `node.newTestsAreRun` | A **new** test file isn't named in a runner file, isn't matched by a glob written in one (`node --test 'test/*.test.js'`), and isn't in `autoGlobs` | `tests` (`**/*.{test,spec}.{js,cjs,mjs,jsx,ts,tsx}`), `runnerFiles` (`package.json`), `autoGlobs` (what the runner finds by itself, e.g. `src/**/*.test.js` for jest), `base` |
187
+
188
+ A coding bot's step might run them in this order:
189
+
190
+ ```json
191
+ {
192
+ "name": "The Coder did only what it planned",
193
+ "steps": [
194
+ { "use": "git.validateLeavesTreeClean", "with": { "command": "npm run ci" } },
195
+ { "use": "git.changesMatchPlan", "with": { "alsoAllow": "CHANGELOG.md" } },
196
+ { "use": "git.commitsScoped" },
197
+ { "use": "git.noChangesTo", "with": { "globs": ".github/workflows/**, .dash/checks/**" }, "label": "Gates unchanged" },
198
+ { "use": "git.sourceHasTests", "with": { "exempt": "**/index.js" } },
199
+ { "use": "node.newTestsAreRun", "with": { "runnerFiles": "package.json, scripts/ci.sh" } },
200
+ { "use": "git.noNewMatches", "with": { "glob": "src/**, !**/*.test.*", "pattern": "console\\.log\\(|\\b(TODO|FIXME)\\b" },
201
+ "message": "Remove the console.log or TODO you added" },
202
+ { "use": "files.maxGrowth" }
203
+ ]
204
+ }
205
+ ```
206
+
207
+ ### Writing your own helper
208
+
209
+ Put a file in `.dash/checks/helpers/` in your repo. Build it only from `ctx` and `expect`, never from `fs`, `child_process` or `fetch` directly:
210
+
211
+ ```js
212
+ // .dash/checks/helpers/changelog.js
213
+ const { defineHelper, expect, note } = require("@trops/dash-check");
214
+
215
+ module.exports = defineHelper({
216
+ id: "team.changelogUpdated", // "group.name"
217
+ group: "Your helpers",
218
+ label: "Changelog updated",
219
+ description: "CHANGELOG.md changed on this branch",
220
+ sentence: "{file} changed since {base}",
221
+ params: {
222
+ file: { type: "path", label: "Changelog", default: "CHANGELOG.md" },
223
+ base: { type: "text", label: "Base branch", default: "{repo.baseBranch}" },
224
+ },
225
+ reads: ["git"],
226
+ async run(ctx, { file, base }) {
227
+ const ref = `origin/${base || (await ctx.git.defaultBranch()) || "main"}`;
228
+ const changed = await ctx.git.changedFiles(ref);
229
+ expect(changed, `files changed since ${ref}`).toContain(file, `${file} isn't updated: add a line for ${ctx.item.id || "this change"}`);
230
+ note(`${file} is updated`);
231
+ },
232
+ });
233
+ ```
234
+
235
+ Param types are `text`, `number`, `boolean`, `select` (with `options`), `path`, `glob`, `regex` and `list`. Values are converted to the type before `run` gets them: a `list` given as `"a, b"` arrives as `["a", "b"]` (a glob's `{a,b}` stays whole: `"**/*.{js,ts}, !dist/**"` is two globs), and a `regex` arrives as a `RegExp`.
236
+
237
+ Then use it from code with `helper("team.changelogUpdated")(ctx, { … })`, or from a recipe with `{ "use": "team.changelogUpdated" }`. It also shows in `dash-check --list-helpers`, and so in Dash's check composer.
238
+
239
+ **Sharing helpers in a package:** list them in its `package.json`:
240
+
241
+ ```json
242
+ { "name": "@acme/dash-checks", "dashCheck": { "helpers": ["./helpers/changelog.js"] } }
243
+ ```
244
+
245
+ Any repo that has the package in `dependencies` or `devDependencies` picks them up. If a helper file won't load, `--list-helpers` reports its error, and the other helpers still load.
246
+
247
+ ### Layer 2: recipes (`@trops/dash-check/recipe`)
248
+
249
+ A recipe is a check as JSON. It holds no code, so it's safe to store on a bot, share and edit in a UI. Save it as `.dash/checks/<name>.check.json`:
250
+
251
+ ```json
252
+ {
253
+ "name": "The work is ready for review",
254
+ "steps": [
255
+ { "use": "git.committedSince", "with": { "base": "{repo.baseBranch}", "min": 1 } },
256
+ { "use": "node.testsPass", "with": { "command": "npm test" } },
257
+ { "use": "files.noneMatch", "with": { "glob": "src/**/*.js", "pattern": "console\\.log" },
258
+ "message": "Remove console.log from src" },
259
+ { "expect": "handoff.files", "matcher": "toHaveLengthAtLeast", "args": [1],
260
+ "message": "The handoff lists no changed files" }
261
+ ]
262
+ }
263
+ ```
264
+
265
+ - `use` names a helper, and `with` holds its params.
266
+ - `expect` names a value by path (`handoff.files`, `item.id`, `env.DASH_BRANCH`, `repo.baseBranch`). Add a `matcher`, its `args`, and `"not": true` to expect the opposite.
267
+ - `when` skips the step unless a value is truthy: a path (`"handoff.prUrl"`) or a placeholder (`"{mergeWhenGreen}"`). A `!` in front turns it around.
268
+ - `message` replaces the step's failure text.
269
+ - `label` renames the row.
270
+
271
+ ```js
272
+ const { validateRecipe, runRecipe, recipeToCode, explain, schema } = require("@trops/dash-check/recipe");
273
+
274
+ validateRecipe(recipe); // { valid, errors: [{ path: "steps[1].with.min", message }] }
275
+ await runRecipe(recipe, input, { cwd, only: [0, 2] });
276
+ // { pass, reason, checks, steps: [{ index, label, sentence, pass, reason, failures }] }
277
+ recipeToCode(recipe); // the same check as a .check.js file ("View as code")
278
+ explain(recipe); // ["Work is committed: at least 1 commit(s) past {repo.baseBranch}, …", …]
279
+ ```
280
+
281
+ `recipeToCode` gives a file that produces the same result as the recipe. The tests check this.
282
+
283
+ ## The CLI
284
+
285
+ | Command | What it does |
286
+ |---|---|
287
+ | `dash-check` | Runs every `.check.js` and `.check.json` in `.dash/checks/`, plus plain executables, the way Dash does |
288
+ | `dash-check <name> --input sample.json` | Runs one check. With no `--input`, `<name>.sample.json` next to it is used |
289
+ | `dash-check --watch` | Reruns on every change |
290
+ | `dash-check --ci` | Prints a Jest-like report. A missing check counts as a failure |
291
+ | `dash-check --json` | Gives the results as JSON |
292
+ | `dash-check --list` | Lists the checks |
293
+ | `dash-check --list-helpers [--json] [--input f]` | Every helper and matcher with its metadata (the composer's palette). With `--input`, it also lists the value paths in that sample |
294
+ | `dash-check --explain <file>` | Prints a recipe as sentences, or a code file's check names |
295
+ | `dash-check new <name> [--recipe]` | Scaffolds a check and its sample input |
296
+
297
+ Options: `--cwd <dir>`, `--timeout <sec>` (2 minutes by default), and `--no-color`.
298
+
299
+ **Exit codes:** 0 when everything passed, 1 when a check failed, and 2 for bad arguments or a missing check.
300
+
301
+ A check gets the same contract as in Dash:
302
+
303
+ - `cwd` is the repo;
304
+ - the input is JSON in `$DASH_INPUT` and on stdin;
305
+ - the environment has `DASH_ITEM_*`, `DASH_WORKSPACE`, `DASH_BRANCH` and `DASH_STEP`.
306
+
307
+ A `.check.js` file runs in its own `node`. A `.check.json` recipe runs in the CLI. Any other file in `.dash/checks/` runs by its `#!` line, or else by its extension: `.js` with node, `.sh` with bash, `.py` with python3, and anything else with sh.
308
+
309
+ A `.check.js` file also runs by itself, `node .dash/checks/validate.check.js`, the way Dash runs a repo check.
310
+
311
+ **In CI**, add a step:
312
+
313
+ ```yaml
314
+ - run: npx dash-check --ci
315
+ ```
316
+
317
+ ## Use in Dash
318
+
319
+ - **A repo check.** Commit `.dash/checks/<name>.check.js` (or `.check.json`) in a PR. A bot step `{ "kind": "repo", "name": "<name>" }` runs it from the repo's **base branch**, so a bot working on a branch can't change the check that judges it.
320
+ - **What the check sees.** Dash passes the job's item, workspace, picked rows and settings in the input. Those are `ctx.item`, `ctx.workspace`, `ctx.row` and `ctx.settings`.
321
+ - **`ctx.tool`.** Dash answers `ctx.tool` through `DASH_TOOL_SOCKET`, a local socket that takes one JSON line `{ "id", "provider", "tool", "args" }` and answers `{ "id", "result" }` or `{ "id", "error" }`.
322
+ - **The composer.** Dash's composer lists the palette from `dash-check --list-helpers --json`. It runs a recipe on a sample with `runRecipe` (Try it), and shows it as code with `recipeToCode` (View as code).
323
+
324
+ ## Examples
325
+
326
+ [`examples/`](examples) holds the Code Factory's checks, written with dash-check. Each has a `.sample.json` that passes and a `.bad.json` that fails. The ones that look at a repo say in `_about` what the repo looks like (the tests build it):
327
+
328
+ | Example | Proves | Reads |
329
+ |---|---|---|
330
+ | `ticket-has-acceptance` | The ticket has acceptance items | handoff |
331
+ | `plan-complete` | The plan has a cause, fix and test, and picks a known repo | handoff, picks, settings |
332
+ | `on-branch` | The workspace is on the job's branch, fresh from the base | git, workspace, row |
333
+ | `setup` | `npm ci` passes | run |
334
+ | `every-acceptance-has-a-test` | Every acceptance item has a test; the plan names real files | handoff, workspace |
335
+ | `validate` | Lint and tests pass, reported apart | run |
336
+ | `work-committed` | The work is committed, and the commit names the item | git, item |
337
+ | `changes-match-plan` | Only the planned files changed; a lockfile only with `node.lockfileExplained` | git, fs, handoff, row |
338
+ | `pr-ci` | The PR's CI passed (no CI is a failure) | github |
339
+ | `merge-outcome` | The PR was merged only when allowed | github, settings |
340
+ | `workspace-cleaned` | The job's folder is gone | workspace |
341
+ | `jira-comment-from-job` | The ticket got this job's comment | tool, job |
342
+ | `ready-for-review.check.json` | The composer mockup's recipe | git, run, fs, handoff |
343
+ | `plan-scope.check.json` | `git.changesMatchPlan` | git, fs, handoff |
344
+ | `commits-scoped.check.json` | `git.commitsScoped` | git, handoff, item |
345
+ | `lockfile-explained.check.json` | `node.lockfileExplained` | git, fs |
346
+ | `source-has-tests.check.json` | `git.sourceHasTests`, its mode a team setting | git, fs, settings |
347
+ | `no-new-debt.check.json` | `git.noNewMatches`: no new console.log, TODO or eslint-disable | git |
348
+ | `max-growth.check.json` | `files.maxGrowth` | git, fs |
349
+ | `validate-leaves-tree-clean.check.json` | `git.validateLeavesTreeClean` | git, run |
350
+ | `test-script-is-real.check.json` | `node.testScriptIsReal` | fs, run |
351
+ | `new-tests-are-run.check.json` | `node.newTestsAreRun` | git, fs, settings |
352
+ | `helpers/changelog.js` | A team's own helper | git |
353
+
354
+ ## License
355
+
356
+ MIT © trops
package/cli/bin.js ADDED
@@ -0,0 +1,12 @@
1
+ #!/usr/bin/env node
2
+ "use strict";
3
+
4
+ require("./index")
5
+ .main(process.argv.slice(2))
6
+ .then((code) => {
7
+ process.exitCode = code;
8
+ })
9
+ .catch((e) => {
10
+ process.stderr.write(`dash-check broke: ${e && e.stack ? e.stack : e}\n`);
11
+ process.exitCode = 2;
12
+ });
package/cli/index.d.ts ADDED
@@ -0,0 +1,7 @@
1
+ // Types for @trops/dash-check/cli.
2
+ export function main(argv: string[], io?: { log?: (s: string) => void; err?: (s: string) => void; signal?: AbortSignal }): Promise<number>;
3
+ export function parseArgs(argv: string[]): Record<string, any>;
4
+ export function listChecks(cwd: string): Array<{ name: string; file: string; type: "js" | "recipe" | "exec"; sample?: string }>;
5
+ export function runEntry(entry: { name: string; file: string; type: "js" | "recipe" | "exec" }, opts: { cwd: string; input: Record<string, any>; timeoutSec?: number }): Promise<any>;
6
+ export function lastProof(output: string): { proof: { reason?: string; details?: any }; index: number; lines: string[] } | null;
7
+ export const CHECKS_DIR: string;