@theholocron/pkg-utils 1.11.0 → 1.12.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/dist/index.d.mts +69 -0
- package/dist/index.mjs +124 -0
- package/package.json +1 -1
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
//#region src/package-json.type.d.ts
|
|
2
|
+
/** The `package.json` fields these utils and their callers read; any other field passes through untyped. */
|
|
3
|
+
interface PackageJson {
|
|
4
|
+
name?: string;
|
|
5
|
+
version?: string;
|
|
6
|
+
private?: boolean;
|
|
7
|
+
packageManager?: string;
|
|
8
|
+
scripts?: Record<string, string>;
|
|
9
|
+
dependencies?: Record<string, string>;
|
|
10
|
+
devDependencies?: Record<string, string>;
|
|
11
|
+
peerDependencies?: Record<string, string>;
|
|
12
|
+
optionalDependencies?: Record<string, string>;
|
|
13
|
+
[field: string]: unknown;
|
|
14
|
+
}
|
|
15
|
+
//#endregion
|
|
16
|
+
//#region src/dependency-names.d.ts
|
|
17
|
+
export type DependencyField = "dependencies" | "devDependencies" | "peerDependencies" | "optionalDependencies";
|
|
18
|
+
/**
|
|
19
|
+
* Every package name declared in `pkg`'s dependency maps — `dependencies` and
|
|
20
|
+
* `devDependencies` unless `fields` says otherwise. An absent `pkg`, a missing
|
|
21
|
+
* map, an array or any other non-object contributes nothing.
|
|
22
|
+
*/
|
|
23
|
+
export declare function dependencyNames(pkg: PackageJson | undefined, fields?: readonly DependencyField[]): ReadonlySet<string>;
|
|
24
|
+
//#endregion
|
|
25
|
+
//#region src/detect-package-manager.d.ts
|
|
26
|
+
export type PackageManager = "pnpm" | "npm" | "yarn" | "bun";
|
|
27
|
+
export interface DetectPackageManagerOptions {
|
|
28
|
+
/** Replaces the `package.json` read — for callers with an injected or in-memory filesystem. */
|
|
29
|
+
readFile?: (path: string) => string;
|
|
30
|
+
/** Replaces the lockfile existence check. */
|
|
31
|
+
fileExists?: (path: string) => boolean;
|
|
32
|
+
/** Returned when neither `package.json` nor a lockfile says. Defaults to `"pnpm"`. */
|
|
33
|
+
fallback?: PackageManager;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* The package manager `dir` uses: the name in `package.json#packageManager`
|
|
37
|
+
* (`pnpm@10.0.0` → `pnpm`) when it's one of pnpm, npm, yarn or bun, else the
|
|
38
|
+
* first lockfile found, else `options.fallback`.
|
|
39
|
+
*/
|
|
40
|
+
export declare function detectPackageManager(dir: string, options?: DetectPackageManagerOptions): PackageManager;
|
|
41
|
+
//#endregion
|
|
42
|
+
//#region src/parse-package-json.d.ts
|
|
43
|
+
/**
|
|
44
|
+
* Parses `package.json` text. Returns `undefined` for anything that isn't a
|
|
45
|
+
* JSON object — invalid JSON, or valid JSON of another type (an array, `null`,
|
|
46
|
+
* a string) — rather than throwing. A leading byte-order mark is ignored, as
|
|
47
|
+
* npm does.
|
|
48
|
+
*
|
|
49
|
+
* The fields {@link PackageJson} types are checked, so the result honours its
|
|
50
|
+
* type: a recognized field of the wrong type (a numeric `name`, an array
|
|
51
|
+
* `dependencies`) is left out, and the string-map fields keep only their
|
|
52
|
+
* string-valued entries. Every other field passes through untouched.
|
|
53
|
+
*/
|
|
54
|
+
export declare function parsePackageJson(text: string): PackageJson | undefined;
|
|
55
|
+
//#endregion
|
|
56
|
+
//#region src/read-package-json.d.ts
|
|
57
|
+
export interface ReadPackageJsonOptions {
|
|
58
|
+
/** Replaces the file read — for callers with an injected or in-memory filesystem. */
|
|
59
|
+
readFile?: (path: string) => string;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* The parsed `<dir>/package.json`, or `undefined` when it is missing,
|
|
63
|
+
* unreadable or not a JSON object.
|
|
64
|
+
*/
|
|
65
|
+
export declare function readPackageJson(dir: string): Promise<PackageJson | undefined>;
|
|
66
|
+
/** Synchronous twin of {@link readPackageJson}, with an injectable `readFile`. */
|
|
67
|
+
export declare function readPackageJsonSync(dir: string, options?: ReadPackageJsonOptions): PackageJson | undefined;
|
|
68
|
+
//#endregion
|
|
69
|
+
export type { PackageJson };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
import { readFile } from "node:fs/promises";
|
|
4
|
+
//#region src/dependency-names.ts
|
|
5
|
+
/**
|
|
6
|
+
* Every package name declared in `pkg`'s dependency maps — `dependencies` and
|
|
7
|
+
* `devDependencies` unless `fields` says otherwise. An absent `pkg`, a missing
|
|
8
|
+
* map, an array or any other non-object contributes nothing.
|
|
9
|
+
*/
|
|
10
|
+
function dependencyNames(pkg, fields = ["dependencies", "devDependencies"]) {
|
|
11
|
+
const names = /* @__PURE__ */ new Set();
|
|
12
|
+
for (const field of fields) {
|
|
13
|
+
const map = pkg?.[field];
|
|
14
|
+
if (typeof map === "object" && map !== null && !Array.isArray(map)) for (const name of Object.keys(map)) names.add(name);
|
|
15
|
+
}
|
|
16
|
+
return names;
|
|
17
|
+
}
|
|
18
|
+
//#endregion
|
|
19
|
+
//#region src/parse-package-json.ts
|
|
20
|
+
const STRING_FIELDS = [
|
|
21
|
+
"name",
|
|
22
|
+
"version",
|
|
23
|
+
"packageManager"
|
|
24
|
+
];
|
|
25
|
+
const MAP_FIELDS = [
|
|
26
|
+
"scripts",
|
|
27
|
+
"dependencies",
|
|
28
|
+
"devDependencies",
|
|
29
|
+
"peerDependencies",
|
|
30
|
+
"optionalDependencies"
|
|
31
|
+
];
|
|
32
|
+
/** A plain object's string-valued entries; `undefined` for anything that isn't a plain object. */
|
|
33
|
+
function stringMap(value) {
|
|
34
|
+
if (typeof value !== "object" || value === null || Array.isArray(value)) return void 0;
|
|
35
|
+
return Object.fromEntries(Object.entries(value).filter((entry) => typeof entry[1] === "string"));
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Parses `package.json` text. Returns `undefined` for anything that isn't a
|
|
39
|
+
* JSON object — invalid JSON, or valid JSON of another type (an array, `null`,
|
|
40
|
+
* a string) — rather than throwing. A leading byte-order mark is ignored, as
|
|
41
|
+
* npm does.
|
|
42
|
+
*
|
|
43
|
+
* The fields {@link PackageJson} types are checked, so the result honours its
|
|
44
|
+
* type: a recognized field of the wrong type (a numeric `name`, an array
|
|
45
|
+
* `dependencies`) is left out, and the string-map fields keep only their
|
|
46
|
+
* string-valued entries. Every other field passes through untouched.
|
|
47
|
+
*/
|
|
48
|
+
function parsePackageJson(text) {
|
|
49
|
+
let value;
|
|
50
|
+
try {
|
|
51
|
+
value = JSON.parse(text.charCodeAt(0) === 65279 ? text.slice(1) : text);
|
|
52
|
+
} catch {
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
if (typeof value !== "object" || value === null || Array.isArray(value)) return void 0;
|
|
56
|
+
const raw = value;
|
|
57
|
+
const pkg = { ...raw };
|
|
58
|
+
for (const field of STRING_FIELDS) if (typeof raw[field] !== "string") delete pkg[field];
|
|
59
|
+
if (typeof raw["private"] !== "boolean") delete pkg["private"];
|
|
60
|
+
for (const field of MAP_FIELDS) {
|
|
61
|
+
const map = stringMap(raw[field]);
|
|
62
|
+
if (map) pkg[field] = map;
|
|
63
|
+
else delete pkg[field];
|
|
64
|
+
}
|
|
65
|
+
return pkg;
|
|
66
|
+
}
|
|
67
|
+
//#endregion
|
|
68
|
+
//#region src/detect-package-manager.ts
|
|
69
|
+
const MANAGERS = [
|
|
70
|
+
"pnpm",
|
|
71
|
+
"npm",
|
|
72
|
+
"yarn",
|
|
73
|
+
"bun"
|
|
74
|
+
];
|
|
75
|
+
/** Checked in this order; the first lockfile present wins. */
|
|
76
|
+
const LOCKFILES = [
|
|
77
|
+
["pnpm-lock.yaml", "pnpm"],
|
|
78
|
+
["bun.lockb", "bun"],
|
|
79
|
+
["bun.lock", "bun"],
|
|
80
|
+
["yarn.lock", "yarn"],
|
|
81
|
+
["npm-shrinkwrap.json", "npm"],
|
|
82
|
+
["package-lock.json", "npm"]
|
|
83
|
+
];
|
|
84
|
+
/**
|
|
85
|
+
* The package manager `dir` uses: the name in `package.json#packageManager`
|
|
86
|
+
* (`pnpm@10.0.0` → `pnpm`) when it's one of pnpm, npm, yarn or bun, else the
|
|
87
|
+
* first lockfile found, else `options.fallback`.
|
|
88
|
+
*/
|
|
89
|
+
function detectPackageManager(dir, options = {}) {
|
|
90
|
+
const read = options.readFile ?? ((path) => readFileSync(path, "utf8"));
|
|
91
|
+
const exists = options.fileExists ?? existsSync;
|
|
92
|
+
let declared;
|
|
93
|
+
try {
|
|
94
|
+
const field = parsePackageJson(read(join(dir, "package.json")))?.packageManager;
|
|
95
|
+
declared = typeof field === "string" ? field.split("@")[0] : void 0;
|
|
96
|
+
} catch {}
|
|
97
|
+
if (declared && MANAGERS.includes(declared)) return declared;
|
|
98
|
+
for (const [file, manager] of LOCKFILES) if (exists(join(dir, file))) return manager;
|
|
99
|
+
return options.fallback ?? "pnpm";
|
|
100
|
+
}
|
|
101
|
+
//#endregion
|
|
102
|
+
//#region src/read-package-json.ts
|
|
103
|
+
/**
|
|
104
|
+
* The parsed `<dir>/package.json`, or `undefined` when it is missing,
|
|
105
|
+
* unreadable or not a JSON object.
|
|
106
|
+
*/
|
|
107
|
+
async function readPackageJson(dir) {
|
|
108
|
+
try {
|
|
109
|
+
return parsePackageJson(await readFile(join(dir, "package.json"), "utf8"));
|
|
110
|
+
} catch {
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
/** Synchronous twin of {@link readPackageJson}, with an injectable `readFile`. */
|
|
115
|
+
function readPackageJsonSync(dir, options = {}) {
|
|
116
|
+
const read = options.readFile ?? ((path) => readFileSync(path, "utf8"));
|
|
117
|
+
try {
|
|
118
|
+
return parsePackageJson(read(join(dir, "package.json")));
|
|
119
|
+
} catch {
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
//#endregion
|
|
124
|
+
export { dependencyNames, detectPackageManager, parsePackageJson, readPackageJson, readPackageJsonSync };
|