knitmd 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +2 -2
- package/dist/approval.js +37 -0
- package/dist/cli.js +359 -0
- package/dist/commands/attach.js +77 -0
- package/dist/commands/basic.js +503 -0
- package/dist/commands/flow.js +340 -0
- package/dist/commands/hooks.js +257 -0
- package/dist/commands/mcp.js +91 -0
- package/dist/commands/renumber.js +95 -0
- package/dist/commands/report.js +187 -0
- package/dist/commands/setup.js +232 -0
- package/dist/commands/sync.js +146 -0
- package/dist/ctx.js +8 -0
- package/dist/git.js +65 -0
- package/dist/inbox.js +15 -0
- package/dist/linked.js +58 -0
- package/dist/person.js +32 -0
- package/dist/publish.js +95 -0
- package/dist/tracker.js +146 -0
- package/package.json +37 -4
- package/templates/ci/code.yml +41 -0
- package/templates/ci/tracker-sync.yml +38 -0
- package/templates/ci/tracker.yml +50 -0
- package/templates/tracker/GUIDE.md +124 -0
- package/templates/tracker/proj.yml +60 -0
- package/templates/tracker/templates/bug.md +22 -0
- package/templates/tracker/templates/default.md +10 -0
- package/templates/tracker/templates/epic.md +10 -0
- package/templates/tracker/templates/feature.md +14 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Miikka Lammela
|
|
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,3 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Knit
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
The `nit` command line tool. See the [repository README](../../README.md).
|
package/dist/approval.js
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { UserError, approvalError, parseNameStatus, statusChangesAmong, ticketId, ticketsPath, transitionError, } from '@knitmd/core';
|
|
2
|
+
import { showFile, tryGit } from './git.js';
|
|
3
|
+
import { currentPerson } from './person.js';
|
|
4
|
+
/** The status changes of ticket files in a commit; renames (renumbering) are followed. */
|
|
5
|
+
export function statusChangesIn(t, root, commit) {
|
|
6
|
+
const args = ['diff-tree', '-r', '-M', '--root', '--no-commit-id', '--name-status', commit, '--', ticketsPath(t)];
|
|
7
|
+
return statusChangesAmong(parseNameStatus(tryGit(root, args)), (p) => showFile(root, `${commit}^`, p), (p) => showFile(root, commit, p));
|
|
8
|
+
}
|
|
9
|
+
/** The status changes staged for the next commit. */
|
|
10
|
+
export function stagedStatusChanges(t, root) {
|
|
11
|
+
const args = ['diff', '--cached', '-M', '--name-status', '--', ticketsPath(t)];
|
|
12
|
+
return statusChangesAmong(parseNameStatus(tryGit(root, args)), (p) => showFile(root, 'HEAD', p), (p) => showFile(root, '', p));
|
|
13
|
+
}
|
|
14
|
+
function personOrUndefined(ctx, t, cwd) {
|
|
15
|
+
try {
|
|
16
|
+
return currentPerson(ctx, t.config, cwd);
|
|
17
|
+
}
|
|
18
|
+
catch {
|
|
19
|
+
return undefined;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* The local guard of `new`, `set`, `start`, `finish`, `release` and the pre-commit hook: refuses a
|
|
24
|
+
* status change that the workflow does not allow for the current person, and approvals by someone
|
|
25
|
+
* who may not approve. CI (`check-commits`) is the real check; this one catches mistakes early.
|
|
26
|
+
*/
|
|
27
|
+
export function guardStatusChange(ctx, t, cwd, doc, before, after) {
|
|
28
|
+
if (after === undefined || before === after)
|
|
29
|
+
return;
|
|
30
|
+
const person = personOrUndefined(ctx, t, cwd);
|
|
31
|
+
const approval = approvalError(t, person, ticketId(doc), before, after);
|
|
32
|
+
if (approval)
|
|
33
|
+
throw approval;
|
|
34
|
+
const why = transitionError(t, doc, before, after, person);
|
|
35
|
+
if (why)
|
|
36
|
+
throw new UserError(`${ticketId(doc)}: ${why}`, 1);
|
|
37
|
+
}
|
package/dist/cli.js
ADDED
|
@@ -0,0 +1,359 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { UserError } from '@knitmd/core';
|
|
3
|
+
import { readFileSync, realpathSync } from 'node:fs';
|
|
4
|
+
import { isAbsolute, resolve } from 'node:path';
|
|
5
|
+
import { fileURLToPath } from 'node:url';
|
|
6
|
+
import { parseArgs } from 'node:util';
|
|
7
|
+
import { cmdCheck, cmdFmt, cmdList, cmdGuide, cmdInbox, cmdLog, cmdNew, cmdNext, cmdSet, cmdShow, cmdStatus, cmdTick, } from './commands/basic.js';
|
|
8
|
+
import { cmdAttach } from './commands/attach.js';
|
|
9
|
+
import { cmdClaims, cmdFinish, cmdRelease, cmdStart } from './commands/flow.js';
|
|
10
|
+
import { cmdCheckCommits, cmdHooksInstall, hookCommitMsg, hookPreCommit, hookPrepareCommitMsg, } from './commands/hooks.js';
|
|
11
|
+
import { cmdRenumber } from './commands/renumber.js';
|
|
12
|
+
import { cmdReport } from './commands/report.js';
|
|
13
|
+
import { cmdMcp, cmdSkill } from './commands/mcp.js';
|
|
14
|
+
import { cmdInit, cmdLink } from './commands/setup.js';
|
|
15
|
+
import { cmdSync } from './commands/sync.js';
|
|
16
|
+
import { GitError } from './git.js';
|
|
17
|
+
const commit = { commit: { type: 'boolean' }, push: { type: 'boolean' } };
|
|
18
|
+
const COMMANDS = {
|
|
19
|
+
init: {
|
|
20
|
+
usage: 'init [--key KEY] [--central] [--ci] [--mcp] [--no-hooks] [--no-pointers] [--force]',
|
|
21
|
+
summary: 'create the tracker (.project/) in this repository',
|
|
22
|
+
options: {
|
|
23
|
+
key: { type: 'string' },
|
|
24
|
+
central: { type: 'boolean' },
|
|
25
|
+
ci: { type: 'boolean' },
|
|
26
|
+
hooks: { type: 'boolean' },
|
|
27
|
+
pointers: { type: 'boolean' },
|
|
28
|
+
mcp: { type: 'boolean' },
|
|
29
|
+
force: { type: 'boolean' },
|
|
30
|
+
},
|
|
31
|
+
run: (ctx, v) => cmdInit(ctx, v),
|
|
32
|
+
},
|
|
33
|
+
link: {
|
|
34
|
+
usage: 'link <tracker repository> [--repo NAME] [--ci] [--mcp] [--no-hooks] [--no-pointers]',
|
|
35
|
+
summary: 'connect this code repository to a central tracker',
|
|
36
|
+
options: {
|
|
37
|
+
repo: { type: 'string' },
|
|
38
|
+
ci: { type: 'boolean' },
|
|
39
|
+
hooks: { type: 'boolean' },
|
|
40
|
+
pointers: { type: 'boolean' },
|
|
41
|
+
mcp: { type: 'boolean' },
|
|
42
|
+
},
|
|
43
|
+
run: (ctx, v, a) => cmdLink(ctx, v, a[0]),
|
|
44
|
+
},
|
|
45
|
+
new: {
|
|
46
|
+
usage: 'new <type> "<title>" [field=value…] [--force]',
|
|
47
|
+
summary: 'create a ticket with the next free ID',
|
|
48
|
+
options: { force: { type: 'boolean' }, ...commit },
|
|
49
|
+
run: (ctx, v, a) => cmdNew(ctx, { ...v, fields: a.slice(2) }, a[0], a[1]),
|
|
50
|
+
},
|
|
51
|
+
list: {
|
|
52
|
+
usage: 'list [--status S]… [--assignee P] [--milestone M] [--repo R] [--tag T]… [--type T] [--in A..B] [--all]',
|
|
53
|
+
summary: 'list open tickets (--all: also closed ones; --in: the tickets the commits of a range refer to)',
|
|
54
|
+
options: {
|
|
55
|
+
status: { type: 'string', multiple: true },
|
|
56
|
+
assignee: { type: 'string' },
|
|
57
|
+
milestone: { type: 'string' },
|
|
58
|
+
repo: { type: 'string' },
|
|
59
|
+
tag: { type: 'string', multiple: true },
|
|
60
|
+
label: { type: 'string' },
|
|
61
|
+
in: { type: 'string' },
|
|
62
|
+
type: { type: 'string' },
|
|
63
|
+
all: { type: 'boolean' },
|
|
64
|
+
},
|
|
65
|
+
run: (ctx, v) => cmdList(ctx, v),
|
|
66
|
+
},
|
|
67
|
+
show: {
|
|
68
|
+
usage: 'show [ID]',
|
|
69
|
+
summary: 'print a ticket (default: the ticket of the current branch)',
|
|
70
|
+
run: (ctx, v, a) => cmdShow(ctx, v, a[0]),
|
|
71
|
+
},
|
|
72
|
+
next: {
|
|
73
|
+
usage: 'next [--assignee P] [--repo R] [--limit N] [--no-fetch]',
|
|
74
|
+
summary: 'the ready, unblocked, unclaimed tickets in work order',
|
|
75
|
+
options: {
|
|
76
|
+
assignee: { type: 'string' },
|
|
77
|
+
repo: { type: 'string' },
|
|
78
|
+
limit: { type: 'string' },
|
|
79
|
+
fetch: { type: 'boolean' },
|
|
80
|
+
},
|
|
81
|
+
run: (ctx, v) => cmdNext(ctx, v),
|
|
82
|
+
},
|
|
83
|
+
start: {
|
|
84
|
+
usage: 'start <ID> [--worktree [--path DIR]] [--no-push] [--force]',
|
|
85
|
+
summary: 'claim a ticket: claim branch, optional worktree, claim pushed',
|
|
86
|
+
options: { worktree: { type: 'boolean' }, path: { type: 'string' }, force: { type: 'boolean' }, ...commit },
|
|
87
|
+
run: (ctx, v, a) => cmdStart(ctx, v, a[0]),
|
|
88
|
+
},
|
|
89
|
+
attach: {
|
|
90
|
+
usage: 'attach [ID] <file|URL> --note "<what it contains>" [--name FILE]',
|
|
91
|
+
summary: 'attach a file (copied to attachments/<ID>/) or a link to a ticket',
|
|
92
|
+
options: { note: { type: 'string' }, name: { type: 'string' }, ...commit },
|
|
93
|
+
run: (ctx, v, a) => cmdAttach(ctx, v, a),
|
|
94
|
+
},
|
|
95
|
+
log: {
|
|
96
|
+
usage: 'log [ID] "<text>"',
|
|
97
|
+
summary: 'append a line to the ticket log',
|
|
98
|
+
options: commit,
|
|
99
|
+
run: (ctx, v, a) => cmdLog(ctx, v, a),
|
|
100
|
+
},
|
|
101
|
+
tick: {
|
|
102
|
+
usage: 'tick [ID] <n>… [--undo]',
|
|
103
|
+
summary: 'check acceptance criteria by number (as `show` lists them)',
|
|
104
|
+
options: { undo: { type: 'boolean' }, ...commit },
|
|
105
|
+
run: (ctx, v, a) => cmdTick(ctx, v, a),
|
|
106
|
+
},
|
|
107
|
+
finish: {
|
|
108
|
+
usage: 'finish [ID] [--pr] [--no-push]',
|
|
109
|
+
summary: 'verify the criteria, close the ticket with a Closes: commit, push',
|
|
110
|
+
options: { pr: { type: 'boolean' }, ...commit },
|
|
111
|
+
run: (ctx, v, a) => cmdFinish(ctx, v, a[0]),
|
|
112
|
+
},
|
|
113
|
+
claims: {
|
|
114
|
+
usage: 'claims [--no-fetch]',
|
|
115
|
+
summary: 'list open claims (claim branches)',
|
|
116
|
+
options: { fetch: { type: 'boolean' } },
|
|
117
|
+
run: (ctx, v) => cmdClaims(ctx, v),
|
|
118
|
+
},
|
|
119
|
+
release: {
|
|
120
|
+
usage: 'release [ID] [--force]',
|
|
121
|
+
summary: 'give a claim up',
|
|
122
|
+
options: { force: { type: 'boolean' }, ...commit },
|
|
123
|
+
run: (ctx, v, a) => cmdRelease(ctx, v, a[0]),
|
|
124
|
+
},
|
|
125
|
+
set: {
|
|
126
|
+
usage: 'set [ID] field=value… [--force]',
|
|
127
|
+
summary: 'change ticket fields (an empty value removes a field)',
|
|
128
|
+
options: { force: { type: 'boolean' }, ...commit },
|
|
129
|
+
run: (ctx, v, a) => cmdSet(ctx, v, a),
|
|
130
|
+
},
|
|
131
|
+
status: {
|
|
132
|
+
usage: 'status',
|
|
133
|
+
summary: 'terminal summary: milestones, in progress, overdue, blocked',
|
|
134
|
+
run: (ctx, v) => cmdStatus(ctx, v),
|
|
135
|
+
},
|
|
136
|
+
check: {
|
|
137
|
+
usage: 'check [--strict]',
|
|
138
|
+
summary: 'validate the tracker (--strict: warnings fail too)',
|
|
139
|
+
options: { strict: { type: 'boolean' } },
|
|
140
|
+
run: (ctx, v) => cmdCheck(ctx, v),
|
|
141
|
+
},
|
|
142
|
+
'check-commits': {
|
|
143
|
+
usage: 'check-commits <base>..<head>',
|
|
144
|
+
summary: 'validate ticket references and Closes: tickets in a commit range',
|
|
145
|
+
run: (ctx, v, a) => cmdCheckCommits(ctx, v, a[0]),
|
|
146
|
+
},
|
|
147
|
+
inbox: {
|
|
148
|
+
usage: 'inbox [--for PERSON|team:T|ROLE|agent] [--all]',
|
|
149
|
+
summary: 'what waits for you (or someone else): status owner, questions, approvals, with waiting time',
|
|
150
|
+
options: { for: { type: 'string' }, all: { type: 'boolean' } },
|
|
151
|
+
run: (ctx, v) => cmdInbox(ctx, v),
|
|
152
|
+
},
|
|
153
|
+
mcp: {
|
|
154
|
+
usage: 'mcp',
|
|
155
|
+
summary: 'serve the ticket tools and reports to agents over MCP (stdio); see `nit init --mcp`',
|
|
156
|
+
run: (ctx, v) => cmdMcp(ctx, v, main, version()),
|
|
157
|
+
},
|
|
158
|
+
skill: {
|
|
159
|
+
usage: 'skill [--write [--path DIR]]',
|
|
160
|
+
summary: 'print the agent skill generated from the guide; --write installs it (.claude/skills/knit)',
|
|
161
|
+
options: { write: { type: 'boolean' }, path: { type: 'string' } },
|
|
162
|
+
run: (ctx, v) => {
|
|
163
|
+
const lines = [];
|
|
164
|
+
help({ ...ctx, out: (l) => lines.push(l) });
|
|
165
|
+
return cmdSkill(ctx, v, lines.join('\n'));
|
|
166
|
+
},
|
|
167
|
+
},
|
|
168
|
+
guide: {
|
|
169
|
+
usage: 'guide [--write]',
|
|
170
|
+
summary: 'print the workflow section of GUIDE.md from proj.yml; --write updates GUIDE.md',
|
|
171
|
+
options: { write: { type: 'boolean' }, ...commit },
|
|
172
|
+
run: (ctx, v) => cmdGuide(ctx, v),
|
|
173
|
+
},
|
|
174
|
+
fmt: {
|
|
175
|
+
usage: 'fmt [--check] [file…]',
|
|
176
|
+
summary: 'canonical field order, title heading, dates, calc tables',
|
|
177
|
+
options: { check: { type: 'boolean' } },
|
|
178
|
+
run: (ctx, v, a) => cmdFmt(ctx, v, a),
|
|
179
|
+
},
|
|
180
|
+
report: {
|
|
181
|
+
usage: 'report [--name README.md|board.md|gantt.md] [--out DIR] [--commit [--no-push]] [--date D] [--no-fetch]',
|
|
182
|
+
summary: 'render the reports: to stdout, to a folder, or to the reports branch',
|
|
183
|
+
options: {
|
|
184
|
+
name: { type: 'string' },
|
|
185
|
+
out: { type: 'string' },
|
|
186
|
+
date: { type: 'string' },
|
|
187
|
+
fetch: { type: 'boolean' },
|
|
188
|
+
...commit,
|
|
189
|
+
},
|
|
190
|
+
run: (ctx, v) => cmdReport(ctx, v),
|
|
191
|
+
},
|
|
192
|
+
sync: {
|
|
193
|
+
usage: 'sync [--repo NAME] [--commit] [--push]',
|
|
194
|
+
summary: 'central mode: record code repository commits and close finished tickets',
|
|
195
|
+
options: { repo: { type: 'string' }, ...commit },
|
|
196
|
+
run: (ctx, v) => cmdSync(ctx, v),
|
|
197
|
+
},
|
|
198
|
+
renumber: {
|
|
199
|
+
usage: 'renumber <ID> [--to N]',
|
|
200
|
+
summary: 'give a ticket a new number (resolves ID collisions)',
|
|
201
|
+
options: { to: { type: 'string' } },
|
|
202
|
+
run: (ctx, v, a) => cmdRenumber(ctx, v, a[0]),
|
|
203
|
+
},
|
|
204
|
+
hooks: {
|
|
205
|
+
usage: 'hooks install [--force]',
|
|
206
|
+
summary: 'install the Git hooks of this repository',
|
|
207
|
+
options: { force: { type: 'boolean' } },
|
|
208
|
+
run: (ctx, v, a) => {
|
|
209
|
+
if (a[0] !== 'install')
|
|
210
|
+
throw new UserError('usage: nit hooks install [--force]');
|
|
211
|
+
return cmdHooksInstall(ctx, v);
|
|
212
|
+
},
|
|
213
|
+
},
|
|
214
|
+
};
|
|
215
|
+
const HIDDEN = {
|
|
216
|
+
hook: {
|
|
217
|
+
usage: 'hook <pre-commit|prepare-commit-msg|commit-msg> [args…]',
|
|
218
|
+
summary: 'run by the Git hooks',
|
|
219
|
+
run: (ctx, v, a) => {
|
|
220
|
+
hookEnvironment(ctx);
|
|
221
|
+
if (a[0] === 'pre-commit')
|
|
222
|
+
return hookPreCommit(ctx, v);
|
|
223
|
+
if (a[0] === 'prepare-commit-msg')
|
|
224
|
+
return hookPrepareCommitMsg(ctx, v, a[1], a[2]);
|
|
225
|
+
if (a[0] === 'commit-msg')
|
|
226
|
+
return hookCommitMsg(ctx, v, a[1]);
|
|
227
|
+
throw new UserError(`unknown hook '${a[0]}'`);
|
|
228
|
+
},
|
|
229
|
+
},
|
|
230
|
+
};
|
|
231
|
+
/**
|
|
232
|
+
* Git runs hooks at the top of the work tree with GIT_DIR (and for commits GIT_INDEX_FILE) set,
|
|
233
|
+
* often as relative paths. GIT_DIR would make Git commands in other folders (a subfolder, or the
|
|
234
|
+
* tracker repository of a code repository) use the wrong repository, so it is removed and Git
|
|
235
|
+
* finds the repository from the folder as usual. The index file is kept, as an absolute path,
|
|
236
|
+
* because the pre-commit hook must read the index of the commit being made.
|
|
237
|
+
*/
|
|
238
|
+
function hookEnvironment(ctx) {
|
|
239
|
+
const index = process.env.GIT_INDEX_FILE;
|
|
240
|
+
if (index && !isAbsolute(index))
|
|
241
|
+
process.env.GIT_INDEX_FILE = resolve(ctx.cwd, index);
|
|
242
|
+
delete process.env.GIT_DIR;
|
|
243
|
+
delete process.env.GIT_WORK_TREE;
|
|
244
|
+
}
|
|
245
|
+
const GLOBAL = {
|
|
246
|
+
dir: { type: 'string' },
|
|
247
|
+
json: { type: 'boolean' },
|
|
248
|
+
help: { type: 'boolean', short: 'h' },
|
|
249
|
+
};
|
|
250
|
+
function version() {
|
|
251
|
+
const pkg = JSON.parse(readFileSync(new URL('../package.json', import.meta.url), 'utf8'));
|
|
252
|
+
return pkg.version;
|
|
253
|
+
}
|
|
254
|
+
function help(ctx, name) {
|
|
255
|
+
const cmd = name ? COMMANDS[name] : undefined;
|
|
256
|
+
if (cmd) {
|
|
257
|
+
ctx.out(`usage: nit ${cmd.usage}`);
|
|
258
|
+
ctx.out('');
|
|
259
|
+
ctx.out(cmd.summary);
|
|
260
|
+
ctx.out('');
|
|
261
|
+
ctx.out('global options: --dir <tracker folder>, --json, --help');
|
|
262
|
+
return;
|
|
263
|
+
}
|
|
264
|
+
ctx.out('nit: Git-based tickets for people and agents');
|
|
265
|
+
ctx.out('');
|
|
266
|
+
ctx.out('usage: nit <command> [options] (also: git nit <command>)');
|
|
267
|
+
ctx.out('');
|
|
268
|
+
const width = Math.max(...Object.keys(COMMANDS).map((k) => k.length));
|
|
269
|
+
for (const [k, c] of Object.entries(COMMANDS))
|
|
270
|
+
ctx.out(` ${k.padEnd(width)} ${c.summary}`);
|
|
271
|
+
ctx.out('');
|
|
272
|
+
ctx.out('Commands that take [ID] default to the ticket of the current branch.');
|
|
273
|
+
ctx.out('`nit help <command>` shows its options. Global: --dir, --json.');
|
|
274
|
+
}
|
|
275
|
+
/** Environment variables that were called MDPROJ_* before the rename to Knit (ADR 0019). */
|
|
276
|
+
const LEGACY_ENV = ['USER', 'DIR', 'TRACKER', 'TODAY'];
|
|
277
|
+
/** Copies `MDPROJ_<name>` to `NIT_<name>` when only the old name is set. */
|
|
278
|
+
export function withLegacyEnv(env) {
|
|
279
|
+
const missing = LEGACY_ENV.filter((n) => env[`NIT_${n}`] === undefined && env[`MDPROJ_${n}`] !== undefined);
|
|
280
|
+
if (missing.length === 0)
|
|
281
|
+
return env;
|
|
282
|
+
return { ...env, ...Object.fromEntries(missing.map((n) => [`NIT_${n}`, env[`MDPROJ_${n}`]])) };
|
|
283
|
+
}
|
|
284
|
+
export function main(argv, ctxIn) {
|
|
285
|
+
const ctx = { ...ctxIn, env: withLegacyEnv(ctxIn.env) };
|
|
286
|
+
const [name, ...rest] = argv;
|
|
287
|
+
if (!name || name === '--help' || name === '-h') {
|
|
288
|
+
help(ctx);
|
|
289
|
+
return name ? 0 : 2;
|
|
290
|
+
}
|
|
291
|
+
if (name === '--version' || name === 'version') {
|
|
292
|
+
ctx.out(version());
|
|
293
|
+
return 0;
|
|
294
|
+
}
|
|
295
|
+
if (name === 'help') {
|
|
296
|
+
help(ctx, rest[0]);
|
|
297
|
+
return 0;
|
|
298
|
+
}
|
|
299
|
+
const cmd = COMMANDS[name] ?? HIDDEN[name];
|
|
300
|
+
if (!cmd) {
|
|
301
|
+
ctx.err(`nit: unknown command '${name}'. See \`nit help\`.`);
|
|
302
|
+
return 2;
|
|
303
|
+
}
|
|
304
|
+
try {
|
|
305
|
+
const parsed = parseArgs({
|
|
306
|
+
args: rest,
|
|
307
|
+
options: { ...GLOBAL, ...(cmd.options ?? {}) },
|
|
308
|
+
allowPositionals: true,
|
|
309
|
+
allowNegative: true,
|
|
310
|
+
strict: name !== 'hook',
|
|
311
|
+
});
|
|
312
|
+
const values = parsed.values;
|
|
313
|
+
if (values.help) {
|
|
314
|
+
help(ctx, name);
|
|
315
|
+
return 0;
|
|
316
|
+
}
|
|
317
|
+
return cmd.run(ctx, values, parsed.positionals);
|
|
318
|
+
}
|
|
319
|
+
catch (e) {
|
|
320
|
+
if (e instanceof UserError) {
|
|
321
|
+
ctx.err(`nit: ${e.message}`);
|
|
322
|
+
return e.exitCode;
|
|
323
|
+
}
|
|
324
|
+
if (e instanceof GitError) {
|
|
325
|
+
ctx.err(`nit: ${e.message}`);
|
|
326
|
+
return 1;
|
|
327
|
+
}
|
|
328
|
+
if (e instanceof TypeError && e.code?.startsWith('ERR_PARSE_ARGS')) {
|
|
329
|
+
ctx.err(`nit: ${e.message}. See \`nit help ${name}\`.`);
|
|
330
|
+
return 2;
|
|
331
|
+
}
|
|
332
|
+
throw e;
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
function isEntry() {
|
|
336
|
+
if (!process.argv[1])
|
|
337
|
+
return false;
|
|
338
|
+
try {
|
|
339
|
+
return realpathSync(process.argv[1]) === realpathSync(fileURLToPath(import.meta.url));
|
|
340
|
+
}
|
|
341
|
+
catch {
|
|
342
|
+
return false;
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
if (isEntry()) {
|
|
346
|
+
// `nit list | head` closes the pipe early; that is not an error.
|
|
347
|
+
process.stdout.on('error', (e) => {
|
|
348
|
+
if (e.code === 'EPIPE')
|
|
349
|
+
process.exit(0);
|
|
350
|
+
throw e;
|
|
351
|
+
});
|
|
352
|
+
const code = main(process.argv.slice(2), {
|
|
353
|
+
cwd: process.cwd(),
|
|
354
|
+
env: process.env,
|
|
355
|
+
out: (l) => process.stdout.write(`${l}\n`),
|
|
356
|
+
err: (l) => process.stderr.write(`${l}\n`),
|
|
357
|
+
});
|
|
358
|
+
process.exitCode = code;
|
|
359
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { copyFileSync, existsSync, mkdirSync, readFileSync, statSync } from 'node:fs';
|
|
2
|
+
import { basename, dirname, join, relative, resolve, sep } from 'node:path';
|
|
3
|
+
import { IMAGE_TYPES, attachmentRefs, attachmentsRoot, fileType, formatSize, safeName, appendToSection, repoPath, ticketId, UserError, } from '@knitmd/core';
|
|
4
|
+
import { lfsPaths, displayPath, resolveTicketArg, save } from '../tracker.js';
|
|
5
|
+
import { publish } from '../publish.js';
|
|
6
|
+
import { open } from './basic.js';
|
|
7
|
+
function linkText(s) {
|
|
8
|
+
return s
|
|
9
|
+
.replace(/\[/g, '(')
|
|
10
|
+
.replace(/\]/g, ')')
|
|
11
|
+
.replace(/\s*\n\s*/g, ' ');
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* `attach [ID] <file|URL> --note "<what it contains>" [--name <file name>]`: copies a file to
|
|
15
|
+
* `attachments/<ID>/` (or records a link) and lists it under `## Attachments`.
|
|
16
|
+
*/
|
|
17
|
+
export function cmdAttach(ctx, o, args) {
|
|
18
|
+
const source = args.at(-1);
|
|
19
|
+
const id = args.length >= 2 ? args[0] : undefined;
|
|
20
|
+
if (!source)
|
|
21
|
+
throw new UserError('usage: nit attach [ID] <file|URL> --note "<what it contains>" [--name <file>]');
|
|
22
|
+
if (!o.note?.trim()) {
|
|
23
|
+
throw new UserError('--note "<what it contains>" is required, so that readers and agents know without opening it');
|
|
24
|
+
}
|
|
25
|
+
const note = o.note.trim();
|
|
26
|
+
const t = open(ctx, o, { fresh: true });
|
|
27
|
+
const doc = resolveTicketArg(t, id);
|
|
28
|
+
const tid = ticketId(doc);
|
|
29
|
+
const changed = [repoPath(t, doc.path)];
|
|
30
|
+
let target;
|
|
31
|
+
let line;
|
|
32
|
+
if (/^[a-z][a-z0-9+.-]*:\/\//i.test(source)) {
|
|
33
|
+
target = source;
|
|
34
|
+
line = `- [${linkText(note)}](${source})`;
|
|
35
|
+
}
|
|
36
|
+
else {
|
|
37
|
+
const src = resolve(ctx.cwd, source);
|
|
38
|
+
if (!existsSync(src) || !statSync(src).isFile())
|
|
39
|
+
throw new UserError(`no file ${source}`);
|
|
40
|
+
const name = safeName(o.name ?? basename(src));
|
|
41
|
+
const type = fileType(name);
|
|
42
|
+
const { maxSize, types } = t.config.attachments;
|
|
43
|
+
if (types && !types.includes(type)) {
|
|
44
|
+
throw new UserError(`file type '${type || '(none)'}' is not in attachments.types: ${types.join(', ')}`, 1);
|
|
45
|
+
}
|
|
46
|
+
const dest = join(attachmentsRoot(t), tid, name);
|
|
47
|
+
const rel = repoPath(t, dest);
|
|
48
|
+
const size = statSync(src).size;
|
|
49
|
+
if (size > maxSize && !lfsPaths(t.root, [rel]).has(rel)) {
|
|
50
|
+
throw new UserError(`${basename(src)} is ${formatSize(size)}, over attachments.max_size (${formatSize(maxSize)}). ` +
|
|
51
|
+
`Track the type with Git LFS (git lfs track "*.${type}"), or upload it elsewhere and attach the URL`, 1);
|
|
52
|
+
}
|
|
53
|
+
if (existsSync(dest)) {
|
|
54
|
+
if (!readFileSync(dest).equals(readFileSync(src))) {
|
|
55
|
+
throw new UserError(`${displayPath(ctx, dest)} exists with different content; attachments are not changed, so give the new version another name with --name`, 1);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
else {
|
|
59
|
+
mkdirSync(dirname(dest), { recursive: true });
|
|
60
|
+
copyFileSync(src, dest);
|
|
61
|
+
}
|
|
62
|
+
changed.push(rel);
|
|
63
|
+
target = relative(dirname(doc.path), dest).split(sep).join('/');
|
|
64
|
+
line = IMAGE_TYPES.has(type) ? `- ` : `- [${name}](${target}): ${note}`;
|
|
65
|
+
ctx.out(displayPath(ctx, dest));
|
|
66
|
+
}
|
|
67
|
+
const listed = attachmentRefs(t, doc).some((r) => r.inSection && r.target === target);
|
|
68
|
+
if (!listed) {
|
|
69
|
+
appendToSection(doc, 'Attachments', line, ['Acceptance criteria', 'Plan', 'Log']);
|
|
70
|
+
save(doc);
|
|
71
|
+
ctx.out(`${displayPath(ctx, doc.path)}: ${line}`);
|
|
72
|
+
}
|
|
73
|
+
else
|
|
74
|
+
ctx.out(`${tid} already lists ${target}`);
|
|
75
|
+
publish(t, ctx, changed, `Attach ${basename(target)} to ${tid}`, o);
|
|
76
|
+
return 0;
|
|
77
|
+
}
|