@exodus/stasis-core 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/bin/stasis-core.js +9 -98
- package/package.json +4 -2
- package/src/add.js +68 -70
- package/src/artifact-util.js +48 -21
- package/src/brotli.js +23 -3
- package/src/bundle-util.js +65 -10
- package/src/bundle.js +14 -2
- package/src/cli.js +153 -0
- package/src/config.js +14 -4
- package/src/extract.js +148 -41
- package/src/fs.js +1 -19
- package/src/hooks.js +4 -3
- package/src/host.js +12 -1
- package/src/lockfile.js +15 -1
- package/src/state-util.js +26 -0
- package/src/state.js +155 -146
- package/src/util.js +48 -70
package/src/bundle-util.js
CHANGED
|
@@ -10,15 +10,19 @@ import { assertRealPathWithinBase, hasNodeModulesSegment, relativeEscapes, split
|
|
|
10
10
|
const utf8 = new TextDecoder()
|
|
11
11
|
export const packageJSONText = (bytes) => utf8.decode(bytes)
|
|
12
12
|
|
|
13
|
-
|
|
13
|
+
// The `type` of the package.json nearest `file`, read through `kept(path, read)`, a cache by that
|
|
14
|
+
// package.json's path where one is given (a scan's, which reads the same one for each file under it).
|
|
15
|
+
export function packageType(file, host = diskHost, kept = (path, read) => read()) {
|
|
14
16
|
const pkg = host.findPackageJSON(file)
|
|
15
17
|
if (!pkg) return null
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
18
|
+
return kept(pkg, () => {
|
|
19
|
+
try {
|
|
20
|
+
const type = JSON.parse(packageJSONText(host.readFile(pkg))).type
|
|
21
|
+
return type === 'module' || type === 'commonjs' ? type : null
|
|
22
|
+
} catch {
|
|
23
|
+
return null
|
|
24
|
+
}
|
|
25
|
+
})
|
|
22
26
|
}
|
|
23
27
|
|
|
24
28
|
// Directories where a package keeps copies of OTHER packages, each under a package.json of its own,
|
|
@@ -67,6 +71,19 @@ export function namedManifestOf(rel, read) {
|
|
|
67
71
|
return undefined
|
|
68
72
|
}
|
|
69
73
|
|
|
74
|
+
// The package.json nearest above a file `rel` of the package at `pkgDir` (both project-relative,
|
|
75
|
+
// POSIX), short of the package's own: a `{"type":"module"}` marker such as `esm/package.json`, or a
|
|
76
|
+
// subpackage's or vendored package's, which Node takes the file's `type` from and Metro's resolver
|
|
77
|
+
// (getPackageForModule) the file's fields. `exists(rel)` says whether one is there -> its rel, or
|
|
78
|
+
// undefined where the package's own is the nearest.
|
|
79
|
+
export function scopeManifestOf(rel, pkgDir, exists) {
|
|
80
|
+
if (pkgDir !== '.' && !rel.startsWith(`${pkgDir}/`)) return undefined
|
|
81
|
+
for (let dir = posix.dirname(rel); dir !== pkgDir && dir !== '.'; dir = posix.dirname(dir)) {
|
|
82
|
+
if (exists(`${dir}/package.json`)) return `${dir}/package.json`
|
|
83
|
+
}
|
|
84
|
+
return undefined
|
|
85
|
+
}
|
|
86
|
+
|
|
70
87
|
// Nearest package.json (walking up) that identifies a bucket; pkgDir is relative to baseDir ("."
|
|
71
88
|
// at the root). Inside node_modules both name and version are required; a workspace package
|
|
72
89
|
// outside node_modules may omit version (the name alone claims the bucket, matching
|
|
@@ -113,6 +130,17 @@ export function findPackageMetadata(baseDir, fileRelPath, { strict = false, chec
|
|
|
113
130
|
}
|
|
114
131
|
}
|
|
115
132
|
|
|
133
|
+
// findPackageMetadata (with `options`) of each project-relative path, once per directory, the only
|
|
134
|
+
// thing its answer depends on.
|
|
135
|
+
export function packageLookup(baseDir, options) {
|
|
136
|
+
const byDir = new Map()
|
|
137
|
+
return (path) => {
|
|
138
|
+
const dir = posix.dirname(path)
|
|
139
|
+
if (!byDir.has(dir)) byDir.set(dir, findPackageMetadata(baseDir, path, options))
|
|
140
|
+
return byDir.get(dir)
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
116
144
|
// The directory, package-relative, of the package.json naming `manifestName` at `dir` (project-relative,
|
|
117
145
|
// POSIX) where it is a subpackage (isSubpackage) of the package at `pkgDir`; else undefined. The package's
|
|
118
146
|
// own package.json is read only where the name ends in that directory.
|
|
@@ -126,6 +154,18 @@ function hostSubpackageDir(baseDir, pkgDir, dir, manifestName, options) {
|
|
|
126
154
|
// The error codes that mean nothing is at a path.
|
|
127
155
|
export const NO_ENTRY = new Set(['ENOENT', 'ENOTDIR'])
|
|
128
156
|
|
|
157
|
+
// Whether anything is at `p` itself, a link not followed, a broken one too (`host.readlink` throws
|
|
158
|
+
// when nothing is).
|
|
159
|
+
export function lexists(p, host) {
|
|
160
|
+
try {
|
|
161
|
+
host.readlink(p)
|
|
162
|
+
return true
|
|
163
|
+
} catch (err) {
|
|
164
|
+
if (NO_ENTRY.has(err.code)) return false
|
|
165
|
+
throw err
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
129
169
|
// Why `file` can't be read, where `host.stat` gave null, or null when nothing is there (a missing
|
|
130
170
|
// path, one through a file, a link to nothing). host.stat answers null for any failure, as Node's
|
|
131
171
|
// module lookup does; real stat tells them apart: a link loop, a directory that may not be searched
|
|
@@ -149,6 +189,11 @@ export function statStrict(host, file, label) {
|
|
|
149
189
|
throw new Error(`${label}: can't be read (${failure.code ?? failure.message})`, { cause: failure })
|
|
150
190
|
}
|
|
151
191
|
|
|
192
|
+
// The error Node gives for a package.json at `path` that is no JSON object (`cause`: why it doesn't parse).
|
|
193
|
+
export const invalidPackageConfig = (path, cause) => Object.assign(
|
|
194
|
+
cause === undefined ? new Error(`Invalid package config ${path}.`) : new Error(`Invalid package config ${path}.`, { cause }),
|
|
195
|
+
{ code: 'ERR_INVALID_PACKAGE_CONFIG' })
|
|
196
|
+
|
|
152
197
|
// host.stat for a package.json, as Node's lookups read one: null when nothing is there, and one that
|
|
153
198
|
// can't be read (a link loop, a directory that may not be searched) throws ERR_INVALID_PACKAGE_CONFIG,
|
|
154
199
|
// as Node refuses it.
|
|
@@ -341,7 +386,7 @@ function repositoryUrl(json) {
|
|
|
341
386
|
// The `repo` a parsed package.json's `repository` names for `rel`, a directory below it ('' for its
|
|
342
387
|
// own): `github` from its GitHub URL or shorthand, and `directory` from `repository.directory` joined
|
|
343
388
|
// with `rel` (declaredLocation); undefined where it names no GitHub repository.
|
|
344
|
-
|
|
389
|
+
function declaredRepo(json, rel) {
|
|
345
390
|
const github = parseGithubRepository(repositoryUrl(json))
|
|
346
391
|
if (!github) return undefined
|
|
347
392
|
let base = json.repository.directory
|
|
@@ -354,8 +399,18 @@ export function packageRepo(json, rel = '') {
|
|
|
354
399
|
return { github, ...declaredLocation(base, rel) }
|
|
355
400
|
}
|
|
356
401
|
|
|
402
|
+
// A dependency's `repo`, from its parsed package.json: the repository it names (declaredRepo), at the
|
|
403
|
+
// commit its `gitHead` names where that is a full one. npm publish writes `gitHead` into the
|
|
404
|
+
// registry's version document alone, but lerna writes it into the published package.json too, and
|
|
405
|
+
// npm keeps one a package.json has, so the two agree. Undefined where it names no GitHub repository.
|
|
406
|
+
export function packageRepo(json) {
|
|
407
|
+
const repo = declaredRepo(json, '')
|
|
408
|
+
return repo !== undefined && isValidRepoField('commit', json.gitHead) ? { ...repo, commit: json.gitHead } : repo
|
|
409
|
+
}
|
|
410
|
+
|
|
357
411
|
// Bundle `repo` for `dir`: git origin/HEAD at the work tree root, with `dir`'s place in it (`''` at
|
|
358
|
-
// the root), else nearest package.json `repository` (
|
|
412
|
+
// the root), else nearest package.json `repository` (declaredRepo). Its commit is git's HEAD alone,
|
|
413
|
+
// never a package.json's `gitHead`: a project's own files are likely changed since the one it names.
|
|
359
414
|
export function detectRepo(dir, host = diskHost) {
|
|
360
415
|
const start = resolve(dir)
|
|
361
416
|
let pkg = null // null: no package.json `repository` seen yet; undefined: one seen, not GitHub
|
|
@@ -363,7 +418,7 @@ export function detectRepo(dir, host = diskHost) {
|
|
|
363
418
|
const rel = toPosix(relative(cursor, start))
|
|
364
419
|
if (pkg === null) {
|
|
365
420
|
const json = readJson(join(cursor, 'package.json'), host)
|
|
366
|
-
if (repositoryUrl(json) !== undefined) pkg =
|
|
421
|
+
if (repositoryUrl(json) !== undefined) pkg = declaredRepo(json, rel)
|
|
367
422
|
}
|
|
368
423
|
const dotGit = join(cursor, '.git')
|
|
369
424
|
if (host.stat(dotGit) !== null) {
|
package/src/bundle.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import {
|
|
2
2
|
assert,
|
|
3
3
|
assertArtifactPath,
|
|
4
|
+
assertPayloadFree,
|
|
4
5
|
canonicalFileKey,
|
|
5
6
|
duplicateKeyError,
|
|
6
7
|
fileSetToObject,
|
|
@@ -8,6 +9,7 @@ import {
|
|
|
8
9
|
flatFileKeys,
|
|
9
10
|
groupModules,
|
|
10
11
|
hasNodeModulesSegment,
|
|
12
|
+
isLockfileName,
|
|
11
13
|
isPlainObject,
|
|
12
14
|
mergeFormatMaps,
|
|
13
15
|
mergeImportMaps,
|
|
@@ -37,6 +39,12 @@ const LEGACY_VERSION = 0
|
|
|
37
39
|
|
|
38
40
|
const duplicateKey = duplicateKeyError('bundle', 'artifact (bundle=replace)')
|
|
39
41
|
|
|
42
|
+
// No bundle carries a stasis lockfile: extracted, one would replace the lockfile there (the attestation a frozen
|
|
43
|
+
// extract checks the bundle against), or root a stasis project of its own wherever it lands.
|
|
44
|
+
const assertNoLockfile = (keys) => {
|
|
45
|
+
for (const key of keys) assert(!isLockfileName(key), `bundle: ${key} is a stasis lockfile, which no bundle may carry`)
|
|
46
|
+
}
|
|
47
|
+
|
|
40
48
|
// A v0 path's bucket split; '' and '.' both spell the root listing (rel '').
|
|
41
49
|
const inferModuleDir = (path) =>
|
|
42
50
|
splitNodeModulesPath(path) ?? { dir: '.', rel: path === '.' ? '' : path, name: null }
|
|
@@ -143,7 +151,7 @@ export class Bundle {
|
|
|
143
151
|
this.imports = imports ?? new Map()
|
|
144
152
|
this.executable = executable ?? new Set()
|
|
145
153
|
this.reason = reason
|
|
146
|
-
this.repo =
|
|
154
|
+
this.repo = repo
|
|
147
155
|
this.package = pkg
|
|
148
156
|
}
|
|
149
157
|
|
|
@@ -241,6 +249,8 @@ export class Bundle {
|
|
|
241
249
|
|
|
242
250
|
// Flat keys must be unique across buckets: two different bucket splits can flatten to one path, and the `sources` getter would serve either payload.
|
|
243
251
|
const flatKeys = flatFileKeys(modules, 'bundle', duplicateKey)
|
|
252
|
+
assertNoLockfile(flatKeys.keys())
|
|
253
|
+
assertPayloadFree(formats, flatKeys, 'bundle')
|
|
244
254
|
const imports = parseImports(json.imports)
|
|
245
255
|
|
|
246
256
|
if (!contents) for (const [dir, info] of modules) modules.set(dir, lockModule(dir, info))
|
|
@@ -264,7 +274,9 @@ export class Bundle {
|
|
|
264
274
|
|
|
265
275
|
serialize() {
|
|
266
276
|
// Never write an artifact that parse would reject.
|
|
267
|
-
flatFileKeys(this.modules, 'bundle', duplicateKey)
|
|
277
|
+
const flatKeys = flatFileKeys(this.modules, 'bundle', duplicateKey)
|
|
278
|
+
assertNoLockfile(flatKeys.keys())
|
|
279
|
+
assertPayloadFree(this.formats, flatKeys, 'bundle')
|
|
268
280
|
const { modules, sources } = groupModules(this.modules, { skipEmpty: true, what: 'bundle' })
|
|
269
281
|
const full = this.config.scope === 'full'
|
|
270
282
|
const data = { version: VERSION, config: this.config }
|
package/src/cli.js
ADDED
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
// The commands both CLIs take, `stasis-core` and `stasis`, from their arguments: `add`, `extract` and
|
|
2
|
+
// `prune` whole, and `run` in steps, so `stasis run` can add its own options on top -- its options
|
|
3
|
+
// parsed and checked, the config the loader reads set in the environment, and the program run under
|
|
4
|
+
// the loader. Each names itself `label` (the CLI's name) and leaves a usage error to `fail(message)`,
|
|
5
|
+
// which exits.
|
|
6
|
+
|
|
7
|
+
import { spawn } from 'node:child_process'
|
|
8
|
+
import { once } from 'node:events'
|
|
9
|
+
import { constants as osConstants } from 'node:os'
|
|
10
|
+
import { resolve } from 'node:path'
|
|
11
|
+
import { parseBrotliQuality, parseLeadingOptions, valueFlagsOf } from './util.js'
|
|
12
|
+
|
|
13
|
+
const RUN_OPTIONS = {
|
|
14
|
+
lock: { type: 'string', default: 'none' },
|
|
15
|
+
bundle: { type: 'string', default: 'none' },
|
|
16
|
+
'bundle-file': { type: 'string' },
|
|
17
|
+
'resources-bundle-file': { type: 'string' },
|
|
18
|
+
debug: { type: 'boolean' },
|
|
19
|
+
dependencies: { type: 'boolean' },
|
|
20
|
+
'child-process': { type: 'boolean' },
|
|
21
|
+
import: { type: 'string', multiple: true },
|
|
22
|
+
fs: { type: 'string' },
|
|
23
|
+
resources: { type: 'string' },
|
|
24
|
+
'brotli-quality': { type: 'string' },
|
|
25
|
+
}
|
|
26
|
+
// `--fs` is taken only as `--fs=<mode>`: a bare `--fs` is refused rather than given the program as its mode.
|
|
27
|
+
const RUN_VALUE_FLAGS = ['--bundle', '--bundle-file', '--resources-bundle-file', '--lock', '--import', '--resources', '--brotli-quality']
|
|
28
|
+
|
|
29
|
+
// `run`'s options, and the caller's own `options` (boolean ones), shifted off the front of `argv`,
|
|
30
|
+
// which keeps the program and its arguments; checked, a usage error being `fail(message)`'s (which
|
|
31
|
+
// exits). -> the run: `values` as parsed, and the config they set.
|
|
32
|
+
export function parseRunOptions(argv, { options = {}, fail }) {
|
|
33
|
+
const values = parseLeadingOptions(argv, { ...RUN_OPTIONS, ...options }, { valueFlags: RUN_VALUE_FLAGS, onError: fail })
|
|
34
|
+
if (argv.length === 0) fail('Nothing to run: no path to file given')
|
|
35
|
+
const { lock, bundle } = values
|
|
36
|
+
if (!['none', 'ignore', 'add', 'replace', 'frozen'].includes(lock)) fail('Error: invalid --lock value')
|
|
37
|
+
const bundleFile = values['bundle-file'] ? resolve(values['bundle-file']) : ''
|
|
38
|
+
const resourcesBundleFile = values['resources-bundle-file'] ? resolve(values['resources-bundle-file']) : ''
|
|
39
|
+
if (!['none', 'ignore', 'add', 'replace', 'load', 'frozen'].includes(bundle)) fail('Error: invalid --bundle value')
|
|
40
|
+
if (bundleFile && bundle === 'none') fail('Error: --bundle-file requires --bundle=(add|replace|load|frozen|ignore)')
|
|
41
|
+
// --resources-bundle-file needs an active bundle mode (unlike --bundle-file, inert under ignore).
|
|
42
|
+
if (resourcesBundleFile && (bundle === 'none' || bundle === 'ignore')) fail('Error: --resources-bundle-file requires --bundle=(add|replace|load|frozen)')
|
|
43
|
+
if (bundle === 'load' && lock !== 'frozen' && lock !== 'none' && lock !== 'ignore') fail('Error: --bundle=load is incompatible with --lock=(add|replace)')
|
|
44
|
+
if (lock === 'none' && bundle === 'none') fail('Error: stasis needs a lockfile or a bundle: set --lock or --bundle')
|
|
45
|
+
// --fs requires a bundle mode (add|replace|load); `sync` patches the sync fs readers,
|
|
46
|
+
// `async` adds their callback + fs.promises counterparts on top.
|
|
47
|
+
if (values.fs !== undefined && !['sync', 'async'].includes(values.fs)) fail("Error: --fs must be 'sync' or 'async'")
|
|
48
|
+
if (values.fs !== undefined && !['add', 'replace', 'load'].includes(bundle)) fail('Error: --fs requires --bundle=(add|replace|load)')
|
|
49
|
+
const brotliQuality = brotliQualityOption(values['brotli-quality'], fail)
|
|
50
|
+
// --import: extra preload module(s) passed through to the spawned node. Node resolves each
|
|
51
|
+
// against the project cwd; they ride AFTER stasis's own loader import.
|
|
52
|
+
const imports = values.import ?? []
|
|
53
|
+
if (imports.some((s) => s === '')) fail('Error: --import requires a module specifier (e.g. --import=./instrument.mjs)')
|
|
54
|
+
return {
|
|
55
|
+
values,
|
|
56
|
+
lock,
|
|
57
|
+
scope: values.dependencies ? 'node_modules' : 'full',
|
|
58
|
+
bundle,
|
|
59
|
+
bundleFile,
|
|
60
|
+
resourcesBundleFile,
|
|
61
|
+
debug: values.debug ? '1' : '',
|
|
62
|
+
// --child-process: forward forked-child (e.g. Metro worker) capture to the root via per-pid shards.
|
|
63
|
+
childProcess: values['child-process'] ? '1' : '',
|
|
64
|
+
fs: values.fs ?? '',
|
|
65
|
+
// --resources: comma-separated extension/filename allowlist for `--fs` captures.
|
|
66
|
+
resources: values.resources ?? '',
|
|
67
|
+
brotliQuality,
|
|
68
|
+
imports,
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// The `--brotli-quality` given as `value`, a usage error where it's no quality. -> it, or undefined
|
|
73
|
+
export function brotliQualityOption(value, fail) {
|
|
74
|
+
if (value === undefined) return undefined
|
|
75
|
+
try {
|
|
76
|
+
return parseBrotliQuality('--brotli-quality', value)
|
|
77
|
+
} catch (cause) {
|
|
78
|
+
return fail(`Error: ${cause.message}`)
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// Sets the environment variable `name` to `value` (a string), refusing one the environment already
|
|
83
|
+
// sets otherwise.
|
|
84
|
+
export function setEnv(name, value) {
|
|
85
|
+
const env = process.env[name]
|
|
86
|
+
if (env && env !== value) throw new Error(`env conflict: ${name}="${env}", effective: "${value}"`)
|
|
87
|
+
process.env[name] = value
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// Announces `run` on stderr as `label` (the CLI's name), with the caller's own `extra` config after
|
|
91
|
+
// childProcess, and sets it in the environment the loader reads it from.
|
|
92
|
+
export function setRunEnv(label, run, extra = {}) {
|
|
93
|
+
const { lock, scope, bundle, bundleFile, resourcesBundleFile, debug, childProcess, fs, resources, brotliQuality, imports } = run
|
|
94
|
+
console.warn(`[${label}] Running stasis with config:`, { lock, scope, bundle, ...(bundleFile && { bundleFile }), ...(resourcesBundleFile && { resourcesBundleFile }), ...(childProcess && { childProcess: true }), ...extra, ...(imports.length > 0 && { import: imports }), ...(fs && { fs }), ...(resources && { resources }), ...(brotliQuality !== undefined && { brotliQuality }) })
|
|
95
|
+
if (debug) console.warn(`[${label}] Warning: stasis debug mode active`)
|
|
96
|
+
setEnv('EXODUS_STASIS_LOCK', lock)
|
|
97
|
+
setEnv('EXODUS_STASIS_SCOPE', scope)
|
|
98
|
+
setEnv('EXODUS_STASIS_BUNDLE', bundle)
|
|
99
|
+
setEnv('EXODUS_STASIS_BUNDLE_FILE', bundleFile)
|
|
100
|
+
setEnv('EXODUS_STASIS_RESOURCES_BUNDLE_FILE', resourcesBundleFile)
|
|
101
|
+
setEnv('EXODUS_STASIS_DEBUG', debug)
|
|
102
|
+
setEnv('EXODUS_STASIS_CHILD_PROCESS', childProcess)
|
|
103
|
+
setEnv('EXODUS_STASIS_FS', fs)
|
|
104
|
+
setEnv('EXODUS_STASIS_RESOURCES', resources)
|
|
105
|
+
// Only set when given: an unconditional setEnv('') would reject an ambient EXODUS_STASIS_BROTLI_QUALITY as a conflict.
|
|
106
|
+
if (brotliQuality !== undefined) setEnv('EXODUS_STASIS_BROTLI_QUALITY', String(brotliQuality))
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
// Runs the program `argv` in a node taking `nodeArgs`, then `--import`ing `loader` (a URL) and the
|
|
110
|
+
// run's `imports`. Those preloads ride after the loader, so they evaluate under its hooks; their
|
|
111
|
+
// module graphs pass through uncaptured unless the app graph reaches them -- then they're promoted
|
|
112
|
+
// into the capture (see hooks.js). Exits as the program does: its code, or 128+signo when a signal
|
|
113
|
+
// killed it (the shell's convention) instead of the implicit 0.
|
|
114
|
+
export async function runProgram(argv, { nodeArgs = [], loader, imports }) {
|
|
115
|
+
const args = [...nodeArgs, '--import', loader, ...imports.flatMap((specifier) => ['--import', specifier]), ...argv]
|
|
116
|
+
const child = spawn(process.execPath, args, { stdio: 'inherit' })
|
|
117
|
+
const [code, signal] = await once(child, 'close')
|
|
118
|
+
process.exitCode = code ?? 128 + (osConstants.signals[signal] ?? 0)
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
// `add`: packs the listed files (no resolver), taking split targets + resource allowlist from
|
|
122
|
+
// stasis.config.json; it takes no flags. Its runtime errors propagate as they are: wrapping them in
|
|
123
|
+
// fail() would bury the cause.
|
|
124
|
+
export async function addCli(argv, { label, fail }) {
|
|
125
|
+
if (argv.length === 0) fail('Nothing to add: no file given')
|
|
126
|
+
if (argv.some((a) => a.startsWith('-'))) fail('Error: add takes no options; its targets and resource allowlist come from stasis.config.json')
|
|
127
|
+
const { addCommand } = await import('./add.js')
|
|
128
|
+
addCommand({ cwd: process.cwd(), entries: argv, logLabel: label })
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
// `extract`: a bundle's files written out, as `--output` and `--lock` say.
|
|
132
|
+
export async function extractCli(argv, { label, fail }) {
|
|
133
|
+
const options = {
|
|
134
|
+
output: { type: 'string', short: 'o' },
|
|
135
|
+
lock: { type: 'string' },
|
|
136
|
+
}
|
|
137
|
+
const values = parseLeadingOptions(argv, options, { valueFlags: valueFlagsOf(options), onError: fail })
|
|
138
|
+
if (argv.length === 0) fail('Nothing to extract: no bundle file given')
|
|
139
|
+
if (argv.length > 1) fail('Error: extract takes exactly one bundle file')
|
|
140
|
+
// Unset: frozen when the output dir holds a stasis.lock.json, else none (see extractCommand).
|
|
141
|
+
if (values.lock !== undefined && !['frozen', 'add', 'replace', 'ignore', 'none'].includes(values.lock)) fail('Error: invalid --lock value')
|
|
142
|
+
const { extractCommand } = await import('./extract.js')
|
|
143
|
+
extractCommand({ cwd: process.cwd(), bundleFile: argv[0], output: values.output, lock: values.lock, logLabel: label })
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
// `prune`: the project's node_modules (the one given, else the cwd's) cut down to its lockfile.
|
|
147
|
+
export async function pruneCli(argv, { label, fail }) {
|
|
148
|
+
if (argv.length > 1) fail('Error: prune takes at most one path argument')
|
|
149
|
+
const root = argv[0] ? resolve(argv[0]) : process.cwd()
|
|
150
|
+
const { prune } = await import('./prune.js')
|
|
151
|
+
const { removed, validated, minimized } = prune({ root })
|
|
152
|
+
console.warn(`[${label}] prune: validated ${validated.length} file(s), removed ${removed.length} file(s), minimized ${minimized.length} package.json file(s)`)
|
|
153
|
+
}
|
package/src/config.js
CHANGED
|
@@ -2,7 +2,7 @@ import assert from 'node:assert/strict'
|
|
|
2
2
|
|
|
3
3
|
import { diskHost } from './host.js'
|
|
4
4
|
import { canonicalizePath } from './state-util.js'
|
|
5
|
-
import { extSetsEqual, isBrotliQuality, parseBrotliQuality, parseExtraOption, parseResourcesOption } from './util.js'
|
|
5
|
+
import { extSetsEqual, isBrotliQuality, parseBrotliQuality, parseExtraOption, parseInstallDevDependencies, parseLazyDependencies, parseResourcesOption } from './util.js'
|
|
6
6
|
|
|
7
7
|
export const DEFAULT_LOCK = 'add'
|
|
8
8
|
export const DEFAULT_BUNDLE = 'none'
|
|
@@ -21,8 +21,9 @@ const envList = (value) => value.split(',').map((s) => s.trim()).filter(Boolean)
|
|
|
21
21
|
// Every option: its env var, how the env string parses (strings pass through) and, for a non-string
|
|
22
22
|
// option, how a `new Config()` option does. `lockFile` and `bundle` are flag/env-only (never from
|
|
23
23
|
// stasis.config.json), and the file's `lock` can only be CONFIG_FILE_LOCK; `extra` is the reverse,
|
|
24
|
-
// stasis.config.json only (no env var or option),
|
|
25
|
-
//
|
|
24
|
+
// stasis.config.json only (no env var or option), `entries` is `stasis bundle`'s and
|
|
25
|
+
// `installDevDependencies` and `lazyDependencies` the trees laid out from the lockfile's (`stasis
|
|
26
|
+
// github-bundle`), which a run takes nothing from.
|
|
26
27
|
const OPTIONS = {
|
|
27
28
|
scope: { env: 'EXODUS_STASIS_SCOPE', valid: new Set(['node_modules', 'full']) },
|
|
28
29
|
lock: { env: 'EXODUS_STASIS_LOCK', valid: new Set(['none', 'ignore', 'add', 'replace', 'frozen']) },
|
|
@@ -39,7 +40,7 @@ const OPTIONS = {
|
|
|
39
40
|
resources: { env: 'EXODUS_STASIS_RESOURCES', parse: (name, value) => parseResourcesOption('env', envList(value)), option: (value) => parseResourcesOption('options', value) },
|
|
40
41
|
}
|
|
41
42
|
const OPTION_KEYS = Object.keys(OPTIONS)
|
|
42
|
-
const CONFIG_FILE_KEYS = [...OPTION_KEYS.filter((key) => key !== 'lockFile' && key !== 'bundle'), 'extra', 'entries']
|
|
43
|
+
const CONFIG_FILE_KEYS = [...OPTION_KEYS.filter((key) => key !== 'lockFile' && key !== 'bundle'), 'extra', 'entries', 'installDevDependencies', 'lazyDependencies']
|
|
43
44
|
// The one mode a stasis.config.json can pin: a run that writes, loads or ignores is given its modes
|
|
44
45
|
// explicitly (flags, env, options), never by a file.
|
|
45
46
|
const CONFIG_FILE_LOCK = 'frozen'
|
|
@@ -214,6 +215,15 @@ export class Config {
|
|
|
214
215
|
for (const key of keys) {
|
|
215
216
|
// `stasis bundle`'s, validated there.
|
|
216
217
|
if (key === 'entries') continue
|
|
218
|
+
// A run takes node_modules as installed, but a value github-bundle would refuse is refused here too.
|
|
219
|
+
if (key === 'installDevDependencies') {
|
|
220
|
+
parseInstallDevDependencies('stasis.config.json', config.installDevDependencies)
|
|
221
|
+
continue
|
|
222
|
+
}
|
|
223
|
+
if (key === 'lazyDependencies') {
|
|
224
|
+
parseLazyDependencies('stasis.config.json', config.lazyDependencies)
|
|
225
|
+
continue
|
|
226
|
+
}
|
|
217
227
|
if (key === 'resources') this.#opts.resources = parseResourcesOption('stasis.config.json', config.resources)
|
|
218
228
|
else if (key === 'extra') this.#opts.extra = parseExtraOption('stasis.config.json', config.extra)
|
|
219
229
|
// '' -> undefined like the constructor: otherwise canonicalizePath('') claims cwd as a write target.
|