@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/src/util.js CHANGED
@@ -1,8 +1,8 @@
1
1
  import { isUtf8 } from 'node:buffer'
2
- import { basename, isAbsolute, join, normalize, relative, sep } from 'node:path'
2
+ import { basename, isAbsolute, join, normalize, posix, relative, sep } from 'node:path'
3
3
  import { parseArgs } from 'node:util'
4
4
 
5
- import { NODE_FORMATS } from './artifact-util.js'
5
+ import { NODE_FORMATS, isLockfileName } from './artifact-util.js'
6
6
  import { diskHost } from './host.js'
7
7
 
8
8
  // The Node-side half of the util split: byte/name classification for the capture walks, fs/execute-bit
@@ -17,6 +17,9 @@ const NODE_EXT_FORMATS = new Map([
17
17
  ])
18
18
  const JS_UNRESOLVED_EXTS = new Set(['js', 'ts', 'jsx', 'tsx'])
19
19
  export const CODE_EXTENSIONS = new Set([...NODE_EXT_FORMATS.keys(), ...JS_UNRESOLVED_EXTS])
20
+ // The format the name of the file `path` alone gives it, as Node loads it: .json, .mjs, .cjs, .mts and
21
+ // .cts are each one format whatever the file holds or its package declares. -> undefined for any other
22
+ export const nodeExtFormat = (path) => NODE_EXT_FORMATS.get(pathExt(path))
20
23
 
21
24
  export function pathExt(filePath) {
22
25
  const m = /\.([^./\\]+)$/.exec(filePath)
@@ -68,9 +71,13 @@ export function parseResourcesOption(label, resources) {
68
71
  return out
69
72
  }
70
73
 
74
+ const CONFIG_FILE_NAME = 'stasis.config.json'
75
+
71
76
  // stasis.config.json's `extra`: paths, directories and glob patterns relative to the project root,
72
77
  // taken as `stasis add` takes its arguments, and `!`-prefixed ones excluding what they match
73
- // (expandExtra). Each must stay within the root.
78
+ // (expandExtra). Each must stay within the root. A config that sets it names itself too, unless
79
+ // an entry names it already or `!stasis.config.json` leaves it out (another exclusion matching it
80
+ // still does, at expansion): the files it adds travel with the config that adds them.
74
81
  export function parseExtraOption(label, extra) {
75
82
  if (extra === undefined) return []
76
83
  if (!Array.isArray(extra)) {
@@ -85,9 +92,30 @@ export function parseExtraOption(label, extra) {
85
92
  throw new Error(`${label}: extra entry '${entry}' must be a path or pattern within the project root`)
86
93
  }
87
94
  }
88
- return [...extra]
95
+ // Compared as expandExtra matches: no `./`, no trailing `/`.
96
+ const names = (entry) => posix.normalize(entry.startsWith('!') ? entry.slice(1) : entry).replace(/\/+$/u, '') === CONFIG_FILE_NAME
97
+ return extra.some(names) ? [...extra] : [...extra, CONFIG_FILE_NAME]
98
+ }
99
+
100
+ // A stasis.config.json key that is true or false, true where it is left out.
101
+ const configFlag = (key) => (label, value) => {
102
+ if (value === undefined) return true
103
+ if (typeof value !== 'boolean') throw new TypeError(`${label}: ${key} must be true or false, got ${JSON.stringify(value)}`)
104
+ return value
89
105
  }
90
106
 
107
+ // stasis.config.json's `installDevDependencies`: whether the trees laid out from the lockfile (`stasis
108
+ // github-bundle`, @exodus/stasis/vfs-bundle) install devDependencies, as a plain install does (true,
109
+ // the default), or leave out what devDependencies alone reach, as `pnpm install --prod`, `npm ci
110
+ // --omit=dev` and `yarn install --production` do (false).
111
+ export const parseInstallDevDependencies = configFlag('installDevDependencies')
112
+
113
+ // stasis.config.json's `lazyDependencies`: whether the trees `stasis github-bundle` and
114
+ // @exodus/stasis/vfs-bundle's builds lay out from the lockfile are lazy where the package manager
115
+ // lays one out from the lockfile alone (pnpm, npm), each package fetched once the build reads it
116
+ // (true, the default), or have every package fetched before the build (false).
117
+ export const parseLazyDependencies = configFlag('lazyDependencies')
118
+
91
119
  export const isBrotliQuality = (n) => Number.isInteger(n) && n >= 0 && n <= 11
92
120
 
93
121
  // `${n}` must round-trip to the input, so non-canonical forms ('5.0', '05', ' 5 ') throw.
@@ -99,6 +127,10 @@ export function parseBrotliQuality(name, value) {
99
127
  return n
100
128
  }
101
129
 
130
+ // The `valueFlags` (parseLeadingOptions') of parseArgs `options`: each string option's flag and its short alias.
131
+ export const valueFlagsOf = (options) => Object.entries(options).filter(([, { type }]) => type === 'string')
132
+ .flatMap(([name, { short }]) => (short === undefined ? [`--${name}`] : [`--${name}`, `-${short}`]))
133
+
102
134
  // Shifts the leading option tokens off `argv`, stopping at the first positional so `run`'s forwarded
103
135
  // child argv survives. `valueFlags` lists options whose separate-token value (`-o dir`) isn't the positional.
104
136
  export function parseLeadingOptions(argv, options, { valueFlags = [], onError } = {}) {
@@ -138,22 +170,12 @@ export function isNativeArtifact(name) {
138
170
  return NATIVE_ARTIFACT_EXTS.has(pathExt(name))
139
171
  }
140
172
 
141
- // An extensionless non-UTF-8 file is a compiled tool (Hermes' `hermesc`), never source -- skipped.
142
- function isExtensionlessBinary(name, content) {
143
- return pathExt(name) === '' && Buffer.isBuffer(content) && !isUtf8(content)
144
- }
145
-
146
173
  // A binary plist is a real build input, but its bytes can't ride the text-only 'xml' format
147
174
  // classifyFormat gives a `.plist`. Callers treat it as NOT code, so it needs `.plist` in `resources`.
148
175
  export function isBinaryPlist(name, content) {
149
176
  return pathExt(name) === 'plist' && Buffer.isBuffer(content) && !isUtf8(content)
150
177
  }
151
178
 
152
- // Discovered by name: RN scatters podspecs in subdirs `react-native config` misses.
153
- export function isPodspec(name) {
154
- return name.endsWith('.podspec') || name.endsWith('.podspec.json')
155
- }
156
-
157
179
  // Non-JS code formats by extension. Every value must be in KNOWN_FORMATS.
158
180
  const CODE_EXT_FORMATS = new Map([
159
181
  ['sol', 'solidity'],
@@ -220,10 +242,6 @@ export function isExcludedNativeFile(name, { win32 = process.platform === 'win32
220
242
  return NATIVE_EXCLUDE_EXTS.has(ext) || (!win32 && ext === 'bat')
221
243
  }
222
244
 
223
- export function isExcludedNativeDir(name, { win32 = process.platform === 'win32' } = {}) {
224
- return !win32 && name === 'windows'
225
- }
226
-
227
245
  // An Apple prebuilt-slice dir (`ios-arm64`, `tvos-arm64_x86_64-simulator`): compiled output, not source,
228
246
  // at ANY depth. The PLATFORM segment is deliberately loose -- the required ARCH segment is the precision.
229
247
  const APPLE_ARCH = String.raw`(?:arm64e|arm64_32|arm64|armv7k|armv7s|armv7|x86_64|i386)`
@@ -254,7 +272,7 @@ export function stripTypeDeclaration(name) {
254
272
  // and the later capture conflicts.
255
273
  export function isStasisArtifactName(name) {
256
274
  const base = basename(name).toLowerCase()
257
- return base === 'stasis.lock.json' || (base.includes('stasis') && base.endsWith('.br'))
275
+ return isLockfileName(base) || (base.includes('stasis') && base.endsWith('.br'))
258
276
  }
259
277
 
260
278
  // Dirs no walk descends into: VCS/CI/IDE metadata, sample apps, test scaffolding -- none of it is the
@@ -270,13 +288,6 @@ export function isAutoExcludedDir(name) {
270
288
  return AUTO_EXCLUDED_DIRS.has(base) || isAppleSliceDir(base)
271
289
  }
272
290
 
273
- // Dirs the NATIVE walks (Metro plugin + the static --metro bundler) skip, on top of the shared set:
274
- // nested packages (attested separately) and regenerated build output -- none is a build input.
275
- const NATIVE_BUILD_OUTPUT_DIRS = new Set(['node_modules', 'build', '.gradle', '.cxx', 'Pods', 'DerivedData'])
276
- export function isSkippedNativeWalkDir(name) {
277
- return NATIVE_BUILD_OUTPUT_DIRS.has(name) || isAutoExcludedDir(name) || isNativeArtifact(name)
278
- }
279
-
280
291
  // Files a sweep drops: type declarations (types only, erased at runtime), the `.env` family (secrets),
281
292
  // stasis's own outputs (they'd attest themselves), and everything the native capture excludes as
282
293
  // non-build-input noise (docs/legal, editor/lint/CI config, `*.map`/`*.js.flow` sidecars, logs).
@@ -286,15 +297,19 @@ export function isAutoExcludedFile(name) {
286
297
  return isTypeDeclaration(name) || isDotEnvFile(name) || isStasisArtifactName(name) || isExcludedNativeFile(name)
287
298
  }
288
299
 
289
- // The single name->format classifier every capture path is a policy view of. Returns a concrete format
290
- // when the name determines one; null for a JS-family file (.js/.ts/.jsx/.tsx); undefined if unrecognized.
291
- export function classifyFormat(name, { content } = {}) {
300
+ // The format a file's name alone gives it, whatever its extension (pathExt) would: a `*.podspec.json`
301
+ // is `json`, a `*.cmake.in` `cmake`. -> it, or undefined
302
+ export function nameFormat(name) {
292
303
  const base = basename(name).toLowerCase()
293
- // Before the extension rules, so a `*.podspec.json` isn't shadowed.
294
304
  if (base === 'package.json' || base.endsWith('.podspec.json')) return 'json'
295
- // Compound suffix: pathExt only sees the trailing `in`.
296
305
  if (base.endsWith('.cmake.in')) return 'cmake'
297
- const byName = CODE_NAME_FORMATS.get(base)
306
+ return CODE_NAME_FORMATS.get(base)
307
+ }
308
+
309
+ // The single name->format classifier every capture path is a policy view of. Returns a concrete format
310
+ // when the name determines one; null for a JS-family file (.js/.ts/.jsx/.tsx); undefined if unrecognized.
311
+ export function classifyFormat(name, { content } = {}) {
312
+ const byName = nameFormat(name)
298
313
  if (byName !== undefined) return byName
299
314
  const ext = pathExt(name)
300
315
  if (JS_UNRESOLVED_EXTS.has(ext)) return null
@@ -304,41 +319,7 @@ export function classifyFormat(name, { content } = {}) {
304
319
  return undefined
305
320
  }
306
321
 
307
- // The Metro native capture's policy view. Returns { action, format }: 'code' (a native build input),
308
- // 'skip' (excluded noise, or a JS-family file matched BY EXTENSION -- Metro owns those), else 'resource'.
309
- export function classifyNativeCapture(name, { win32 = process.platform === 'win32', content } = {}) {
310
- // `.env` files carry secrets: an automated capture must never sweep them in.
311
- if (isExcludedNativeFile(name, { win32 }) || isDotEnvFile(name)) return { action: 'skip' }
312
- const base = basename(name).toLowerCase()
313
- // Name-matched code is always a native build input, even when its tag is a Node format the JS
314
- // graph would otherwise own.
315
- if (base === 'package.json' || base.endsWith('.podspec.json')) return { action: 'code', format: 'json' }
316
- const byName = CODE_NAME_FORMATS.get(base)
317
- if (byName !== undefined) return { action: 'code', format: byName }
318
- if (CODE_EXTENSIONS.has(pathExt(name))) return { action: 'skip' }
319
- const format = classifyFormat(name, { content })
320
- return format === undefined ? { action: 'resource' } : { action: 'code', format }
321
- }
322
-
323
- // The byte-level half of the native classification, refining what classifyNativeCapture derived from the
324
- // NAME. Both rules only ever demote. A 'resource' carries no format: storage derives base64 from bytes.
325
- export function refineNativeCapture(classified, name, content, resources = new Set()) {
326
- if (isExtensionlessBinary(name, content)) return { action: 'skip' }
327
- if (isBinaryPlist(name, content)) return { action: resources.has('plist') ? 'resource' : 'skip' }
328
- return classified
329
- }
330
-
331
- // Files `pod install` reads while loading podspecs (Ruby helpers, package.json) -- NOT the native
332
- // source those podspecs compile.
333
- export function isNativeManifest(name) {
334
- return isPodspec(name) || pathExt(name) === 'rb' || name === 'package.json'
335
- }
336
-
337
- // RN core build scripts the native walk would defer to Metro as `.js`, but Metro never sees them (RN's
338
- // podspecs invoke them at pod-install). Project-relative, so react-native's `exports` can't hide them.
339
- export const RN_CORE_INCLUDE_FILES = ['sdks/hermes-engine/utils/replace_hermes_version.js']
340
-
341
- export const extSetsEqual = (a, b) => a.size === b.size && [...a].every((ext) => b.has(ext))
322
+ export const extSetsEqual = (a, b) => a.size === b.size && a.isSubsetOf(b)
342
323
 
343
324
  export const EXECUTE_BITS = 0o111
344
325
 
@@ -351,9 +332,6 @@ export function observeExecutable(abs, host = diskHost) {
351
332
  return stats === null ? undefined : isExecutableMode(stats)
352
333
  }
353
334
 
354
- // Boolean view for callers with nothing to refute (recording a fresh set from scratch).
355
- export const isExecutableFile = (abs, host) => observeExecutable(abs, host) === true
356
-
357
335
  // Windows reports no POSIX execute bits, so a capture there records none and must NOT read "no bit" as
358
336
  // "the bit was removed" and strip what a POSIX capture attested.
359
337
  export const canObserveExecuteBits = ({ win32 = process.platform === 'win32' } = {}) => !win32