@packall/core 0.1.0 → 0.2.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/LICENSE +21 -0
- package/README.md +58 -0
- package/dist/archive-6sqTn1C4.js +274 -0
- package/dist/archive-6sqTn1C4.js.map +1 -0
- package/dist/archive.d.ts +47 -15
- package/dist/archive.d.ts.map +1 -1
- package/dist/bundle.d.ts +6 -3
- package/dist/bundle.d.ts.map +1 -1
- package/dist/dependency-range.d.ts.map +1 -1
- package/dist/download.d.ts +2 -1
- package/dist/download.d.ts.map +1 -1
- package/dist/enums/artifact-kind.d.ts.map +1 -1
- package/dist/enums/edge-kind.d.ts.map +1 -1
- package/dist/enums/input-file-kind.d.ts.map +1 -1
- package/dist/enums/layout.d.ts.map +1 -1
- package/dist/enums/lockfile-format.d.ts.map +1 -1
- package/dist/enums/phase.d.ts.map +1 -1
- package/dist/errors.d.ts.map +1 -1
- package/dist/index.d.ts +12 -10
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +169 -344
- package/dist/index.js.map +1 -1
- package/dist/input-file.d.ts.map +1 -1
- package/dist/integrity.d.ts +37 -5
- package/dist/integrity.d.ts.map +1 -1
- package/dist/layout.d.ts +41 -7
- package/dist/layout.d.ts.map +1 -1
- package/dist/locked-resolve.d.ts.map +1 -1
- package/dist/lockfile/bun.d.ts.map +1 -1
- package/dist/lockfile/detect.d.ts.map +1 -1
- package/dist/lockfile/json.d.ts.map +1 -1
- package/dist/lockfile/names.d.ts.map +1 -1
- package/dist/lockfile/npm.d.ts.map +1 -1
- package/dist/lockfile/parse.d.ts.map +1 -1
- package/dist/lockfile/pnpm.d.ts.map +1 -1
- package/dist/lockfile/tree-builder.d.ts.map +1 -1
- package/dist/lockfile/types.d.ts.map +1 -1
- package/dist/manifest.d.ts.map +1 -1
- package/dist/node/archiver.d.ts +5 -0
- package/dist/node/archiver.d.ts.map +1 -0
- package/dist/node/index.d.ts +13 -0
- package/dist/node/index.d.ts.map +1 -0
- package/dist/node/index.js +55 -0
- package/dist/node/index.js.map +1 -0
- package/dist/options.d.ts +3 -3
- package/dist/options.d.ts.map +1 -1
- package/dist/platform.d.ts +5 -10
- package/dist/platform.d.ts.map +1 -1
- package/dist/progress.d.ts.map +1 -1
- package/dist/registry.d.ts.map +1 -1
- package/dist/resolve.d.ts.map +1 -1
- package/dist/schemas/lenient.d.ts.map +1 -1
- package/dist/schemas/package-json.d.ts.map +1 -1
- package/dist/spec.d.ts.map +1 -1
- package/dist/types/value-of.d.ts.map +1 -1
- package/dist/utils/is-record.d.ts.map +1 -1
- package/package.json +11 -10
- package/src/archive.ts +95 -92
- package/src/bundle.ts +736 -616
- package/src/dependency-range.ts +121 -115
- package/src/download.ts +155 -133
- package/src/enums/artifact-kind.ts +5 -5
- package/src/enums/edge-kind.ts +13 -13
- package/src/enums/input-file-kind.ts +9 -9
- package/src/enums/layout.ts +20 -20
- package/src/enums/lockfile-format.ts +6 -6
- package/src/enums/phase.ts +8 -8
- package/src/errors.ts +200 -198
- package/src/index.ts +126 -113
- package/src/input-file.ts +246 -234
- package/src/integrity.ts +165 -121
- package/src/layout.ts +63 -29
- package/src/locked-resolve.ts +471 -461
- package/src/lockfile/bun.ts +214 -207
- package/src/lockfile/detect.ts +80 -80
- package/src/lockfile/json.ts +91 -92
- package/src/lockfile/names.ts +9 -9
- package/src/lockfile/npm.ts +328 -308
- package/src/lockfile/parse.ts +32 -32
- package/src/lockfile/pnpm.ts +230 -225
- package/src/lockfile/tree-builder.ts +116 -116
- package/src/lockfile/types.ts +52 -52
- package/src/manifest.ts +129 -133
- package/src/node/archiver.ts +85 -0
- package/src/node/index.ts +13 -0
- package/src/options.ts +84 -84
- package/src/platform.ts +97 -101
- package/src/progress.ts +105 -106
- package/src/registry.ts +116 -115
- package/src/resolve.ts +560 -537
- package/src/schemas/lenient.ts +67 -60
- package/src/schemas/package-json.ts +11 -10
- package/src/spec.ts +123 -119
- package/src/types/value-of.ts +1 -1
- package/src/utils/is-record.ts +1 -1
package/src/lockfile/bun.ts
CHANGED
|
@@ -1,207 +1,214 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* `bun.lock`.
|
|
3
|
-
*
|
|
4
|
-
* Like npm, bun records ranges on its edges and resolves them through the
|
|
5
|
-
* install-path nesting — but it nests with a plain `/`, and package names
|
|
6
|
-
* contain `/` too, so the parent of a key can only be found by asking which
|
|
7
|
-
* prefixes are themselves entries.
|
|
8
|
-
*/
|
|
9
|
-
import { EdgeKind, LockedRootKind } from "../enums/edge-kind.js"
|
|
10
|
-
import { LockfileFormat } from "../enums/lockfile-format.js"
|
|
11
|
-
import {
|
|
12
|
-
import {
|
|
13
|
-
import {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
const
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
const
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
1
|
+
/**
|
|
2
|
+
* `bun.lock`.
|
|
3
|
+
*
|
|
4
|
+
* Like npm, bun records ranges on its edges and resolves them through the
|
|
5
|
+
* install-path nesting — but it nests with a plain `/`, and package names
|
|
6
|
+
* contain `/` too, so the parent of a key can only be found by asking which
|
|
7
|
+
* prefixes are themselves entries.
|
|
8
|
+
*/
|
|
9
|
+
import { EdgeKind, LockedRootKind } from "../enums/edge-kind.js";
|
|
10
|
+
import { LockfileFormat } from "../enums/lockfile-format.js";
|
|
11
|
+
import { isRecord } from "../utils/is-record.js";
|
|
12
|
+
import { optionalPeerNames, parseJsonObject, stringRecord } from "./json.js";
|
|
13
|
+
import {
|
|
14
|
+
asExactVersion,
|
|
15
|
+
fieldByRootKind,
|
|
16
|
+
importerKey,
|
|
17
|
+
notCovered,
|
|
18
|
+
TreeBuilder,
|
|
19
|
+
} from "./tree-builder.js";
|
|
20
|
+
import type { LockedEdge, LockedTree } from "./types.js";
|
|
21
|
+
|
|
22
|
+
/** One narrowed `packages` entry, keyed by its install path. */
|
|
23
|
+
type BunEntry = {
|
|
24
|
+
readonly key: string;
|
|
25
|
+
readonly name: string;
|
|
26
|
+
readonly version: string;
|
|
27
|
+
readonly dependencies: Readonly<Record<string, string>>;
|
|
28
|
+
readonly optionalDependencies: Readonly<Record<string, string>>;
|
|
29
|
+
readonly peerDependencies: Readonly<Record<string, string>>;
|
|
30
|
+
readonly optionalPeers: ReadonlySet<string>;
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
/** Root blocks in bun's precedence order — the first mention of a name wins. */
|
|
34
|
+
const ROOT_KINDS = [
|
|
35
|
+
LockedRootKind.Prod,
|
|
36
|
+
LockedRootKind.Optional,
|
|
37
|
+
LockedRootKind.Dev,
|
|
38
|
+
LockedRootKind.Peer,
|
|
39
|
+
] as const;
|
|
40
|
+
|
|
41
|
+
export const parseBunLockfile = (
|
|
42
|
+
path: string,
|
|
43
|
+
content: string,
|
|
44
|
+
importer: string | undefined,
|
|
45
|
+
): LockedTree => {
|
|
46
|
+
const root = parseJsonObject(path, content);
|
|
47
|
+
const version = String(root["lockfileVersion"] ?? "?");
|
|
48
|
+
const builder = new TreeBuilder();
|
|
49
|
+
|
|
50
|
+
const packages = root["packages"];
|
|
51
|
+
const entries = isRecord(packages)
|
|
52
|
+
? collectEntries(builder, packages)
|
|
53
|
+
: new Map<string, BunEntry>();
|
|
54
|
+
|
|
55
|
+
const keys = new Set(entries.keys());
|
|
56
|
+
for (const entry of entries.values()) {
|
|
57
|
+
builder.add({
|
|
58
|
+
name: entry.name,
|
|
59
|
+
version: entry.version,
|
|
60
|
+
dependencies: edgesOf(builder, entries, keys, entry),
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
collectRoots(builder, entries, keys, root["workspaces"], path, importer);
|
|
65
|
+
|
|
66
|
+
return builder.finish({ format: LockfileFormat.Bun, lockfileVersion: version, path, importer });
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Narrows bun's tuple entries.
|
|
71
|
+
*
|
|
72
|
+
* The value is a positional array whose shape depends on where the package came
|
|
73
|
+
* from. A registry package is `[id, registry, info, integrity]`; a workspace
|
|
74
|
+
* member or a git dependency is shorter and its `id` carries the protocol.
|
|
75
|
+
*/
|
|
76
|
+
const collectEntries = (
|
|
77
|
+
builder: TreeBuilder,
|
|
78
|
+
packages: Record<string, unknown>,
|
|
79
|
+
): Map<string, BunEntry> => {
|
|
80
|
+
const entries = new Map<string, BunEntry>();
|
|
81
|
+
|
|
82
|
+
for (const [key, raw] of Object.entries(packages)) {
|
|
83
|
+
if (!Array.isArray(raw)) continue;
|
|
84
|
+
|
|
85
|
+
const [id, , info] = raw;
|
|
86
|
+
if (typeof id !== "string") continue;
|
|
87
|
+
|
|
88
|
+
const at = id.lastIndexOf("@");
|
|
89
|
+
if (at <= 0) continue;
|
|
90
|
+
const name = id.slice(0, at);
|
|
91
|
+
const spec = id.slice(at + 1);
|
|
92
|
+
|
|
93
|
+
const version = asExactVersion(name, spec);
|
|
94
|
+
if (version._tag === "Unsupported") {
|
|
95
|
+
builder.skip(name, spec, version.reason);
|
|
96
|
+
continue;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
const details = isRecord(info) ? info : {};
|
|
100
|
+
|
|
101
|
+
entries.set(key, {
|
|
102
|
+
key,
|
|
103
|
+
name,
|
|
104
|
+
version: version.version,
|
|
105
|
+
dependencies: stringRecord(details["dependencies"]),
|
|
106
|
+
optionalDependencies: stringRecord(details["optionalDependencies"]),
|
|
107
|
+
peerDependencies: stringRecord(details["peerDependencies"]),
|
|
108
|
+
optionalPeers: collectOptionalPeers(details),
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
return entries;
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
/** bun lists optional peers as a name array rather than npm's metadata object. */
|
|
116
|
+
const collectOptionalPeers = (details: Record<string, unknown>): ReadonlySet<string> => {
|
|
117
|
+
const listed = details["optionalPeers"];
|
|
118
|
+
const names = Array.isArray(listed)
|
|
119
|
+
? listed.flatMap((entry) => (typeof entry === "string" ? entry : []))
|
|
120
|
+
: [];
|
|
121
|
+
return new Set([...names, ...optionalPeerNames(details["peerDependenciesMeta"])]);
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
const edgesOf = (
|
|
125
|
+
builder: TreeBuilder,
|
|
126
|
+
entries: ReadonlyMap<string, BunEntry>,
|
|
127
|
+
keys: ReadonlySet<string>,
|
|
128
|
+
entry: BunEntry,
|
|
129
|
+
): ReadonlyArray<LockedEdge> => {
|
|
130
|
+
const edges: Array<LockedEdge> = [];
|
|
131
|
+
|
|
132
|
+
const add = (names: ReadonlyArray<string>, kind: EdgeKind): void => {
|
|
133
|
+
for (const name of names) {
|
|
134
|
+
const target = findEntry(entries, keys, entry.key, name);
|
|
135
|
+
if (target === undefined) {
|
|
136
|
+
if (kind === EdgeKind.Prod)
|
|
137
|
+
builder.dangling(`${entry.name}@${entry.version}`, name);
|
|
138
|
+
continue;
|
|
139
|
+
}
|
|
140
|
+
edges.push({ name: target.name, version: target.version, kind });
|
|
141
|
+
}
|
|
142
|
+
};
|
|
143
|
+
|
|
144
|
+
add(Object.keys(entry.dependencies), EdgeKind.Prod);
|
|
145
|
+
add(Object.keys(entry.optionalDependencies), EdgeKind.Optional);
|
|
146
|
+
add(
|
|
147
|
+
Object.keys(entry.peerDependencies).filter((name) => !entry.optionalPeers.has(name)),
|
|
148
|
+
EdgeKind.Peer,
|
|
149
|
+
);
|
|
150
|
+
|
|
151
|
+
return edges;
|
|
152
|
+
};
|
|
153
|
+
|
|
154
|
+
const collectRoots = (
|
|
155
|
+
builder: TreeBuilder,
|
|
156
|
+
entries: ReadonlyMap<string, BunEntry>,
|
|
157
|
+
keys: ReadonlySet<string>,
|
|
158
|
+
workspaces: unknown,
|
|
159
|
+
path: string,
|
|
160
|
+
member: string | undefined,
|
|
161
|
+
): void => {
|
|
162
|
+
if (!isRecord(workspaces)) return;
|
|
163
|
+
|
|
164
|
+
const selected =
|
|
165
|
+
member === undefined
|
|
166
|
+
? Object.values(workspaces)
|
|
167
|
+
: Object.hasOwn(workspaces, importerKey(member))
|
|
168
|
+
? [workspaces[importerKey(member)]]
|
|
169
|
+
: notCovered(path, member, workspacePaths(workspaces));
|
|
170
|
+
|
|
171
|
+
for (const workspace of selected) {
|
|
172
|
+
if (!isRecord(workspace)) continue;
|
|
173
|
+
for (const kind of ROOT_KINDS) {
|
|
174
|
+
const declared = stringRecord(workspace[fieldByRootKind[kind]]);
|
|
175
|
+
for (const [name, specifier] of Object.entries(declared)) {
|
|
176
|
+
const target = findEntry(entries, keys, "", name);
|
|
177
|
+
if (target === undefined) continue;
|
|
178
|
+
builder.addRoot({ name: target.name, version: target.version, kind, specifier });
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
};
|
|
183
|
+
|
|
184
|
+
const workspacePaths = (workspaces: Record<string, unknown>): ReadonlyArray<string> =>
|
|
185
|
+
Object.keys(workspaces).map((key) => (key === "" ? "." : key));
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* bun nests with a plain `/`, and package names contain `/` too, so a parent
|
|
189
|
+
* key cannot be found by string-splitting alone — `@babel/core` would split
|
|
190
|
+
* into a scope that is not a key. `parentKey` only accepts a prefix that is
|
|
191
|
+
* itself an entry, which resolves the ambiguity.
|
|
192
|
+
*/
|
|
193
|
+
const findEntry = (
|
|
194
|
+
entries: ReadonlyMap<string, BunEntry>,
|
|
195
|
+
keys: ReadonlySet<string>,
|
|
196
|
+
fromKey: string,
|
|
197
|
+
name: string,
|
|
198
|
+
): BunEntry | undefined => {
|
|
199
|
+
let scope = fromKey;
|
|
200
|
+
for (;;) {
|
|
201
|
+
const found = entries.get(scope === "" ? name : `${scope}/${name}`);
|
|
202
|
+
if (found !== undefined) return found;
|
|
203
|
+
if (scope === "") return undefined;
|
|
204
|
+
scope = parentKey(keys, scope);
|
|
205
|
+
}
|
|
206
|
+
};
|
|
207
|
+
|
|
208
|
+
const parentKey = (keys: ReadonlySet<string>, key: string): string => {
|
|
209
|
+
for (let at = key.lastIndexOf("/"); at > 0; at = key.lastIndexOf("/", at - 1)) {
|
|
210
|
+
const prefix = key.slice(0, at);
|
|
211
|
+
if (keys.has(prefix)) return prefix;
|
|
212
|
+
}
|
|
213
|
+
return "";
|
|
214
|
+
};
|
package/src/lockfile/detect.ts
CHANGED
|
@@ -1,80 +1,80 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Working out which lockfile — if any — some content is.
|
|
3
|
-
*
|
|
4
|
-
* Detection is by content, matching how `--file` treats everything else: a
|
|
5
|
-
* lockfile renamed to `deps.txt` still parses, and a `package-lock.json` that
|
|
6
|
-
* is actually a package.json still gets read as one.
|
|
7
|
-
*/
|
|
8
|
-
import { LockfileFormat } from "../enums/lockfile-format.js"
|
|
9
|
-
import {
|
|
10
|
-
import {
|
|
11
|
-
|
|
12
|
-
/** What a blob of file content turned out to be. */
|
|
13
|
-
export type LockfileDetection =
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* Deliberately cheap and total: it never throws, so `--file` can try this
|
|
25
|
-
* first and fall through to its other shapes when the answer is `NotALockfile`.
|
|
26
|
-
*/
|
|
27
|
-
export const detectLockfile = (content: string): LockfileDetection => {
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
/**
|
|
60
|
-
* Separates the two JSON lockfiles from each other and from a package.json.
|
|
61
|
-
*
|
|
62
|
-
* Both npm and bun write a numeric `lockfileVersion`, so that field alone
|
|
63
|
-
* cannot decide it. bun is the one that carries a `workspaces` map keyed by the
|
|
64
|
-
* empty string; npm keeps the root project under `packages[""]` instead.
|
|
65
|
-
*/
|
|
66
|
-
const detectJsonLockfile = (trimmed: string): LockfileDetection => {
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* Working out which lockfile — if any — some content is.
|
|
3
|
+
*
|
|
4
|
+
* Detection is by content, matching how `--file` treats everything else: a
|
|
5
|
+
* lockfile renamed to `deps.txt` still parses, and a `package-lock.json` that
|
|
6
|
+
* is actually a package.json still gets read as one.
|
|
7
|
+
*/
|
|
8
|
+
import { LockfileFormat } from "../enums/lockfile-format.js";
|
|
9
|
+
import { isRecord } from "../utils/is-record.js";
|
|
10
|
+
import { tryParseJsonc } from "./json.js";
|
|
11
|
+
|
|
12
|
+
/** What a blob of file content turned out to be. */
|
|
13
|
+
export type LockfileDetection =
|
|
14
|
+
| { readonly _tag: "Supported"; readonly format: LockfileFormat }
|
|
15
|
+
/**
|
|
16
|
+
* Recognisably a lockfile, but not one we can read. Named explicitly so the
|
|
17
|
+
* error says "yarn.lock is not supported yet" rather than leaving somebody to
|
|
18
|
+
* work out why their file parsed as a spec list.
|
|
19
|
+
*/
|
|
20
|
+
| { readonly _tag: "Unsupported"; readonly label: string; readonly hint: string }
|
|
21
|
+
| { readonly _tag: "NotALockfile" };
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Deliberately cheap and total: it never throws, so `--file` can try this
|
|
25
|
+
* first and fall through to its other shapes when the answer is `NotALockfile`.
|
|
26
|
+
*/
|
|
27
|
+
export const detectLockfile = (content: string): LockfileDetection => {
|
|
28
|
+
const trimmed = content.trimStart();
|
|
29
|
+
|
|
30
|
+
// bun's binary lockfile leads with its own magic string. Worth naming,
|
|
31
|
+
// because `bun install` wrote this by default until 1.2 and the fix is one
|
|
32
|
+
// command.
|
|
33
|
+
if (trimmed.startsWith("bun-lockfile-format-v0")) {
|
|
34
|
+
return {
|
|
35
|
+
_tag: "Unsupported",
|
|
36
|
+
label: "a binary bun lockfile (bun.lockb)",
|
|
37
|
+
hint: "Run `bun install --save-text-lockfile` to produce a bun.lock, and pass that instead.",
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
if (/^__metadata:/m.test(trimmed) || /^# yarn lockfile v\d/m.test(trimmed)) {
|
|
42
|
+
return {
|
|
43
|
+
_tag: "Unsupported",
|
|
44
|
+
label: "a yarn lockfile",
|
|
45
|
+
hint: "yarn is not supported yet. Point --file at the package.json instead, or pass --lockfile off.",
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
if (trimmed.startsWith("{")) return detectJsonLockfile(trimmed);
|
|
50
|
+
|
|
51
|
+
// pnpm is the only YAML one, and it always declares its version first.
|
|
52
|
+
if (/^lockfileVersion:/m.test(trimmed) || /^(importers|snapshots):/m.test(trimmed)) {
|
|
53
|
+
return { _tag: "Supported", format: LockfileFormat.Pnpm };
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
return { _tag: "NotALockfile" };
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Separates the two JSON lockfiles from each other and from a package.json.
|
|
61
|
+
*
|
|
62
|
+
* Both npm and bun write a numeric `lockfileVersion`, so that field alone
|
|
63
|
+
* cannot decide it. bun is the one that carries a `workspaces` map keyed by the
|
|
64
|
+
* empty string; npm keeps the root project under `packages[""]` instead.
|
|
65
|
+
*/
|
|
66
|
+
const detectJsonLockfile = (trimmed: string): LockfileDetection => {
|
|
67
|
+
const parsed = tryParseJsonc(trimmed);
|
|
68
|
+
if (!isRecord(parsed)) return { _tag: "NotALockfile" };
|
|
69
|
+
|
|
70
|
+
const workspaces = parsed["workspaces"];
|
|
71
|
+
if (isRecord(workspaces) && Object.hasOwn(workspaces, "")) {
|
|
72
|
+
return { _tag: "Supported", format: LockfileFormat.Bun };
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
if (typeof parsed["lockfileVersion"] === "number") {
|
|
76
|
+
return { _tag: "Supported", format: LockfileFormat.Npm };
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
return { _tag: "NotALockfile" };
|
|
80
|
+
};
|