@exodus/stasis-core 1.0.0-beta.13 → 1.0.0-beta.15

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,35 @@ 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
+
119
+ // stasis.config.json's `generatePrisma`: whether `stasis github-bundle` and @exodus/stasis/vfs-bundle's
120
+ // builds, given no `generate`, write the Prisma Client of each project the entries reach whose schema
121
+ // asks for one, as `prisma generate` would (true, the default), or none (false).
122
+ export const parseGeneratePrisma = configFlag('generatePrisma')
123
+
91
124
  export const isBrotliQuality = (n) => Number.isInteger(n) && n >= 0 && n <= 11
92
125
 
93
126
  // `${n}` must round-trip to the input, so non-canonical forms ('5.0', '05', ' 5 ') throw.
@@ -99,6 +132,10 @@ export function parseBrotliQuality(name, value) {
99
132
  return n
100
133
  }
101
134
 
135
+ // The `valueFlags` (parseLeadingOptions') of parseArgs `options`: each string option's flag and its short alias.
136
+ export const valueFlagsOf = (options) => Object.entries(options).filter(([, { type }]) => type === 'string')
137
+ .flatMap(([name, { short }]) => (short === undefined ? [`--${name}`] : [`--${name}`, `-${short}`]))
138
+
102
139
  // Shifts the leading option tokens off `argv`, stopping at the first positional so `run`'s forwarded
103
140
  // child argv survives. `valueFlags` lists options whose separate-token value (`-o dir`) isn't the positional.
104
141
  export function parseLeadingOptions(argv, options, { valueFlags = [], onError } = {}) {
@@ -138,22 +175,12 @@ export function isNativeArtifact(name) {
138
175
  return NATIVE_ARTIFACT_EXTS.has(pathExt(name))
139
176
  }
140
177
 
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
178
  // A binary plist is a real build input, but its bytes can't ride the text-only 'xml' format
147
179
  // classifyFormat gives a `.plist`. Callers treat it as NOT code, so it needs `.plist` in `resources`.
148
180
  export function isBinaryPlist(name, content) {
149
181
  return pathExt(name) === 'plist' && Buffer.isBuffer(content) && !isUtf8(content)
150
182
  }
151
183
 
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
184
  // Non-JS code formats by extension. Every value must be in KNOWN_FORMATS.
158
185
  const CODE_EXT_FORMATS = new Map([
159
186
  ['sol', 'solidity'],
@@ -220,10 +247,6 @@ export function isExcludedNativeFile(name, { win32 = process.platform === 'win32
220
247
  return NATIVE_EXCLUDE_EXTS.has(ext) || (!win32 && ext === 'bat')
221
248
  }
222
249
 
223
- export function isExcludedNativeDir(name, { win32 = process.platform === 'win32' } = {}) {
224
- return !win32 && name === 'windows'
225
- }
226
-
227
250
  // An Apple prebuilt-slice dir (`ios-arm64`, `tvos-arm64_x86_64-simulator`): compiled output, not source,
228
251
  // at ANY depth. The PLATFORM segment is deliberately loose -- the required ARCH segment is the precision.
229
252
  const APPLE_ARCH = String.raw`(?:arm64e|arm64_32|arm64|armv7k|armv7s|armv7|x86_64|i386)`
@@ -254,7 +277,7 @@ export function stripTypeDeclaration(name) {
254
277
  // and the later capture conflicts.
255
278
  export function isStasisArtifactName(name) {
256
279
  const base = basename(name).toLowerCase()
257
- return base === 'stasis.lock.json' || (base.includes('stasis') && base.endsWith('.br'))
280
+ return isLockfileName(base) || (base.includes('stasis') && base.endsWith('.br'))
258
281
  }
259
282
 
260
283
  // Dirs no walk descends into: VCS/CI/IDE metadata, sample apps, test scaffolding -- none of it is the
@@ -270,13 +293,6 @@ export function isAutoExcludedDir(name) {
270
293
  return AUTO_EXCLUDED_DIRS.has(base) || isAppleSliceDir(base)
271
294
  }
272
295
 
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
296
  // Files a sweep drops: type declarations (types only, erased at runtime), the `.env` family (secrets),
281
297
  // stasis's own outputs (they'd attest themselves), and everything the native capture excludes as
282
298
  // non-build-input noise (docs/legal, editor/lint/CI config, `*.map`/`*.js.flow` sidecars, logs).
@@ -286,15 +302,19 @@ export function isAutoExcludedFile(name) {
286
302
  return isTypeDeclaration(name) || isDotEnvFile(name) || isStasisArtifactName(name) || isExcludedNativeFile(name)
287
303
  }
288
304
 
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 } = {}) {
305
+ // The format a file's name alone gives it, whatever its extension (pathExt) would: a `*.podspec.json`
306
+ // is `json`, a `*.cmake.in` `cmake`. -> it, or undefined
307
+ export function nameFormat(name) {
292
308
  const base = basename(name).toLowerCase()
293
- // Before the extension rules, so a `*.podspec.json` isn't shadowed.
294
309
  if (base === 'package.json' || base.endsWith('.podspec.json')) return 'json'
295
- // Compound suffix: pathExt only sees the trailing `in`.
296
310
  if (base.endsWith('.cmake.in')) return 'cmake'
297
- const byName = CODE_NAME_FORMATS.get(base)
311
+ return CODE_NAME_FORMATS.get(base)
312
+ }
313
+
314
+ // The single name->format classifier every capture path is a policy view of. Returns a concrete format
315
+ // when the name determines one; null for a JS-family file (.js/.ts/.jsx/.tsx); undefined if unrecognized.
316
+ export function classifyFormat(name, { content } = {}) {
317
+ const byName = nameFormat(name)
298
318
  if (byName !== undefined) return byName
299
319
  const ext = pathExt(name)
300
320
  if (JS_UNRESOLVED_EXTS.has(ext)) return null
@@ -304,41 +324,7 @@ export function classifyFormat(name, { content } = {}) {
304
324
  return undefined
305
325
  }
306
326
 
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))
327
+ export const extSetsEqual = (a, b) => a.size === b.size && a.isSubsetOf(b)
342
328
 
343
329
  export const EXECUTE_BITS = 0o111
344
330
 
@@ -351,9 +337,6 @@ export function observeExecutable(abs, host = diskHost) {
351
337
  return stats === null ? undefined : isExecutableMode(stats)
352
338
  }
353
339
 
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
340
  // Windows reports no POSIX execute bits, so a capture there records none and must NOT read "no bit" as
358
341
  // "the bit was removed" and strip what a POSIX capture attested.
359
342
  export const canObserveExecuteBits = ({ win32 = process.platform === 'win32' } = {}) => !win32