@exodus/stasis 1.0.0-beta.14 → 1.0.0-beta.16
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 +2 -1
- package/bin/stasis.js +99 -16
- package/package.json +22 -9
- package/src/audit-corrections.js +11 -42
- package/src/audit.js +241 -190
- package/src/babel.js +39 -122
- package/src/bundle-from-lockfile.js +9 -21
- package/src/bundle-reader.js +5 -9
- package/src/bundle.js +0 -3
- package/src/cmd/build.js +82 -174
- package/src/cmd/bundle-options.js +113 -167
- package/src/cmd/bundle.js +942 -1294
- package/src/cmd/diff.js +1 -4
- package/src/cmd/github-bundle.js +21 -26
- package/src/cmd/output.js +15 -12
- package/src/cmd/sbom.js +3 -8
- package/src/cmd/tokens.js +16 -23
- package/src/define.js +36 -94
- package/src/diff.js +72 -128
- package/src/esbuild.js +0 -4
- package/src/flow.js +18 -13
- package/src/installed.js +265 -0
- package/src/json-stream.js +4 -9
- package/src/loader-mock.js +3 -8
- package/src/loader.js +1 -5
- package/src/loaders/bash.js +41 -93
- package/src/loaders/cargo.js +298 -805
- package/src/loaders/foundry.js +109 -285
- package/src/loaders/paths.js +13 -13
- package/src/loaders/php.js +210 -489
- package/src/loaders/rust.js +507 -1430
- package/src/loaders/solidity-ownership.js +48 -128
- package/src/loaders/solidity.js +39 -122
- package/src/loaders/toml.js +7 -17
- package/src/lockfile.js +0 -3
- package/src/metro-assets.js +10 -384
- package/src/metro-resolver.js +58 -134
- package/src/metro-transformer.js +0 -5
- package/src/metro.js +0 -4
- package/src/mock.js +32 -80
- package/src/parse.js +4 -4
- package/src/prune.js +0 -3
- package/src/resolve-fields.js +137 -532
- package/src/resolve-node.js +27 -45
- package/src/resolve-typescript.js +166 -298
- package/src/rollup.js +0 -4
- package/src/run-installed.js +10 -0
- package/src/sbom.js +52 -128
- package/src/scan.js +142 -284
- package/src/sixel.js +310 -0
- package/src/table.js +63 -31
- package/src/validate-notes.d.ts +19 -0
- package/src/validate-notes.js +72 -0
- package/src/validate.d.ts +159 -0
- package/src/validate.js +596 -522
- package/src/vfs-bundle/entries.js +40 -57
- package/src/vfs-bundle/github.js +80 -122
- package/src/vfs-bundle/glob.js +4 -45
- package/src/vfs-bundle/lazy.js +27 -56
- package/src/vfs-bundle/prisma.js +235 -105
- package/src/vfs-bundle/soldeer.js +28 -49
- package/src/vfs-bundle/tree.js +295 -318
- package/src/vfs-bundle/yarnclean.js +10 -31
- package/src/vfs-bundle.js +23 -64
- package/src/webpack.js +0 -4
- package/src/why.js +78 -232
- package/src/installed-commits.js +0 -184
|
@@ -1,20 +1,20 @@
|
|
|
1
1
|
import { isAbsolute, relative, resolve } from 'node:path'
|
|
2
2
|
|
|
3
3
|
import { diskHost } from '@exodus/stasis-core/host'
|
|
4
|
-
import {
|
|
4
|
+
import { POLICY_KEYS, parsePolicy } from '@exodus/stasis-core/policy'
|
|
5
|
+
import { CONFIG_FILE, isPlainObject, JS_EXTENSIONS, parseBrotliQuality, parseResourcesOption, relativeEscapes, SCOPES, toPosix, upsert } from '@exodus/stasis-core/util'
|
|
5
6
|
import { parseFeatureList } from '../loaders/cargo.js'
|
|
6
7
|
import { parseDefineFlags } from '../define.js'
|
|
7
8
|
import { directoryEntryError, isSolidityEntry, readConfigFile } from './bundle.js'
|
|
9
|
+
import { lockfileOnlyError } from './output.js'
|
|
8
10
|
|
|
9
|
-
const CONFIG_FILE = 'stasis.config.json'
|
|
10
|
-
|
|
11
|
-
// `stasis bundle`'s flags, as parseArgs takes them.
|
|
12
11
|
export const BUNDLE_FLAGS = {
|
|
13
12
|
mapping: { type: 'string' },
|
|
14
13
|
manifests: { type: 'boolean' },
|
|
15
14
|
output: { type: 'string', short: 'o' },
|
|
16
15
|
scope: { type: 'string' },
|
|
17
16
|
lockfile: { type: 'string' },
|
|
17
|
+
'lockfile-only': { type: 'boolean' },
|
|
18
18
|
conditions: { type: 'string' },
|
|
19
19
|
mainFields: { type: 'string' },
|
|
20
20
|
metro: { type: 'boolean' },
|
|
@@ -40,150 +40,123 @@ export const BUNDLE_FLAGS = {
|
|
|
40
40
|
deptree: { type: 'boolean' },
|
|
41
41
|
}
|
|
42
42
|
|
|
43
|
-
// The flags of the bundle as a whole
|
|
44
|
-
|
|
45
|
-
// sets per entry instead.
|
|
46
|
-
const OUTPUT_FLAGS = new Set(['output', 'lockfile', 'brotli-quality', 'add', 'deptree'])
|
|
47
|
-
// The entry flags taking a list (comma-separated, or repeated), which an entry's options may give as an array.
|
|
43
|
+
// The flags of the bundle as a whole; every other one is an entry's, which stasis.config.json's `entries` sets.
|
|
44
|
+
const OUTPUT_FLAGS = new Set(['output', 'lockfile', 'lockfile-only', 'brotli-quality', 'add', 'deptree'])
|
|
48
45
|
const LIST_FLAGS = new Set(['conditions', 'mainFields', 'platforms', 'resources', 'cargo-features'])
|
|
49
|
-
//
|
|
50
|
-
// hold one): an entry's options give them as an array, or one as a string.
|
|
46
|
+
// Given once per value, which a comma doesn't separate (a define's JSON value may hold one).
|
|
51
47
|
const REPEATED_FLAGS = new Set(['define'])
|
|
52
|
-
// The entry flags that run a program -> what they run. Set in stasis.config.json, they take effect
|
|
53
|
-
//
|
|
54
|
-
// make a bare `stasis bundle` run anything.
|
|
48
|
+
// The entry flags that run a program -> what they run. Set in stasis.config.json, they take effect only with
|
|
49
|
+
// the same flag (and value) given too, as consent: the config can't make a bare `stasis bundle` run anything.
|
|
55
50
|
const CONSENT_FLAGS = new Map([
|
|
56
51
|
['babel', "the project's Babel config"],
|
|
57
52
|
['metro-resolver', "the project's metro-resolver"],
|
|
58
53
|
['cargo', "cargo, and what the project's .cargo/config.toml names"],
|
|
59
54
|
['cargo-target', 'rustc'],
|
|
60
55
|
])
|
|
61
|
-
// A flag as given: `--name`, or `--name=value` for one taking a value.
|
|
62
56
|
const flagText = (name, value) => (value === true ? `--${name}` : `--${name}=${value}`)
|
|
63
57
|
|
|
64
|
-
|
|
58
|
+
function listFlag(value, error) {
|
|
59
|
+
if (value === undefined) return undefined
|
|
60
|
+
const list = value.split(',').map((s) => s.trim()).filter(Boolean)
|
|
61
|
+
if (list.length === 0) throw new Error(error)
|
|
62
|
+
return list
|
|
63
|
+
}
|
|
64
|
+
|
|
65
65
|
function entryKey(cwd, entry) {
|
|
66
66
|
const rel = relative(resolve(cwd), resolve(cwd, entry))
|
|
67
67
|
return relativeEscapes(rel) ? null : toPosix(rel) || '.'
|
|
68
68
|
}
|
|
69
69
|
|
|
70
|
-
// The
|
|
71
|
-
|
|
70
|
+
// The entryKeys of `entries`, and the first of them `config` names: it may name all of them or none, not some.
|
|
71
|
+
function configuredKeys(cwd, entries, config, prefix = '') {
|
|
72
|
+
const keys = entries.map((entry) => entryKey(cwd, entry))
|
|
73
|
+
const first = config === null ? undefined : entries.find((_, i) => config.has(keys[i]))
|
|
74
|
+
const unnamed = first === undefined ? undefined : entries.find((_, i) => !config.has(keys[i]))
|
|
75
|
+
if (unnamed !== undefined) {
|
|
76
|
+
throw new Error(`${prefix}${CONFIG_FILE}'s entries name ${first} but not ${unnamed}: bundle either the entries it names or others, not both`)
|
|
77
|
+
}
|
|
78
|
+
return { keys, first }
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function prefixed(prefix, fn) {
|
|
82
|
+
try {
|
|
83
|
+
return fn()
|
|
84
|
+
} catch (cause) {
|
|
85
|
+
throw new Error(`${prefix}${cause.message}`, { cause })
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
const isJsEntry = (entry) => JS_EXTENSIONS.values().some((ext) => entry.endsWith(ext))
|
|
90
|
+
|
|
91
|
+
// Which language `entries` all are in, or `jsAndSol` for JS and Solidity entries together, each built apart.
|
|
72
92
|
function entriesLanguage(entries, cwd, host = diskHost) {
|
|
73
|
-
|
|
93
|
+
// Beside JS entries, a directory one is of the Solidity ones.
|
|
94
|
+
const dirError = directoryEntryError(entries.filter((f) => !isJsEntry(f)), cwd, host)
|
|
74
95
|
if (dirError !== null) throw new Error(dirError)
|
|
75
96
|
const lang = {
|
|
76
97
|
allSol: entries.every((f) => isSolidityEntry(f, cwd, host)),
|
|
77
98
|
allPhp: entries.every((f) => f.endsWith('.php')),
|
|
78
|
-
allJs: entries.every(
|
|
99
|
+
allJs: entries.every(isJsEntry),
|
|
79
100
|
allBash: entries.every((f) => /\.(?:sh|bash)$/u.test(f)),
|
|
80
101
|
allRust: entries.every((f) => f.endsWith('.rs')),
|
|
102
|
+
allTeal: entries.every((f) => f.endsWith('.teal')),
|
|
81
103
|
}
|
|
82
|
-
if (
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
return lang
|
|
104
|
+
if (Object.values(lang).some(Boolean)) return lang
|
|
105
|
+
if (entries.every((f) => isJsEntry(f) || isSolidityEntry(f, cwd, host))) return { ...lang, jsAndSol: true }
|
|
106
|
+
throw new Error('bundle entries must all be .sol, all be .php, all be .js/.cjs/.mjs/.ts/.cts/.mts/.jsx/.tsx, all be .sh/.bash, all be .rs, or all be .teal, or be .js/.cjs/.mjs/.ts/.cts/.mts/.jsx/.tsx and .sol together')
|
|
86
107
|
}
|
|
87
108
|
|
|
88
|
-
// bundleCommand's options for building `entries` (in `lang`, entriesLanguage's) from the parsed
|
|
89
|
-
// entry flags in `values`. A usage error throws.
|
|
90
109
|
function buildOptions(values, { allSol, allJs, allRust }) {
|
|
91
110
|
if (values.mapping && !allSol) throw new Error('--mapping is only valid for .sol bundles')
|
|
92
|
-
// --manifests: carry the Solidity build's description files (foundry.toml, remappings.txt, ...).
|
|
93
111
|
const manifests = Boolean(values.manifests)
|
|
94
112
|
if (manifests && !allSol) throw new Error('--manifests is only valid for .sol bundles')
|
|
95
|
-
// --cargo: take the Rust feature/dependency resolution from `cargo metadata` (runs cargo; opt-in).
|
|
96
113
|
const cargo = Boolean(values.cargo)
|
|
97
114
|
if (cargo && !allRust) throw new Error('--cargo is only valid for Rust bundles')
|
|
98
|
-
// --cargo-features / --cargo-no-default-features / --cargo-all-features: cargo's own feature flags
|
|
99
|
-
// for the entries' packages (`pkg/feat` targets one, or a dependency). --cargo-features is
|
|
100
|
-
// repeatable and/or comma-separated; parseFeatureList is the one splitter (the resolver reuses it).
|
|
101
115
|
const cargoFeatures = parseFeatureList(values['cargo-features'] ?? [])
|
|
102
116
|
if (values['cargo-features'] !== undefined && cargoFeatures.length === 0) {
|
|
103
117
|
throw new Error('--cargo-features must list at least one feature (e.g. --cargo-features=serde,app/tls)')
|
|
104
118
|
}
|
|
105
119
|
const cargoNoDefaultFeatures = Boolean(values['cargo-no-default-features'])
|
|
106
120
|
const cargoAllFeatures = Boolean(values['cargo-all-features'])
|
|
107
|
-
// --cargo-target=<triple|host>: the build's target, whose cfgs (from `rustc --print cfg`) decide
|
|
108
|
-
// `#[cfg(unix)]`-style code. A triple is letters, digits, `-`, `_` and `.` (`x86_64-unknown-linux-gnu`).
|
|
109
121
|
const cargoTarget = values['cargo-target'] ?? null
|
|
110
122
|
if (cargoTarget !== null && !/^[\w.-]+$/u.test(cargoTarget)) {
|
|
111
123
|
throw new Error('--cargo-target must be a target triple or "host" (e.g. --cargo-target=aarch64-apple-darwin)')
|
|
112
124
|
}
|
|
113
|
-
// --cargo-manifests: carry each bundled package's Cargo.toml and build script, the workspace
|
|
114
|
-
// Cargo.toml, Cargo.lock and .cargo/config.toml too (the Rust counterpart of --package-json).
|
|
115
125
|
const cargoManifests = Boolean(values['cargo-manifests'])
|
|
116
126
|
if (!allRust) {
|
|
117
127
|
const given = { 'cargo-features': cargoFeatures.length > 0, 'cargo-no-default-features': cargoNoDefaultFeatures, 'cargo-all-features': cargoAllFeatures, 'cargo-target': cargoTarget !== null, 'cargo-manifests': cargoManifests }
|
|
118
128
|
for (const [flag, on] of Object.entries(given)) if (on) throw new Error(`--${flag} is only valid for Rust bundles`)
|
|
119
129
|
}
|
|
120
130
|
if (values.scope && !allJs) throw new Error('--scope is only valid for JS bundles')
|
|
121
|
-
if (values.scope && !
|
|
122
|
-
throw new Error('--scope must be node_modules or full')
|
|
123
|
-
}
|
|
124
|
-
// --mainFields/--metro always emit a full-scope bundle, so they'd silently ignore a narrower --scope.
|
|
131
|
+
if (values.scope && !SCOPES.has(values.scope)) throw new Error('--scope must be node_modules or full')
|
|
125
132
|
if (values.scope !== undefined && values.scope !== 'full' && (values.mainFields !== undefined || values.metro)) {
|
|
126
133
|
throw new Error(`--scope=${values.scope} is not supported with --mainFields or --metro (they always build a full-scope bundle)`)
|
|
127
134
|
}
|
|
128
|
-
// --conditions: extra exports/imports resolution conditions merged onto Node's defaults;
|
|
129
|
-
// they don't honour legacy `mainFields` or platform suffixes (see --mainFields).
|
|
130
135
|
if (values.conditions !== undefined && !allJs) throw new Error('--conditions is only valid for JS bundles')
|
|
131
|
-
const conditions = values.conditions
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
if (values.conditions !== undefined && conditions.length === 0) {
|
|
135
|
-
throw new Error('--conditions must list at least one condition name (e.g. --conditions=react-native,browser)')
|
|
136
|
-
}
|
|
137
|
-
// --flow: strip Flow type syntax (via stasis's own flow-remove-types) before parsing; JS-only.
|
|
136
|
+
const conditions = listFlag(values.conditions, '--conditions must list at least one condition name (e.g. --conditions=react-native,browser)') ?? []
|
|
137
|
+
// --metro parses JSX and Flow, as Metro's Babel does in every file.
|
|
138
|
+
const metro = Boolean(values.metro)
|
|
138
139
|
if (values.flow && !allJs) throw new Error('--flow is only valid for JS bundles')
|
|
139
|
-
const flow = Boolean(values.flow)
|
|
140
|
-
// --typescript: resolve like tsc -- a specifier naming a missing JS output (./x.js, .mjs, .cjs,
|
|
141
|
-
// extensionless) lands on its on-disk TS source (./x.ts, .mts, .cts); an existing .js always wins.
|
|
140
|
+
const flow = Boolean(values.flow) || metro
|
|
142
141
|
if (values.typescript && !allJs) throw new Error('--typescript is only valid for JS bundles')
|
|
143
142
|
const typescript = Boolean(values.typescript)
|
|
144
|
-
// --tsconfig: the config whose compilerOptions.paths aliases --typescript honours (default: the
|
|
145
|
-
// tsconfig.json beside each importing file's package.json). Meaningless without --typescript.
|
|
146
143
|
if (values.tsconfig !== undefined && !typescript) throw new Error('--tsconfig is only valid with --typescript')
|
|
147
|
-
// --mainFields: legacy package entry fields (e.g. react-native,browser,main) for the non-exports resolver.
|
|
148
144
|
if (values.mainFields !== undefined && !allJs) throw new Error('--mainFields is only valid for JS bundles')
|
|
149
|
-
const mainFields = values.mainFields
|
|
150
|
-
? undefined
|
|
151
|
-
: values.mainFields.split(',').map((s) => s.trim()).filter(Boolean)
|
|
152
|
-
if (values.mainFields !== undefined && mainFields.length === 0) {
|
|
153
|
-
throw new Error('--mainFields must list at least one field (e.g. --mainFields=react-native,browser,main)')
|
|
154
|
-
}
|
|
155
|
-
// --metro: resolve like Metro/RN (its own conditions + mainFields, platform suffixes per
|
|
156
|
-
// --platforms target). --platforms is repeatable and/or comma-separated; the values union.
|
|
157
|
-
const metro = Boolean(values.metro)
|
|
158
|
-
// --metro-resolver: resolve through the project's own metro-resolver instead of the built-in
|
|
159
|
-
// approximation. Only meaningful alongside --metro (enforced below and in classifyEntries).
|
|
145
|
+
const mainFields = listFlag(values.mainFields, '--mainFields must list at least one field (e.g. --mainFields=react-native,browser,main)')
|
|
160
146
|
const metroResolver = Boolean(values['metro-resolver'])
|
|
161
147
|
const platforms = [...new Set((values.platforms ?? []).flatMap((p) => p.split(',')).map((s) => s.trim()).filter(Boolean))]
|
|
162
|
-
// A platform name becomes an edge key
|
|
148
|
+
// A platform name becomes an edge key, so no '/' (parsers reject it) or '*' (a reserved placeholder).
|
|
163
149
|
for (const p of platforms) {
|
|
164
150
|
if (p === '*' || p.includes('/')) throw new Error(`invalid --platforms value '${p}' (a platform name can't contain '/' or be '*')`)
|
|
165
151
|
}
|
|
166
152
|
if ((metro || platforms.length > 0) && !allJs) throw new Error('--metro is only valid for JS bundles')
|
|
167
|
-
|
|
168
|
-
// like tsc, auto-enables JSX for .jsx/.tsx only). Orthogonal to the resolver, so it pairs with
|
|
169
|
-
// plain/--mainFields/--metro alike -- JS-only, since no other entry language is scanned.
|
|
170
|
-
const jsx = Boolean(values.jsx)
|
|
153
|
+
const jsx = Boolean(values.jsx) || metro
|
|
171
154
|
if (jsx && !allJs) throw new Error('--jsx is only valid for JS bundles')
|
|
172
|
-
// --package-json: fold each bundled module's package.json into the bundle. JS-only (only JS
|
|
173
|
-
// modules carry an npm package.json; the sol/php/bash/rust bucketizers have no such manifest).
|
|
174
155
|
const packageJSON = Boolean(values['package-json'])
|
|
175
156
|
if (packageJSON && !allJs) throw new Error('--package-json is only valid for JS bundles')
|
|
176
|
-
// --resources: comma-separated extension/filename allowlist for assets reached through the graph
|
|
177
|
-
// (e.g. --resources=png,svg). They're carried as resources instead of failing "can't carry" --
|
|
178
|
-
// for graphs that aren't fully loadable in JS (Metro consumes such assets). JS-only.
|
|
179
157
|
if (values.resources !== undefined && !allJs) throw new Error('--resources is only valid for JS bundles')
|
|
180
|
-
const resources = values.resources
|
|
181
|
-
|
|
182
|
-
: values.resources.split(',').map((s) => s.trim()).filter(Boolean)
|
|
183
|
-
if (values.resources !== undefined && resources.length === 0) {
|
|
184
|
-
throw new Error('--resources must list at least one extension or filename (e.g. --resources=png,svg)')
|
|
185
|
-
}
|
|
186
|
-
parseResourcesOption('--resources', resources) // validate early for a clean usage error (code exts / bad chars rejected)
|
|
158
|
+
const resources = listFlag(values.resources, '--resources must list at least one extension or filename (e.g. --resources=png,svg)')
|
|
159
|
+
parseResourcesOption('--resources', resources) // checked here, for a clean usage error
|
|
187
160
|
if (metro) {
|
|
188
161
|
if (conditions.length > 0) throw new Error("--conditions can't be combined with --metro (it sets its own conditions)")
|
|
189
162
|
if (mainFields !== undefined) throw new Error("--mainFields can't be combined with --metro (it sets its own mainFields)")
|
|
@@ -192,53 +165,42 @@ function buildOptions(values, { allSol, allJs, allRust }) {
|
|
|
192
165
|
throw new Error('--platforms is only valid with --metro')
|
|
193
166
|
}
|
|
194
167
|
if (metroResolver && !metro) throw new Error('--metro-resolver is only valid with --metro')
|
|
195
|
-
// --babel: resolve through the import aliases (babel-plugin-module-resolver) of the project's
|
|
196
|
-
// babel.config.*, which Metro runs before it resolves. Runs that config, so opt-in; --metro only.
|
|
197
168
|
const babel = Boolean(values.babel)
|
|
198
169
|
if (babel && !metro) throw new Error('--babel is only valid with --metro')
|
|
199
|
-
// --babel-aliases: the same aliases from a JSON file of the project (module-resolver's `alias` option as
|
|
200
|
-
// data), which runs nothing, so stasis.config.json's entries may set it alone. --metro only.
|
|
201
170
|
if (values['babel-aliases'] !== undefined && !metro) throw new Error('--babel-aliases is only valid with --metro')
|
|
202
171
|
if (values['babel-aliases'] !== undefined && babel) throw new Error("--babel-aliases can't be combined with --babel (both set the import aliases)")
|
|
203
|
-
// metro-resolver can't substitute .js -> .ts, so --typescript would silently not apply -- reject it.
|
|
204
172
|
if (typescript && metroResolver) throw new Error("--typescript is not supported with --metro-resolver (the project's metro-resolver doesn't substitute .js -> .ts)")
|
|
205
|
-
// --define=KEY=VALUE: a global's value (JSON), repeatable; the scan leaves out the imports of the
|
|
206
|
-
// branches the values keep from running (`if (__DEV__)` under __DEV__=false). JS-only.
|
|
207
173
|
if (values.define !== undefined && !allJs) throw new Error('--define is only valid for JS bundles')
|
|
208
174
|
const define = values.define === undefined ? undefined : parseDefineFlags(null, values.define)
|
|
209
175
|
return { mappingFile: values.mapping, manifests, scope: values.scope, conditions, mainFields, metro, metroResolver, babel, babelAliases: values['babel-aliases'], platforms, jsx, flow, typescript, tsconfig: values.tsconfig, resources, define, packageJSON, cargo, cargoFeatures, cargoNoDefaultFeatures, cargoAllFeatures, cargoTarget, cargoManifests }
|
|
210
176
|
}
|
|
211
177
|
|
|
212
|
-
// The CONSENT_FLAGS that run a program of the project's from disk,
|
|
213
|
-
// disk: none that --deptree lays out in memory.
|
|
178
|
+
// The CONSENT_FLAGS that run a program of the project's from disk, where --deptree installs nothing.
|
|
214
179
|
const DISK_RUN_FLAGS = ['babel', 'metro-resolver']
|
|
215
180
|
|
|
216
|
-
|
|
217
|
-
function outputOptions(values, { allJs, allSol }) {
|
|
181
|
+
function outputOptions(values, { allJs, allSol, jsAndSol }) {
|
|
218
182
|
if (values.lockfile && !allJs) throw new Error('--lockfile is only valid for JS bundles')
|
|
219
|
-
// --deptree: the dependencies laid out in memory from the lockfile (pnpm, yarn 1, npm or Soldeer's),
|
|
220
|
-
// never read from disk, which holds none.
|
|
221
183
|
const deptree = Boolean(values.deptree)
|
|
222
|
-
if (deptree && !allJs && !allSol) throw new Error('--deptree is only valid for JS and .sol bundles')
|
|
184
|
+
if (deptree && !allJs && !allSol && !jsAndSol) throw new Error('--deptree is only valid for JS and .sol bundles')
|
|
223
185
|
for (const flag of deptree ? DISK_RUN_FLAGS : []) {
|
|
224
186
|
if (values[flag]) throw new Error(`--${flag} can't be combined with --deptree (it runs ${CONSENT_FLAGS.get(flag)} from disk, where --deptree installs nothing)`)
|
|
225
187
|
}
|
|
226
|
-
// --brotli-quality: valid for every entry language (no allJs gate).
|
|
227
188
|
const brotliQuality = values['brotli-quality'] === undefined ? undefined : parseBrotliQuality('--brotli-quality', values['brotli-quality'])
|
|
228
|
-
// --add merges into the existing bundle at --output, so it can't target write-only stdout.
|
|
229
189
|
const add = Boolean(values.add)
|
|
230
190
|
if (add && values.output === '-') throw new Error('--add cannot be combined with --output=-')
|
|
231
|
-
|
|
191
|
+
const lockfileOnly = Boolean(values['lockfile-only'])
|
|
192
|
+
const lockfileOnlyRefused = lockfileOnly ? lockfileOnlyError({ lockfile: values.lockfile, output: values.output, add, brotliQuality: values['brotli-quality'] }) : null
|
|
193
|
+
if (lockfileOnlyRefused !== null) throw new Error(lockfileOnlyRefused)
|
|
194
|
+
return { output: values.output, lockfile: values.lockfile, lockfileOnly, brotliQuality, add, deptree }
|
|
232
195
|
}
|
|
233
196
|
|
|
234
|
-
// An entry's options in stasis.config.json's `entries`
|
|
235
|
-
//
|
|
236
|
-
// taking a value, which a list flag takes as an array too, and an array of it for a repeatable one
|
|
237
|
-
// (each of a REPEATED_FLAGS one's values, as given).
|
|
197
|
+
// An entry's options in stasis.config.json's `entries` as parseArgs would give the flags they alias: `true`
|
|
198
|
+
// for a boolean set (`false` is no flag), a list flag's array as one string, a REPEATED_FLAGS one's as given.
|
|
238
199
|
function entryFlags(what, options) {
|
|
239
200
|
if (!isPlainObject(options)) throw new Error(`${what} must be an object of stasis bundle options`)
|
|
240
201
|
const values = {}
|
|
241
202
|
for (const [flag, value] of Object.entries(options)) {
|
|
203
|
+
if (POLICY_KEYS.includes(flag)) continue
|
|
242
204
|
if (OUTPUT_FLAGS.has(flag)) throw new Error(`${what}: ${flag} is the bundle's option, not an entry's: pass --${flag}`)
|
|
243
205
|
if (!Object.hasOwn(BUNDLE_FLAGS, flag)) throw new Error(`${what}: unknown option ${JSON.stringify(flag)}`)
|
|
244
206
|
const { type, multiple } = BUNDLE_FLAGS[flag]
|
|
@@ -260,9 +222,7 @@ function entryFlags(what, options) {
|
|
|
260
222
|
return values
|
|
261
223
|
}
|
|
262
224
|
|
|
263
|
-
// The `entries` of the stasis.config.json in `cwd
|
|
264
|
-
// -> { entry: the name as written, values: its options as flags (entryFlags') }. null where there is
|
|
265
|
-
// none.
|
|
225
|
+
// The `entries` of the stasis.config.json in `cwd`, by entryKey; an entry's own Policy is held beside the config's.
|
|
266
226
|
export function readConfigEntries(cwd = process.cwd(), host = diskHost) {
|
|
267
227
|
const entries = readConfigFile(undefined, cwd, host)?.entries
|
|
268
228
|
if (entries === undefined) return null
|
|
@@ -273,29 +233,31 @@ export function readConfigEntries(cwd = process.cwd(), host = diskHost) {
|
|
|
273
233
|
const key = isAbsolute(entry) ? null : entryKey(cwd, entry)
|
|
274
234
|
if (key === null) throw new Error(`${what}: an entry must be a path relative to the directory of ${CONFIG_FILE}, within it`)
|
|
275
235
|
if (named.has(key)) throw new Error(`${what} names the same entry as ${JSON.stringify(named.get(key).entry)}`)
|
|
276
|
-
|
|
236
|
+
const values = entryFlags(what, options)
|
|
237
|
+
const policy = parsePolicy(what, options)
|
|
238
|
+
named.set(key, { entry, values, ...(policy !== null && { policy }) })
|
|
277
239
|
}
|
|
278
240
|
return named
|
|
279
241
|
}
|
|
280
242
|
|
|
281
|
-
//
|
|
282
|
-
//
|
|
283
|
-
// built with its options there, the ones with the same options together (a build each), and none
|
|
284
|
-
// may be given as a flag but the bundle's own (OUTPUT_FLAGS) and the consent to a CONSENT_FLAGS
|
|
285
|
-
// option, which an entry setting it needs and some entry must set. Where it names none, they are all
|
|
286
|
-
// built with the flags; it may not name only some. A usage error throws.
|
|
243
|
+
// Where `config` names every entry, each is built with its options there, the flags given only the bundle's own
|
|
244
|
+
// and the consent its CONSENT_FLAGS need; where it names none, all are built with the flags.
|
|
287
245
|
export function bundlePlan({ cwd = process.cwd(), entries, values, config = null }) {
|
|
288
246
|
const lang = entriesLanguage(entries, cwd)
|
|
289
247
|
const output = outputOptions(values, lang)
|
|
290
|
-
const keys
|
|
291
|
-
|
|
292
|
-
if (
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
248
|
+
const { keys, first } = configuredKeys(cwd, entries, config)
|
|
249
|
+
if (first === undefined && !lang.jsAndSol) return { ...output, entries, ...buildOptions(values, lang) }
|
|
250
|
+
if (first === undefined) {
|
|
251
|
+
return { ...output, builds: [...Map.groupBy(entries, isJsEntry).values()].map((part) => {
|
|
252
|
+
try {
|
|
253
|
+
return { entries: part, ...buildOptions(values, entriesLanguage(part, cwd)) }
|
|
254
|
+
} catch (cause) {
|
|
255
|
+
throw new Error(`${cause.message}, and ${part[0]} is not of one: give the JS and .sol entries' options apart, in ${CONFIG_FILE}'s entries`, { cause })
|
|
256
|
+
}
|
|
257
|
+
}) }
|
|
296
258
|
}
|
|
297
259
|
const flag = Object.keys(values).find((name) => !OUTPUT_FLAGS.has(name) && !CONSENT_FLAGS.has(name))
|
|
298
|
-
if (flag !== undefined) throw new Error(`--${flag} can't be given for ${
|
|
260
|
+
if (flag !== undefined) throw new Error(`--${flag} can't be given for ${first}, whose options ${CONFIG_FILE}'s entries set: set it there`)
|
|
299
261
|
const named = keys.map((key) => config.get(key))
|
|
300
262
|
for (const consent of CONSENT_FLAGS.keys()) {
|
|
301
263
|
const given = values[consent]
|
|
@@ -311,28 +273,34 @@ export function bundlePlan({ cwd = process.cwd(), entries, values, config = null
|
|
|
311
273
|
const consent = flagText(unconsented, flags[unconsented])
|
|
312
274
|
throw new Error(`${CONFIG_FILE}: entries ${JSON.stringify(name)} sets ${consent.slice(2)}, which runs ${CONSENT_FLAGS.get(unconsented)}: pass ${consent} to allow it`)
|
|
313
275
|
}
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
} catch (cause) {
|
|
318
|
-
throw new Error(`${CONFIG_FILE}: entries ${JSON.stringify(name)}: ${cause.message}`, { cause })
|
|
319
|
-
}
|
|
320
|
-
const id = JSON.stringify(options)
|
|
321
|
-
if (builds.has(id)) builds.get(id).entries.push(entry)
|
|
322
|
-
else builds.set(id, { entries: [entry], ...options })
|
|
276
|
+
const what = `${CONFIG_FILE}: entries ${JSON.stringify(name)}: `
|
|
277
|
+
const language = prefixed(what, () => entriesLanguage([entry], cwd))
|
|
278
|
+
addToBuild(builds, entry, named[i], prefixed(what, () => buildOptions(flags, language)), language)
|
|
323
279
|
}
|
|
324
|
-
return { ...output, builds:
|
|
280
|
+
return { ...output, builds: withPolicies(builds) }
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
// To the build of the same language, options and Policy (the first entry's, relabelled by withPolicies).
|
|
284
|
+
function addToBuild(builds, entry, named, options, language) {
|
|
285
|
+
const id = JSON.stringify([language, options, named.policy?.key])
|
|
286
|
+
const build = upsert(builds, id, () => ({ entries: [], ...options, written: [], policy: named.policy }))
|
|
287
|
+
build.entries.push(entry)
|
|
288
|
+
build.written.push(named.entry)
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
function withPolicies(builds, prefix = '') {
|
|
292
|
+
return [...builds.values()].map(({ written, policy, ...build }) => (policy === undefined ? build
|
|
293
|
+
: { ...build, policy: policy.withLabel(`${prefix}${CONFIG_FILE}: entries ${written.map((entry) => JSON.stringify(entry)).join(', ')}`) }))
|
|
325
294
|
}
|
|
326
295
|
|
|
327
|
-
// buildOptions' options, each by the flag it is given as; and those whose values are a set, in
|
|
328
|
-
// whatever order given (each value of `resources` as parseResourcesOption takes it).
|
|
329
296
|
const OPTION_FLAGS = {
|
|
330
297
|
mappingFile: 'mapping', manifests: 'manifests', scope: 'scope', conditions: 'conditions', mainFields: 'mainFields', metro: 'metro', metroResolver: 'metro-resolver', babel: 'babel', babelAliases: 'babel-aliases', platforms: 'platforms', jsx: 'jsx', flow: 'flow', typescript: 'typescript', tsconfig: 'tsconfig', resources: 'resources', define: 'define', packageJSON: 'package-json', cargo: 'cargo', cargoFeatures: 'cargo-features', cargoNoDefaultFeatures: 'cargo-no-default-features', cargoAllFeatures: 'cargo-all-features', cargoTarget: 'cargo-target', cargoManifests: 'cargo-manifests',
|
|
331
298
|
}
|
|
299
|
+
// Those whose values are a set, in whatever order given.
|
|
332
300
|
const SET_OPTIONS = new Set(['conditions', 'platforms', 'resources', 'cargoFeatures'])
|
|
333
|
-
// buildOptions' options where no flag is given.
|
|
334
301
|
const DEFAULT_OPTIONS = buildOptions({}, {})
|
|
335
|
-
// An option's value as compared: a set's sorted
|
|
302
|
+
// An option's value as compared: a set's sorted (each of `resources` as parseResourcesOption takes it), defines
|
|
303
|
+
// by name, 'null' for no value or no define.
|
|
336
304
|
const comparable = (key, value) => {
|
|
337
305
|
if (value === undefined || value === null || (key === 'define' && isPlainObject(value) && Object.keys(value).length === 0)) return 'null'
|
|
338
306
|
if (key === 'resources' && Array.isArray(value)) value = value.map((v) => (typeof v === 'string' ? v.toLowerCase().replace(/^\./u, '') : v))
|
|
@@ -340,62 +308,40 @@ const comparable = (key, value) => {
|
|
|
340
308
|
if (key === 'define' && isPlainObject(value)) return JSON.stringify(Object.entries(value).toSorted(([a], [b]) => (a < b ? -1 : 1)))
|
|
341
309
|
return JSON.stringify(value)
|
|
342
310
|
}
|
|
343
|
-
// An option as its flags give it: `--jsx`, `--platforms=ios,android`, `--define=__DEV__=false`.
|
|
344
311
|
const optionText = (key, value) => (key === 'define'
|
|
345
312
|
? Object.entries(value).map(([name, v]) => `--define=${name}=${JSON.stringify(v)}`).join(' ')
|
|
346
313
|
: flagText(OPTION_FLAGS[key], Array.isArray(value) ? value.join(',') : value))
|
|
347
314
|
|
|
348
|
-
// The builds of `entries`
|
|
349
|
-
//
|
|
350
|
-
// (@exodus/stasis/vfs-bundle's): every entry it names where none are given (none, of an empty
|
|
351
|
-
// `entries`: nothing else stands in for them), as paths from `cwd`.
|
|
352
|
-
// -> { entries, builds }, each build `{ entries, ...options }` with the options there (buildOptions'),
|
|
353
|
-
// the entries with the same ones together; null where it names none of those given, or has no
|
|
354
|
-
// `entries`: they are built with `options` alone. It may not name only some. Each of `options`
|
|
355
|
-
// (buildOptions' shape) given other than at its default has to be the one each entry's options
|
|
356
|
-
// set, else it is refused, neither taken over them nor left aside; one left out is theirs. One that
|
|
357
|
-
// runs a program (CONSENT_FLAGS'), set by an entry or given, is refused: nothing of the project
|
|
358
|
-
// runs, so no option allows it. A usage error throws.
|
|
315
|
+
// The builds of `entries` (by default, all it names) the config names, for a bundle of a project it runs nothing
|
|
316
|
+
// of (vfs-bundle's), or null where it names none. Each option given off its default must be what each entry sets.
|
|
359
317
|
export function configuredBuilds(name, { cwd, host = diskHost, entries, options = {} }) {
|
|
360
|
-
const
|
|
361
|
-
try {
|
|
362
|
-
return fn()
|
|
363
|
-
} catch (cause) {
|
|
364
|
-
throw new Error(`${what}${cause.message}`, { cause })
|
|
365
|
-
}
|
|
366
|
-
}
|
|
367
|
-
const config = prefixed(() => readConfigEntries(cwd, host))
|
|
318
|
+
const config = prefixed(`${name}: `, () => readConfigEntries(cwd, host))
|
|
368
319
|
if (config === null || (entries !== undefined && !Array.isArray(entries))) return null
|
|
369
320
|
if (entries === undefined) {
|
|
370
321
|
entries = [...config.keys()]
|
|
371
322
|
if (entries.length === 0) return { entries, builds: [] }
|
|
372
323
|
}
|
|
373
|
-
const keys
|
|
374
|
-
|
|
375
|
-
if (configured.length === 0) return null
|
|
376
|
-
const unnamed = entries.find((_, i) => !config.has(keys[i]))
|
|
377
|
-
if (unnamed !== undefined) {
|
|
378
|
-
throw new Error(`${name}: ${CONFIG_FILE}'s entries name ${configured[0]} but not ${unnamed}: bundle either the entries it names or others, not both`)
|
|
379
|
-
}
|
|
324
|
+
const { keys, first } = configuredKeys(cwd, entries, config, `${name}: `)
|
|
325
|
+
if (first === undefined) return null
|
|
380
326
|
const given = Object.keys(OPTION_FLAGS).filter((key) => options[key] !== undefined && comparable(key, options[key]) !== comparable(key, DEFAULT_OPTIONS[key]))
|
|
381
327
|
const builds = new Map()
|
|
382
328
|
for (const [i, entry] of entries.entries()) {
|
|
383
|
-
const
|
|
329
|
+
const named = config.get(keys[i])
|
|
330
|
+
const { entry: written, values } = named
|
|
384
331
|
const what = `${name}: ${CONFIG_FILE}: entries ${JSON.stringify(written)}`
|
|
385
332
|
const running = [...CONSENT_FLAGS.keys()].find((flag) => values[flag] !== undefined)
|
|
386
333
|
if (running !== undefined) throw new Error(`${what} sets ${running}, which runs ${CONSENT_FLAGS.get(running)}, and nothing of the project runs here`)
|
|
387
|
-
const
|
|
334
|
+
const language = prefixed(`${what}: `, () => entriesLanguage([entry], cwd, host))
|
|
335
|
+
const built = prefixed(`${what}: `, () => buildOptions(values, language))
|
|
388
336
|
for (const key of given) {
|
|
389
337
|
if (CONSENT_FLAGS.has(OPTION_FLAGS[key])) continue
|
|
390
338
|
if (comparable(key, options[key]) === comparable(key, built[key])) continue
|
|
391
339
|
const set = comparable(key, built[key]) === comparable(key, DEFAULT_OPTIONS[key]) ? `without --${OPTION_FLAGS[key]}` : `with ${optionText(key, built[key])}`
|
|
392
340
|
throw new Error(`${name}: ${optionText(key, options[key])} is given, but ${CONFIG_FILE}'s entries build ${JSON.stringify(written)} ${set}: an entry it names is built with its options there`)
|
|
393
341
|
}
|
|
394
|
-
|
|
395
|
-
if (builds.has(id)) builds.get(id).entries.push(entry)
|
|
396
|
-
else builds.set(id, { entries: [entry], ...built })
|
|
342
|
+
addToBuild(builds, entry, named, built, language)
|
|
397
343
|
}
|
|
398
344
|
const running = given.find((key) => CONSENT_FLAGS.has(OPTION_FLAGS[key]))
|
|
399
345
|
if (running !== undefined) throw new Error(`${name}: ${optionText(running, options[running])} is given, which runs ${CONSENT_FLAGS.get(OPTION_FLAGS[running])}, and nothing of the project runs here`)
|
|
400
|
-
return { entries, builds:
|
|
346
|
+
return { entries, builds: withPolicies(builds, `${name}: `) }
|
|
401
347
|
}
|