paperlint 18.4.1 → 18.6.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/CLAUDE.md +6 -2
- package/dist/adapters/hotcrp/portal.io.d.ts.map +1 -1
- package/dist/adapters/hotcrp/portal.io.js +1 -0
- package/dist/adapters/hotcrp/portal.io.js.map +1 -1
- package/dist/adapters/hotcrp/response.d.ts.map +1 -1
- package/dist/adapters/hotcrp/response.js +15 -0
- package/dist/adapters/hotcrp/response.js.map +1 -1
- package/dist/adapters/latex/abstract.d.ts +11 -0
- package/dist/adapters/latex/abstract.d.ts.map +1 -1
- package/dist/adapters/latex/abstract.js +1 -1
- package/dist/adapters/latex/abstract.js.map +1 -1
- package/dist/adapters/latex/index.d.ts.map +1 -1
- package/dist/adapters/latex/index.js +3 -0
- package/dist/adapters/latex/index.js.map +1 -1
- package/dist/adapters/latex/macros.d.ts +6 -0
- package/dist/adapters/latex/macros.d.ts.map +1 -0
- package/dist/adapters/latex/macros.js +27 -0
- package/dist/adapters/latex/macros.js.map +1 -0
- package/dist/adapters/osf/host.io.d.ts +12 -0
- package/dist/adapters/osf/host.io.d.ts.map +1 -0
- package/dist/adapters/osf/host.io.js +119 -0
- package/dist/adapters/osf/host.io.js.map +1 -0
- package/dist/adapters/osf/index.d.ts +10 -0
- package/dist/adapters/osf/index.d.ts.map +1 -0
- package/dist/adapters/osf/index.js +10 -0
- package/dist/adapters/osf/index.js.map +1 -0
- package/dist/adapters/osf/response.d.ts +11 -0
- package/dist/adapters/osf/response.d.ts.map +1 -0
- package/dist/adapters/osf/response.js +87 -0
- package/dist/adapters/osf/response.js.map +1 -0
- package/dist/artifact-upload-rule.d.ts +66 -0
- package/dist/artifact-upload-rule.d.ts.map +1 -0
- package/dist/artifact-upload-rule.js +151 -0
- package/dist/artifact-upload-rule.js.map +1 -0
- package/dist/artifact.d.ts +35 -0
- package/dist/artifact.d.ts.map +1 -0
- package/dist/artifact.js +251 -0
- package/dist/artifact.js.map +1 -0
- package/dist/cli.d.ts +7 -0
- package/dist/cli.d.ts.map +1 -1
- package/dist/cli.js +99 -7
- package/dist/cli.js.map +1 -1
- package/dist/domain/artifact-upload.d.ts +44 -0
- package/dist/domain/artifact-upload.d.ts.map +1 -0
- package/dist/domain/artifact-upload.js +80 -0
- package/dist/domain/artifact-upload.js.map +1 -0
- package/dist/domain/submission.d.ts +4 -0
- package/dist/domain/submission.d.ts.map +1 -1
- package/dist/domain/submission.js.map +1 -1
- package/dist/eslint-rules/CLAUDE.md.spec.d.ts.map +1 -1
- package/dist/eslint-rules/CLAUDE.md.spec.js +1 -0
- package/dist/eslint-rules/CLAUDE.md.spec.js.map +1 -1
- package/dist/ports/artifact-host.d.ts +54 -0
- package/dist/ports/artifact-host.d.ts.map +1 -0
- package/dist/ports/artifact-host.js +2 -0
- package/dist/ports/artifact-host.js.map +1 -0
- package/dist/ports/latex.d.ts +9 -0
- package/dist/ports/latex.d.ts.map +1 -1
- package/dist/submission-confirm-hook.d.ts +11 -1
- package/dist/submission-confirm-hook.d.ts.map +1 -1
- package/dist/submission-confirm-hook.js +83 -21
- package/dist/submission-confirm-hook.js.map +1 -1
- package/dist/submission.d.ts +19 -2
- package/dist/submission.d.ts.map +1 -1
- package/dist/submission.js +121 -17
- package/dist/submission.js.map +1 -1
- package/dist/types.d.ts +6 -0
- package/dist/types.d.ts.map +1 -1
- package/docs/artifact-upload.md +124 -0
- package/docs/optional-rules.md +1 -0
- package/docs/osf-artifact.md +4 -1
- package/docs/rules/artifact/upload-current.md +85 -0
- package/docs/rules.md +1 -0
- package/docs/submission.md +19 -4
- package/eslint-rules/CLAUDE.md +5 -1
- package/eslint-rules/CLAUDE.md.spec.ts +3 -0
- package/package.json +1 -1
- package/src/adapters/hotcrp/portal.io.ts +1 -0
- package/src/adapters/hotcrp/portal.test.ts +43 -2
- package/src/adapters/hotcrp/response.ts +21 -0
- package/src/adapters/latex/abstract.ts +3 -1
- package/src/adapters/latex/index.ts +3 -0
- package/src/adapters/latex/macros.test.ts +31 -0
- package/src/adapters/latex/macros.ts +43 -0
- package/src/adapters/osf/cassette/README.md +20 -0
- package/src/adapters/osf/cassette/get-file.json +108 -0
- package/src/adapters/osf/cassette/get-listing.json +111 -0
- package/src/adapters/osf/cassette/get-missing.json +1 -0
- package/src/adapters/osf/cassette/put-new.json +31 -0
- package/src/adapters/osf/host.io.ts +156 -0
- package/src/adapters/osf/host.test.ts +274 -0
- package/src/adapters/osf/index.ts +14 -0
- package/src/adapters/osf/response.ts +105 -0
- package/src/artifact-upload-rule.test.ts +213 -0
- package/src/artifact-upload-rule.ts +213 -0
- package/src/artifact.test.ts +504 -0
- package/src/artifact.ts +402 -0
- package/src/cli.test.ts +149 -1
- package/src/cli.ts +121 -8
- package/src/domain/artifact-upload.test.ts +80 -0
- package/src/domain/artifact-upload.ts +111 -0
- package/src/domain/submission.ts +4 -0
- package/src/ports/artifact-host.ts +65 -0
- package/src/ports/latex.ts +10 -0
- package/src/rule-cases.test.ts +44 -0
- package/src/submission-confirm-hook.test.ts +113 -1
- package/src/submission-confirm-hook.ts +120 -27
- package/src/submission-fields-run.test.ts +1 -0
- package/src/submission-prior-reviews.test.ts +9 -1
- package/src/submission-show.test.ts +81 -0
- package/src/submission-update.test.ts +164 -6
- package/src/submission.ts +208 -24
- package/src/types.ts +6 -0
package/src/artifact.ts
ADDED
|
@@ -0,0 +1,402 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `paperlint artifact record|upload|show` — write and read the upload record of a paper's
|
|
3
|
+
* reproduction artifact (`src/domain/artifact-upload.ts`, docs/artifact-upload.md). All optional:
|
|
4
|
+
* `artifact/upload-current` reads the record whoever wrote it.
|
|
5
|
+
*
|
|
6
|
+
* record any host, no network: hash the zip, snapshot the paper's numbers, write the record
|
|
7
|
+
* upload the optional wrapper over a host's adapter (`--to osf`): a DRY RUN unless `--save`;
|
|
8
|
+
* with it, upload, read the host's hashes back, write the same record
|
|
9
|
+
* show the record, and — where the host's adapter reads hashes — `match yes|no` against it
|
|
10
|
+
*
|
|
11
|
+
* The numbers file is `--numbers`, else the `numbers` option of `artifact/upload-current` in the
|
|
12
|
+
* paper's own `paperlint.json`; the record's path is that rule's `record` option, else the default.
|
|
13
|
+
*
|
|
14
|
+
* 🔴 THE TOKEN IS AN INPUT, AND ONLY AN INPUT: read from the environment through `env`, handed to the
|
|
15
|
+
* adapter, never printed. Its variable is named the way `submission` names a site's
|
|
16
|
+
* (`src/domain/portal-token.ts`); the site-less one is refused by name.
|
|
17
|
+
*/
|
|
18
|
+
import { basename, resolve } from "node:path";
|
|
19
|
+
import { omit } from "remeda";
|
|
20
|
+
import { callerPath } from "./caller-path.ts";
|
|
21
|
+
import {
|
|
22
|
+
md5Hex,
|
|
23
|
+
parseUploadRecord,
|
|
24
|
+
recordOf,
|
|
25
|
+
RECORD_VERSION,
|
|
26
|
+
uploadRecordText,
|
|
27
|
+
type UploadRecord,
|
|
28
|
+
} from "./domain/artifact-upload.ts";
|
|
29
|
+
import { lookUpToken } from "./domain/portal-token.ts";
|
|
30
|
+
import { err, ok, type Result } from "./domain/result.ts";
|
|
31
|
+
import { sha256Hex } from "./domain/sha256.ts";
|
|
32
|
+
import type { ArtifactHost, HostedFile } from "./ports/artifact-host.ts";
|
|
33
|
+
import {
|
|
34
|
+
printedNumbers,
|
|
35
|
+
uploadOptions,
|
|
36
|
+
type UploadOptions,
|
|
37
|
+
type UploadRuleDeps,
|
|
38
|
+
} from "./artifact-upload-rule.ts";
|
|
39
|
+
|
|
40
|
+
export interface ArtifactInput {
|
|
41
|
+
readonly sub: string | undefined;
|
|
42
|
+
/** The operands after the subcommand: the zip (record, upload), then the paper folder. */
|
|
43
|
+
readonly operands: readonly string[];
|
|
44
|
+
readonly url: string | null;
|
|
45
|
+
readonly host: string | null;
|
|
46
|
+
readonly to: string | null;
|
|
47
|
+
readonly node: string | null;
|
|
48
|
+
readonly numbers: string | null;
|
|
49
|
+
readonly save: boolean;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export interface ArtifactDeps extends UploadRuleDeps {
|
|
53
|
+
/** Every host with an adapter, by the name a record and `--to` use. */
|
|
54
|
+
readonly hosts: Readonly<Record<string, ArtifactHost>>;
|
|
55
|
+
readonly env: (name: string) => string | undefined;
|
|
56
|
+
readonly now: () => string;
|
|
57
|
+
readonly cwd: string;
|
|
58
|
+
readonly log: (line: string) => void;
|
|
59
|
+
readonly err: (line: string) => void;
|
|
60
|
+
/** The rule's entry (`[severity, options]`) as lint's config gives it for the paper in `dir`, or undefined. */
|
|
61
|
+
readonly ruleEntry: (dir: string) => Promise<unknown>;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const RULE = "artifact/upload-current";
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* The rule's options where lint turns it on for the paper in `dir`, as ESLint computes them for the
|
|
68
|
+
* file the rule runs on (`ruleEntry`), so the commands and lint read one answer whatever shape the settings
|
|
69
|
+
* take; the defaults when the rule is not on there.
|
|
70
|
+
*/
|
|
71
|
+
export async function paperUploadOptions(
|
|
72
|
+
dir: string,
|
|
73
|
+
deps: Pick<ArtifactDeps, "ruleEntry">,
|
|
74
|
+
): Promise<UploadOptions> {
|
|
75
|
+
const entry = await deps.ruleEntry(dir);
|
|
76
|
+
return uploadOptions(Array.isArray(entry) ? entry[1] : undefined);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** What a record is written from: the zip, the paper, the record's path, the numbers now. */
|
|
80
|
+
interface Prepared {
|
|
81
|
+
readonly zip: string;
|
|
82
|
+
readonly bytes: Uint8Array;
|
|
83
|
+
readonly paperDir: string;
|
|
84
|
+
readonly recordPath: string;
|
|
85
|
+
readonly numbers: Readonly<Record<string, string>>;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** The numbers file's values for the paper `paperDir`, or the line that says why there are none. */
|
|
89
|
+
function numbersOf(
|
|
90
|
+
paperDir: string,
|
|
91
|
+
file: string | null,
|
|
92
|
+
d: ArtifactDeps,
|
|
93
|
+
): Result<Readonly<Record<string, string>>, string> {
|
|
94
|
+
if (file === null)
|
|
95
|
+
return err(
|
|
96
|
+
`which file holds the paper's numbers? pass --numbers <file> (relative to the paper), or turn ${RULE} on with its "numbers" option`,
|
|
97
|
+
);
|
|
98
|
+
const p = printedNumbers(paperDir, file, d);
|
|
99
|
+
if (p.ok) return ok(Object.fromEntries(p.value.values));
|
|
100
|
+
return err(
|
|
101
|
+
p.error === "noNumbersFile"
|
|
102
|
+
? `no numbers file ${file} in ${paperDir}`
|
|
103
|
+
: `the numbers file ${file} defines no macro without arguments (\\newcommand{\\key}{value})`,
|
|
104
|
+
);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/** The zip and the paper's numbers, or the line that says why there are none. */
|
|
108
|
+
async function prepare(
|
|
109
|
+
i: ArtifactInput,
|
|
110
|
+
d: ArtifactDeps,
|
|
111
|
+
): Promise<Result<Prepared, string>> {
|
|
112
|
+
const [zipArg, dirArg] = i.operands;
|
|
113
|
+
if (zipArg === undefined)
|
|
114
|
+
return err(
|
|
115
|
+
`name the uploaded file: paperlint artifact ${String(i.sub)} <zip> …`,
|
|
116
|
+
);
|
|
117
|
+
const zip = resolve(d.cwd, zipArg);
|
|
118
|
+
const bytes = d.files.readBytes(callerPath(zip));
|
|
119
|
+
if (bytes === null) return err(`no file ${zipArg}`);
|
|
120
|
+
const paperDir = resolve(d.cwd, dirArg ?? ".");
|
|
121
|
+
const o = await paperUploadOptions(paperDir, d);
|
|
122
|
+
const numbers = numbersOf(paperDir, i.numbers ?? o.numbers, d);
|
|
123
|
+
if (!numbers.ok) return numbers;
|
|
124
|
+
const recordPath = resolve(paperDir, o.record);
|
|
125
|
+
return ok({ zip, bytes, paperDir, recordPath, numbers: numbers.value });
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/** Write the record, or say why it is not one (a bad `--url`), and what was written. */
|
|
129
|
+
function write(r: UploadRecord, p: Prepared, d: ArtifactDeps): number {
|
|
130
|
+
const valid = recordOf(r);
|
|
131
|
+
if (!valid.ok) {
|
|
132
|
+
d.err(`paperlint artifact: not written — ${valid.error}`);
|
|
133
|
+
return 2;
|
|
134
|
+
}
|
|
135
|
+
d.files.writeAtomic(
|
|
136
|
+
callerPath(p.recordPath),
|
|
137
|
+
new TextEncoder().encode(uploadRecordText(valid.value)),
|
|
138
|
+
);
|
|
139
|
+
d.log(`wrote ${p.recordPath}`);
|
|
140
|
+
d.log(` ${r.host} ${r.url}`);
|
|
141
|
+
d.log(` ${r.file} sha256 ${r.sha256 ?? "—"} md5 ${r.md5 ?? "—"}`);
|
|
142
|
+
d.log(
|
|
143
|
+
` ${String(Object.keys(r.numbers).length)} numbers from the numbers file, as they are now`,
|
|
144
|
+
);
|
|
145
|
+
return 0;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/** The record of `p` uploaded to `url` on `host`, now. */
|
|
149
|
+
const recordFor = (
|
|
150
|
+
p: Prepared,
|
|
151
|
+
host: string,
|
|
152
|
+
url: string,
|
|
153
|
+
d: ArtifactDeps,
|
|
154
|
+
): UploadRecord => ({
|
|
155
|
+
version: RECORD_VERSION,
|
|
156
|
+
host,
|
|
157
|
+
url,
|
|
158
|
+
file: basename(p.zip),
|
|
159
|
+
sha256: sha256Hex(p.bytes),
|
|
160
|
+
md5: md5Hex(p.bytes),
|
|
161
|
+
uploadedAt: d.now(),
|
|
162
|
+
numbers: p.numbers,
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
/** The name of the host whose adapter recognises `url`, else `other`. */
|
|
166
|
+
const hostNamed = (url: string, d: ArtifactDeps): string =>
|
|
167
|
+
Object.entries(d.hosts).find(([, h]) => h.targetOf(url) !== null)?.[0] ??
|
|
168
|
+
"other";
|
|
169
|
+
|
|
170
|
+
async function record(i: ArtifactInput, d: ArtifactDeps): Promise<number> {
|
|
171
|
+
const p = await prepare(i, d);
|
|
172
|
+
if (!p.ok) return fail(p.error, d);
|
|
173
|
+
if (i.url === null) return fail("where is it? pass --url <url>", d);
|
|
174
|
+
return write(
|
|
175
|
+
recordFor(p.value, i.host ?? hostNamed(i.url, d), i.url, d),
|
|
176
|
+
p.value,
|
|
177
|
+
d,
|
|
178
|
+
);
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/** Say why, and the exit code of a command that could not start. */
|
|
182
|
+
const fail = (why: string, d: ArtifactDeps): number => {
|
|
183
|
+
d.err(`paperlint artifact: ${why}`);
|
|
184
|
+
return 2;
|
|
185
|
+
};
|
|
186
|
+
|
|
187
|
+
/** A host's token works wherever the host does: it has no sites to tell apart. */
|
|
188
|
+
export const neverSiteOnly = (): boolean => false;
|
|
189
|
+
|
|
190
|
+
/** The token for `host`, read through `env`; the lines that say how to set it when it is not. */
|
|
191
|
+
function tokenFor(
|
|
192
|
+
host: ArtifactHost,
|
|
193
|
+
d: ArtifactDeps,
|
|
194
|
+
): Result<string, readonly string[]> {
|
|
195
|
+
const t = lookUpToken(
|
|
196
|
+
{
|
|
197
|
+
variable: host.token.variable,
|
|
198
|
+
shared: null,
|
|
199
|
+
siteOnly: neverSiteOnly,
|
|
200
|
+
siteLess: host.token.siteLess,
|
|
201
|
+
},
|
|
202
|
+
d.env,
|
|
203
|
+
);
|
|
204
|
+
if (t.ok) return ok(t.value.token);
|
|
205
|
+
return err([
|
|
206
|
+
t.error.kind === "site-less"
|
|
207
|
+
? `${host.token.siteLess} is set but not read: set ${host.token.variable} instead`
|
|
208
|
+
: `no token: ${host.token.variable} is not set`,
|
|
209
|
+
` 1. make one: ${host.token.help}`,
|
|
210
|
+
` 2. set it in your environment — never paste it into a chat: export ${host.token.variable}=<the token>`,
|
|
211
|
+
]);
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/** The host `--to` names, if paperlint has an adapter for it that uploads. */
|
|
215
|
+
function uploader(
|
|
216
|
+
i: ArtifactInput,
|
|
217
|
+
d: ArtifactDeps,
|
|
218
|
+
): Result<ArtifactHost, string> {
|
|
219
|
+
const h = i.to === null ? undefined : d.hosts[i.to];
|
|
220
|
+
return h?.can.upload === true
|
|
221
|
+
? ok(h)
|
|
222
|
+
: err(
|
|
223
|
+
`name a host paperlint uploads to: --to ${Object.keys(d.hosts).join(" | ")} (for any other host, upload it yourself and run \`paperlint artifact record <zip> --url <url>\`)`,
|
|
224
|
+
);
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/** An upload ready to go: the host, the zip and numbers, the project, the zip's sha256. */
|
|
228
|
+
interface Plan {
|
|
229
|
+
readonly name: string;
|
|
230
|
+
readonly host: ArtifactHost;
|
|
231
|
+
readonly p: Prepared;
|
|
232
|
+
readonly target: string;
|
|
233
|
+
readonly sha: string;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/** The upload `i` asks for, printed, or why it cannot be made. */
|
|
237
|
+
async function plan(
|
|
238
|
+
i: ArtifactInput,
|
|
239
|
+
d: ArtifactDeps,
|
|
240
|
+
): Promise<Result<Plan, string>> {
|
|
241
|
+
const h = uploader(i, d);
|
|
242
|
+
if (!h.ok) return h;
|
|
243
|
+
const p = await prepare(i, d);
|
|
244
|
+
if (!p.ok) return p;
|
|
245
|
+
const old = readRecord(p.value.recordPath, d);
|
|
246
|
+
const target = i.node ?? (old.ok ? h.value.targetOf(old.value.url) : null);
|
|
247
|
+
if (target === null)
|
|
248
|
+
return err("which project? pass --node <id> (the project's id in its url)");
|
|
249
|
+
const sha = sha256Hex(p.value.bytes);
|
|
250
|
+
const name = String(i.to);
|
|
251
|
+
d.log(
|
|
252
|
+
`upload ${basename(p.value.zip)} (${String(p.value.bytes.length)} bytes, sha256 ${sha}) to ${name} project ${target}`,
|
|
253
|
+
);
|
|
254
|
+
d.log(
|
|
255
|
+
`then write ${p.value.recordPath} with the host's hashes and ${String(Object.keys(p.value.numbers).length)} numbers`,
|
|
256
|
+
);
|
|
257
|
+
return ok({ name, host: h.value, p: p.value, target, sha });
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
/** Upload with `token`, check what the host holds, and write the record. */
|
|
261
|
+
async function send(x: Plan, token: string, d: ArtifactDeps): Promise<number> {
|
|
262
|
+
const { p } = x;
|
|
263
|
+
const up = await x.host.upload(
|
|
264
|
+
{ target: x.target, name: basename(p.zip), bytes: p.bytes },
|
|
265
|
+
token,
|
|
266
|
+
);
|
|
267
|
+
if (!up.ok)
|
|
268
|
+
return fail(`upload failed (${up.error.kind}): ${up.error.detail}`, d);
|
|
269
|
+
if (up.value.sha256 !== null && up.value.sha256 !== x.sha) {
|
|
270
|
+
d.err(
|
|
271
|
+
`paperlint artifact: ${x.name} holds sha256 ${up.value.sha256}, not the ${x.sha} sent; no record written`,
|
|
272
|
+
);
|
|
273
|
+
return 1;
|
|
274
|
+
}
|
|
275
|
+
const r = recordFor(p, x.name, up.value.url, d);
|
|
276
|
+
return write(hostedRecord(r, up.value), p, d);
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
async function upload(i: ArtifactInput, d: ArtifactDeps): Promise<number> {
|
|
280
|
+
const x = await plan(i, d);
|
|
281
|
+
if (!x.ok) return fail(x.error, d);
|
|
282
|
+
const token = tokenFor(x.value.host, d);
|
|
283
|
+
// The person's yes names the project (`submission-confirm`), so `--save` takes it from the command
|
|
284
|
+
// line only, never from the record: the project the yes was given for is the one uploaded to.
|
|
285
|
+
if (i.save && i.node === null)
|
|
286
|
+
return fail(
|
|
287
|
+
`--save uploads only to a project named with --node: run again with --save --node ${x.value.target}`,
|
|
288
|
+
d,
|
|
289
|
+
);
|
|
290
|
+
if (i.save)
|
|
291
|
+
return token.ok
|
|
292
|
+
? send(x.value, token.value, d)
|
|
293
|
+
: fail(token.error.join("\n"), d);
|
|
294
|
+
d.log(
|
|
295
|
+
token.ok
|
|
296
|
+
? `token: ${x.value.host.token.variable} is set`
|
|
297
|
+
: token.error.join("\n"),
|
|
298
|
+
);
|
|
299
|
+
d.log(
|
|
300
|
+
`DRY RUN: nothing uploaded, nothing written. Run again with --save --node ${x.value.target} to upload.`,
|
|
301
|
+
);
|
|
302
|
+
return 0;
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
/** The record with the host's own id and hashes, as it read them back. */
|
|
306
|
+
const hostedRecord = (r: UploadRecord, f: HostedFile): UploadRecord => {
|
|
307
|
+
const rest = omit(r, ["sha256", "md5"]);
|
|
308
|
+
return {
|
|
309
|
+
...rest,
|
|
310
|
+
id: f.id,
|
|
311
|
+
...(f.sha256 === null ? {} : { sha256: f.sha256 }),
|
|
312
|
+
...(f.md5 === null ? {} : { md5: f.md5 }),
|
|
313
|
+
};
|
|
314
|
+
};
|
|
315
|
+
|
|
316
|
+
/** The record at `path`, or why there is none to read. */
|
|
317
|
+
function readRecord(
|
|
318
|
+
path: string,
|
|
319
|
+
d: ArtifactDeps,
|
|
320
|
+
): Result<UploadRecord, string> {
|
|
321
|
+
const bytes = d.files.readBytes(callerPath(path));
|
|
322
|
+
if (bytes === null)
|
|
323
|
+
return err(
|
|
324
|
+
`no upload record ${path}: write one with \`paperlint artifact record <zip> --url <url>\``,
|
|
325
|
+
);
|
|
326
|
+
const r = parseUploadRecord(new TextDecoder().decode(bytes));
|
|
327
|
+
return r.ok ? r : err(`the upload record ${path} cannot be read: ${r.error}`);
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
/** Each hash the record and the host both give, and whether they agree. */
|
|
331
|
+
const compared = (r: UploadRecord, f: HostedFile, h: ArtifactHost) =>
|
|
332
|
+
h.can.hashes.flatMap((k) => {
|
|
333
|
+
const mine = r[k];
|
|
334
|
+
const theirs = f[k];
|
|
335
|
+
return mine === undefined || theirs === null
|
|
336
|
+
? []
|
|
337
|
+
: [{ k, theirs, same: mine === theirs }];
|
|
338
|
+
});
|
|
339
|
+
|
|
340
|
+
/** The host's hashes of the recorded file, against the record's: `match yes|no`; the exit code. */
|
|
341
|
+
async function check(
|
|
342
|
+
rec: UploadRecord,
|
|
343
|
+
h: ArtifactHost,
|
|
344
|
+
d: ArtifactDeps,
|
|
345
|
+
): Promise<number> {
|
|
346
|
+
const token = tokenFor(h, d);
|
|
347
|
+
const f = await h.read(
|
|
348
|
+
{ url: rec.url, file: rec.file, id: rec.id ?? null },
|
|
349
|
+
token.ok ? token.value : null,
|
|
350
|
+
);
|
|
351
|
+
if (!f.ok)
|
|
352
|
+
return fail(
|
|
353
|
+
`could not read the file on ${rec.host} (${f.error.kind}): ${f.error.detail}`,
|
|
354
|
+
d,
|
|
355
|
+
);
|
|
356
|
+
const c = compared(rec, f.value, h);
|
|
357
|
+
c.forEach((x) => {
|
|
358
|
+
d.log(` host: ${x.k} ${x.theirs}`);
|
|
359
|
+
});
|
|
360
|
+
if (c.length === 0) {
|
|
361
|
+
d.log("match unknown: the record holds no hash the host gives");
|
|
362
|
+
return 0;
|
|
363
|
+
}
|
|
364
|
+
const yes = c.every((x) => x.same);
|
|
365
|
+
d.log(`match ${yes ? "yes" : "no"}`);
|
|
366
|
+
return yes ? 0 : 1;
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
async function show(i: ArtifactInput, d: ArtifactDeps): Promise<number> {
|
|
370
|
+
const dir = resolve(d.cwd, i.operands[0] ?? ".");
|
|
371
|
+
const r = readRecord(
|
|
372
|
+
resolve(dir, (await paperUploadOptions(dir, d)).record),
|
|
373
|
+
d,
|
|
374
|
+
);
|
|
375
|
+
if (!r.ok) return fail(r.error, d);
|
|
376
|
+
const rec = r.value;
|
|
377
|
+
d.log(`${rec.host} ${rec.url}`);
|
|
378
|
+
d.log(
|
|
379
|
+
` ${rec.file}, uploaded ${rec.uploadedAt}, ${String(Object.keys(rec.numbers).length)} numbers`,
|
|
380
|
+
);
|
|
381
|
+
d.log(` record: sha256 ${rec.sha256 ?? "—"} md5 ${rec.md5 ?? "—"}`);
|
|
382
|
+
const h = d.hosts[rec.host];
|
|
383
|
+
if (h !== undefined && h.can.hashes.length > 0) return check(rec, h, d);
|
|
384
|
+
d.log(
|
|
385
|
+
`paperlint cannot read hashes from ${rec.host}: the record's own are above; compare them on the host's page`,
|
|
386
|
+
);
|
|
387
|
+
return 0;
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
/** `paperlint artifact <sub> …`; the exit code. */
|
|
391
|
+
export async function runArtifact(
|
|
392
|
+
i: ArtifactInput,
|
|
393
|
+
d: ArtifactDeps,
|
|
394
|
+
): Promise<number> {
|
|
395
|
+
if (i.sub === "record") return record(i, d);
|
|
396
|
+
if (i.sub === "upload") return upload(i, d);
|
|
397
|
+
if (i.sub === "show") return show(i, d);
|
|
398
|
+
return fail(
|
|
399
|
+
`${i.sub === undefined ? "no subcommand" : `unknown subcommand ${i.sub}`}: record | upload | show`,
|
|
400
|
+
d,
|
|
401
|
+
);
|
|
402
|
+
}
|
package/src/cli.test.ts
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
*/
|
|
7
7
|
import assert from "node:assert/strict";
|
|
8
8
|
import { spawn } from "node:child_process";
|
|
9
|
-
import { existsSync, readdirSync } from "node:fs";
|
|
9
|
+
import { existsSync, readdirSync, readFileSync } from "node:fs";
|
|
10
10
|
import { createServer } from "node:http";
|
|
11
11
|
import { dirname, join } from "node:path";
|
|
12
12
|
import { test, vi } from "vitest";
|
|
@@ -17,6 +17,7 @@ import {
|
|
|
17
17
|
venuePreset,
|
|
18
18
|
writeTree,
|
|
19
19
|
} from "../test/support.ts";
|
|
20
|
+
import { parseUploadRecord } from "./domain/artifact-upload.ts";
|
|
20
21
|
import type { runToolchain } from "./toolchain.ts";
|
|
21
22
|
import {
|
|
22
23
|
chooseVenue,
|
|
@@ -1218,3 +1219,150 @@ test("versions: migrate moves an old-layout paper on disk into folders per attem
|
|
|
1218
1219
|
"versions/lock.json records every frozen file as it stands",
|
|
1219
1220
|
);
|
|
1220
1221
|
});
|
|
1222
|
+
|
|
1223
|
+
test("artifact: the command is wired — an OSF upload's dry run sends nothing and reads the token's presence from the environment", async () => {
|
|
1224
|
+
const dir = join(root, "artifact-dry-run");
|
|
1225
|
+
writeTree(dir, {
|
|
1226
|
+
"package.json": "{}",
|
|
1227
|
+
"papers/p/artifact.zip": "artifact bytes\n",
|
|
1228
|
+
"papers/p/numbersdef.tex": "\\newcommand{\\nRepos}{64}\n",
|
|
1229
|
+
});
|
|
1230
|
+
vi.stubEnv("OSF_TOKEN_OSF_IO", "not-printed");
|
|
1231
|
+
const r = await cli(
|
|
1232
|
+
[
|
|
1233
|
+
"artifact",
|
|
1234
|
+
"upload",
|
|
1235
|
+
"--to",
|
|
1236
|
+
"osf",
|
|
1237
|
+
"artifact.zip",
|
|
1238
|
+
"--node",
|
|
1239
|
+
"abcde",
|
|
1240
|
+
"--numbers=numbersdef.tex",
|
|
1241
|
+
],
|
|
1242
|
+
join(dir, "papers/p"),
|
|
1243
|
+
);
|
|
1244
|
+
vi.unstubAllEnvs();
|
|
1245
|
+
// Guards: exit 0, the plan, the token said to be set and never shown, nothing uploaded or written.
|
|
1246
|
+
assert.deepEqual(r, {
|
|
1247
|
+
code: 0,
|
|
1248
|
+
err: "",
|
|
1249
|
+
out: [
|
|
1250
|
+
"upload artifact.zip (15 bytes, sha256 59bd16dfc39e768f82bb8ec74467e571e80ed284683b586f077dbf1aa2483ecd) to osf project abcde",
|
|
1251
|
+
`then write ${join(dir, "papers/p/submission/artifact-upload.json")} with the host's hashes and 1 numbers`,
|
|
1252
|
+
"token: OSF_TOKEN_OSF_IO is set",
|
|
1253
|
+
"DRY RUN: nothing uploaded, nothing written. Run again with --save --node abcde to upload.",
|
|
1254
|
+
].join("\n"),
|
|
1255
|
+
});
|
|
1256
|
+
assert.equal(existsSync(join(dir, "papers/p/submission")), false);
|
|
1257
|
+
});
|
|
1258
|
+
|
|
1259
|
+
test("artifact record: the rule's options from a list of blocks in the paper's paperlint.json, the same ones lint uses", async () => {
|
|
1260
|
+
const dir = join(root, "artifact-blocks");
|
|
1261
|
+
writeTree(dir, {
|
|
1262
|
+
"package.json": "{}",
|
|
1263
|
+
"papers/p/paper.tex": "\\input{numbersdef}\\nRepos\n",
|
|
1264
|
+
"papers/p/artifact.zip": "artifact bytes\n",
|
|
1265
|
+
"papers/p/numbersdef.tex": "\\newcommand{\\nRepos}{64}\n",
|
|
1266
|
+
"papers/p/paperlint.json": JSON.stringify({
|
|
1267
|
+
rules: [
|
|
1268
|
+
{
|
|
1269
|
+
files: ["PIPELINE-STATUS.md"],
|
|
1270
|
+
rules: {
|
|
1271
|
+
"artifact/upload-current": [
|
|
1272
|
+
"warn",
|
|
1273
|
+
{ numbers: "numbersdef.tex", record: "custom/upload.json" },
|
|
1274
|
+
],
|
|
1275
|
+
},
|
|
1276
|
+
},
|
|
1277
|
+
],
|
|
1278
|
+
}),
|
|
1279
|
+
});
|
|
1280
|
+
const r = await cli(
|
|
1281
|
+
["artifact", "record", "artifact.zip", "--url", "https://osf.io/abcde/"],
|
|
1282
|
+
join(dir, "papers/p"),
|
|
1283
|
+
);
|
|
1284
|
+
// Guards: no --numbers needed, and the record goes where the rule will read it.
|
|
1285
|
+
assert.equal(r.code, 0, r.err);
|
|
1286
|
+
const written = parseUploadRecord(
|
|
1287
|
+
readFileSync(join(dir, "papers/p/custom/upload.json"), "utf8"),
|
|
1288
|
+
);
|
|
1289
|
+
assert.deepEqual(written.ok && written.value.numbers, { nRepos: "64" });
|
|
1290
|
+
assert.equal(
|
|
1291
|
+
existsSync(join(dir, "papers/p/submission/artifact-upload.json")),
|
|
1292
|
+
false,
|
|
1293
|
+
);
|
|
1294
|
+
});
|
|
1295
|
+
|
|
1296
|
+
test("artifact record: settings lint cannot read leave the defaults, and --numbers still records", async () => {
|
|
1297
|
+
const cases = {
|
|
1298
|
+
"a project paperlint.json that does not parse": {
|
|
1299
|
+
"paperlint.json": "{",
|
|
1300
|
+
},
|
|
1301
|
+
"a paper paperlint.json naming a rule paperlint does not ship": {
|
|
1302
|
+
"papers/p/paperlint.json": JSON.stringify({
|
|
1303
|
+
rules: { "no/such-rule": "warn" },
|
|
1304
|
+
}),
|
|
1305
|
+
},
|
|
1306
|
+
};
|
|
1307
|
+
for (const [name, settings] of Object.entries(cases)) {
|
|
1308
|
+
const dir = join(
|
|
1309
|
+
root,
|
|
1310
|
+
`artifact-unread-${String(Object.keys(cases).indexOf(name))}`,
|
|
1311
|
+
);
|
|
1312
|
+
writeTree(dir, {
|
|
1313
|
+
"package.json": "{}",
|
|
1314
|
+
"papers/p/paper.tex": "\\nRepos\n",
|
|
1315
|
+
"papers/p/artifact.zip": "artifact bytes\n",
|
|
1316
|
+
"papers/p/numbersdef.tex": "\\newcommand{\\nRepos}{64}\n",
|
|
1317
|
+
...settings,
|
|
1318
|
+
});
|
|
1319
|
+
const r = await cli(
|
|
1320
|
+
[
|
|
1321
|
+
"artifact",
|
|
1322
|
+
"record",
|
|
1323
|
+
"artifact.zip",
|
|
1324
|
+
"--url",
|
|
1325
|
+
"https://osf.io/abcde/",
|
|
1326
|
+
"--numbers",
|
|
1327
|
+
"numbersdef.tex",
|
|
1328
|
+
],
|
|
1329
|
+
join(dir, "papers/p"),
|
|
1330
|
+
);
|
|
1331
|
+
// Guards: the record is written at the default path; lint, not this command, reports the settings.
|
|
1332
|
+
assert.equal(r.code, 0, `${name}: ${r.err}`);
|
|
1333
|
+
assert.equal(
|
|
1334
|
+
existsSync(join(dir, "papers/p/submission/artifact-upload.json")),
|
|
1335
|
+
true,
|
|
1336
|
+
name,
|
|
1337
|
+
);
|
|
1338
|
+
}
|
|
1339
|
+
});
|
|
1340
|
+
|
|
1341
|
+
test("parseArgs: artifact's flags, each a value", () => {
|
|
1342
|
+
assert.deepEqual(
|
|
1343
|
+
parseArgs([
|
|
1344
|
+
"artifact",
|
|
1345
|
+
"record",
|
|
1346
|
+
"a.zip",
|
|
1347
|
+
"--url",
|
|
1348
|
+
"https://x.org",
|
|
1349
|
+
"--host=zenodo",
|
|
1350
|
+
"--to",
|
|
1351
|
+
"osf",
|
|
1352
|
+
"--node",
|
|
1353
|
+
"abcde",
|
|
1354
|
+
"--numbers",
|
|
1355
|
+
"n.tex",
|
|
1356
|
+
]),
|
|
1357
|
+
{
|
|
1358
|
+
...parseArgs([]),
|
|
1359
|
+
cmd: "artifact",
|
|
1360
|
+
paths: ["record", "a.zip"],
|
|
1361
|
+
url: "https://x.org",
|
|
1362
|
+
host: "zenodo",
|
|
1363
|
+
to: "osf",
|
|
1364
|
+
node: "abcde",
|
|
1365
|
+
numbers: "n.tex",
|
|
1366
|
+
},
|
|
1367
|
+
);
|
|
1368
|
+
});
|