@_linked/cli 1.46.1 → 1.47.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/CHANGELOG.md +113 -0
- package/README.md +48 -36
- package/hooks/drift.cjs +169 -0
- package/hooks/git-hook.cjs +170 -0
- package/hooks/hook-shim.cjs +63 -0
- package/hooks/lock-at-rest.cjs +268 -0
- package/hooks/manifests.cjs +216 -0
- package/hooks/platform.cjs +25 -0
- package/lib/esm/app-release/build-backend.d.ts.map +1 -1
- package/lib/esm/app-release/build-backend.js +2 -1
- package/lib/esm/app-release/build-backend.js.map +1 -1
- package/lib/esm/cli-methods.d.ts.map +1 -1
- package/lib/esm/cli-methods.js +166 -150
- package/lib/esm/cli-methods.js.map +1 -1
- package/lib/esm/cli.js +20 -22
- package/lib/esm/cli.js.map +1 -1
- package/lib/esm/commands/build-app.d.ts.map +1 -1
- package/lib/esm/commands/build-app.js +2 -1
- package/lib/esm/commands/build-app.js.map +1 -1
- package/lib/esm/commands/build-package.d.ts.map +1 -1
- package/lib/esm/commands/build-package.js +6 -2
- package/lib/esm/commands/build-package.js.map +1 -1
- package/lib/esm/commands/localize.d.ts +9 -15
- package/lib/esm/commands/localize.d.ts.map +1 -1
- package/lib/esm/commands/localize.js +43 -74
- package/lib/esm/commands/localize.js.map +1 -1
- package/lib/esm/commands/setup-publish.d.ts.map +1 -1
- package/lib/esm/commands/setup-publish.js +22 -15
- package/lib/esm/commands/setup-publish.js.map +1 -1
- package/lib/esm/commands/start.d.ts +8 -0
- package/lib/esm/commands/start.d.ts.map +1 -1
- package/lib/esm/commands/start.js +20 -8
- package/lib/esm/commands/start.js.map +1 -1
- package/lib/esm/installed-packages.d.ts +5 -13
- package/lib/esm/installed-packages.d.ts.map +1 -1
- package/lib/esm/installed-packages.js +5 -38
- package/lib/esm/installed-packages.js.map +1 -1
- package/lib/esm/lifecycle.d.ts +35 -22
- package/lib/esm/lifecycle.d.ts.map +1 -1
- package/lib/esm/lifecycle.js +62 -36
- package/lib/esm/lifecycle.js.map +1 -1
- package/lib/esm/localize/adopt.d.ts +5 -5
- package/lib/esm/localize/adopt.d.ts.map +1 -1
- package/lib/esm/localize/adopt.js +46 -55
- package/lib/esm/localize/adopt.js.map +1 -1
- package/lib/esm/localize/app-root.d.ts +16 -0
- package/lib/esm/localize/app-root.d.ts.map +1 -0
- package/lib/esm/localize/app-root.js +94 -0
- package/lib/esm/localize/app-root.js.map +1 -0
- package/lib/esm/localize/app-setup.d.ts +21 -0
- package/lib/esm/localize/app-setup.d.ts.map +1 -0
- package/lib/esm/localize/app-setup.js +178 -0
- package/lib/esm/localize/app-setup.js.map +1 -0
- package/lib/esm/localize/delocalize.d.ts.map +1 -1
- package/lib/esm/localize/delocalize.js +169 -104
- package/lib/esm/localize/delocalize.js.map +1 -1
- package/lib/esm/localize/drift-hook.d.ts +18 -0
- package/lib/esm/localize/drift-hook.d.ts.map +1 -0
- package/lib/esm/localize/drift-hook.js +37 -0
- package/lib/esm/localize/drift-hook.js.map +1 -0
- package/lib/esm/localize/drift.d.ts +36 -0
- package/lib/esm/localize/drift.d.ts.map +1 -0
- package/lib/esm/localize/drift.js +34 -0
- package/lib/esm/localize/drift.js.map +1 -0
- package/lib/esm/localize/errors.d.ts +14 -8
- package/lib/esm/localize/errors.d.ts.map +1 -1
- package/lib/esm/localize/errors.js +14 -8
- package/lib/esm/localize/errors.js.map +1 -1
- package/lib/esm/localize/fsops.d.ts +11 -27
- package/lib/esm/localize/fsops.d.ts.map +1 -1
- package/lib/esm/localize/fsops.js +40 -115
- package/lib/esm/localize/fsops.js.map +1 -1
- package/lib/esm/localize/git-hooks.d.ts +33 -0
- package/lib/esm/localize/git-hooks.d.ts.map +1 -0
- package/lib/esm/localize/git-hooks.js +146 -0
- package/lib/esm/localize/git-hooks.js.map +1 -0
- package/lib/esm/localize/hook-module.d.ts +6 -0
- package/lib/esm/localize/hook-module.d.ts.map +1 -0
- package/lib/esm/localize/hook-module.js +37 -0
- package/lib/esm/localize/hook-module.js.map +1 -0
- package/lib/esm/localize/index.d.ts +14 -15
- package/lib/esm/localize/index.d.ts.map +1 -1
- package/lib/esm/localize/index.js +8 -9
- package/lib/esm/localize/index.js.map +1 -1
- package/lib/esm/localize/list.d.ts +6 -10
- package/lib/esm/localize/list.d.ts.map +1 -1
- package/lib/esm/localize/list.js +49 -189
- package/lib/esm/localize/list.js.map +1 -1
- package/lib/esm/localize/localize.d.ts +46 -62
- package/lib/esm/localize/localize.d.ts.map +1 -1
- package/lib/esm/localize/localize.js +411 -285
- package/lib/esm/localize/localize.js.map +1 -1
- package/lib/esm/localize/members.d.ts +13 -0
- package/lib/esm/localize/members.d.ts.map +1 -0
- package/lib/esm/localize/members.js +46 -0
- package/lib/esm/localize/members.js.map +1 -0
- package/lib/esm/localize/resolve.d.ts +1 -2
- package/lib/esm/localize/resolve.d.ts.map +1 -1
- package/lib/esm/localize/resolve.js +3 -5
- package/lib/esm/localize/resolve.js.map +1 -1
- package/lib/esm/localize/run.d.ts +1 -0
- package/lib/esm/localize/run.d.ts.map +1 -1
- package/lib/esm/localize/run.js +11 -6
- package/lib/esm/localize/run.js.map +1 -1
- package/lib/esm/localize/setup-lockfile.d.ts +13 -0
- package/lib/esm/localize/setup-lockfile.d.ts.map +1 -0
- package/lib/esm/localize/setup-lockfile.js +80 -0
- package/lib/esm/localize/setup-lockfile.js.map +1 -0
- package/lib/esm/utils/createPackageLocation.d.ts +4 -13
- package/lib/esm/utils/createPackageLocation.d.ts.map +1 -1
- package/lib/esm/utils/createPackageLocation.js +12 -21
- package/lib/esm/utils/createPackageLocation.js.map +1 -1
- package/lib/esm/utils/packageSetup.d.ts +5 -2
- package/lib/esm/utils/packageSetup.d.ts.map +1 -1
- package/lib/esm/utils/packageSetup.js +9 -3
- package/lib/esm/utils/packageSetup.js.map +1 -1
- package/lib/esm/utils/platform.d.ts +24 -0
- package/lib/esm/utils/platform.d.ts.map +1 -0
- package/lib/esm/utils/platform.js +62 -0
- package/lib/esm/utils/platform.js.map +1 -0
- package/lib/esm/utils/shapeReferences.d.ts.map +1 -1
- package/lib/esm/utils/shapeReferences.js +3 -2
- package/lib/esm/utils/shapeReferences.js.map +1 -1
- package/lib/esm/utils/spawn.d.ts +65 -0
- package/lib/esm/utils/spawn.d.ts.map +1 -0
- package/lib/esm/utils/spawn.js +181 -0
- package/lib/esm/utils/spawn.js.map +1 -0
- package/lib/esm/utils.d.ts.map +1 -1
- package/lib/esm/utils.js +19 -5
- package/lib/esm/utils.js.map +1 -1
- package/lib/esm/vite-config.d.ts +5 -12
- package/lib/esm/vite-config.d.ts.map +1 -1
- package/lib/esm/vite-config.js +41 -33
- package/lib/esm/vite-config.js.map +1 -1
- package/package.json +4 -3
- package/lib/esm/localize/ensure.d.ts +0 -44
- package/lib/esm/localize/ensure.d.ts.map +0 -1
- package/lib/esm/localize/ensure.js +0 -151
- package/lib/esm/localize/ensure.js.map +0 -1
- package/lib/esm/localize/manifest.d.ts +0 -63
- package/lib/esm/localize/manifest.d.ts.map +0 -1
- package/lib/esm/localize/manifest.js +0 -133
- package/lib/esm/localize/manifest.js.map +0 -1
- package/lib/esm/localize/one-copy-hook.d.ts +0 -16
- package/lib/esm/localize/one-copy-hook.d.ts.map +0 -1
- package/lib/esm/localize/one-copy-hook.js +0 -20
- package/lib/esm/localize/one-copy-hook.js.map +0 -1
- package/lib/esm/localize/provided.d.ts +0 -28
- package/lib/esm/localize/provided.d.ts.map +0 -1
- package/lib/esm/localize/provided.js +0 -89
- package/lib/esm/localize/provided.js.map +0 -1
- package/lib/esm/localize/prune.d.ts +0 -165
- package/lib/esm/localize/prune.d.ts.map +0 -1
- package/lib/esm/localize/prune.js +0 -619
- package/lib/esm/localize/prune.js.map +0 -1
- package/lib/esm/localize/relink.d.ts +0 -13
- package/lib/esm/localize/relink.d.ts.map +0 -1
- package/lib/esm/localize/relink.js +0 -179
- package/lib/esm/localize/relink.js.map +0 -1
- package/lib/esm/localized-dedupe.d.ts +0 -26
- package/lib/esm/localized-dedupe.d.ts.map +0 -1
- package/lib/esm/localized-dedupe.js +0 -139
- package/lib/esm/localized-dedupe.js.map +0 -1
- package/lib/esm/utils/relinkHooks.d.ts +0 -19
- package/lib/esm/utils/relinkHooks.d.ts.map +0 -1
- package/lib/esm/utils/relinkHooks.js +0 -74
- package/lib/esm/utils/relinkHooks.js.map +0 -1
|
@@ -1,619 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Pruning -- stop a checkout's own `node_modules` holding a second copy of
|
|
3
|
-
* something the app already provides. On by default after every install in a
|
|
4
|
-
* checkout and on every `--relink`; `--no-prune` (`{prune: false}`) skips it.
|
|
5
|
-
*
|
|
6
|
-
* `shouldPrune(opts)` is the one place that default lives.
|
|
7
|
-
*
|
|
8
|
-
* Why it is needed. `npm install` inside a checkout (localize.ts, step 3)
|
|
9
|
-
* installs what the CHECKOUT's lockfile says: its own copy of every
|
|
10
|
-
* dependency, often older than the app's. Node resolves a module from the
|
|
11
|
-
* importer's REAL path and takes the first `node_modules` it meets going up,
|
|
12
|
-
* so a localized `A` that imports `B` gets `A/node_modules/B` -- a registry
|
|
13
|
-
* copy -- and never the localized `B`; and a framework package that holds a
|
|
14
|
-
* registry of classes (or React) loads once per copy. Measured with three
|
|
15
|
-
* localized `@_linked/*` packages: plain Node and `tsx` loaded three copies of
|
|
16
|
-
* the same core.
|
|
17
|
-
*
|
|
18
|
-
* What it does. For every localized checkout, each package at the top of its
|
|
19
|
-
* `node_modules` that is a CANDIDATE in that checkout (below) is removed when
|
|
20
|
-
* the app provides it -- `<appRoot>/node_modules/<name>` exists -- at a
|
|
21
|
-
* version that satisfies every range that resolves to that copy: the
|
|
22
|
-
* checkout's own declaration and that of every installed package that would
|
|
23
|
-
* load it. Node's upward search then finds the app's copy instead. Nothing
|
|
24
|
-
* else in the checkout moves.
|
|
25
|
-
*
|
|
26
|
-
* The candidates start from `providedPackages` (provided.ts) -- the one rule
|
|
27
|
-
* the run-time check and Vite's dedupe use too -- and are taken PER CHECKOUT:
|
|
28
|
-
*
|
|
29
|
-
* - a **localized sibling** -- always counts as provided, whatever the range,
|
|
30
|
-
* because the live checkout is the point of localizing it. A range it does
|
|
31
|
-
* not satisfy is said out loud, never acted on;
|
|
32
|
-
* - one of **this checkout's own runtime dependencies** (`dependencies`,
|
|
33
|
-
* `peerDependencies`, `optionalDependencies`) that the rule provides -- the
|
|
34
|
-
* app has it at a version satisfying every localized checkout's range;
|
|
35
|
-
* - `react` and `react-dom`.
|
|
36
|
-
*
|
|
37
|
-
* Nothing else is touched -- this checkout's devDependency stays even when
|
|
38
|
-
* another checkout depends on the same package at runtime, so the checkout's
|
|
39
|
-
* tooling does not come to depend on the app's. A runtime dependency the rule
|
|
40
|
-
* leaves out because a localized range misses the app's version is kept and
|
|
41
|
-
* reported, like any other copy kept below.
|
|
42
|
-
*
|
|
43
|
-
* Never removed, candidate or not:
|
|
44
|
-
*
|
|
45
|
-
* - a copy whose ranges the app's version does not satisfy;
|
|
46
|
-
* - a copy whose own dependencies would resolve to something else from the
|
|
47
|
-
* app's copy than they do from the checkout (`contextChange` below): the
|
|
48
|
-
* checkout keeps its own copies of whatever failed the guard, and removing
|
|
49
|
-
* the copy would mix the two -- measured as a checkout's `tsc` seeing two
|
|
50
|
-
* React type sets;
|
|
51
|
-
* - anything in a checkout that is not under the app root, because then the
|
|
52
|
-
* upward search never reaches the app's `node_modules` and the removed
|
|
53
|
-
* package would simply be missing;
|
|
54
|
-
* - anything in a checkout recorded with `prune: false` (localized with
|
|
55
|
-
* `--no-prune`).
|
|
56
|
-
*
|
|
57
|
-
* Kept copies are reported once per checkout, as a summary; `--list` gives
|
|
58
|
-
* each one's reason. A removal can make another copy removable, so a prune
|
|
59
|
-
* repeats until a pass removes nothing.
|
|
60
|
-
*
|
|
61
|
-
* A package with a `bin` is NOT exempt. Measured in a checkout under an app
|
|
62
|
-
* root with its own `@_linked/cli` removed: `npx linked build`,
|
|
63
|
-
* `npm exec linked -- --help`, a script running `npm exec linked` and a script
|
|
64
|
-
* running a bare `linked` all resolved `linked` to the app root's copy,
|
|
65
|
-
* because npm puts every ancestor's `node_modules/.bin` on the PATH. The
|
|
66
|
-
* removed package's links in the checkout's `node_modules/.bin` are removed
|
|
67
|
-
* with it, so nothing dangles.
|
|
68
|
-
*
|
|
69
|
-
* It never runs npm and never touches the checkout's `package.json` or
|
|
70
|
-
* `package-lock.json`. `npm install` in the checkout puts the copies back;
|
|
71
|
-
* `ensure` (ensure.ts) is the install-then-prune step every in-checkout
|
|
72
|
-
* install goes through, and the run-time check (`checkOneCopy`) takes them out
|
|
73
|
-
* again before `linked start` and friends.
|
|
74
|
-
*/
|
|
75
|
-
import fs from 'node:fs';
|
|
76
|
-
import path from 'node:path';
|
|
77
|
-
import { isInside, isSymlink, readJson } from './fsops.js';
|
|
78
|
-
import semver from 'semver';
|
|
79
|
-
import { providedPackages } from './provided.js';
|
|
80
|
-
/** The rule (`providedPackages`, provided.ts) over `entries`, plus every entry (sibling). */
|
|
81
|
-
export function candidatesFor(entries, deps) {
|
|
82
|
-
const rule = providedPackages(deps.appRoot, Object.keys(entries));
|
|
83
|
-
return {
|
|
84
|
-
names: new Set([...rule.names, ...Object.keys(entries)]),
|
|
85
|
-
skipped: new Map(rule.skipped.map((s) => [s.name, s])),
|
|
86
|
-
};
|
|
87
|
-
}
|
|
88
|
-
/**
|
|
89
|
-
* Does the app's installed `version` satisfy `range`? `null` means "cannot
|
|
90
|
-
* tell" -- a dist-tag, `file:`, `npm:`, `workspace:`, a git URL, or a version
|
|
91
|
-
* that is not one -- and every caller then keeps the copy. The failure mode is
|
|
92
|
-
* therefore always the safe one: a copy kept that could have gone, never a
|
|
93
|
-
* copy removed that was needed.
|
|
94
|
-
*
|
|
95
|
-
* npm's own `semver` decides, so prereleases follow npm's rule: `1.3.0-beta.1`
|
|
96
|
-
* satisfies a range only when one of its comparators names a prerelease on
|
|
97
|
-
* the same `major.minor.patch`.
|
|
98
|
-
*/
|
|
99
|
-
export function satisfies(version, range) {
|
|
100
|
-
if (typeof version !== 'string' || typeof range !== 'string')
|
|
101
|
-
return null;
|
|
102
|
-
if (semver.valid(version) === null)
|
|
103
|
-
return null;
|
|
104
|
-
if (semver.validRange(range) === null)
|
|
105
|
-
return null;
|
|
106
|
-
return semver.satisfies(version, range);
|
|
107
|
-
}
|
|
108
|
-
/** Pruning is on unless a caller says `{prune: false}`. */
|
|
109
|
-
export function shouldPrune(opts = {}) {
|
|
110
|
-
return opts.prune !== false;
|
|
111
|
-
}
|
|
112
|
-
/** Is pruning on for this recorded checkout? `--no-prune` at localize/adopt records `prune: false`. */
|
|
113
|
-
export function prunesEntry(entry) {
|
|
114
|
-
return entry.prune !== false;
|
|
115
|
-
}
|
|
116
|
-
const REQUIRE_FIELDS = [
|
|
117
|
-
'dependencies',
|
|
118
|
-
'peerDependencies',
|
|
119
|
-
'optionalDependencies',
|
|
120
|
-
];
|
|
121
|
-
const DECLARE_FIELDS = [
|
|
122
|
-
'dependencies',
|
|
123
|
-
'devDependencies',
|
|
124
|
-
'peerDependencies',
|
|
125
|
-
'optionalDependencies',
|
|
126
|
-
];
|
|
127
|
-
/** Candidates in every checkout, declared or not: a second React breaks hooks whoever loads it. */
|
|
128
|
-
const ALWAYS_CANDIDATES = new Set(['react', 'react-dom']);
|
|
129
|
-
/** prune repeats until a pass removes nothing, at most this many passes. */
|
|
130
|
-
export const MAX_PRUNE_PASSES = 5;
|
|
131
|
-
export function newPruneCache() {
|
|
132
|
-
return { indexes: new Map(), builds: 0 };
|
|
133
|
-
}
|
|
134
|
-
/**
|
|
135
|
-
* Decide, for every recorded checkout and every candidate copy in its
|
|
136
|
-
* `node_modules`, whether pruning removes it -- without removing anything.
|
|
137
|
-
* `pruneProvided` applies it; the run-time check (`checkOneCopy`) and
|
|
138
|
-
* `--list` call it on its own, which is why it has to be cheap: a copy whose
|
|
139
|
-
* checkout's own range rules it out is decided from that `package.json`
|
|
140
|
-
* alone, and every other range comes from one requirement index per
|
|
141
|
-
* `node_modules`, built at most once per call.
|
|
142
|
-
*
|
|
143
|
-
* Candidates are PER OWNER: a copy is a candidate in checkout O when the rule
|
|
144
|
-
* provides it AND it is a localized sibling, `react`/`react-dom`, or one of
|
|
145
|
-
* O's own runtime dependencies (`dependencies`, `peerDependencies`,
|
|
146
|
-
* `optionalDependencies`). O's devDependencies are O's tooling, whatever
|
|
147
|
-
* another checkout declares at runtime.
|
|
148
|
-
*/
|
|
149
|
-
export function planPrune(entries, deps, scope = {}, cache = newPruneCache(), candidates = candidatesFor(entries, deps)) {
|
|
150
|
-
var _a;
|
|
151
|
-
const plan = { remove: [], keep: [], outside: [] };
|
|
152
|
-
const appRoot = realOrSelf(deps.appRoot);
|
|
153
|
-
const only = scope.deps ? new Set(scope.deps) : null;
|
|
154
|
-
for (const [name, entry] of Object.entries(entries)) {
|
|
155
|
-
if (scope.owners && !scope.owners.includes(name))
|
|
156
|
-
continue;
|
|
157
|
-
if (!prunesEntry(entry))
|
|
158
|
-
continue;
|
|
159
|
-
const pkgDir = path.join(deps.appRoot, entry.path);
|
|
160
|
-
if (!fs.existsSync(path.join(pkgDir, 'package.json')))
|
|
161
|
-
continue;
|
|
162
|
-
if (!isInside(realOrSelf(pkgDir), appRoot)) {
|
|
163
|
-
plan.outside.push({ name, path: entry.path });
|
|
164
|
-
continue;
|
|
165
|
-
}
|
|
166
|
-
const pkg = (_a = readJson(path.join(pkgDir, 'package.json'))) !== null && _a !== void 0 ? _a : {};
|
|
167
|
-
const runtime = new Set(REQUIRE_FIELDS.flatMap((f) => { var _a; return Object.keys((_a = pkg[f]) !== null && _a !== void 0 ? _a : {}); }));
|
|
168
|
-
// The names that can be a decision here, looked up directly rather than
|
|
169
|
-
// by listing node_modules (thousands of entries per checkout): this
|
|
170
|
-
// checkout's candidates, and what the rule skipped that it declares.
|
|
171
|
-
const names = new Set();
|
|
172
|
-
for (const n of candidates.names) {
|
|
173
|
-
if (entries[n] || ALWAYS_CANDIDATES.has(n) || runtime.has(n))
|
|
174
|
-
names.add(n);
|
|
175
|
-
}
|
|
176
|
-
for (const n of candidates.skipped.keys()) {
|
|
177
|
-
if (declared(pkg, DECLARE_FIELDS, n) !== undefined)
|
|
178
|
-
names.add(n);
|
|
179
|
-
}
|
|
180
|
-
names.delete(name);
|
|
181
|
-
const ctx = { name, entry, pkg, names, entries, candidates, only, deps };
|
|
182
|
-
for (const nm of nodeModulesRoots(pkgDir, entry)) {
|
|
183
|
-
for (const d of decideIn(nm, ctx, cache)) {
|
|
184
|
-
(d.action === 'remove' ? plan.remove : plan.keep).push(d);
|
|
185
|
-
}
|
|
186
|
-
}
|
|
187
|
-
}
|
|
188
|
-
return plan;
|
|
189
|
-
}
|
|
190
|
-
/**
|
|
191
|
-
* @param entries the localized packages: every one counts as a sibling
|
|
192
|
-
* @param opts unused beyond `prune`, which callers check with `shouldPrune`
|
|
193
|
-
* @param deps `{appRoot, log, warn}`
|
|
194
|
-
* @param scope prune only some checkouts / names (the run-time check)
|
|
195
|
-
*
|
|
196
|
-
* A removal can make another copy removable -- the copy that went was the one
|
|
197
|
-
* asking a range the app misses, or the one a sibling copy's dependency
|
|
198
|
-
* resolved to -- so it plans and removes again until a pass removes nothing
|
|
199
|
-
* (at most `MAX_PRUNE_PASSES`): one call converges. A checkout recorded with
|
|
200
|
-
* `prune: false` is never touched.
|
|
201
|
-
*/
|
|
202
|
-
export function pruneProvided(entries, opts, deps, scope = {}, cache = newPruneCache()) {
|
|
203
|
-
const removed = [];
|
|
204
|
-
let kept = [];
|
|
205
|
-
const candidates = candidatesFor(entries, deps);
|
|
206
|
-
for (let pass = 0; pass < MAX_PRUNE_PASSES; pass++) {
|
|
207
|
-
const passScope = pass === 0 ? scope : { owners: scope.owners };
|
|
208
|
-
const plan = planPrune(entries, deps, passScope, cache, candidates);
|
|
209
|
-
if (pass === 0) {
|
|
210
|
-
for (const o of plan.outside) {
|
|
211
|
-
deps.log(`[localize] ${o.name}: ${o.path} is outside the app root, so Node cannot reach the app's ` +
|
|
212
|
-
`node_modules from it — its own dependencies are left as they are.`);
|
|
213
|
-
}
|
|
214
|
-
}
|
|
215
|
-
for (const d of plan.remove) {
|
|
216
|
-
fs.rmSync(d.dir, { recursive: true, force: true });
|
|
217
|
-
removeEmptyScope(d.nm, d.dep);
|
|
218
|
-
removeBinLinksInto(d.nm, d.dir);
|
|
219
|
-
removed.push(d);
|
|
220
|
-
}
|
|
221
|
-
kept = plan.keep;
|
|
222
|
-
if (!plan.remove.length)
|
|
223
|
-
break;
|
|
224
|
-
}
|
|
225
|
-
report(removed, kept, deps);
|
|
226
|
-
return { removed, kept };
|
|
227
|
-
}
|
|
228
|
-
/**
|
|
229
|
-
* Is `dep` -- declared by a checkout at `range` -- something the app provides,
|
|
230
|
-
* so that its absence from the checkout's own `node_modules` is not a
|
|
231
|
-
* dependency gone missing? `--relink` asks this before reinstalling a
|
|
232
|
-
* checkout, so a copy pruned on purpose does not trigger a reinstall on every
|
|
233
|
-
* postinstall. `pkg` is the checkout's `package.json`: only what would be a
|
|
234
|
-
* candidate in THAT checkout counts.
|
|
235
|
-
*/
|
|
236
|
-
export function isProvidedByApp(dep, range, entries, candidates, deps, pkg) {
|
|
237
|
-
const app = appCopy(dep, deps);
|
|
238
|
-
if (!app)
|
|
239
|
-
return false;
|
|
240
|
-
if (entries[dep])
|
|
241
|
-
return true;
|
|
242
|
-
if (!candidates.names.has(dep))
|
|
243
|
-
return false;
|
|
244
|
-
if (pkg &&
|
|
245
|
-
!ALWAYS_CANDIDATES.has(dep) &&
|
|
246
|
-
declared(pkg, REQUIRE_FIELDS, dep) === undefined)
|
|
247
|
-
return false;
|
|
248
|
-
return range === undefined || satisfies(app.version, range) === true;
|
|
249
|
-
}
|
|
250
|
-
function decideIn(nm, ctx, cache) {
|
|
251
|
-
const out = [];
|
|
252
|
-
const { name: owner, entry, pkg, names, entries, candidates, only, deps } = ctx;
|
|
253
|
-
for (const dep of [...names].sort()) {
|
|
254
|
-
if (only && !only.has(dep))
|
|
255
|
-
continue;
|
|
256
|
-
const skip = candidates.skipped.get(dep);
|
|
257
|
-
const inRule = candidates.names.has(dep);
|
|
258
|
-
const sibling = Boolean(entries[dep]);
|
|
259
|
-
const dir = path.join(nm, dep);
|
|
260
|
-
const nested = readJson(path.join(dir, 'package.json'));
|
|
261
|
-
if (!nested)
|
|
262
|
-
continue;
|
|
263
|
-
const app = appCopy(dep, deps);
|
|
264
|
-
if (!app)
|
|
265
|
-
continue; // the app does not provide it, so this copy is the only one
|
|
266
|
-
if (realOrSelf(dir) === app.real)
|
|
267
|
-
continue; // already the app's copy
|
|
268
|
-
const base = {
|
|
269
|
-
ownerName: owner,
|
|
270
|
-
owner: entry.path,
|
|
271
|
-
dep,
|
|
272
|
-
dir,
|
|
273
|
-
where: path.relative(deps.appRoot, dir),
|
|
274
|
-
nm,
|
|
275
|
-
version: nested.version,
|
|
276
|
-
app: app.version,
|
|
277
|
-
sibling,
|
|
278
|
-
};
|
|
279
|
-
if (!inRule) {
|
|
280
|
-
// The rule leaves it out: a localized checkout asks a range the app
|
|
281
|
-
// misses. Said for a checkout that declares it; a copy it only has
|
|
282
|
-
// transitively is none of its business.
|
|
283
|
-
if (declared(pkg, DECLARE_FIELDS, dep) === undefined)
|
|
284
|
-
continue;
|
|
285
|
-
out.push(Object.assign(Object.assign({}, base), { action: 'keep', reason: 'range', unmet: skip.asks }));
|
|
286
|
-
continue;
|
|
287
|
-
}
|
|
288
|
-
const own = declared(pkg, DECLARE_FIELDS, dep);
|
|
289
|
-
const ownAsk = own === undefined ? [] : [{ from: owner, range: own }];
|
|
290
|
-
if (sibling) {
|
|
291
|
-
// Always removed: the live checkout is the point of localizing it. A
|
|
292
|
-
// range it misses is drift, said out loud and never acted on.
|
|
293
|
-
const ranges = [...ownAsk, ...requirementIndex(nm, cache).asks(dep)];
|
|
294
|
-
out.push(Object.assign(Object.assign({}, base), { action: 'remove', unmet: ranges.filter((r) => satisfies(app.version, r.range) === false) }));
|
|
295
|
-
continue;
|
|
296
|
-
}
|
|
297
|
-
// The checkout's own range first: when it rules the copy out, no index is needed.
|
|
298
|
-
if (own !== undefined && satisfies(app.version, own) !== true) {
|
|
299
|
-
out.push(Object.assign(Object.assign({}, base), { action: 'keep', reason: 'range', unmet: ownAsk }));
|
|
300
|
-
continue;
|
|
301
|
-
}
|
|
302
|
-
const unmet = requirementIndex(nm, cache)
|
|
303
|
-
.asks(dep)
|
|
304
|
-
.filter((r) => satisfies(app.version, r.range) !== true);
|
|
305
|
-
if (unmet.length) {
|
|
306
|
-
out.push(Object.assign(Object.assign({}, base), { action: 'keep', reason: 'range', unmet }));
|
|
307
|
-
continue;
|
|
308
|
-
}
|
|
309
|
-
const context = contextChange(dir, app.real, nested);
|
|
310
|
-
if (context) {
|
|
311
|
-
out.push(Object.assign(Object.assign({}, base), { action: 'keep', reason: 'context', unmet: [], context }));
|
|
312
|
-
continue;
|
|
313
|
-
}
|
|
314
|
-
out.push(Object.assign(Object.assign({}, base), { action: 'remove', unmet: [] }));
|
|
315
|
-
}
|
|
316
|
-
return out;
|
|
317
|
-
}
|
|
318
|
-
/**
|
|
319
|
-
* Would removing the copy at `copyDir` change what ITS OWN dependencies
|
|
320
|
-
* resolve to? Removing it hands its importers the app's copy, which resolves
|
|
321
|
-
* its dependencies from the app root -- while the checkout keeps its own copy
|
|
322
|
-
* of everything that failed the guard. Measured: a checkout kept its
|
|
323
|
-
* `react`/`@types/react` 18 (its range), lost `prism-react-renderer` (in
|
|
324
|
-
* range), and the app's copy of that resolved `react` -- and with it the
|
|
325
|
-
* types -- to the app's 19: the checkout's own `tsc` saw both React type sets
|
|
326
|
-
* and failed. So the copy stays when any of its `dependencies`,
|
|
327
|
-
* `peerDependencies` or `optionalDependencies` (and the `@types/` package
|
|
328
|
-
* TypeScript would take for it) resolves, from the copy's place in the
|
|
329
|
-
* checkout, to a different package than from the app's copy: a different real
|
|
330
|
-
* path that is not the same `name@version`, or nothing at all from the app's.
|
|
331
|
-
* The same version at another path is the same code, so it does not count; a
|
|
332
|
-
* dependency the checkout cannot resolve either is no change.
|
|
333
|
-
*/
|
|
334
|
-
function contextChange(copyDir, appReal, pkg) {
|
|
335
|
-
var _a;
|
|
336
|
-
const names = new Set();
|
|
337
|
-
for (const f of REQUIRE_FIELDS) {
|
|
338
|
-
for (const d of Object.keys((_a = pkg[f]) !== null && _a !== void 0 ? _a : {})) {
|
|
339
|
-
names.add(d);
|
|
340
|
-
names.add(typesPackageFor(d));
|
|
341
|
-
}
|
|
342
|
-
}
|
|
343
|
-
for (const d of names) {
|
|
344
|
-
const here = resolveFrom(copyDir, d);
|
|
345
|
-
if (!here)
|
|
346
|
-
continue;
|
|
347
|
-
const there = resolveFrom(appReal, d);
|
|
348
|
-
if (!there)
|
|
349
|
-
return { dep: d, here: here.version, app: null };
|
|
350
|
-
if (here.real !== there.real && here.version !== there.version)
|
|
351
|
-
return { dep: d, here: here.version, app: there.version };
|
|
352
|
-
}
|
|
353
|
-
return undefined;
|
|
354
|
-
}
|
|
355
|
-
/** `react` → `@types/react`; `@scope/name` → `@types/scope__name`; an `@types/` name is itself. */
|
|
356
|
-
function typesPackageFor(name) {
|
|
357
|
-
if (name.startsWith('@types/'))
|
|
358
|
-
return name;
|
|
359
|
-
return `@types/${name.startsWith('@') ? name.slice(1).replace('/', '__') : name}`;
|
|
360
|
-
}
|
|
361
|
-
/** Node's upward search for `name` from the directory `from`: the first `node_modules/<name>`. */
|
|
362
|
-
function resolveFrom(from, name) {
|
|
363
|
-
for (let dir = from;;) {
|
|
364
|
-
if (path.basename(dir) !== 'node_modules') {
|
|
365
|
-
const candidate = path.join(dir, 'node_modules', name);
|
|
366
|
-
const pkg = readJson(path.join(candidate, 'package.json'));
|
|
367
|
-
if (pkg)
|
|
368
|
-
return { real: realOrSelf(candidate), version: String(pkg.version) };
|
|
369
|
-
}
|
|
370
|
-
const parent = path.dirname(dir);
|
|
371
|
-
if (parent === dir)
|
|
372
|
-
return null;
|
|
373
|
-
dir = parent;
|
|
374
|
-
}
|
|
375
|
-
}
|
|
376
|
-
/** The package's directory, and the `node_modules` directories between `nm` and it (its own included). */
|
|
377
|
-
function locate(nm, i) {
|
|
378
|
-
if (!i.located) {
|
|
379
|
-
const segs = i.rel.split('/');
|
|
380
|
-
const dir = path.join(nm, ...segs);
|
|
381
|
-
const between = [path.join(dir, 'node_modules')];
|
|
382
|
-
for (let k = 0; k < segs.length; k++) {
|
|
383
|
-
if (segs[k] === 'node_modules')
|
|
384
|
-
between.push(path.join(nm, ...segs.slice(0, k + 1)));
|
|
385
|
-
}
|
|
386
|
-
i.located = { dir, between };
|
|
387
|
-
}
|
|
388
|
-
return i.located;
|
|
389
|
-
}
|
|
390
|
-
function requirementIndex(nm, cache) {
|
|
391
|
-
var _a;
|
|
392
|
-
let index = cache.indexes.get(nm);
|
|
393
|
-
if (!index) {
|
|
394
|
-
cache.builds++;
|
|
395
|
-
const byDep = (_a = fromHiddenLockfile(nm)) !== null && _a !== void 0 ? _a : fromWalk(nm);
|
|
396
|
-
index = {
|
|
397
|
-
asks(dep) {
|
|
398
|
-
var _a;
|
|
399
|
-
const out = [];
|
|
400
|
-
for (const { installed, range } of (_a = byDep.get(dep)) !== null && _a !== void 0 ? _a : []) {
|
|
401
|
-
if (installed.name === dep)
|
|
402
|
-
continue;
|
|
403
|
-
const { dir, between } = locate(nm, installed);
|
|
404
|
-
if (!fs.existsSync(path.join(dir, 'package.json')))
|
|
405
|
-
continue;
|
|
406
|
-
if (between.some((d) => fs.existsSync(path.join(d, dep))))
|
|
407
|
-
continue;
|
|
408
|
-
out.push({ from: `${installed.name}@${installed.version}`, range });
|
|
409
|
-
}
|
|
410
|
-
return out;
|
|
411
|
-
},
|
|
412
|
-
};
|
|
413
|
-
cache.indexes.set(nm, index);
|
|
414
|
-
}
|
|
415
|
-
return index;
|
|
416
|
-
}
|
|
417
|
-
/** Record what the package at `rel` (below `nm`) requires. */
|
|
418
|
-
function addRequirer(byDep, rel, pkg) {
|
|
419
|
-
var _a;
|
|
420
|
-
const at = rel.lastIndexOf('/node_modules/');
|
|
421
|
-
const installed = {
|
|
422
|
-
name: at === -1 ? rel : rel.slice(at + '/node_modules/'.length),
|
|
423
|
-
version: pkg.version,
|
|
424
|
-
rel,
|
|
425
|
-
};
|
|
426
|
-
const seen = new Set();
|
|
427
|
-
for (const f of REQUIRE_FIELDS) {
|
|
428
|
-
for (const [dep, range] of Object.entries((_a = pkg[f]) !== null && _a !== void 0 ? _a : {})) {
|
|
429
|
-
if (typeof range !== 'string' || seen.has(dep))
|
|
430
|
-
continue;
|
|
431
|
-
seen.add(dep);
|
|
432
|
-
let list = byDep.get(dep);
|
|
433
|
-
if (!list)
|
|
434
|
-
byDep.set(dep, (list = []));
|
|
435
|
-
list.push({ installed, range });
|
|
436
|
-
}
|
|
437
|
-
}
|
|
438
|
-
}
|
|
439
|
-
const NODE_MODULES_PREFIX = 'node_modules/';
|
|
440
|
-
function fromHiddenLockfile(nm) {
|
|
441
|
-
const lock = readJson(path.join(nm, '.package-lock.json'));
|
|
442
|
-
if (!(lock === null || lock === void 0 ? void 0 : lock.packages) || typeof lock.packages !== 'object')
|
|
443
|
-
return null;
|
|
444
|
-
const byDep = new Map();
|
|
445
|
-
for (const [key, meta] of Object.entries(lock.packages)) {
|
|
446
|
-
if (!key.startsWith(NODE_MODULES_PREFIX) || !meta || meta.link)
|
|
447
|
-
continue;
|
|
448
|
-
addRequirer(byDep, key.slice(NODE_MODULES_PREFIX.length), meta);
|
|
449
|
-
}
|
|
450
|
-
return byDep;
|
|
451
|
-
}
|
|
452
|
-
function fromWalk(nm) {
|
|
453
|
-
const byDep = new Map();
|
|
454
|
-
const walk = (dir, prefix) => {
|
|
455
|
-
for (const name of topLevelPackages(dir)) {
|
|
456
|
-
const pkgDir = path.join(dir, name);
|
|
457
|
-
if (isSymlink(pkgDir))
|
|
458
|
-
continue; // resolves from its real path, which is not here
|
|
459
|
-
const rel = prefix + name;
|
|
460
|
-
const pkg = readJson(path.join(pkgDir, 'package.json'));
|
|
461
|
-
if (pkg)
|
|
462
|
-
addRequirer(byDep, rel, pkg);
|
|
463
|
-
const inner = path.join(pkgDir, 'node_modules');
|
|
464
|
-
if (fs.existsSync(inner))
|
|
465
|
-
walk(inner, `${rel}/node_modules/`);
|
|
466
|
-
}
|
|
467
|
-
};
|
|
468
|
-
walk(nm, '');
|
|
469
|
-
return byDep;
|
|
470
|
-
}
|
|
471
|
-
function report(removed, kept, deps) {
|
|
472
|
-
var _a, _b;
|
|
473
|
-
const byOwner = new Map();
|
|
474
|
-
for (const r of removed)
|
|
475
|
-
byOwner.set(r.owner, [...((_a = byOwner.get(r.owner)) !== null && _a !== void 0 ? _a : []), r]);
|
|
476
|
-
for (const [owner, list] of byOwner) {
|
|
477
|
-
deps.log(`[localize] ${owner}: removed its own copies of what the app provides — ` +
|
|
478
|
-
list
|
|
479
|
-
.map((r) => `${r.dep}@${r.version} (app: ${r.sibling ? `localized, ${r.app}` : r.app})`)
|
|
480
|
-
.join(', '));
|
|
481
|
-
for (const r of list) {
|
|
482
|
-
for (const d of r.unmet) {
|
|
483
|
-
deps.warn(`[localize] ${owner}: ${d.from} asks ${r.dep}@${d.range}; the localized checkout is ${r.app}. ` +
|
|
484
|
-
`It is used anyway — that is what localizing it means — but the range is now fiction.`);
|
|
485
|
-
}
|
|
486
|
-
}
|
|
487
|
-
}
|
|
488
|
-
const keptBy = new Map();
|
|
489
|
-
for (const k of kept)
|
|
490
|
-
keptBy.set(k.owner, [...((_b = keptBy.get(k.owner)) !== null && _b !== void 0 ? _b : []), k]);
|
|
491
|
-
for (const [owner, list] of keptBy) {
|
|
492
|
-
const one = list.length === 1;
|
|
493
|
-
deps.warn(`[localize] ${owner}: kept ${list.length} own ${one ? 'copy' : 'copies'} the app's cannot ` +
|
|
494
|
-
`replace: ${summarizeKept(list)} — so the app loads ${one ? 'it' : 'them'} twice. ` +
|
|
495
|
-
`\`linked localize --list\` says why.`);
|
|
496
|
-
}
|
|
497
|
-
}
|
|
498
|
-
const SUMMARY_LIMIT = 8;
|
|
499
|
-
/** `react@18.3.1 (app 19.2.0), prism-react-renderer@2.4.1 (its react would change), … and 4 more` */
|
|
500
|
-
export function summarizeKept(list) {
|
|
501
|
-
const shown = list.slice(0, SUMMARY_LIMIT).map(keptShort);
|
|
502
|
-
const more = list.length - shown.length;
|
|
503
|
-
return shown.join(', ') + (more > 0 ? `, … and ${more} more` : '');
|
|
504
|
-
}
|
|
505
|
-
function keptShort(k) {
|
|
506
|
-
return k.reason === 'context'
|
|
507
|
-
? `${k.dep}@${k.version} (its ${k.context.dep} would change)`
|
|
508
|
-
: `${k.dep}@${k.version} (app ${k.app})`;
|
|
509
|
-
}
|
|
510
|
-
/** The full reason a copy stays, for `--list`. */
|
|
511
|
-
export function explainKept(k) {
|
|
512
|
-
if (k.reason === 'context') {
|
|
513
|
-
const c = k.context;
|
|
514
|
-
return (`${k.where}@${k.version}: from the checkout its ${c.dep} is ${c.here}, from the app's copy ` +
|
|
515
|
-
`${c.app === null ? 'nothing' : c.app} — removing it would change what it loads`);
|
|
516
|
-
}
|
|
517
|
-
return (`${k.where}@${k.version}: ` +
|
|
518
|
-
k.unmet.map((u) => `${u.from} asks ${k.dep}@${u.range}`).join(', ') +
|
|
519
|
-
`, and the app has ${k.dep}@${k.app}` +
|
|
520
|
-
(k.unmet.some((u) => satisfies(k.app, u.range) === null)
|
|
521
|
-
? ' (or the range is one localize cannot read)'
|
|
522
|
-
: ''));
|
|
523
|
-
}
|
|
524
|
-
function declared(pkg, fields, dep) {
|
|
525
|
-
var _a;
|
|
526
|
-
for (const f of fields)
|
|
527
|
-
if (((_a = pkg[f]) === null || _a === void 0 ? void 0 : _a[dep]) !== undefined)
|
|
528
|
-
return pkg[f][dep];
|
|
529
|
-
return undefined;
|
|
530
|
-
}
|
|
531
|
-
/** The app's own copy of `dep`: `<appRoot>/node_modules/<dep>`, followed through a link. */
|
|
532
|
-
function appCopy(dep, deps) {
|
|
533
|
-
const dir = path.join(deps.appRoot, 'node_modules', dep);
|
|
534
|
-
const pkg = readJson(path.join(dir, 'package.json'));
|
|
535
|
-
if (!(pkg === null || pkg === void 0 ? void 0 : pkg.version))
|
|
536
|
-
return null;
|
|
537
|
-
return { version: pkg.version, real: realOrSelf(dir) };
|
|
538
|
-
}
|
|
539
|
-
/** The package dir's `node_modules`, and the clone root's when the package is a monorepo subdir. */
|
|
540
|
-
export function nodeModulesRoots(pkgDir, entry) {
|
|
541
|
-
const roots = [path.join(pkgDir, 'node_modules')];
|
|
542
|
-
if (entry.subdir) {
|
|
543
|
-
const rel = path.normalize(entry.subdir);
|
|
544
|
-
if (pkgDir.endsWith(path.sep + rel))
|
|
545
|
-
roots.push(path.join(pkgDir.slice(0, -(rel.length + 1)), 'node_modules'));
|
|
546
|
-
}
|
|
547
|
-
return roots.filter((d) => fs.existsSync(d));
|
|
548
|
-
}
|
|
549
|
-
/** Package names directly under a `node_modules`, scoped ones as `@scope/name`. */
|
|
550
|
-
function topLevelPackages(nm) {
|
|
551
|
-
const out = [];
|
|
552
|
-
let items;
|
|
553
|
-
try {
|
|
554
|
-
items = fs.readdirSync(nm, { withFileTypes: true });
|
|
555
|
-
}
|
|
556
|
-
catch (_a) {
|
|
557
|
-
return out;
|
|
558
|
-
}
|
|
559
|
-
for (const item of items) {
|
|
560
|
-
if (item.name.startsWith('.'))
|
|
561
|
-
continue;
|
|
562
|
-
if (item.name.startsWith('@')) {
|
|
563
|
-
let inner = [];
|
|
564
|
-
try {
|
|
565
|
-
inner = fs.readdirSync(path.join(nm, item.name));
|
|
566
|
-
}
|
|
567
|
-
catch (_b) {
|
|
568
|
-
/* not a directory */
|
|
569
|
-
}
|
|
570
|
-
for (const sub of inner)
|
|
571
|
-
if (!sub.startsWith('.'))
|
|
572
|
-
out.push(`${item.name}/${sub}`);
|
|
573
|
-
}
|
|
574
|
-
else {
|
|
575
|
-
out.push(item.name);
|
|
576
|
-
}
|
|
577
|
-
}
|
|
578
|
-
return out;
|
|
579
|
-
}
|
|
580
|
-
/** Links in `<nm>/.bin` that pointed into a package just removed: dangling now. */
|
|
581
|
-
function removeBinLinksInto(nm, removedDir) {
|
|
582
|
-
const bin = path.join(nm, '.bin');
|
|
583
|
-
let names;
|
|
584
|
-
try {
|
|
585
|
-
names = fs.readdirSync(bin);
|
|
586
|
-
}
|
|
587
|
-
catch (_a) {
|
|
588
|
-
return;
|
|
589
|
-
}
|
|
590
|
-
for (const name of names) {
|
|
591
|
-
const link = path.join(bin, name);
|
|
592
|
-
if (!isSymlink(link))
|
|
593
|
-
continue;
|
|
594
|
-
const target = path.resolve(bin, fs.readlinkSync(link));
|
|
595
|
-
if (isInside(target, removedDir))
|
|
596
|
-
fs.unlinkSync(link);
|
|
597
|
-
}
|
|
598
|
-
}
|
|
599
|
-
function removeEmptyScope(nm, dep) {
|
|
600
|
-
if (!dep.startsWith('@'))
|
|
601
|
-
return;
|
|
602
|
-
const scope = path.join(nm, dep.split('/')[0]);
|
|
603
|
-
try {
|
|
604
|
-
if (!fs.readdirSync(scope).length)
|
|
605
|
-
fs.rmdirSync(scope);
|
|
606
|
-
}
|
|
607
|
-
catch (_a) {
|
|
608
|
-
/* already gone */
|
|
609
|
-
}
|
|
610
|
-
}
|
|
611
|
-
export function realOrSelf(p) {
|
|
612
|
-
try {
|
|
613
|
-
return fs.realpathSync(p);
|
|
614
|
-
}
|
|
615
|
-
catch (_a) {
|
|
616
|
-
return p;
|
|
617
|
-
}
|
|
618
|
-
}
|
|
619
|
-
//# sourceMappingURL=prune.js.map
|