@exodus/stasis 1.0.0-beta.9 → 1.0.0-rc.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 +6 -2
- package/bin/stasis.js +222 -334
- package/package.json +38 -14
- package/src/audit-corrections.js +11 -42
- package/src/audit.js +256 -212
- package/src/babel.js +119 -82
- package/src/bundle-from-lockfile.js +9 -21
- package/src/bundle-reader.js +24 -8
- package/src/bundle.js +0 -3
- package/src/cmd/build.js +211 -117
- package/src/cmd/bundle-options.js +347 -0
- package/src/cmd/bundle.js +1664 -1001
- package/src/cmd/diff.js +1 -4
- package/src/cmd/github-bundle.js +48 -21
- package/src/cmd/output.js +24 -12
- package/src/cmd/sbom.js +6 -9
- package/src/cmd/tokens.js +60 -0
- package/src/define.js +218 -0
- package/src/diff.js +73 -129
- package/src/esbuild.js +0 -4
- package/src/flow.js +27 -0
- 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 +43 -120
- package/src/loaders/cargo.js +301 -808
- package/src/loaders/foundry.js +112 -290
- package/src/loaders/paths.js +15 -26
- package/src/loaders/php.js +211 -492
- package/src/loaders/rust.js +518 -1461
- package/src/loaders/solidity-ownership.js +51 -142
- package/src/loaders/solidity.js +42 -188
- package/src/loaders/toml.js +7 -17
- package/src/lockfile.js +0 -3
- package/src/metro-assets.js +41 -0
- package/src/metro-resolver.js +58 -133
- package/src/metro-transformer.js +0 -6
- package/src/metro.js +0 -4
- package/src/mock.js +32 -80
- package/src/parse.js +8 -7
- package/src/prune.js +0 -3
- package/src/resolve-fields.js +152 -249
- package/src/resolve-node.js +29 -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 +70 -97
- package/src/scan.js +240 -282
- package/src/sixel.js +310 -0
- package/src/table.js +95 -0
- 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 +1315 -0
- package/src/vfs-bundle/entries.js +50 -46
- package/src/vfs-bundle/github.js +114 -94
- package/src/vfs-bundle/glob.js +379 -0
- package/src/vfs-bundle/lazy.js +99 -0
- package/src/vfs-bundle/prisma.js +235 -105
- package/src/vfs-bundle/soldeer.js +28 -49
- package/src/vfs-bundle/tree.js +418 -277
- package/src/vfs-bundle/yarnclean.js +79 -0
- package/src/vfs-bundle.js +24 -55
- package/src/webpack.js +0 -4
- package/src/why.js +79 -231
- package/src/installed-commits.js +0 -182
|
@@ -0,0 +1,347 @@
|
|
|
1
|
+
import { isAbsolute, relative, resolve } from 'node:path'
|
|
2
|
+
|
|
3
|
+
import { diskHost } from '@exodus/stasis-core/host'
|
|
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'
|
|
6
|
+
import { parseFeatureList } from '../loaders/cargo.js'
|
|
7
|
+
import { parseDefineFlags } from '../define.js'
|
|
8
|
+
import { directoryEntryError, isSolidityEntry, readConfigFile } from './bundle.js'
|
|
9
|
+
import { lockfileOnlyError } from './output.js'
|
|
10
|
+
|
|
11
|
+
export const BUNDLE_FLAGS = {
|
|
12
|
+
mapping: { type: 'string' },
|
|
13
|
+
manifests: { type: 'boolean' },
|
|
14
|
+
output: { type: 'string', short: 'o' },
|
|
15
|
+
scope: { type: 'string' },
|
|
16
|
+
lockfile: { type: 'string' },
|
|
17
|
+
'lockfile-only': { type: 'boolean' },
|
|
18
|
+
conditions: { type: 'string' },
|
|
19
|
+
mainFields: { type: 'string' },
|
|
20
|
+
metro: { type: 'boolean' },
|
|
21
|
+
'metro-resolver': { type: 'boolean' },
|
|
22
|
+
babel: { type: 'boolean' },
|
|
23
|
+
'babel-aliases': { type: 'string' },
|
|
24
|
+
platforms: { type: 'string', multiple: true },
|
|
25
|
+
jsx: { type: 'boolean' },
|
|
26
|
+
flow: { type: 'boolean' },
|
|
27
|
+
typescript: { type: 'boolean' },
|
|
28
|
+
tsconfig: { type: 'string' },
|
|
29
|
+
resources: { type: 'string' },
|
|
30
|
+
'package-json': { type: 'boolean' },
|
|
31
|
+
define: { type: 'string', multiple: true },
|
|
32
|
+
cargo: { type: 'boolean' },
|
|
33
|
+
'cargo-features': { type: 'string', multiple: true },
|
|
34
|
+
'cargo-no-default-features': { type: 'boolean' },
|
|
35
|
+
'cargo-all-features': { type: 'boolean' },
|
|
36
|
+
'cargo-target': { type: 'string' },
|
|
37
|
+
'cargo-manifests': { type: 'boolean' },
|
|
38
|
+
'brotli-quality': { type: 'string' },
|
|
39
|
+
add: { type: 'boolean' },
|
|
40
|
+
deptree: { type: 'boolean' },
|
|
41
|
+
}
|
|
42
|
+
|
|
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'])
|
|
45
|
+
const LIST_FLAGS = new Set(['conditions', 'mainFields', 'platforms', 'resources', 'cargo-features'])
|
|
46
|
+
// Given once per value, which a comma doesn't separate (a define's JSON value may hold one).
|
|
47
|
+
const REPEATED_FLAGS = new Set(['define'])
|
|
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.
|
|
50
|
+
const CONSENT_FLAGS = new Map([
|
|
51
|
+
['babel', "the project's Babel config"],
|
|
52
|
+
['metro-resolver', "the project's metro-resolver"],
|
|
53
|
+
['cargo', "cargo, and what the project's .cargo/config.toml names"],
|
|
54
|
+
['cargo-target', 'rustc'],
|
|
55
|
+
])
|
|
56
|
+
const flagText = (name, value) => (value === true ? `--${name}` : `--${name}=${value}`)
|
|
57
|
+
|
|
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
|
+
function entryKey(cwd, entry) {
|
|
66
|
+
const rel = relative(resolve(cwd), resolve(cwd, entry))
|
|
67
|
+
return relativeEscapes(rel) ? null : toPosix(rel) || '.'
|
|
68
|
+
}
|
|
69
|
+
|
|
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.
|
|
92
|
+
function entriesLanguage(entries, cwd, host = diskHost) {
|
|
93
|
+
// Beside JS entries, a directory one is of the Solidity ones.
|
|
94
|
+
const dirError = directoryEntryError(entries.filter((f) => !isJsEntry(f)), cwd, host)
|
|
95
|
+
if (dirError !== null) throw new Error(dirError)
|
|
96
|
+
const lang = {
|
|
97
|
+
allSol: entries.every((f) => isSolidityEntry(f, cwd, host)),
|
|
98
|
+
allPhp: entries.every((f) => f.endsWith('.php')),
|
|
99
|
+
allJs: entries.every(isJsEntry),
|
|
100
|
+
allBash: entries.every((f) => /\.(?:sh|bash)$/u.test(f)),
|
|
101
|
+
allRust: entries.every((f) => f.endsWith('.rs')),
|
|
102
|
+
allTeal: entries.every((f) => f.endsWith('.teal')),
|
|
103
|
+
}
|
|
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')
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function buildOptions(values, { allSol, allJs, allRust }) {
|
|
110
|
+
if (values.mapping && !allSol) throw new Error('--mapping is only valid for .sol bundles')
|
|
111
|
+
const manifests = Boolean(values.manifests)
|
|
112
|
+
if (manifests && !allSol) throw new Error('--manifests is only valid for .sol bundles')
|
|
113
|
+
const cargo = Boolean(values.cargo)
|
|
114
|
+
if (cargo && !allRust) throw new Error('--cargo is only valid for Rust bundles')
|
|
115
|
+
const cargoFeatures = parseFeatureList(values['cargo-features'] ?? [])
|
|
116
|
+
if (values['cargo-features'] !== undefined && cargoFeatures.length === 0) {
|
|
117
|
+
throw new Error('--cargo-features must list at least one feature (e.g. --cargo-features=serde,app/tls)')
|
|
118
|
+
}
|
|
119
|
+
const cargoNoDefaultFeatures = Boolean(values['cargo-no-default-features'])
|
|
120
|
+
const cargoAllFeatures = Boolean(values['cargo-all-features'])
|
|
121
|
+
const cargoTarget = values['cargo-target'] ?? null
|
|
122
|
+
if (cargoTarget !== null && !/^[\w.-]+$/u.test(cargoTarget)) {
|
|
123
|
+
throw new Error('--cargo-target must be a target triple or "host" (e.g. --cargo-target=aarch64-apple-darwin)')
|
|
124
|
+
}
|
|
125
|
+
const cargoManifests = Boolean(values['cargo-manifests'])
|
|
126
|
+
if (!allRust) {
|
|
127
|
+
const given = { 'cargo-features': cargoFeatures.length > 0, 'cargo-no-default-features': cargoNoDefaultFeatures, 'cargo-all-features': cargoAllFeatures, 'cargo-target': cargoTarget !== null, 'cargo-manifests': cargoManifests }
|
|
128
|
+
for (const [flag, on] of Object.entries(given)) if (on) throw new Error(`--${flag} is only valid for Rust bundles`)
|
|
129
|
+
}
|
|
130
|
+
if (values.scope && !allJs) throw new Error('--scope is only valid for JS bundles')
|
|
131
|
+
if (values.scope && !SCOPES.has(values.scope)) throw new Error('--scope must be node_modules or full')
|
|
132
|
+
if (values.scope !== undefined && values.scope !== 'full' && (values.mainFields !== undefined || values.metro)) {
|
|
133
|
+
throw new Error(`--scope=${values.scope} is not supported with --mainFields or --metro (they always build a full-scope bundle)`)
|
|
134
|
+
}
|
|
135
|
+
if (values.conditions !== undefined && !allJs) throw new Error('--conditions is only valid for JS bundles')
|
|
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)
|
|
139
|
+
if (values.flow && !allJs) throw new Error('--flow is only valid for JS bundles')
|
|
140
|
+
const flow = Boolean(values.flow) || metro
|
|
141
|
+
if (values.typescript && !allJs) throw new Error('--typescript is only valid for JS bundles')
|
|
142
|
+
const typescript = Boolean(values.typescript)
|
|
143
|
+
if (values.tsconfig !== undefined && !typescript) throw new Error('--tsconfig is only valid with --typescript')
|
|
144
|
+
if (values.mainFields !== undefined && !allJs) throw new Error('--mainFields is only valid for JS bundles')
|
|
145
|
+
const mainFields = listFlag(values.mainFields, '--mainFields must list at least one field (e.g. --mainFields=react-native,browser,main)')
|
|
146
|
+
const metroResolver = Boolean(values['metro-resolver'])
|
|
147
|
+
const platforms = [...new Set((values.platforms ?? []).flatMap((p) => p.split(',')).map((s) => s.trim()).filter(Boolean))]
|
|
148
|
+
// A platform name becomes an edge key, so no '/' (parsers reject it) or '*' (a reserved placeholder).
|
|
149
|
+
for (const p of platforms) {
|
|
150
|
+
if (p === '*' || p.includes('/')) throw new Error(`invalid --platforms value '${p}' (a platform name can't contain '/' or be '*')`)
|
|
151
|
+
}
|
|
152
|
+
if ((metro || platforms.length > 0) && !allJs) throw new Error('--metro is only valid for JS bundles')
|
|
153
|
+
const jsx = Boolean(values.jsx) || metro
|
|
154
|
+
if (jsx && !allJs) throw new Error('--jsx is only valid for JS bundles')
|
|
155
|
+
const packageJSON = Boolean(values['package-json'])
|
|
156
|
+
if (packageJSON && !allJs) throw new Error('--package-json is only valid for JS bundles')
|
|
157
|
+
if (values.resources !== undefined && !allJs) throw new Error('--resources is only valid for JS bundles')
|
|
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
|
|
160
|
+
if (metro) {
|
|
161
|
+
if (conditions.length > 0) throw new Error("--conditions can't be combined with --metro (it sets its own conditions)")
|
|
162
|
+
if (mainFields !== undefined) throw new Error("--mainFields can't be combined with --metro (it sets its own mainFields)")
|
|
163
|
+
if (platforms.length === 0) throw new Error('--metro requires --platforms (e.g. --platforms=ios,android)')
|
|
164
|
+
} else if (platforms.length > 0) {
|
|
165
|
+
throw new Error('--platforms is only valid with --metro')
|
|
166
|
+
}
|
|
167
|
+
if (metroResolver && !metro) throw new Error('--metro-resolver is only valid with --metro')
|
|
168
|
+
const babel = Boolean(values.babel)
|
|
169
|
+
if (babel && !metro) throw new Error('--babel is only valid with --metro')
|
|
170
|
+
if (values['babel-aliases'] !== undefined && !metro) throw new Error('--babel-aliases is only valid with --metro')
|
|
171
|
+
if (values['babel-aliases'] !== undefined && babel) throw new Error("--babel-aliases can't be combined with --babel (both set the import aliases)")
|
|
172
|
+
if (typescript && metroResolver) throw new Error("--typescript is not supported with --metro-resolver (the project's metro-resolver doesn't substitute .js -> .ts)")
|
|
173
|
+
if (values.define !== undefined && !allJs) throw new Error('--define is only valid for JS bundles')
|
|
174
|
+
const define = values.define === undefined ? undefined : parseDefineFlags(null, values.define)
|
|
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 }
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
// The CONSENT_FLAGS that run a program of the project's from disk, where --deptree installs nothing.
|
|
179
|
+
const DISK_RUN_FLAGS = ['babel', 'metro-resolver']
|
|
180
|
+
|
|
181
|
+
function outputOptions(values, { allJs, allSol, jsAndSol }) {
|
|
182
|
+
if (values.lockfile && !allJs) throw new Error('--lockfile is only valid for JS bundles')
|
|
183
|
+
const deptree = Boolean(values.deptree)
|
|
184
|
+
if (deptree && !allJs && !allSol && !jsAndSol) throw new Error('--deptree is only valid for JS and .sol bundles')
|
|
185
|
+
for (const flag of deptree ? DISK_RUN_FLAGS : []) {
|
|
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)`)
|
|
187
|
+
}
|
|
188
|
+
const brotliQuality = values['brotli-quality'] === undefined ? undefined : parseBrotliQuality('--brotli-quality', values['brotli-quality'])
|
|
189
|
+
const add = Boolean(values.add)
|
|
190
|
+
if (add && values.output === '-') throw new Error('--add cannot be combined with --output=-')
|
|
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 }
|
|
195
|
+
}
|
|
196
|
+
|
|
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.
|
|
199
|
+
function entryFlags(what, options) {
|
|
200
|
+
if (!isPlainObject(options)) throw new Error(`${what} must be an object of stasis bundle options`)
|
|
201
|
+
const values = {}
|
|
202
|
+
for (const [flag, value] of Object.entries(options)) {
|
|
203
|
+
if (POLICY_KEYS.includes(flag)) continue
|
|
204
|
+
if (OUTPUT_FLAGS.has(flag)) throw new Error(`${what}: ${flag} is the bundle's option, not an entry's: pass --${flag}`)
|
|
205
|
+
if (!Object.hasOwn(BUNDLE_FLAGS, flag)) throw new Error(`${what}: unknown option ${JSON.stringify(flag)}`)
|
|
206
|
+
const { type, multiple } = BUNDLE_FLAGS[flag]
|
|
207
|
+
if (type === 'boolean') {
|
|
208
|
+
if (typeof value !== 'boolean') throw new Error(`${what}: ${flag} must be true or false`)
|
|
209
|
+
if (value) values[flag] = true
|
|
210
|
+
continue
|
|
211
|
+
}
|
|
212
|
+
const takesArray = LIST_FLAGS.has(flag) || REPEATED_FLAGS.has(flag)
|
|
213
|
+
const list = takesArray && Array.isArray(value) && value.every((v) => typeof v === 'string')
|
|
214
|
+
if (typeof value !== 'string' && !list) throw new Error(`${what}: ${flag} must be a string${takesArray ? ' or an array of strings' : ''}`)
|
|
215
|
+
if (REPEATED_FLAGS.has(flag)) {
|
|
216
|
+
values[flag] = [value].flat()
|
|
217
|
+
continue
|
|
218
|
+
}
|
|
219
|
+
const text = list ? value.join(',') : value
|
|
220
|
+
values[flag] = multiple ? [text] : text
|
|
221
|
+
}
|
|
222
|
+
return values
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
// The `entries` of the stasis.config.json in `cwd`, by entryKey; an entry's own Policy is held beside the config's.
|
|
226
|
+
export function readConfigEntries(cwd = process.cwd(), host = diskHost) {
|
|
227
|
+
const entries = readConfigFile(undefined, cwd, host)?.entries
|
|
228
|
+
if (entries === undefined) return null
|
|
229
|
+
if (!isPlainObject(entries)) throw new Error(`${CONFIG_FILE}: entries must be an object of each entry's stasis bundle options`)
|
|
230
|
+
const named = new Map()
|
|
231
|
+
for (const [entry, options] of Object.entries(entries)) {
|
|
232
|
+
const what = `${CONFIG_FILE}: entries ${JSON.stringify(entry)}`
|
|
233
|
+
const key = isAbsolute(entry) ? null : entryKey(cwd, entry)
|
|
234
|
+
if (key === null) throw new Error(`${what}: an entry must be a path relative to the directory of ${CONFIG_FILE}, within it`)
|
|
235
|
+
if (named.has(key)) throw new Error(`${what} names the same entry as ${JSON.stringify(named.get(key).entry)}`)
|
|
236
|
+
const values = entryFlags(what, options)
|
|
237
|
+
const policy = parsePolicy(what, options)
|
|
238
|
+
named.set(key, { entry, values, ...(policy !== null && { policy }) })
|
|
239
|
+
}
|
|
240
|
+
return named
|
|
241
|
+
}
|
|
242
|
+
|
|
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.
|
|
245
|
+
export function bundlePlan({ cwd = process.cwd(), entries, values, config = null }) {
|
|
246
|
+
const lang = entriesLanguage(entries, cwd)
|
|
247
|
+
const output = outputOptions(values, lang)
|
|
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
|
+
}) }
|
|
258
|
+
}
|
|
259
|
+
const flag = Object.keys(values).find((name) => !OUTPUT_FLAGS.has(name) && !CONSENT_FLAGS.has(name))
|
|
260
|
+
if (flag !== undefined) throw new Error(`--${flag} can't be given for ${first}, whose options ${CONFIG_FILE}'s entries set: set it there`)
|
|
261
|
+
const named = keys.map((key) => config.get(key))
|
|
262
|
+
for (const consent of CONSENT_FLAGS.keys()) {
|
|
263
|
+
const given = values[consent]
|
|
264
|
+
if (given !== undefined && !named.some(({ values: flags }) => flags[consent] === given)) {
|
|
265
|
+
throw new Error(`${flagText(consent, given)} is given, but ${CONFIG_FILE}'s entries set ${flagText(consent, given).slice(2)} for none of ${entries.join(', ')}`)
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
const builds = new Map()
|
|
269
|
+
for (const [i, entry] of entries.entries()) {
|
|
270
|
+
const { entry: name, values: flags } = named[i]
|
|
271
|
+
const unconsented = [...CONSENT_FLAGS.keys()].find((consent) => flags[consent] !== undefined && values[consent] !== flags[consent])
|
|
272
|
+
if (unconsented !== undefined) {
|
|
273
|
+
const consent = flagText(unconsented, flags[unconsented])
|
|
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`)
|
|
275
|
+
}
|
|
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)
|
|
279
|
+
}
|
|
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(', ')}`) }))
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
const OPTION_FLAGS = {
|
|
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',
|
|
298
|
+
}
|
|
299
|
+
// Those whose values are a set, in whatever order given.
|
|
300
|
+
const SET_OPTIONS = new Set(['conditions', 'platforms', 'resources', 'cargoFeatures'])
|
|
301
|
+
const DEFAULT_OPTIONS = buildOptions({}, {})
|
|
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.
|
|
304
|
+
const comparable = (key, value) => {
|
|
305
|
+
if (value === undefined || value === null || (key === 'define' && isPlainObject(value) && Object.keys(value).length === 0)) return 'null'
|
|
306
|
+
if (key === 'resources' && Array.isArray(value)) value = value.map((v) => (typeof v === 'string' ? v.toLowerCase().replace(/^\./u, '') : v))
|
|
307
|
+
if (SET_OPTIONS.has(key) && Array.isArray(value)) return JSON.stringify([...new Set(value)].toSorted())
|
|
308
|
+
if (key === 'define' && isPlainObject(value)) return JSON.stringify(Object.entries(value).toSorted(([a], [b]) => (a < b ? -1 : 1)))
|
|
309
|
+
return JSON.stringify(value)
|
|
310
|
+
}
|
|
311
|
+
const optionText = (key, value) => (key === 'define'
|
|
312
|
+
? Object.entries(value).map(([name, v]) => `--define=${name}=${JSON.stringify(v)}`).join(' ')
|
|
313
|
+
: flagText(OPTION_FLAGS[key], Array.isArray(value) ? value.join(',') : value))
|
|
314
|
+
|
|
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.
|
|
317
|
+
export function configuredBuilds(name, { cwd, host = diskHost, entries, options = {} }) {
|
|
318
|
+
const config = prefixed(`${name}: `, () => readConfigEntries(cwd, host))
|
|
319
|
+
if (config === null || (entries !== undefined && !Array.isArray(entries))) return null
|
|
320
|
+
if (entries === undefined) {
|
|
321
|
+
entries = [...config.keys()]
|
|
322
|
+
if (entries.length === 0) return { entries, builds: [] }
|
|
323
|
+
}
|
|
324
|
+
const { keys, first } = configuredKeys(cwd, entries, config, `${name}: `)
|
|
325
|
+
if (first === undefined) return null
|
|
326
|
+
const given = Object.keys(OPTION_FLAGS).filter((key) => options[key] !== undefined && comparable(key, options[key]) !== comparable(key, DEFAULT_OPTIONS[key]))
|
|
327
|
+
const builds = new Map()
|
|
328
|
+
for (const [i, entry] of entries.entries()) {
|
|
329
|
+
const named = config.get(keys[i])
|
|
330
|
+
const { entry: written, values } = named
|
|
331
|
+
const what = `${name}: ${CONFIG_FILE}: entries ${JSON.stringify(written)}`
|
|
332
|
+
const running = [...CONSENT_FLAGS.keys()].find((flag) => values[flag] !== undefined)
|
|
333
|
+
if (running !== undefined) throw new Error(`${what} sets ${running}, which runs ${CONSENT_FLAGS.get(running)}, and nothing of the project runs here`)
|
|
334
|
+
const language = prefixed(`${what}: `, () => entriesLanguage([entry], cwd, host))
|
|
335
|
+
const built = prefixed(`${what}: `, () => buildOptions(values, language))
|
|
336
|
+
for (const key of given) {
|
|
337
|
+
if (CONSENT_FLAGS.has(OPTION_FLAGS[key])) continue
|
|
338
|
+
if (comparable(key, options[key]) === comparable(key, built[key])) continue
|
|
339
|
+
const set = comparable(key, built[key]) === comparable(key, DEFAULT_OPTIONS[key]) ? `without --${OPTION_FLAGS[key]}` : `with ${optionText(key, built[key])}`
|
|
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`)
|
|
341
|
+
}
|
|
342
|
+
addToBuild(builds, entry, named, built, language)
|
|
343
|
+
}
|
|
344
|
+
const running = given.find((key) => CONSENT_FLAGS.has(OPTION_FLAGS[key]))
|
|
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`)
|
|
346
|
+
return { entries, builds: withPolicies(builds, `${name}: `) }
|
|
347
|
+
}
|