@exodus/stasis 1.0.0-beta.12 → 1.0.0-beta.14
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 +4 -1
- package/bin/stasis.js +111 -139
- package/package.json +21 -12
- package/src/audit.js +3 -40
- package/src/babel.js +44 -37
- package/src/bundle-reader.js +24 -4
- package/src/cmd/build.js +194 -8
- package/src/cmd/bundle-options.js +103 -24
- package/src/cmd/bundle.js +986 -239
- package/src/cmd/github-bundle.js +17 -8
- package/src/cmd/output.js +10 -3
- package/src/cmd/tokens.js +67 -0
- package/src/diff.js +2 -2
- package/src/flow.js +22 -0
- package/src/installed-commits.js +4 -2
- package/src/loaders/bash.js +2 -27
- package/src/loaders/cargo.js +6 -6
- package/src/loaders/foundry.js +4 -6
- package/src/loaders/paths.js +4 -15
- package/src/loaders/php.js +3 -5
- package/src/loaders/rust.js +19 -39
- package/src/loaders/solidity-ownership.js +4 -15
- package/src/loaders/solidity.js +4 -67
- package/src/metro-assets.js +415 -0
- package/src/metro-resolver.js +5 -3
- package/src/parse.js +4 -3
- package/src/resolve-fields.js +60 -28
- package/src/resolve-node.js +9 -7
- package/src/resolve-typescript.js +3 -3
- package/src/scan.js +140 -53
- package/src/table.js +63 -0
- package/src/validate.js +1241 -0
- package/src/vfs-bundle/entries.js +24 -3
- package/src/vfs-bundle/github.js +72 -23
- package/src/vfs-bundle/glob.js +420 -0
- package/src/vfs-bundle/lazy.js +128 -0
- package/src/vfs-bundle/tree.js +242 -79
- package/src/vfs-bundle/yarnclean.js +100 -0
- package/src/vfs-bundle.js +26 -16
package/src/scan.js
CHANGED
|
@@ -3,10 +3,12 @@ import { fileURLToPath, pathToFileURL } from 'node:url'
|
|
|
3
3
|
import { createRequire, isBuiltin } from 'node:module'
|
|
4
4
|
import assert from 'node:assert/strict'
|
|
5
5
|
import { packageType } from '@exodus/stasis-core/bundle-util'
|
|
6
|
-
import { classifyExtension, classifyFormat, isTypeDeclaration, relativeEscapes } from '@exodus/stasis-core/util'
|
|
6
|
+
import { classifyExtension, classifyFormat, isTypeDeclaration, nestedMap, relativeEscapes } from '@exodus/stasis-core/util'
|
|
7
7
|
import { inNodeModules, resolveTypescriptFallback, typescriptSiblings } from './resolve-typescript.js'
|
|
8
8
|
import { branchFolder, parseDefine } from './define.js'
|
|
9
9
|
import { diskHost } from '@exodus/stasis-core/host'
|
|
10
|
+
import { untouchedMemo } from './vfs-bundle/lazy.js'
|
|
11
|
+
import { FLOW_EXTS, stripFlowTypes } from './flow.js'
|
|
10
12
|
|
|
11
13
|
// Static require/import graph walker: parses source, never loads or executes user code.
|
|
12
14
|
// Dynamic specifiers (`require(name)`, `import('./'+x)`) are recorded as unresolved. With
|
|
@@ -21,44 +23,28 @@ const RESOLVABLE_EXTS = new Set([...SCRIPT_EXTS, '.json'])
|
|
|
21
23
|
// generics collide with JSX, so tsc/oxc reserve JSX for .tsx — JSX-in-TS belongs in a .tsx file.
|
|
22
24
|
const JSX_EXTS = new Set(['.js', '.cjs', '.mjs'])
|
|
23
25
|
|
|
24
|
-
// Flow type syntax lives in the JS (non-TS) family; oxc parses TS natively, and running
|
|
25
|
-
// flow-remove-types over a .ts/.tsx file would corrupt TS-only constructs, so --flow only strips
|
|
26
|
-
// these. .jsx is included (it's JS + JSX, and can carry Flow types like .js); .tsx is not (it's
|
|
27
|
-
// TypeScript). flow-remove-types preserves JSX, so a stripped .jsx re-parses under lang:jsx.
|
|
28
|
-
const FLOW_EXTS = new Set(['.js', '.cjs', '.mjs', '.jsx'])
|
|
29
26
|
const TS_EXTS = new Set(['.ts', '.cts', '.mts', '.tsx'])
|
|
30
27
|
|
|
31
|
-
// Required lazily so non-JS bundlers don't load the native oxc parser.
|
|
28
|
+
// Required lazily so non-JS bundlers don't load the native oxc parser. Its parseSync hands each AST
|
|
29
|
+
// over by raw transfer where the platform takes it (oxc's rawTransferSupported), read from the
|
|
30
|
+
// parser's own buffer, rather than as JSON, which takes longer to parse than the source did.
|
|
32
31
|
let _parser
|
|
33
32
|
export function getParser() {
|
|
34
|
-
_parser
|
|
33
|
+
if (_parser === undefined) {
|
|
34
|
+
const oxc = createRequire(import.meta.url)('oxc-parser')
|
|
35
|
+
_parser = oxc.rawTransferSupported() ? { ...oxc, parseSync: (file, source, options) => oxc.parseSync(file, source, { experimentalRawTransfer: true, ...options }) } : oxc
|
|
36
|
+
}
|
|
35
37
|
return _parser
|
|
36
38
|
}
|
|
37
39
|
|
|
38
|
-
// flow-remove-types is an optional dependency, loaded lazily and only under --flow: it strips
|
|
39
|
-
// Flow type syntax down to plain JS before oxc (which parses JS/TS, not Flow) sees it. A missing
|
|
40
|
-
// dep is an env error, so surface it with an actionable install hint rather than a bare resolve throw.
|
|
41
|
-
let _flowRemoveTypes
|
|
42
|
-
function getFlowRemoveTypes() {
|
|
43
|
-
if (_flowRemoveTypes) return _flowRemoveTypes
|
|
44
|
-
try {
|
|
45
|
-
_flowRemoveTypes = createRequire(import.meta.url)('flow-remove-types')
|
|
46
|
-
} catch (cause) {
|
|
47
|
-
throw new Error(
|
|
48
|
-
"--flow needs the optional 'flow-remove-types' dependency; install it (e.g. `npm i flow-remove-types`)",
|
|
49
|
-
{ cause }
|
|
50
|
-
)
|
|
51
|
-
}
|
|
52
|
-
return _flowRemoveTypes
|
|
53
|
-
}
|
|
54
40
|
|
|
55
41
|
// classifyFormat is the shared name->format authority (static and runtime bundles must agree); it
|
|
56
42
|
// returns null for the .js/.ts family whose module system comes from package.json `type` (null =
|
|
57
43
|
// detect from syntax, signaled up as null).
|
|
58
|
-
function formatForFile(file, host) {
|
|
44
|
+
function formatForFile(file, host, kept) {
|
|
59
45
|
const format = classifyFormat(file)
|
|
60
46
|
if (format != null) return format
|
|
61
|
-
const type = packageType(file, host)
|
|
47
|
+
const type = packageType(file, host, kept)
|
|
62
48
|
if (type === null) return null
|
|
63
49
|
return `${type}${extname(file) === '.ts' ? '-typescript' : ''}`
|
|
64
50
|
}
|
|
@@ -130,14 +116,15 @@ function findCallSpecifiers(ast, push, fold = null) {
|
|
|
130
116
|
// (`import type { A }` and `import { type A }` yield identical all-isType entries, and
|
|
131
117
|
// `export {} from` yields no entry at all), so the statements are read off the AST, where
|
|
132
118
|
// importKind/exportKind carry it. `defines` (parseDefine's) leave out the calls in branches they
|
|
133
|
-
// keep from running; static import statements are never in one.
|
|
134
|
-
|
|
119
|
+
// keep from running; static import statements are never in one. `typesOnly(node)`, where given,
|
|
120
|
+
// leaves out a statement it holds lists types alone (see listsTypesOnly).
|
|
121
|
+
function collectSpecifiers(program, defines = new Map(), typesOnly = () => false) {
|
|
135
122
|
const specs = []
|
|
136
123
|
for (const node of program.body) {
|
|
137
124
|
if (node.type === 'ImportDeclaration') {
|
|
138
|
-
if (node.importKind !== 'type') specs.push({ kind: 'import', spec: node.source.value })
|
|
125
|
+
if (node.importKind !== 'type' && !typesOnly(node)) specs.push({ kind: 'import', spec: node.source.value })
|
|
139
126
|
} else if (node.type === 'ExportNamedDeclaration') {
|
|
140
|
-
if (node.source != null && node.exportKind !== 'type') specs.push({ kind: 'export-from', spec: node.source.value })
|
|
127
|
+
if (node.source != null && node.exportKind !== 'type' && !typesOnly(node)) specs.push({ kind: 'export-from', spec: node.source.value })
|
|
141
128
|
} else if (node.type === 'ExportAllDeclaration') {
|
|
142
129
|
if (node.exportKind !== 'type') specs.push({ kind: 'export-from', spec: node.source.value })
|
|
143
130
|
}
|
|
@@ -183,10 +170,29 @@ export function importsTypescriptByOutputName(entries, host = diskHost) {
|
|
|
183
170
|
return targets.length > 0 && targets.every((target) => host.stat(target) === null && mapsTo(target))
|
|
184
171
|
}
|
|
185
172
|
|
|
173
|
+
const TYPE_KINDS = new Set(['type', 'typeof'])
|
|
174
|
+
|
|
175
|
+
// Whether the import or re-export statement `node` lists specifiers, every one an inline type
|
|
176
|
+
// (`import { type A, typeof B } from`, `export { type C } from`), which Babel's Flow and TypeScript
|
|
177
|
+
// transforms erase whole, as Metro builds (Node keeps one, as `import {} from`). A file --flow
|
|
178
|
+
// stripped has lost them, blanked to whitespace in place in its `strippedSource`, so there the
|
|
179
|
+
// statement lists types alone where it reads otherwise in its `source` (a comment in it stays);
|
|
180
|
+
// a statement written with none (`import {} from`) lists no types. The Metro plugin's transformer
|
|
181
|
+
// draws the same line (typesOnly).
|
|
182
|
+
function listsTypesOnly(node, { source, strippedSource }) {
|
|
183
|
+
const specifiers = node.specifiers ?? []
|
|
184
|
+
if (specifiers.length > 0) return specifiers.every((specifier) => TYPE_KINDS.has(specifier.importKind ?? specifier.exportKind))
|
|
185
|
+
return strippedSource !== null && source.slice(node.start, node.end) !== strippedSource.slice(node.start, node.end)
|
|
186
|
+
}
|
|
187
|
+
|
|
186
188
|
function condKey(set) {
|
|
187
189
|
return [...set].join(', ')
|
|
188
190
|
}
|
|
189
191
|
|
|
192
|
+
// How many files the scans parsed, and with a `memo` (#scanKept) resolved again from a parse kept,
|
|
193
|
+
// and took as kept, all told: what a build is reported by (EXODUS_STASIS_DEBUG).
|
|
194
|
+
export const scanCounts = { parsed: 0, resolved: 0, replayed: 0 }
|
|
195
|
+
|
|
190
196
|
export class Scan {
|
|
191
197
|
files = new Map()
|
|
192
198
|
imports = new Map()
|
|
@@ -212,7 +218,13 @@ export class Scan {
|
|
|
212
218
|
// `host`: the filesystem the walk reads and resolves through (@exodus/stasis-core/host).
|
|
213
219
|
// `define` (each global name -> its value, see define.js): a require()/import() in a branch they
|
|
214
220
|
// keep from running (`if (__DEV__)` under `{ __DEV__: false }`) is no edge.
|
|
215
|
-
|
|
221
|
+
// `babel`: the files are built through Babel (Metro), whose Flow and TypeScript transforms erase an
|
|
222
|
+
// import or re-export listing types alone (`import { type A } from`), so it's no edge either, but
|
|
223
|
+
// in a TypeScript file `keepsTypeImports(file)` holds Babel keeps them for (its transform's
|
|
224
|
+
// `onlyRemoveTypeImports`, as --babel reads the project's config); Flow's erases them always.
|
|
225
|
+
// `memo`: what scans before this one, with these options, read of each file, and where to keep
|
|
226
|
+
// what this one reads (see #scanKept).
|
|
227
|
+
constructor({ conditions = [], resolve = null, jsx = false, flow = false, typescript = false, typescriptPaths = null, typescriptResolve = null, resources = new Set(), define, babel = false, keepsTypeImports = null, host = diskHost, memo = null } = {}) {
|
|
216
228
|
this.extraConditions = [...conditions]
|
|
217
229
|
this.customResolve = resolve
|
|
218
230
|
this.typescriptResolve = typescriptResolve
|
|
@@ -222,9 +234,17 @@ export class Scan {
|
|
|
222
234
|
this.typescriptPaths = typescriptPaths
|
|
223
235
|
this.resources = resources
|
|
224
236
|
this.defines = parseDefine('scan', define)
|
|
237
|
+
this.babel = babel
|
|
238
|
+
this.keepsTypeImports = keepsTypeImports
|
|
225
239
|
this.host = host
|
|
240
|
+
this.memo = memo
|
|
241
|
+
this.#types = untouchedMemo(host.touches, { retouch: host.retouch })
|
|
226
242
|
}
|
|
227
243
|
|
|
244
|
+
// The `type` of each package.json the files scanned are under, read once (formatForFile's), as
|
|
245
|
+
// a host's own reads are kept (vfs-bundle/lazy.js's untouchedMemo).
|
|
246
|
+
#types
|
|
247
|
+
|
|
228
248
|
// A reached file whose extension/filename is in the --resources allowlist: carried as an opaque
|
|
229
249
|
// resource (bytes only, no parse, no edges), not rejected as "a source bundle can't carry".
|
|
230
250
|
// classifyExtension returns 'resource' only for non-code names in the allowlist, so this never
|
|
@@ -242,14 +262,75 @@ export class Scan {
|
|
|
242
262
|
this.entries.add(url)
|
|
243
263
|
queue.push(url)
|
|
244
264
|
}
|
|
245
|
-
|
|
246
|
-
|
|
265
|
+
// FIFO by an index, not shift(): the queue holds every edge's target, and shift() copies a large one.
|
|
266
|
+
for (let head = 0; head < queue.length; head++) {
|
|
267
|
+
const url = queue[head]
|
|
247
268
|
if (this.files.has(url)) continue
|
|
248
|
-
this
|
|
269
|
+
if (this.memo === null) {
|
|
270
|
+
scanCounts.parsed++
|
|
271
|
+
this.#scanFile(url, queue)
|
|
272
|
+
} else {
|
|
273
|
+
this.#scanKept(url, queue)
|
|
274
|
+
}
|
|
249
275
|
}
|
|
250
276
|
return this
|
|
251
277
|
}
|
|
252
278
|
|
|
279
|
+
// The parse of the file being scanned a scan before kept (#scanKept), and the parse of it made here.
|
|
280
|
+
#reparse
|
|
281
|
+
#parse
|
|
282
|
+
|
|
283
|
+
// With a `memo` (url -> what a scan read of it, kept across the rounds a lazy tree is read in,
|
|
284
|
+
// vfs-bundle/lazy.js): a file a scan before kept is taken as it read it, what it found and queued
|
|
285
|
+
// and the warnings it gave alike. One scanned here is kept where reading it touched no package not
|
|
286
|
+
// unpacked yet (`host.touches()` counts each such read, through a host that caches nothing such a
|
|
287
|
+
// read reads), so read what the full tree holds: the next scan reads only the files that did, and
|
|
288
|
+
// those it reaches. Of one that did, its parse is kept alone, which is the file's whatever the tree
|
|
289
|
+
// holds, or of that, what it imports (#scanFile): the next scan resolves its imports again, but
|
|
290
|
+
// parses it no more.
|
|
291
|
+
#scanKept(url, queue) {
|
|
292
|
+
const kept = this.memo.get(url)
|
|
293
|
+
scanCounts[kept?.queued !== undefined ? 'replayed' : kept?.parse === undefined ? 'parsed' : 'resolved'] += 1
|
|
294
|
+
if (kept?.queued !== undefined) {
|
|
295
|
+
this.files.set(url, kept.file)
|
|
296
|
+
for (const [key, specMap] of kept.imports) nestedMap(this.imports, key).set(url, specMap)
|
|
297
|
+
this.unresolved.push(...kept.unresolved)
|
|
298
|
+
this.parseErrors.push(...kept.parseErrors)
|
|
299
|
+
queue.push(...kept.queued)
|
|
300
|
+
for (const args of kept.warnings) console.warn(...args)
|
|
301
|
+
return
|
|
302
|
+
}
|
|
303
|
+
const touches = this.host.touches()
|
|
304
|
+
const at = { unresolved: this.unresolved.length, parseErrors: this.parseErrors.length, queue: queue.length }
|
|
305
|
+
// The scan of a file is synchronous: what console.warn is given meanwhile is the file's.
|
|
306
|
+
const warnings = []
|
|
307
|
+
const warn = console.warn
|
|
308
|
+
console.warn = (...args) => {
|
|
309
|
+
warnings.push(args)
|
|
310
|
+
warn.apply(console, args)
|
|
311
|
+
}
|
|
312
|
+
this.#reparse = kept?.parse
|
|
313
|
+
this.#parse = undefined
|
|
314
|
+
try {
|
|
315
|
+
this.#scanFile(url, queue)
|
|
316
|
+
} finally {
|
|
317
|
+
console.warn = warn
|
|
318
|
+
this.#reparse = undefined
|
|
319
|
+
}
|
|
320
|
+
if (this.host.touches() !== touches) {
|
|
321
|
+
if (this.#parse !== undefined) this.memo.set(url, { parse: this.#parse })
|
|
322
|
+
return
|
|
323
|
+
}
|
|
324
|
+
this.memo.set(url, {
|
|
325
|
+
file: this.files.get(url),
|
|
326
|
+
imports: [...this.imports].filter(([, byParent]) => byParent.has(url)).map(([key, byParent]) => [key, byParent.get(url)]),
|
|
327
|
+
unresolved: this.unresolved.slice(at.unresolved),
|
|
328
|
+
parseErrors: this.parseErrors.slice(at.parseErrors),
|
|
329
|
+
queued: queue.slice(at.queue),
|
|
330
|
+
warnings,
|
|
331
|
+
})
|
|
332
|
+
}
|
|
333
|
+
|
|
253
334
|
// Node's resolver: must match the runtime resolver's condition set (omitting one resolves
|
|
254
335
|
// `exports`-gated packages to a different file) AND its order: the recorded key must equal the
|
|
255
336
|
// runtime hooks' string for exact lookups. Extras go where Node puts user conditions in each set.
|
|
@@ -302,16 +383,13 @@ export class Scan {
|
|
|
302
383
|
return hit == null ? null : pathToFileURL(hit).toString()
|
|
303
384
|
}
|
|
304
385
|
|
|
305
|
-
// Strip Flow type syntax to plain JS
|
|
306
|
-
//
|
|
307
|
-
//
|
|
308
|
-
//
|
|
386
|
+
// Strip Flow type syntax to plain JS (flow.js's stripFlowTypes). Only invoked
|
|
387
|
+
// as a parse fallback (see #scanFile), so plain-JS files never reach it. Returns the rewritten
|
|
388
|
+
// source, or null when the transform itself fails on this file -- the caller then keeps oxc's
|
|
389
|
+
// original parse error.
|
|
309
390
|
#stripFlow(src, file) {
|
|
310
|
-
const flowRemoveTypes = getFlowRemoveTypes()
|
|
311
391
|
try {
|
|
312
|
-
|
|
313
|
-
// types with whitespace, so byte offsets and every import specifier are preserved.
|
|
314
|
-
return flowRemoveTypes(src, { all: true }).toString()
|
|
392
|
+
return stripFlowTypes(src)
|
|
315
393
|
} catch (cause) {
|
|
316
394
|
console.warn(`[stasis] --flow: could not strip Flow types from ${file}; leaving it to oxc (${cause.message})`)
|
|
317
395
|
return null
|
|
@@ -325,7 +403,7 @@ export class Scan {
|
|
|
325
403
|
parseFile(file) {
|
|
326
404
|
const ext = extname(file)
|
|
327
405
|
// declared === null: typeless package; Node decides .js/.ts by syntax, resolved after parse.
|
|
328
|
-
const declared = formatForFile(file, this.host)
|
|
406
|
+
const declared = formatForFile(file, this.host, this.#types)
|
|
329
407
|
const src = this.host.readFile(file).toString('utf8')
|
|
330
408
|
|
|
331
409
|
// Resolve the parser outside the try: a missing oxc-parser is an env error, not this
|
|
@@ -392,6 +470,7 @@ export class Scan {
|
|
|
392
470
|
// the bundle stores the pristine on-disk source (State/buildResolvedJsBundle re-read it), so
|
|
393
471
|
// attestation still covers the real bytes. A clean re-parse replaces the errored one; otherwise
|
|
394
472
|
// we keep the original result so a genuine (non-Flow) parse error still surfaces.
|
|
473
|
+
let strippedSource = null
|
|
395
474
|
if (errors.length > 0 && this.flow && FLOW_EXTS.has(ext)) {
|
|
396
475
|
const flowSrc = this.#stripFlow(src, file)
|
|
397
476
|
if (flowSrc !== null) {
|
|
@@ -401,12 +480,14 @@ export class Scan {
|
|
|
401
480
|
parsed = asFlow
|
|
402
481
|
format = declared ?? detectedFormat(parsed)
|
|
403
482
|
errors = []
|
|
483
|
+
strippedSource = flowSrc
|
|
404
484
|
}
|
|
405
485
|
} catch { /* keep the original parse + its errors */ }
|
|
406
486
|
}
|
|
407
487
|
}
|
|
408
488
|
|
|
409
|
-
|
|
489
|
+
// `source`, and `strippedSource` where `parsed` is of its Flow-stripped form, for listsTypesOnly.
|
|
490
|
+
return { format, parsed, errors, source: src, strippedSource }
|
|
410
491
|
}
|
|
411
492
|
|
|
412
493
|
#scanFile(url, queue) {
|
|
@@ -428,7 +509,8 @@ export class Scan {
|
|
|
428
509
|
return
|
|
429
510
|
}
|
|
430
511
|
|
|
431
|
-
const result = this.parseFile(file)
|
|
512
|
+
const result = this.#reparse ?? this.parseFile(file)
|
|
513
|
+
this.#parse = result
|
|
432
514
|
if (result.crash !== undefined) {
|
|
433
515
|
this.#recordParseError(url, result.declared, result.crash, false)
|
|
434
516
|
return
|
|
@@ -443,11 +525,20 @@ export class Scan {
|
|
|
443
525
|
this.#recordParseError(url, format, parseError, true)
|
|
444
526
|
return
|
|
445
527
|
}
|
|
446
|
-
// Script (CJS) files: oxc's recovered AST
|
|
528
|
+
// Script (CJS) files: walk on through oxc's recovered AST, so a scan reports the rest of the
|
|
529
|
+
// graph's errors too; the parse error itself fails a bundle (analyzeScanner).
|
|
447
530
|
this.parseErrors.push({ url, format, message: parseError, recovered: true })
|
|
448
531
|
}
|
|
449
532
|
|
|
450
|
-
const
|
|
533
|
+
const typesErased = () => result.strippedSource !== null || !this.keepsTypeImports?.(file)
|
|
534
|
+
let { specs } = result
|
|
535
|
+
if (specs === undefined) {
|
|
536
|
+
const touches = this.memo === null ? 0 : this.host.touches()
|
|
537
|
+
specs = collectSpecifiers(parsed.program, this.defines, this.babel ? (node) => listsTypesOnly(node, result) && typesErased() : undefined)
|
|
538
|
+
// What a scan keeps of a file whose reading touched a package not unpacked yet (#scanKept): of
|
|
539
|
+
// its parse, what it imports, where finding that touched none, the parse itself let go.
|
|
540
|
+
if (this.memo !== null && this.host.touches() === touches) this.#parse = { format, errors, specs }
|
|
541
|
+
}
|
|
451
542
|
|
|
452
543
|
// The edge KIND picks the resolution context, not the parent's format: createRequire()d
|
|
453
544
|
// require() inside ESM resolves under REQUIRE conditions, literal import() inside CJS under
|
|
@@ -458,8 +549,7 @@ export class Scan {
|
|
|
458
549
|
const specMaps = new Map() // condition key -> specifier -> child URL
|
|
459
550
|
const addChild = (s, key, childURL, childPath) => {
|
|
460
551
|
edges.push({ ...s, child: childURL })
|
|
461
|
-
|
|
462
|
-
specMaps.get(key).set(s.spec, childURL)
|
|
552
|
+
nestedMap(specMaps, key).set(s.spec, childURL)
|
|
463
553
|
if (RESOLVABLE_EXTS.has(extname(childPath)) || this.#isResource(childPath)) queue.push(childURL)
|
|
464
554
|
}
|
|
465
555
|
// Records the file --typescript would land the miss on too (typescriptURL), resolved as the miss was.
|
|
@@ -512,10 +602,7 @@ export class Scan {
|
|
|
512
602
|
addChild(s, key, pathToFileURL(childPath).toString(), childPath)
|
|
513
603
|
}
|
|
514
604
|
|
|
515
|
-
for (const [key, specMap] of specMaps)
|
|
516
|
-
if (!this.imports.has(key)) this.imports.set(key, new Map())
|
|
517
|
-
this.imports.get(key).set(url, specMap)
|
|
518
|
-
}
|
|
605
|
+
for (const [key, specMap] of specMaps) nestedMap(this.imports, key).set(url, specMap)
|
|
519
606
|
this.files.set(url, parseError ? { format, edges, parseError } : { format, edges })
|
|
520
607
|
}
|
|
521
608
|
|
package/src/table.js
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
// A report's tables, as `stasis audit` and `stasis validate` print them, and their colours where the
|
|
2
|
+
// stream they go to takes colours.
|
|
3
|
+
import { styleText } from 'node:util'
|
|
4
|
+
|
|
5
|
+
// Strip line breaks (LF/CR/CRLF, U+2028/U+2029) from a cell (an advisory's title, say) so they can't
|
|
6
|
+
// break the box layout.
|
|
7
|
+
const cell = (v) => String(v ?? '').replace(/[\r\n\u2028\u2029]+/gu, ' ')
|
|
8
|
+
|
|
9
|
+
// Split a value into the physical lines a cell occupies. Non-multiline columns
|
|
10
|
+
// collapse every line break to a space via `cell` (one line). A column named in
|
|
11
|
+
// `multiline` (the `--why` reason column) keeps intentional `\n` breaks -- one
|
|
12
|
+
// physical line per entry -- while still flattening stray CR/separators in each.
|
|
13
|
+
const cellLines = (v, multiline) =>
|
|
14
|
+
multiline ? String(v ?? '').split('\n').map((s) => cell(s)) : [cell(v)]
|
|
15
|
+
|
|
16
|
+
// `rows` as a boxed table of `columns`, its header their names. `paint(text, column, row)`, where
|
|
17
|
+
// given, styles the text of each cell of a row (with colour, say), its padding after, unstyled.
|
|
18
|
+
export function formatTable(rows, columns, { multiline = [], paint } = {}) {
|
|
19
|
+
if (rows.length === 0) return ''
|
|
20
|
+
const ml = new Set(multiline)
|
|
21
|
+
const linesOf = (obj) => columns.map((c) => cellLines(obj[c], ml.has(c)))
|
|
22
|
+
const header = columns.map((c) => [c])
|
|
23
|
+
const body = rows.map((r) => linesOf(r))
|
|
24
|
+
const widths = columns.map((c, i) =>
|
|
25
|
+
Math.max(c.length, ...body.map((cells) => Math.max(0, ...cells[i].map((l) => l.length))))
|
|
26
|
+
)
|
|
27
|
+
const line = (l, m, r, fill) => l + widths.map((w) => fill.repeat(w + 2)).join(m) + r
|
|
28
|
+
// A row spans as many physical lines as its tallest cell; shorter cells pad
|
|
29
|
+
// out with blanks so the borders stay aligned.
|
|
30
|
+
const render = (cells, row) => {
|
|
31
|
+
const height = Math.max(...cells.map((c) => c.length))
|
|
32
|
+
const out = []
|
|
33
|
+
for (let i = 0; i < height; i++) {
|
|
34
|
+
out.push('│ ' + cells.map((c, j) => {
|
|
35
|
+
const text = c[i] ?? ''
|
|
36
|
+
return (paint && row && text !== '' ? paint(text, columns[j], row) : text) + ' '.repeat(widths[j] - text.length)
|
|
37
|
+
}).join(' │ ') + ' │')
|
|
38
|
+
}
|
|
39
|
+
return out.join('\n')
|
|
40
|
+
}
|
|
41
|
+
return [
|
|
42
|
+
line('┌', '┬', '┐', '─'),
|
|
43
|
+
render(header),
|
|
44
|
+
line('├', '┼', '┤', '─'),
|
|
45
|
+
...body.map((cells, at) => render(cells, rows[at])),
|
|
46
|
+
line('└', '┴', '┘', '─'),
|
|
47
|
+
].join('\n')
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// Whether `stream` takes colours, as @exodus/test tells it: CI's logs do, though no terminal; a
|
|
51
|
+
// terminal that has them does (a TTY's hasColors); a pipe, or a file, doesn't.
|
|
52
|
+
export function takesColors(stream) {
|
|
53
|
+
return Boolean(process.env.CI) || Boolean(stream?.hasColors?.())
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// Dimmed text's format: CI's logs show gray, not faint.
|
|
57
|
+
export const DIM = process.env.CI ? 'gray' : 'dim'
|
|
58
|
+
|
|
59
|
+
// `paint(format, text)`: `text` in `format` (util.styleText's: a colour, `bold`, or a list of them)
|
|
60
|
+
// where `colors` (else as `stream` takes them, takesColors), else as it is.
|
|
61
|
+
export function painter(stream, colors = takesColors(stream)) {
|
|
62
|
+
return (format, text) => (colors && format && text !== '' ? styleText(format, text, { validateStream: false }) : text)
|
|
63
|
+
}
|