@exodus/stasis 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.
@@ -0,0 +1,100 @@
1
+ import { posix } from 'node:path'
2
+
3
+ import { loadMinimatch } from './glob.js'
4
+
5
+ // Yarn 1's .yarnclean, which `yarn autoclean` (and `yarn install`, where the lockfile's directory
6
+ // holds one) cleans node_modules by: each line a pattern of paths to remove, `!` one of paths to
7
+ // keep, `#` a comment. Here every .yarnclean in the directory installed or above it cleans the tree
8
+ // a package manager lays out, whichever it is, as Yarn 1.22.22's autoclean cleans its own
9
+ // (sortFilter and matchesFilter, src/util/filter.js), on minimatch with the options Yarn gives
10
+ // micromatch. A path is matched from the node_modules directory holding its package, as Yarn's
11
+ // hoisted layout has it (pnpm's .pnpm/x@1/node_modules/x/README.md as x/README.md), and none of the
12
+ // package manager's own (.bin, .pnpm, .modules.yaml) is.
13
+
14
+ export const YARNCLEAN = '.yarnclean'
15
+
16
+ // The lines ignoreLinesToRegex takes for patterns: no comment, blank line or bare `!`.
17
+ const patternLines = (lines) => lines.filter((line) => !(line === '' || line === '!' || line[0] === '#' || /^\s+$/u.test(line)))
18
+
19
+ // ignoreLinesToRegex's filter of a line, with the expressions matchesFilter matches by: `regex`, of
20
+ // its trimmed spelling as micromatch's makeRe makes one for Yarn, dotfiles and all, case aside, and
21
+ // `plain`, of the pattern as micromatch's isMatch takes it, by default; never matching where the
22
+ // pattern makes none.
23
+ const NONE = /(?!)/u
24
+ function filterOf(line, { makeRe }) {
25
+ const negation = line[0] === '!'
26
+ const pattern = (negation ? line.slice(1) : line).replace(/\/$/u, '')
27
+ return { negation, regex: makeRe(pattern.trim(), { dot: true, nocase: true }) || NONE, plain: makeRe(pattern) || NONE }
28
+ }
29
+
30
+ // matchesFilter: a path from the node_modules directory holding its package, by its whole spelling,
31
+ // from `/`, or by its name.
32
+ const matches = ({ regex, plain }, loc) => regex.test(loc) || regex.test(`/${loc}`) || regex.test(posix.basename(loc)) || plain.test(loc)
33
+
34
+ // The path of `path` from the innermost node_modules directory below `dir` holding it, or `dir`;
35
+ // null for the package manager's own: a dot-named entry of one, or that directory itself.
36
+ function packagePath(dir, path) {
37
+ const segments = path.slice(dir.length + 1).split('/')
38
+ const tail = segments.slice(segments.lastIndexOf('node_modules') + 1)
39
+ return tail.length === 0 || tail[0].startsWith('.') ? null : tail.join('/')
40
+ }
41
+
42
+ // Of every .yarnclean in `cwd` and the directories above it, read through `host`: a function
43
+ // cleaning the node_modules directory at a path of a Vfs as sortFilter cleans one, or null where
44
+ // none holds a pattern; its `keeps`, whether a `!` pattern keeps paths, which turns what is cleaned
45
+ // of one package on what the others hold. minimatch is loaded here, where one does: before anything
46
+ // is installed.
47
+ export function yarnclean(host, cwd) {
48
+ const lines = []
49
+ for (let dir = cwd; ; dir = posix.dirname(dir)) {
50
+ const file = posix.join(dir, YARNCLEAN)
51
+ if (host.stat(file)?.isFile()) lines.push(...patternLines(host.readFile(file).toString('utf8').split('\n')))
52
+ if (dir === '/') break
53
+ }
54
+ if (lines.length === 0) return null
55
+ const minimatch = loadMinimatch(`A ${YARNCLEAN}, which cleans the node_modules a lockfile lays out,`)
56
+ const filters = lines.map((line) => filterOf(line, minimatch))
57
+ const negations = filters.filter((filter) => filter.negation)
58
+ const positives = filters.filter((filter) => !filter.negation)
59
+ return Object.assign((vfs, dir) => {
60
+ if (!vfs.isDirectory(dir)) return
61
+ const entries = [...vfs.walk(dir)].slice(1).map(({ path, type }) => ({ path, type, loc: packagePath(dir, path) })).filter(({ loc }) => loc !== null)
62
+ // A path a `!` filter matches is kept, and so is every directory holding one.
63
+ const kept = new Set()
64
+ const holdingKept = new Set()
65
+ const keep = (path) => {
66
+ kept.add(path)
67
+ for (let up = posix.dirname(path); up !== dir; up = posix.dirname(up)) holdingKept.add(up)
68
+ }
69
+ for (const { path, loc } of negations.length === 0 ? [] : entries) if (negations.some((filter) => matches(filter, loc))) keep(path)
70
+ // So is a link to a path kept, or to a directory holding one, as that is, wherever it is: pnpm
71
+ // links each package (under its scope's directory) into its store, where Yarn's hoisted layout
72
+ // holds the package itself.
73
+ const leadsToKept = (path) => {
74
+ let real
75
+ try {
76
+ real = vfs.realpath(path)
77
+ } catch {
78
+ return false // a link to nothing, or a loop
79
+ }
80
+ return kept.has(real) || holdingKept.has(real)
81
+ }
82
+ for (const { path, type } of kept.size === 0 ? [] : entries) if (type === 'symlink' && leadsToKept(path)) keep(path)
83
+ // Any other a filter matches goes, or below a directory one matches, everything below it with
84
+ // it, but a directory holding a kept path: it stays with that, and what else is below it goes.
85
+ const matched = new Set()
86
+ const matchedAbove = (path) => {
87
+ for (let up = posix.dirname(path); up !== dir; up = posix.dirname(up)) if (matched.has(up)) return true
88
+ return false
89
+ }
90
+ let gone = null
91
+ for (const { path, loc } of entries) {
92
+ if ((gone !== null && path.startsWith(gone)) || kept.has(path)) continue
93
+ if (positives.some((filter) => matches(filter, loc))) matched.add(path)
94
+ else if (!matchedAbove(path)) continue
95
+ if (holdingKept.has(path)) continue
96
+ vfs.rm(path, { recursive: true })
97
+ gone = `${path}/`
98
+ }
99
+ }, { keeps: negations.length > 0 })
100
+ }
package/src/vfs-bundle.js CHANGED
@@ -1,7 +1,8 @@
1
1
  import { posix } from 'node:path'
2
2
 
3
3
  import { Vfs } from '@preventive/vfs'
4
- import { KINDS, checkAhead } from './vfs-bundle/entries.js'
4
+ import { installsDevDependencies } from './cmd/bundle.js'
5
+ import { KINDS, checkAhead, defaultEntries } from './vfs-bundle/entries.js'
5
6
  import { suggestedRepoEntries } from './vfs-bundle/github.js'
6
7
  import { NODE_MODULES_MANAGERS, checkCache, checkTarget, checkVfs, detectPackageManager, loadTree, packageManagerFor, vfsHost } from './vfs-bundle/tree.js'
7
8
 
@@ -16,7 +17,9 @@ import { NODE_MODULES_MANAGERS, checkCache, checkTarget, checkVfs, detectPackage
16
17
  // dependency is GitHub's tarball of its commit, fetched through the `client` buildVfsBundle is given.
17
18
  // buildGitHubBundle builds one from a GitHub repo at a commit, or the one a tag names (the default
18
19
  // branch's head without either), its tree fetched from GitHub and held to its git tree id, cached
19
- // there the same way, and its Soldeer git dependencies through the same client.
20
+ // there the same way, and its Soldeer git dependencies through the same client. A pnpm or npm tree
21
+ // is lazy unless `lazy` is false or stasis.config.json's `lazyDependencies` is: laid out with nothing
22
+ // fetched, and each package fetched once the build reads it (vfs-bundle/lazy.js).
20
23
 
21
24
  export { buildVfsBundle } from './cmd/bundle.js'
22
25
  export { buildGitHubBundle } from './vfs-bundle/github.js'
@@ -31,9 +34,11 @@ export function createVfsHost(vfs) {
31
34
  }
32
35
 
33
36
  // -> { root, vfs, projects, host, stats, installed, packageManager, packageManagerVersion }, as
34
- // 'pnpm', 'yarn1' or 'npm' installs node_modules: the directory in the project's Vfs it installs cwd
35
- // from, which holds the lockfile; the Vfs the tree is laid out into, rooted there; the projects'
36
- // directories from there; the host reading the project through the tree; deptree's counts, and the
37
+ // 'pnpm', 'yarn1' or 'npm' installs node_modules, then cleaned by each .yarnclean in cwd and above
38
+ // it as `yarn autoclean` cleans Yarn's (vfs-bundle/yarnclean.js): the directory in the project's Vfs
39
+ // it installs cwd from, which holds the lockfile; the Vfs the tree is laid out into, rooted there;
40
+ // the projects' directories from there; the host reading the project through the tree; deptree's
41
+ // counts, and the
37
42
  // packages it installs (each by its `path` from there, with the `commit` its version document
38
43
  // names, where it names a full one); and the
39
44
  // package manager reproduced, at `packageManagerVersion` if given, else the one the root
@@ -45,7 +50,9 @@ export function createVfsHost(vfs) {
45
50
  // os, libc defaults to 'unknown'). Without a `packageManager`, it is the one of the three whose
46
51
  // lockfile installs cwd, where only one's does. `cache` is where the packages' tarballs and version
47
52
  // documents are kept, as @preventive/upstream's CacheOptions take it: left out, in setCacheDir's
48
- // cache; a store with `read` and `write`, in place of it; or false, nowhere.
53
+ // cache; a store with `read` and `write`, in place of it; or false, nowhere. Where the
54
+ // stasis.config.json in cwd sets `installDevDependencies` false, what devDependencies alone reach is
55
+ // left out, as buildVfsBundle's tree leaves it out.
49
56
  export async function loadNodeModules({ vfs, packageManager, cwd = '/', packageManagerVersion, os, cpu, libc, cache } = {}) {
50
57
  checkVfs('loadNodeModules', vfs)
51
58
  checkTarget('loadNodeModules', { os, cpu, libc })
@@ -54,18 +61,21 @@ export async function loadNodeModules({ vfs, packageManager, cwd = '/', packageM
54
61
  // A real path, as detection and the layout take it.
55
62
  cwd = project.realpath(posix.resolve('/', cwd))
56
63
  packageManager = packageManagerFor('loadNodeModules', project, cwd, { packageManager, packageManagerVersion, os }, NODE_MODULES_MANAGERS)
57
- return loadTree({ project, packageManager, cwd, packageManagerVersion, os, cpu, libc, cache })
64
+ const installDevDependencies = installsDevDependencies('loadNodeModules', cwd, project)
65
+ return loadTree({ project, packageManager, cwd, packageManagerVersion, os, cpu, libc, cache, installDevDependencies })
58
66
  }
59
67
 
60
68
  // -> the entries buildGitHubBundle takes where none are given, as paths from the project's
61
- // directory, of the kind its package manager builds: `packageManager` if given, else the one
62
- // detected as the build detects it. For pnpm and yarn 1, the entry points its package.json names,
63
- // resolved as the build resolves them with the same `conditions`, `mainFields`, `metro`,
64
- // `platforms` and `typescript` (packageEntries); for Soldeer, its .sol entry points by name
65
- // and layout (solidityEntries). The options are checked as the build checks them, and `os`, the
66
- // one the build is for, detects the package manager as there. Of the project held in `vfs`, from
67
- // `cwd`; or of a GitHub repo, `{ github, sha, tag, directory, client }` as buildGitHubBundle takes
68
- // them, downloaded as it downloads them.
69
+ // directory: those its stasis.config.json's `entries` names, where it has `entries` (none of an
70
+ // empty one), checked with the options as the build checks them; else of the kind its package
71
+ // manager builds: `packageManager` if given, else the one detected as the build detects it. For
72
+ // pnpm and yarn 1, the entry points its package.json names, resolved as the build resolves them
73
+ // with the same `conditions`, `mainFields`, `metro`, `platforms` and `typescript` (packageEntries);
74
+ // for Soldeer, its .sol entry points by name and layout (solidityEntries). The options are checked
75
+ // as the build checks them, and `os`, the one the build is for, detects the package manager as
76
+ // there. Of the project held in `vfs`, from `cwd`; or of a GitHub repo, `{ github, sha, tag,
77
+ // directory | path, client }` as buildGitHubBundle takes them, downloaded as it downloads them: of a
78
+ // `path` to a file, that file.
69
79
  export async function suggestedEntries({ vfs, cwd = '/', packageManager, conditions, mainFields, metro, platforms, jsx, typescript, os, ...repo } = {}) {
70
80
  checkTarget('suggestedEntries', { os })
71
81
  const resolution = { conditions, mainFields, metro, platforms, jsx, typescript }
@@ -76,5 +86,5 @@ export async function suggestedEntries({ vfs, cwd = '/', packageManager, conditi
76
86
  const host = vfsHost(vfs)
77
87
  const at = posix.resolve('/', cwd)
78
88
  const pm = checkAhead('suggestedEntries', packageManager ?? detectPackageManager('suggestedEntries', host, at, { os }), resolution)
79
- return KINDS[pm.kind].entries(host, at, resolution)
89
+ return defaultEntries('suggestedEntries', host, at, KINDS[pm.kind], resolution).entries
80
90
  }