@metacensus/library-research 0.13.0 → 0.15.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 +43 -11
- package/dist/commands/add.js +29 -12
- package/dist/commands/check.js +1 -1
- package/dist/commands/dev.js +24 -13
- package/dist/commands/reshape.d.ts +2 -0
- package/dist/commands/reshape.js +21 -0
- package/dist/context.d.ts +3 -1
- package/dist/context.js +2 -0
- package/dist/corpus.d.ts +432 -0
- package/dist/corpus.js +348 -0
- package/dist/data.d.ts +16 -12
- package/dist/data.js +38 -26
- package/dist/datasets.d.ts +8 -0
- package/dist/{roles.js → datasets.js} +35 -29
- package/dist/dev/host.d.ts +7 -1
- package/dist/dev/host.js +15 -13
- package/dist/host.d.ts +3 -1
- package/dist/host.js +6 -2
- package/dist/index.d.ts +3 -2
- package/dist/index.js +2 -1
- package/dist/mount.d.ts +7 -3
- package/dist/mount.js +7 -7
- package/dist/papers.js +3 -2
- package/dist/playwright.d.ts +1 -1
- package/dist/playwright.js +24 -6
- package/dist/project-id.d.ts +4 -0
- package/dist/project-id.js +7 -0
- package/dist/react.d.ts +18 -18
- package/dist/react.js +60 -37
- package/dist/reader.d.ts +18 -4
- package/dist/reader.js +25 -2
- package/dist/schema.d.ts +10 -35
- package/dist/schema.js +57 -34
- package/dist/testing.d.ts +11 -6
- package/dist/testing.js +12 -10
- package/dist/types.d.ts +62 -13
- package/dist/vite-config.js +2 -12
- package/dist/vite.d.ts +10 -2
- package/dist/vite.js +71 -19
- package/dist/zod.d.ts +2 -0
- package/dist/zod.js +4 -0
- package/package.json +14 -14
- package/schema/corpus.schema.json +3949 -29
- package/schema/project-id.schema.json +6 -0
- package/schema/research.schema.json +33 -7
- package/dist/roles.d.ts +0 -7
package/dist/corpus.js
ADDED
|
@@ -0,0 +1,348 @@
|
|
|
1
|
+
import { z } from './zod.js';
|
|
2
|
+
export const kinds = {
|
|
3
|
+
pdf: 'application/pdf',
|
|
4
|
+
doc: 'application/msword',
|
|
5
|
+
docx: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
|
|
6
|
+
xls: 'application/vnd.ms-excel',
|
|
7
|
+
xlsx: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
|
|
8
|
+
zip: 'application/zip',
|
|
9
|
+
gif: 'image/gif',
|
|
10
|
+
jpg: 'image/jpeg',
|
|
11
|
+
tif: 'image/tiff',
|
|
12
|
+
txt: 'text/plain',
|
|
13
|
+
html: 'text/plain',
|
|
14
|
+
xml: 'text/plain',
|
|
15
|
+
};
|
|
16
|
+
const kind = z
|
|
17
|
+
.enum(Object.keys(kinds))
|
|
18
|
+
.describe('the file kind, one of kinds.json; the file is papers/<sha>.<kind>');
|
|
19
|
+
const sha = z
|
|
20
|
+
.string()
|
|
21
|
+
.regex(/^[0-9a-f]{64}$/)
|
|
22
|
+
.describe("the sha256 of the file's bytes, lowercase hex");
|
|
23
|
+
// CSL-JSON 1.0.2's item types and variables (csl-data.json), less `id`, `DOI`
|
|
24
|
+
// and `citation-key`, which a work's id holds.
|
|
25
|
+
const types = [
|
|
26
|
+
'article',
|
|
27
|
+
'article-journal',
|
|
28
|
+
'article-magazine',
|
|
29
|
+
'article-newspaper',
|
|
30
|
+
'bill',
|
|
31
|
+
'book',
|
|
32
|
+
'broadcast',
|
|
33
|
+
'chapter',
|
|
34
|
+
'classic',
|
|
35
|
+
'collection',
|
|
36
|
+
'dataset',
|
|
37
|
+
'document',
|
|
38
|
+
'entry',
|
|
39
|
+
'entry-dictionary',
|
|
40
|
+
'entry-encyclopedia',
|
|
41
|
+
'event',
|
|
42
|
+
'figure',
|
|
43
|
+
'graphic',
|
|
44
|
+
'hearing',
|
|
45
|
+
'interview',
|
|
46
|
+
'legal_case',
|
|
47
|
+
'legislation',
|
|
48
|
+
'manuscript',
|
|
49
|
+
'map',
|
|
50
|
+
'motion_picture',
|
|
51
|
+
'musical_score',
|
|
52
|
+
'pamphlet',
|
|
53
|
+
'paper-conference',
|
|
54
|
+
'patent',
|
|
55
|
+
'performance',
|
|
56
|
+
'periodical',
|
|
57
|
+
'personal_communication',
|
|
58
|
+
'post',
|
|
59
|
+
'post-weblog',
|
|
60
|
+
'regulation',
|
|
61
|
+
'report',
|
|
62
|
+
'review',
|
|
63
|
+
'review-book',
|
|
64
|
+
'software',
|
|
65
|
+
'song',
|
|
66
|
+
'speech',
|
|
67
|
+
'standard',
|
|
68
|
+
'thesis',
|
|
69
|
+
'treaty',
|
|
70
|
+
'webpage',
|
|
71
|
+
];
|
|
72
|
+
const names = [
|
|
73
|
+
'author',
|
|
74
|
+
'chair',
|
|
75
|
+
'collection-editor',
|
|
76
|
+
'compiler',
|
|
77
|
+
'composer',
|
|
78
|
+
'container-author',
|
|
79
|
+
'contributor',
|
|
80
|
+
'curator',
|
|
81
|
+
'director',
|
|
82
|
+
'editor',
|
|
83
|
+
'editorial-director',
|
|
84
|
+
'executive-producer',
|
|
85
|
+
'guest',
|
|
86
|
+
'host',
|
|
87
|
+
'illustrator',
|
|
88
|
+
'interviewer',
|
|
89
|
+
'narrator',
|
|
90
|
+
'organizer',
|
|
91
|
+
'original-author',
|
|
92
|
+
'performer',
|
|
93
|
+
'producer',
|
|
94
|
+
'recipient',
|
|
95
|
+
'reviewed-author',
|
|
96
|
+
'script-writer',
|
|
97
|
+
'series-creator',
|
|
98
|
+
'translator',
|
|
99
|
+
];
|
|
100
|
+
const dates = [
|
|
101
|
+
'accessed',
|
|
102
|
+
'available-date',
|
|
103
|
+
'event-date',
|
|
104
|
+
'issued',
|
|
105
|
+
'original-date',
|
|
106
|
+
'submitted',
|
|
107
|
+
];
|
|
108
|
+
const numbers = [
|
|
109
|
+
'chapter-number',
|
|
110
|
+
'citation-number',
|
|
111
|
+
'collection-number',
|
|
112
|
+
'edition',
|
|
113
|
+
'first-reference-note-number',
|
|
114
|
+
'issue',
|
|
115
|
+
'number',
|
|
116
|
+
'number-of-pages',
|
|
117
|
+
'number-of-volumes',
|
|
118
|
+
'page',
|
|
119
|
+
'page-first',
|
|
120
|
+
'part',
|
|
121
|
+
'printing',
|
|
122
|
+
'supplement',
|
|
123
|
+
'version',
|
|
124
|
+
'volume',
|
|
125
|
+
];
|
|
126
|
+
const strings = [
|
|
127
|
+
'abstract',
|
|
128
|
+
'annote',
|
|
129
|
+
'archive',
|
|
130
|
+
'archive_collection',
|
|
131
|
+
'archive_location',
|
|
132
|
+
'archive-place',
|
|
133
|
+
'authority',
|
|
134
|
+
'call-number',
|
|
135
|
+
'citation-label',
|
|
136
|
+
'collection-title',
|
|
137
|
+
'container-title',
|
|
138
|
+
'container-title-short',
|
|
139
|
+
'dimensions',
|
|
140
|
+
'division',
|
|
141
|
+
'event',
|
|
142
|
+
'event-place',
|
|
143
|
+
'event-title',
|
|
144
|
+
'genre',
|
|
145
|
+
'ISBN',
|
|
146
|
+
'ISSN',
|
|
147
|
+
'journalAbbreviation',
|
|
148
|
+
'jurisdiction',
|
|
149
|
+
'keyword',
|
|
150
|
+
'language',
|
|
151
|
+
'locator',
|
|
152
|
+
'medium',
|
|
153
|
+
'note',
|
|
154
|
+
'original-publisher',
|
|
155
|
+
'original-publisher-place',
|
|
156
|
+
'original-title',
|
|
157
|
+
'part-title',
|
|
158
|
+
'PMCID',
|
|
159
|
+
'PMID',
|
|
160
|
+
'publisher',
|
|
161
|
+
'publisher-place',
|
|
162
|
+
'references',
|
|
163
|
+
'reviewed-genre',
|
|
164
|
+
'reviewed-title',
|
|
165
|
+
'scale',
|
|
166
|
+
'section',
|
|
167
|
+
'shortTitle',
|
|
168
|
+
'source',
|
|
169
|
+
'status',
|
|
170
|
+
'title',
|
|
171
|
+
'title-short',
|
|
172
|
+
'URL',
|
|
173
|
+
'volume-title',
|
|
174
|
+
'volume-title-short',
|
|
175
|
+
'year-suffix',
|
|
176
|
+
];
|
|
177
|
+
const name = z
|
|
178
|
+
.array(z.strictObject({
|
|
179
|
+
family: z.string().optional(),
|
|
180
|
+
given: z.string().optional(),
|
|
181
|
+
'dropping-particle': z.string().optional(),
|
|
182
|
+
'non-dropping-particle': z.string().optional(),
|
|
183
|
+
suffix: z.string().optional(),
|
|
184
|
+
'comma-suffix': z.union([z.string(), z.number(), z.boolean()]).optional(),
|
|
185
|
+
'static-ordering': z
|
|
186
|
+
.union([z.string(), z.number(), z.boolean()])
|
|
187
|
+
.optional(),
|
|
188
|
+
literal: z.string().optional(),
|
|
189
|
+
'parse-names': z.union([z.string(), z.number(), z.boolean()]).optional(),
|
|
190
|
+
}))
|
|
191
|
+
.describe('a CSL name variable: its people, in order');
|
|
192
|
+
const part = z.union([z.string(), z.number()]);
|
|
193
|
+
const date = z
|
|
194
|
+
.union([
|
|
195
|
+
z.string(),
|
|
196
|
+
z.strictObject({
|
|
197
|
+
'date-parts': z
|
|
198
|
+
.array(z.array(part).min(1).max(3))
|
|
199
|
+
.min(1)
|
|
200
|
+
.max(2)
|
|
201
|
+
.optional(),
|
|
202
|
+
season: part.optional(),
|
|
203
|
+
circa: z.union([z.string(), z.number(), z.boolean()]).optional(),
|
|
204
|
+
literal: z.string().optional(),
|
|
205
|
+
raw: z.string().optional(),
|
|
206
|
+
}),
|
|
207
|
+
])
|
|
208
|
+
.describe('a CSL date variable: an EDTF string, or date-parts');
|
|
209
|
+
const number = z.union([z.string(), z.number()]);
|
|
210
|
+
const string = z.string();
|
|
211
|
+
const variables = (keys, schema) => Object.fromEntries(keys.map((key) => [key, schema.optional()]));
|
|
212
|
+
const base = {
|
|
213
|
+
type: z.enum(types).optional(),
|
|
214
|
+
categories: z.array(z.string()).optional(),
|
|
215
|
+
custom: z.record(z.string(), z.unknown()).optional(),
|
|
216
|
+
};
|
|
217
|
+
export const Csl = z
|
|
218
|
+
.strictObject({
|
|
219
|
+
...base,
|
|
220
|
+
...variables(names, name),
|
|
221
|
+
...variables(dates, date),
|
|
222
|
+
...variables(numbers, number),
|
|
223
|
+
...variables(strings, string),
|
|
224
|
+
})
|
|
225
|
+
.describe("CSL-JSON 1.0.2's item metadata; its id, DOI and citation-key are the work's id");
|
|
226
|
+
const field = Csl.keyof();
|
|
227
|
+
export const Paper = z
|
|
228
|
+
.strictObject({
|
|
229
|
+
kind,
|
|
230
|
+
open: z
|
|
231
|
+
.literal(true)
|
|
232
|
+
.optional()
|
|
233
|
+
.describe('the file may be served to anyone, not only the project'),
|
|
234
|
+
})
|
|
235
|
+
.describe('one held file');
|
|
236
|
+
export const Edition = z
|
|
237
|
+
.strictObject({
|
|
238
|
+
csl: Csl.optional().describe("the fields where it differs from its work's"),
|
|
239
|
+
papers: z
|
|
240
|
+
.record(sha, Paper)
|
|
241
|
+
.describe('the files held of it, by sha; empty for one named but not held'),
|
|
242
|
+
})
|
|
243
|
+
.describe('one edition of a work');
|
|
244
|
+
export const Work = z
|
|
245
|
+
.strictObject({
|
|
246
|
+
csl: Csl,
|
|
247
|
+
editions: z.array(Edition).min(1).describe('its editions, at least one'),
|
|
248
|
+
pending: z
|
|
249
|
+
.array(field)
|
|
250
|
+
.refine((pending) => new Set(pending).size === pending.length, 'pending repeats')
|
|
251
|
+
.meta({ uniqueItems: true })
|
|
252
|
+
.optional()
|
|
253
|
+
.describe('the CSL fields that wait on a person'),
|
|
254
|
+
})
|
|
255
|
+
.superRefine(({ csl, pending = [] }, ctx) => {
|
|
256
|
+
for (const key of ['title', 'type'])
|
|
257
|
+
if (csl[key] === undefined && !pending.includes(key))
|
|
258
|
+
ctx.addIssue({
|
|
259
|
+
code: 'custom',
|
|
260
|
+
path: ['csl', key],
|
|
261
|
+
message: `${key} is required unless pending lists it`,
|
|
262
|
+
});
|
|
263
|
+
})
|
|
264
|
+
.describe('one work: its metadata and its editions; csl.title and csl.type are required unless pending lists them');
|
|
265
|
+
const doi = z
|
|
266
|
+
.string()
|
|
267
|
+
.regex(/^10\.\d{4,9}(\.\d+)*\/[^\sA-Z]+$/)
|
|
268
|
+
.describe('a DOI, lowercase so a key is unique');
|
|
269
|
+
const minted = z
|
|
270
|
+
.string()
|
|
271
|
+
.regex(/^(?!sha-)[a-z][a-z0-9-]*$/)
|
|
272
|
+
.describe('a citation key the project mints');
|
|
273
|
+
const placeholder = z
|
|
274
|
+
.string()
|
|
275
|
+
.regex(/^sha-[0-9a-f]{12}$/)
|
|
276
|
+
.describe("the loader's key, from its sha; the key waits on a person");
|
|
277
|
+
export const Corpus = z
|
|
278
|
+
.record(z.union([doi, minted, placeholder]), Work)
|
|
279
|
+
.superRefine((corpus, ctx) => {
|
|
280
|
+
const seen = new Map();
|
|
281
|
+
for (const [key, work] of Object.entries(corpus))
|
|
282
|
+
for (const edition of work.editions)
|
|
283
|
+
for (const held of Object.keys(edition.papers)) {
|
|
284
|
+
if (seen.has(held))
|
|
285
|
+
ctx.addIssue({
|
|
286
|
+
code: 'custom',
|
|
287
|
+
path: [key],
|
|
288
|
+
message: `${held} is held by ${seen.get(held)} and ${key}`,
|
|
289
|
+
});
|
|
290
|
+
seen.set(held, key);
|
|
291
|
+
}
|
|
292
|
+
})
|
|
293
|
+
.describe('every work by id: a DOI, a minted citation key, or a placeholder');
|
|
294
|
+
/** The corpus before works: one entry per file, by sha. */
|
|
295
|
+
export const LegacyCorpus = z.record(sha, z.strictObject({
|
|
296
|
+
kind,
|
|
297
|
+
title: z.string().optional(),
|
|
298
|
+
citation: z.string().optional(),
|
|
299
|
+
open: z.literal(true).optional(),
|
|
300
|
+
}));
|
|
301
|
+
/** Every held file by sha, with the work and edition holding it. */
|
|
302
|
+
export function papers(corpus) {
|
|
303
|
+
const index = new Map();
|
|
304
|
+
for (const [key, work] of Object.entries(corpus))
|
|
305
|
+
for (const edition of work.editions)
|
|
306
|
+
for (const [sha, paper] of Object.entries(edition.papers))
|
|
307
|
+
index.set(sha, { key, work, edition, paper });
|
|
308
|
+
return index;
|
|
309
|
+
}
|
|
310
|
+
/** What waits on a person: each placeholder's key, and each pending field. */
|
|
311
|
+
export function pending(corpus) {
|
|
312
|
+
return Object.entries(corpus).flatMap(([key, work]) => {
|
|
313
|
+
const shas = work.editions.flatMap(({ papers }) => Object.keys(papers));
|
|
314
|
+
const judgments = [
|
|
315
|
+
...(placeholder.safeParse(key).success ? ['key'] : []),
|
|
316
|
+
...(work.pending ?? []),
|
|
317
|
+
];
|
|
318
|
+
return judgments.map((judgment) => ({ work: key, shas, judgment }));
|
|
319
|
+
});
|
|
320
|
+
}
|
|
321
|
+
/** The placeholder key of the file `sha`. */
|
|
322
|
+
export const placeholderOf = (sha) => `sha-${sha.slice(0, 12)}`;
|
|
323
|
+
/** One legacy entry as a work of one edition, judging nothing. */
|
|
324
|
+
export function workOf(sha, { kind, title, citation, open }) {
|
|
325
|
+
const csl = {};
|
|
326
|
+
if (title !== undefined)
|
|
327
|
+
csl.title = title;
|
|
328
|
+
if (citation !== undefined)
|
|
329
|
+
csl.note = citation;
|
|
330
|
+
const pending = [
|
|
331
|
+
...(title === undefined ? ['title'] : []),
|
|
332
|
+
'type',
|
|
333
|
+
...(citation === undefined ? [] : ['note']),
|
|
334
|
+
];
|
|
335
|
+
const paper = open ? { kind, open } : { kind };
|
|
336
|
+
return { csl, editions: [{ papers: { [sha]: paper } }], pending };
|
|
337
|
+
}
|
|
338
|
+
/** Today's corpus in the new shape, one work per sha, and the judgments it leaves. */
|
|
339
|
+
export function reshape(legacy) {
|
|
340
|
+
const corpus = {};
|
|
341
|
+
for (const [sha, entry] of Object.entries(legacy)) {
|
|
342
|
+
const key = placeholderOf(sha);
|
|
343
|
+
if (corpus[key])
|
|
344
|
+
throw new Error(`${key}: ${Object.keys(corpus[key].editions[0].papers)[0]} and ${sha} share a key`);
|
|
345
|
+
corpus[key] = workOf(sha, entry);
|
|
346
|
+
}
|
|
347
|
+
return { corpus, judgments: pending(corpus) };
|
|
348
|
+
}
|
package/dist/data.d.ts
CHANGED
|
@@ -1,23 +1,27 @@
|
|
|
1
1
|
import { z } from 'zod';
|
|
2
|
-
|
|
2
|
+
import type { Corpus as Parsed } from './types.js';
|
|
3
|
+
/** Reads a project's research.json; each dataset reads its files when called. */
|
|
3
4
|
export declare function project(dir: string): {
|
|
4
5
|
title: string;
|
|
5
|
-
|
|
6
|
+
datasets: Record<string, () => unknown>;
|
|
6
7
|
pages: Record<string, () => Record<string, string>>;
|
|
7
|
-
people: Record<string,
|
|
8
|
+
people: Record<string, {
|
|
9
|
+
name: string;
|
|
10
|
+
role?: string | undefined;
|
|
11
|
+
pages?: string[] | undefined;
|
|
12
|
+
}> | undefined;
|
|
13
|
+
roles: string[] | undefined;
|
|
8
14
|
};
|
|
9
|
-
/** The files a private
|
|
10
|
-
export declare function privatePages(dir: string,
|
|
11
|
-
/** The files a
|
|
15
|
+
/** The files a private dataset's glob matches, by the page each names; throws on a key a page cannot have. */
|
|
16
|
+
export declare function privatePages(dir: string, dataset: string, glob: string): Record<string, string>;
|
|
17
|
+
/** The files a dataset's glob matches; a wildcard keys each by what it matched. */
|
|
12
18
|
export declare function files(dir: string, glob: string): {
|
|
13
19
|
keyed: boolean;
|
|
14
20
|
entries: [file: string, key: string][];
|
|
15
21
|
};
|
|
16
22
|
export declare function match(dir: string, { keyed, entries }: ReturnType<typeof files>): unknown;
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
open?: true | undefined;
|
|
22
|
-
}>;
|
|
23
|
+
/** The project's corpus.json, `{}` where it has none; a legacy corpus fails, naming `reshape`. */
|
|
24
|
+
export declare function readCorpus(dir: string): Parsed;
|
|
25
|
+
/** Writes corpus.json, one work per line, sorted by key. */
|
|
26
|
+
export declare function writeCorpus(dir: string, corpus: Parsed): void;
|
|
23
27
|
export declare function read<T>(dir: string, file: string, schema?: z.ZodType<T>): T;
|
package/dist/data.js
CHANGED
|
@@ -1,36 +1,37 @@
|
|
|
1
|
-
import { globSync, lstatSync, readFileSync, realpathSync } from 'node:fs';
|
|
1
|
+
import { globSync, lstatSync, readFileSync, realpathSync, writeFileSync, } from 'node:fs';
|
|
2
2
|
import { join, sep } from 'node:path';
|
|
3
|
+
import { LegacyCorpus } from './corpus.js';
|
|
3
4
|
import { Corpus, Research } from './schema.js';
|
|
4
|
-
/** Reads a project's research.json; each
|
|
5
|
+
/** Reads a project's research.json; each dataset reads its files when called. */
|
|
5
6
|
export function project(dir) {
|
|
6
|
-
const { title, data, private: held = {}, people, } = read(dir, 'research.json', Research);
|
|
7
|
-
const
|
|
7
|
+
const { title, data, private: held = {}, people, roles, } = read(dir, 'research.json', Research);
|
|
8
|
+
const datasets = {
|
|
8
9
|
corpus: () => readCorpus(dir),
|
|
9
10
|
};
|
|
10
|
-
for (const [
|
|
11
|
-
|
|
11
|
+
for (const [dataset, glob] of Object.entries(data))
|
|
12
|
+
datasets[dataset] = () => match(dir, files(dir, glob));
|
|
12
13
|
const pages = {};
|
|
13
|
-
for (const [
|
|
14
|
-
pages[
|
|
15
|
-
return { title,
|
|
14
|
+
for (const [dataset, glob] of Object.entries(held))
|
|
15
|
+
pages[dataset] = () => privatePages(dir, dataset, glob);
|
|
16
|
+
return { title, datasets, pages, people, roles };
|
|
16
17
|
}
|
|
17
|
-
/** The files a private
|
|
18
|
-
export function privatePages(dir,
|
|
18
|
+
/** The files a private dataset's glob matches, by the page each names; throws on a key a page cannot have. */
|
|
19
|
+
export function privatePages(dir, dataset, glob) {
|
|
19
20
|
const { entries } = files(dir, glob);
|
|
20
21
|
const bad = entries.filter(([, key]) => !/^[a-z0-9][a-z0-9-]*$/.test(key));
|
|
21
22
|
if (bad.length)
|
|
22
23
|
throw new Error(bad
|
|
23
|
-
.map(([file, key]) => `${join(dir, file)}: private
|
|
24
|
+
.map(([file, key]) => `${join(dir, file)}: private dataset "${dataset}": page "${key}" is not [a-z0-9][a-z0-9-]*`)
|
|
24
25
|
.join('\n'));
|
|
25
26
|
const pages = {};
|
|
26
27
|
for (const [file, key] of entries) {
|
|
27
28
|
if (Object.hasOwn(pages, key))
|
|
28
|
-
throw new Error(`private
|
|
29
|
+
throw new Error(`private dataset "${dataset}": page "${key}" matched by ${pages[key]} and ${file}`);
|
|
29
30
|
pages[key] = file;
|
|
30
31
|
}
|
|
31
32
|
return pages;
|
|
32
33
|
}
|
|
33
|
-
/** The files a
|
|
34
|
+
/** The files a dataset's glob matches; a wildcard keys each by what it matched. */
|
|
34
35
|
export function files(dir, glob) {
|
|
35
36
|
const found = globSync(glob, { cwd: dir }).sort();
|
|
36
37
|
if (!found.length)
|
|
@@ -48,14 +49,24 @@ export function match(dir, { keyed, entries }) {
|
|
|
48
49
|
? Object.fromEntries(entries.map(([file, key]) => [key, read(dir, file)]))
|
|
49
50
|
: read(dir, entries[0][0]);
|
|
50
51
|
}
|
|
52
|
+
/** The project's corpus.json, `{}` where it has none; a legacy corpus fails, naming `reshape`. */
|
|
51
53
|
export function readCorpus(dir) {
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
54
|
+
if (!lstatSync(join(dir, 'corpus.json'), { throwIfNoEntry: false }))
|
|
55
|
+
return {};
|
|
56
|
+
const json = read(dir, 'corpus.json');
|
|
57
|
+
if (!Corpus.safeParse(json).success && LegacyCorpus.safeParse(json).success)
|
|
58
|
+
throw new Error(`${join(dir, 'corpus.json')}: the legacy shape; run npx library-research reshape`);
|
|
59
|
+
return checked(join(dir, 'corpus.json'), json, Corpus);
|
|
60
|
+
}
|
|
61
|
+
/** Writes corpus.json, one work per line, sorted by key. */
|
|
62
|
+
export function writeCorpus(dir, corpus) {
|
|
63
|
+
const lines = Object.entries(Corpus.parse(corpus))
|
|
64
|
+
.sort(([a], [b]) => a.localeCompare(b))
|
|
65
|
+
.map(([key, work]) => ` "${key}": ${JSON.stringify(work)}`);
|
|
66
|
+
writeFileSync(join(dir, 'corpus.json'), `{\n${lines.join(',\n')}\n}\n`);
|
|
55
67
|
}
|
|
56
68
|
export function read(dir, file, schema) {
|
|
57
69
|
const path = join(dir, file);
|
|
58
|
-
const fail = (why) => new Error(`${path}: ${why}`);
|
|
59
70
|
let json;
|
|
60
71
|
try {
|
|
61
72
|
if (!realpathSync(path).startsWith(realpathSync(dir) + sep))
|
|
@@ -63,14 +74,15 @@ export function read(dir, file, schema) {
|
|
|
63
74
|
json = JSON.parse(readFileSync(path, 'utf8'));
|
|
64
75
|
}
|
|
65
76
|
catch (e) {
|
|
66
|
-
throw
|
|
77
|
+
throw new Error(`${path}: ${e.message}`);
|
|
67
78
|
}
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
79
|
+
return schema ? checked(path, json, schema) : json;
|
|
80
|
+
}
|
|
81
|
+
function checked(path, json, schema) {
|
|
82
|
+
const parsed = schema.safeParse(json);
|
|
83
|
+
if (!parsed.success)
|
|
84
|
+
throw new Error(`${path}: ${parsed.error.issues
|
|
73
85
|
.map(({ path, message }) => `${path.join('.')}: ${message}`)
|
|
74
|
-
.join('; '));
|
|
75
|
-
return
|
|
86
|
+
.join('; ')}`);
|
|
87
|
+
return parsed.data;
|
|
76
88
|
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Reads each dataset of the project in `dir`, the folder holding
|
|
3
|
+
* research.json, parsed by its schema in ui/datasets.ts, and its corpus. A
|
|
4
|
+
* private dataset is read as `{ page: parsed }`, its schema parsing one page's
|
|
5
|
+
* file. A `roles` that ui/datasets.ts exports, the tuple `Register` types
|
|
6
|
+
* roles from, must equal research.json's. Throws one line per failure.
|
|
7
|
+
*/
|
|
8
|
+
export declare function load(dir: string): Promise<Record<string, unknown>>;
|
|
@@ -3,54 +3,60 @@ import { join } from 'node:path';
|
|
|
3
3
|
import { pathToFileURL } from 'node:url';
|
|
4
4
|
import { files, match, privatePages, read, readCorpus } from './data.js';
|
|
5
5
|
import { Research } from './schema.js';
|
|
6
|
-
const schemaFile = 'ui/
|
|
7
|
-
/** The `roles` ui/
|
|
6
|
+
const schemaFile = 'ui/datasets.ts';
|
|
7
|
+
/** The `datasets` and `roles` ui/datasets.ts exports, which Node loads by stripping its types. */
|
|
8
8
|
async function schemas(dir) {
|
|
9
9
|
const path = join(dir, schemaFile);
|
|
10
10
|
if (!lstatSync(path, { throwIfNoEntry: false }))
|
|
11
|
-
return {};
|
|
11
|
+
return { datasets: {} };
|
|
12
12
|
const manifest = join(dir, 'ui/package.json');
|
|
13
13
|
const { type } = JSON.parse(readFileSync(manifest, 'utf8'));
|
|
14
14
|
if (type !== 'module')
|
|
15
15
|
throw new Error(`${manifest}: needs "type": "module" for ${schemaFile}`);
|
|
16
|
-
let
|
|
16
|
+
let exported;
|
|
17
17
|
try {
|
|
18
|
-
|
|
19
|
-
.roles;
|
|
18
|
+
exported = (await import(pathToFileURL(path).href));
|
|
20
19
|
}
|
|
21
20
|
catch (error) {
|
|
22
21
|
throw new Error(`${path}: ${error.message.split('\n')[0]}`);
|
|
23
22
|
}
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
23
|
+
const { datasets, roles } = exported;
|
|
24
|
+
if (typeof datasets !== 'object' || !datasets)
|
|
25
|
+
throw new Error(`${path}: exports no datasets object`);
|
|
26
|
+
return { datasets: datasets, roles };
|
|
27
27
|
}
|
|
28
|
+
const sameRoles = (typed, roles) => Array.isArray(typed) &&
|
|
29
|
+
typed.length === roles.length &&
|
|
30
|
+
roles.every((role) => typed.includes(role));
|
|
28
31
|
/**
|
|
29
|
-
* Reads each
|
|
30
|
-
* research.json, parsed by its schema in ui/
|
|
31
|
-
* private
|
|
32
|
-
* file.
|
|
32
|
+
* Reads each dataset of the project in `dir`, the folder holding
|
|
33
|
+
* research.json, parsed by its schema in ui/datasets.ts, and its corpus. A
|
|
34
|
+
* private dataset is read as `{ page: parsed }`, its schema parsing one page's
|
|
35
|
+
* file. A `roles` that ui/datasets.ts exports, the tuple `Register` types
|
|
36
|
+
* roles from, must equal research.json's. Throws one line per failure.
|
|
33
37
|
*/
|
|
34
38
|
export async function load(dir) {
|
|
35
|
-
const { data, private: held = {} } = read(dir, 'research.json', Research);
|
|
36
|
-
const found = await schemas(dir);
|
|
39
|
+
const { data, private: held = {}, roles = [], } = read(dir, 'research.json', Research);
|
|
40
|
+
const { datasets: found, roles: typed } = await schemas(dir);
|
|
37
41
|
const loaded = {};
|
|
38
42
|
const failures = [];
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
+
if (typed !== undefined && !sameRoles(typed, roles))
|
|
44
|
+
failures.push(`${join(dir, schemaFile)}: roles ${JSON.stringify(typed)} differ from research.json's ${JSON.stringify(roles)}`);
|
|
45
|
+
for (const dataset of Object.keys(found))
|
|
46
|
+
if (!Object.hasOwn(data, dataset) && !Object.hasOwn(held, dataset))
|
|
47
|
+
failures.push(`${join(dir, schemaFile)}: schema for "${dataset}", which research.json does not name`);
|
|
48
|
+
for (const [dataset, glob] of Object.entries(data)) {
|
|
43
49
|
try {
|
|
44
50
|
const matched = files(dir, glob);
|
|
45
51
|
const value = match(dir, matched);
|
|
46
|
-
const schema = Object.hasOwn(found,
|
|
52
|
+
const schema = Object.hasOwn(found, dataset) ? found[dataset] : undefined;
|
|
47
53
|
if (typeof schema?.safeParse !== 'function') {
|
|
48
|
-
failures.push(`${
|
|
54
|
+
failures.push(`${dataset}: no schema in ${schemaFile}`);
|
|
49
55
|
continue;
|
|
50
56
|
}
|
|
51
57
|
const parsed = schema.safeParse(value);
|
|
52
58
|
if (parsed.success)
|
|
53
|
-
loaded[
|
|
59
|
+
loaded[dataset] = parsed.data;
|
|
54
60
|
else {
|
|
55
61
|
const byPlace = new Map();
|
|
56
62
|
for (const { path, message } of parsed.error.issues) {
|
|
@@ -65,33 +71,33 @@ export async function load(dir) {
|
|
|
65
71
|
]);
|
|
66
72
|
}
|
|
67
73
|
for (const [place, issues] of byPlace)
|
|
68
|
-
failures.push(`${place}: ${
|
|
74
|
+
failures.push(`${place}: ${dataset}: ${issues.join('; ')}`);
|
|
69
75
|
}
|
|
70
76
|
}
|
|
71
77
|
catch (error) {
|
|
72
78
|
failures.push(error.message);
|
|
73
79
|
}
|
|
74
80
|
}
|
|
75
|
-
for (const [
|
|
81
|
+
for (const [dataset, glob] of Object.entries(held)) {
|
|
76
82
|
try {
|
|
77
|
-
const schema = Object.hasOwn(found,
|
|
83
|
+
const schema = Object.hasOwn(found, dataset) ? found[dataset] : undefined;
|
|
78
84
|
if (typeof schema?.safeParse !== 'function') {
|
|
79
|
-
failures.push(`${
|
|
85
|
+
failures.push(`${dataset}: no schema in ${schemaFile}`);
|
|
80
86
|
continue;
|
|
81
87
|
}
|
|
82
88
|
const pages = {};
|
|
83
|
-
for (const [page, file] of Object.entries(privatePages(dir,
|
|
89
|
+
for (const [page, file] of Object.entries(privatePages(dir, dataset, glob))) {
|
|
84
90
|
const parsed = schema.safeParse(read(dir, file));
|
|
85
91
|
if (parsed.success)
|
|
86
92
|
pages[page] = parsed.data;
|
|
87
93
|
else
|
|
88
|
-
failures.push(`${join(dir, file)}: ${
|
|
94
|
+
failures.push(`${join(dir, file)}: ${dataset}: ${parsed
|
|
89
95
|
.error.issues.map(({ path, message }) => path.length
|
|
90
96
|
? `${path.map(String).join('.')}: ${message}`
|
|
91
97
|
: message)
|
|
92
98
|
.join('; ')}`);
|
|
93
99
|
}
|
|
94
|
-
loaded[
|
|
100
|
+
loaded[dataset] = pages;
|
|
95
101
|
}
|
|
96
102
|
catch (error) {
|
|
97
103
|
failures.push(error.message);
|
package/dist/dev/host.d.ts
CHANGED
|
@@ -1,2 +1,8 @@
|
|
|
1
1
|
import { type People } from '../reader.js';
|
|
2
|
-
|
|
2
|
+
/** research.json's `people` and `roles`. */
|
|
3
|
+
type Cast = {
|
|
4
|
+
people?: People;
|
|
5
|
+
roles?: string[];
|
|
6
|
+
};
|
|
7
|
+
export declare function start(username: string, cast: Cast): Promise<void>;
|
|
8
|
+
export {};
|