@visulima/package 5.0.0-alpha.11 → 5.0.0-alpha.13

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.
@@ -1,70 +1,71 @@
1
1
  /**
2
- * An asynchronous function that finds a lock file in the specified directory or any of its parent directories.
3
- * @param cwd Optional. The directory path to start the search from. The type of `cwd` is part of an `Options` type,
4
- * specifically `URL | string`. Defaults to the current working directory.
5
- * @returns A `Promise` that resolves with the path of the found lock file.
6
- * The type of the returned promise is `Promise<string>`.
7
- * @throws An `Error` if no lock file is found.
8
- */
9
- export declare const findLockFile: (cwd?: URL | string) => Promise<string>;
10
- export declare const findLockFileSync: (cwd?: URL | string) => string;
11
- export type PackageManager = "bun" | "npm" | "pnpm" | "yarn";
12
- export type PackageManagerResult = {
13
- packageManager: PackageManager;
14
- path: string;
2
+ * An asynchronous function that finds a lock file in the specified directory or any of its parent directories.
3
+ * @param cwd Optional. The directory path to start the search from. The type of `cwd` is part of an `Options` type,
4
+ * specifically `URL | string`. Defaults to the current working directory.
5
+ * @returns A `Promise` that resolves with the path of the found lock file.
6
+ * The type of the returned promise is `Promise&lt;string>`.
7
+ * @throws An `Error` if no lock file is found.
8
+ */
9
+ declare const findLockFile: (cwd?: URL | string) => Promise<string>;
10
+ declare const findLockFileSync: (cwd?: URL | string) => string;
11
+ type PackageManager = "bun" | "npm" | "pnpm" | "yarn";
12
+ type PackageManagerResult = {
13
+ packageManager: PackageManager;
14
+ path: string;
15
15
  };
16
16
  /**
17
- * An asynchronous function that finds the package manager used in a project based on the presence of lock files
18
- * or package.json configuration. If found, it returns the package manager and the path to the lock file or package.json.
19
- * Throws an error if no lock file or package.json is found.
20
- * @param cwd Optional. The current working directory to start the search from. The type of `cwd` is part of an `Options`
21
- * type, specifically `URL | string`.
22
- * @returns A `Promise` that resolves to an object containing the package manager and path.
23
- * The return type of the function is `Promise&lt;PackageManagerResult>`.
24
- * @throws An `Error` if no lock file or package.json is found.
25
- */
26
- export declare const findPackageManager: (cwd?: URL | string) => Promise<PackageManagerResult>;
17
+ * An asynchronous function that finds the package manager used in a project based on the presence of lock files
18
+ * or package.json configuration. If found, it returns the package manager and the path to the lock file or package.json.
19
+ * Throws an error if no lock file or package.json is found.
20
+ * @param cwd Optional. The current working directory to start the search from. The type of `cwd` is part of an `Options`
21
+ * type, specifically `URL | string`.
22
+ * @returns A `Promise` that resolves to an object containing the package manager and path.
23
+ * The return type of the function is `Promise&lt;PackageManagerResult>`.
24
+ * @throws An `Error` if no lock file or package.json is found.
25
+ */
26
+ declare const findPackageManager: (cwd?: URL | string) => Promise<PackageManagerResult>;
27
27
  /**
28
- * An function that finds the package manager used in a project based on the presence of lock files
29
- * or package.json configuration. If found, it returns the package manager and the path to the lock file or package.json.
30
- * Throws an error if no lock file or package.json is found.
31
- * @param cwd Optional. The current working directory to start the search from. The type of `cwd` is part of an `Options`
32
- * type, specifically `URL | string`.
33
- * @returns A `Promise` that resolves to an object containing the package manager and path.
34
- * The return type of the function is `Promise&lt;PackageManagerResult>`.
35
- * @throws An `Error` if no lock file or package.json is found.
36
- */
37
- export declare const findPackageManagerSync: (cwd?: URL | string) => PackageManagerResult;
28
+ * An function that finds the package manager used in a project based on the presence of lock files
29
+ * or package.json configuration. If found, it returns the package manager and the path to the lock file or package.json.
30
+ * Throws an error if no lock file or package.json is found.
31
+ * @param cwd Optional. The current working directory to start the search from. The type of `cwd` is part of an `Options`
32
+ * type, specifically `URL | string`.
33
+ * @returns A `Promise` that resolves to an object containing the package manager and path.
34
+ * The return type of the function is `Promise&lt;PackageManagerResult>`.
35
+ * @throws An `Error` if no lock file or package.json is found.
36
+ */
37
+ declare const findPackageManagerSync: (cwd?: URL | string) => PackageManagerResult;
38
38
  /**
39
- * Function that retrieves the version of the specified package manager.
40
- * @param name The name of the package manager. The type of `name` is `string`.
41
- * @returns The version of the package manager. The return type of the function is `string`.
42
- */
43
- export declare const getPackageManagerVersion: (name: string) => string;
39
+ * Function that retrieves the version of the specified package manager.
40
+ * @param name The name of the package manager. The type of `name` is `string`.
41
+ * @returns The version of the package manager. The return type of the function is `string`.
42
+ */
43
+ declare const getPackageManagerVersion: (name: string) => string;
44
44
  /**
45
- * An asynchronous function that detects what package manager executes the process.
46
- *
47
- * Supports npm, pnpm, Yarn, cnpm, and bun. And also any other package manager that sets the npm_config_user_agent env variable.
48
- * @returns An object containing the name and version of the package manager,
49
- * or undefined if the package manager information cannot be determined.
50
- */
51
- export declare const identifyInitiatingPackageManager: () => {
52
- name: PackageManager | "cnpm";
53
- version: string;
45
+ * An asynchronous function that detects what package manager executes the process.
46
+ *
47
+ * Supports npm, pnpm, Yarn, cnpm, and bun. And also any other package manager that sets the npm_config_user_agent env variable.
48
+ * @returns An object containing the name and version of the package manager,
49
+ * or undefined if the package manager information cannot be determined.
50
+ */
51
+ declare const identifyInitiatingPackageManager: () => {
52
+ name: PackageManager | "cnpm";
53
+ version: string;
54
54
  } | undefined;
55
55
  /**
56
- * Function that generates a message to install missing packages.
57
- * @param packageName The name of the package that requires the missing packages.
58
- * @param missingPackages An array of missing package names.
59
- * @param options An object containing optional parameters:
60
- * @param options.packageManagers An array of package managers to include in the message. Defaults to \["npm", "pnpm", "yarn"\].
61
- * @param options.postMessage A string to append to the end of the message.
62
- * @param options.preMessage A string to prepend to the beginning of the message.
63
- * @returns A string message with instructions to install the missing packages using the specified package managers.
64
- * @throws An `Error` if no package managers are provided in the options.
65
- */
66
- export declare const generateMissingPackagesInstallMessage: (packageName: string, missingPackages: string[], options: {
67
- packageManagers?: PackageManager[];
68
- postMessage?: string;
69
- preMessage?: string;
56
+ * Function that generates a message to install missing packages.
57
+ * @param packageName The name of the package that requires the missing packages.
58
+ * @param missingPackages An array of missing package names.
59
+ * @param options An object containing optional parameters:
60
+ * @param options.packageManagers An array of package managers to include in the message. Defaults to \["npm", "pnpm", "yarn"\].
61
+ * @param options.postMessage A string to append to the end of the message.
62
+ * @param options.preMessage A string to prepend to the beginning of the message.
63
+ * @returns A string message with instructions to install the missing packages using the specified package managers.
64
+ * @throws An `Error` if no package managers are provided in the options.
65
+ */
66
+ declare const generateMissingPackagesInstallMessage: (packageName: string, missingPackages: string[], options: {
67
+ packageManagers?: PackageManager[];
68
+ postMessage?: string;
69
+ preMessage?: string;
70
70
  }) => string;
71
+ export { PackageManager, PackageManagerResult, findLockFile, findLockFileSync, findPackageManager, findPackageManagerSync, generateMissingPackagesInstallMessage, getPackageManagerVersion, identifyInitiatingPackageManager };
@@ -1,190 +1,13 @@
1
- import { createRequire as __cjs_createRequire } from "node:module";
1
+ var b=Object.defineProperty;var g=(e,a)=>b(e,"name",{value:a,configurable:!0});import{createRequire as P}from"node:module";import{findUpSync as u,findUp as h}from"@visulima/fs";import{NotFoundError as k}from"@visulima/fs/error";import{join as l,dirname as i}from"@visulima/path";import{parsePackageJsonSync as M}from"./package-json.js";const $=P(import.meta.url),s=typeof globalThis<"u"&&typeof globalThis.process<"u"?globalThis.process:process,m=g(e=>{if(typeof s<"u"&&s.versions&&s.versions.node){const[a,n]=s.versions.node.split(".").map(Number);if(a>22||a===22&&n>=3||a===20&&n>=16)return s.getBuiltinModule(e)}return $(e)},"__cjs_getBuiltinModule"),{execSync:S}=m("node:child_process"),{existsSync:f,readFileSync:F}=m("node:fs");var E=Object.defineProperty,r=g((e,a)=>E(e,"name",{value:a,configurable:!0}),"r");const d=["yarn.lock","package-lock.json","pnpm-lock.yaml","npm-shrinkwrap.json","bun.lockb"],y=r(e=>{let a;if(d.forEach(t=>{!a&&f(l(e,t))&&(a=l(e,t))}),a)return a;const n=l(e,"package.json");if(f(n)&&M(F(n,"utf8")).packageManager!==void 0)return n},"packageMangerFindUpMatcher"),w=r(e=>{if(!e)throw new k("Could not find a package manager");if(e.endsWith("package.json")){const a=M(e);if(a.packageManager){const n=["npm","yarn","pnpm","bun"].find(t=>a.packageManager.startsWith(t));if(n)return{packageManager:n,path:i(e)}}}if(e.endsWith("yarn.lock"))return{packageManager:"yarn",path:i(e)};if(e.endsWith("package-lock.json")||e.endsWith("npm-shrinkwrap.json"))return{packageManager:"npm",path:i(e)};if(e.endsWith("pnpm-lock.yaml"))return{packageManager:"pnpm",path:i(e)};if(e.endsWith("bun.lockb"))return{packageManager:"bun",path:i(e)};throw new k("Could not find a package manager")},"resolvePackageManagerFromFile"),T=r(async e=>{const a=await h(d,{type:"file",...e&&{cwd:e}});if(!a)throw new Error("Could not find lock file");return a},"findLockFile"),U=r(e=>{const a=u(d,{type:"file",...e&&{cwd:e}});if(!a)throw new Error("Could not find lock file");return a},"findLockFileSync"),q=r(async e=>{const a=await h(y,{...e&&{cwd:e}});return w(a)},"findPackageManager"),B=r(e=>{const a=u(y,{...e&&{cwd:e}});return w(a)},"findPackageManagerSync"),D=r(e=>S(`${e} --version`).toString("utf8").trim(),"getPackageManagerVersion"),O=r(()=>{if(!process.env.npm_config_user_agent)return;const e=process.env.npm_config_user_agent.split(" ")[0],a=e.lastIndexOf("/"),n=e.slice(0,Math.max(0,a));return{name:n==="npminstall"?"cnpm":n,version:e.slice(Math.max(0,a+1))}},"identifyInitiatingPackageManager"),R=r((e,a,n)=>{const t=a.length===1?"":"s";if(n.packageManagers??=["npm","pnpm","yarn"],n.packageManagers.length===0)throw new Error("No package managers provided, please provide at least one package manager");if(a.length===0)throw new Error("No missing packages provided, please provide at least one missing package");let p=`
2
+ ${n.preMessage??""}
3
+ ${e} could not find the following package${t}
2
4
 
3
- const __cjs_require = __cjs_createRequire(import.meta.url);
5
+ ${a.join(`
6
+ `)}
4
7
 
5
- const __cjs_getProcess = typeof globalThis !== "undefined" && typeof globalThis.process !== "undefined" ? globalThis.process : process;
8
+ To install the missing package${t}, please run the following command:
9
+ `;const v=r(o=>o.split("@").includes("@")?o:`${o}@latest`,"atLatest"),_=n.packageManagers.map(o=>{const c=a.map(j=>v(j)).join(" ");switch(o){case"bun":return` bun add ${c} -D`;case"npm":return` npm install ${c} --save-dev`;case"pnpm":return` pnpm add ${c} -D`;case"yarn":return` yarn add ${c} --dev`;default:throw new Error("Unknown package manager")}});return p+=_.join(`
6
10
 
7
- const __cjs_getBuiltinModule = (module) => {
8
- // Check if we're in Node.js and version supports getBuiltinModule
9
- if (typeof __cjs_getProcess !== "undefined" && __cjs_getProcess.versions && __cjs_getProcess.versions.node) {
10
- const [major, minor] = __cjs_getProcess.versions.node.split(".").map(Number);
11
- // Node.js 20.16.0+ and 22.3.0+
12
- if (major > 22 || (major === 22 && minor >= 3) || (major === 20 && minor >= 16)) {
13
- return __cjs_getProcess.getBuiltinModule(module);
14
- }
15
- }
16
- // Fallback to createRequire
17
- return __cjs_require(module);
18
- };
11
+ or
19
12
 
20
- const {
21
- execSync
22
- } = __cjs_getBuiltinModule("node:child_process");
23
- const {
24
- existsSync,
25
- readFileSync
26
- } = __cjs_getBuiltinModule("node:fs");
27
- import { findUp, findUpSync } from '@visulima/fs';
28
- import { NotFoundError } from '@visulima/fs/error';
29
- import { join, dirname } from '@visulima/path';
30
- import { parsePackageJsonSync } from './package-json.js';
31
-
32
- const lockFileNames = ["yarn.lock", "package-lock.json", "pnpm-lock.yaml", "npm-shrinkwrap.json", "bun.lockb"];
33
- const packageMangerFindUpMatcher = (directory) => {
34
- let lockFile;
35
- lockFileNames.forEach((lockFileName) => {
36
- if (!lockFile && existsSync(join(directory, lockFileName))) {
37
- lockFile = join(directory, lockFileName);
38
- }
39
- });
40
- if (lockFile) {
41
- return lockFile;
42
- }
43
- const packageJsonFilePath = join(directory, "package.json");
44
- if (existsSync(packageJsonFilePath)) {
45
- const packageJson = parsePackageJsonSync(readFileSync(packageJsonFilePath, "utf8"));
46
- if (packageJson.packageManager !== void 0) {
47
- return packageJsonFilePath;
48
- }
49
- }
50
- return void 0;
51
- };
52
- const resolvePackageManagerFromFile = (foundFile) => {
53
- if (!foundFile) {
54
- throw new NotFoundError("Could not find a package manager");
55
- }
56
- if (foundFile.endsWith("package.json")) {
57
- const packageJson = parsePackageJsonSync(foundFile);
58
- if (packageJson.packageManager) {
59
- const packageManagerNames = ["npm", "yarn", "pnpm", "bun"];
60
- const foundPackageManager = packageManagerNames.find((prefix) => packageJson.packageManager.startsWith(prefix));
61
- if (foundPackageManager) {
62
- return {
63
- packageManager: foundPackageManager,
64
- path: dirname(foundFile)
65
- };
66
- }
67
- }
68
- }
69
- if (foundFile.endsWith("yarn.lock")) {
70
- return {
71
- packageManager: "yarn",
72
- path: dirname(foundFile)
73
- };
74
- }
75
- if (foundFile.endsWith("package-lock.json") || foundFile.endsWith("npm-shrinkwrap.json")) {
76
- return {
77
- packageManager: "npm",
78
- path: dirname(foundFile)
79
- };
80
- }
81
- if (foundFile.endsWith("pnpm-lock.yaml")) {
82
- return {
83
- packageManager: "pnpm",
84
- path: dirname(foundFile)
85
- };
86
- }
87
- if (foundFile.endsWith("bun.lockb")) {
88
- return {
89
- packageManager: "bun",
90
- path: dirname(foundFile)
91
- };
92
- }
93
- throw new NotFoundError("Could not find a package manager");
94
- };
95
- const findLockFile = async (cwd) => {
96
- const filePath = await findUp(lockFileNames, {
97
- type: "file",
98
- ...cwd && { cwd }
99
- });
100
- if (!filePath) {
101
- throw new Error("Could not find lock file");
102
- }
103
- return filePath;
104
- };
105
- const findLockFileSync = (cwd) => {
106
- const filePath = findUpSync(lockFileNames, {
107
- type: "file",
108
- ...cwd && { cwd }
109
- });
110
- if (!filePath) {
111
- throw new Error("Could not find lock file");
112
- }
113
- return filePath;
114
- };
115
- const findPackageManager = async (cwd) => {
116
- const foundFile = await findUp(packageMangerFindUpMatcher, {
117
- ...cwd && { cwd }
118
- });
119
- return resolvePackageManagerFromFile(foundFile);
120
- };
121
- const findPackageManagerSync = (cwd) => {
122
- const foundFile = findUpSync(packageMangerFindUpMatcher, {
123
- ...cwd && { cwd }
124
- });
125
- return resolvePackageManagerFromFile(foundFile);
126
- };
127
- const getPackageManagerVersion = (name) => execSync(`${name} --version`).toString("utf8").trim();
128
- const identifyInitiatingPackageManager = () => {
129
- if (!process.env.npm_config_user_agent) {
130
- return void 0;
131
- }
132
- const pmSpec = process.env.npm_config_user_agent.split(" ")[0];
133
- const separatorPos = pmSpec.lastIndexOf("/");
134
- const name = pmSpec.slice(0, Math.max(0, separatorPos));
135
- return {
136
- name: name === "npminstall" ? "cnpm" : name,
137
- version: pmSpec.slice(Math.max(0, separatorPos + 1))
138
- };
139
- };
140
- const generateMissingPackagesInstallMessage = (packageName, missingPackages, options) => {
141
- const s = missingPackages.length === 1 ? "" : "s";
142
- options.packageManagers ??= ["npm", "pnpm", "yarn"];
143
- if (options.packageManagers.length === 0) {
144
- throw new Error("No package managers provided, please provide at least one package manager");
145
- }
146
- if (missingPackages.length === 0) {
147
- throw new Error("No missing packages provided, please provide at least one missing package");
148
- }
149
- let message = `
150
- ${options.preMessage ?? ""}
151
- ${packageName} could not find the following package${s}
152
-
153
- ${missingPackages.join("\n ")}
154
-
155
- To install the missing package${s}, please run the following command:
156
- `;
157
- const atLatest = (name) => {
158
- if (!name.split("@").includes("@")) {
159
- return `${name}@latest`;
160
- }
161
- return name;
162
- };
163
- const packageManagerCommands = options.packageManagers.map((packageManager) => {
164
- const missingPackagesString = missingPackages.map((element) => atLatest(element)).join(" ");
165
- switch (packageManager) {
166
- case "bun": {
167
- return ` bun add ${missingPackagesString} -D`;
168
- }
169
- case "npm": {
170
- return ` npm install ${missingPackagesString} --save-dev`;
171
- }
172
- case "pnpm": {
173
- return ` pnpm add ${missingPackagesString} -D`;
174
- }
175
- case "yarn": {
176
- return ` yarn add ${missingPackagesString} --dev`;
177
- }
178
- default: {
179
- throw new Error("Unknown package manager");
180
- }
181
- }
182
- });
183
- message += packageManagerCommands.join("\n\nor\n\n");
184
- if (options.postMessage) {
185
- message += options.postMessage;
186
- }
187
- return message;
188
- };
189
-
190
- export { findLockFile, findLockFileSync, findPackageManager, findPackageManagerSync, generateMissingPackagesInstallMessage, getPackageManagerVersion, identifyInitiatingPackageManager };
13
+ `),n.postMessage&&(p+=n.postMessage),p},"generateMissingPackagesInstallMessage");export{T as findLockFile,U as findLockFileSync,q as findPackageManager,B as findPackageManagerSync,R as generateMissingPackagesInstallMessage,D as getPackageManagerVersion,O as identifyInitiatingPackageManager};
package/dist/package.d.ts CHANGED
@@ -1,11 +1,12 @@
1
1
  /**
2
- * An asynchronous function that finds the root directory of a project based on certain lookup criteria.
3
- * @param cwd Optional. The current working directory to start the search from. The type of `cwd` is `string`.
4
- * @returns A `Promise` that resolves to the path of the root directory. The type of the returned promise is `Promise&lt;string>`.
5
- * @throws An `Error` if the root directory could not be found.
6
- * @example
7
- * const rootDirectory = await findPackageRoot();
8
- * console.log(rootDirectory); // '/path/to/project'
9
- */
10
- export declare const findPackageRoot: (cwd?: URL | string) => Promise<string>;
11
- export declare const findPackageRootSync: (cwd?: URL | string) => string;
2
+ * An asynchronous function that finds the root directory of a project based on certain lookup criteria.
3
+ * @param cwd Optional. The current working directory to start the search from. The type of `cwd` is `string`.
4
+ * @returns A `Promise` that resolves to the path of the root directory. The type of the returned promise is `Promise&lt;string>`.
5
+ * @throws An `Error` if the root directory could not be found.
6
+ * @example
7
+ * const rootDirectory = await findPackageRoot();
8
+ * console.log(rootDirectory); // '/path/to/project'
9
+ */
10
+ declare const findPackageRoot: (cwd?: URL | string) => Promise<string>;
11
+ declare const findPackageRootSync: (cwd?: URL | string) => string;
12
+ export { findPackageRoot, findPackageRootSync };
package/dist/package.js CHANGED
@@ -1,81 +1 @@
1
- import { createRequire as __cjs_createRequire } from "node:module";
2
-
3
- const __cjs_require = __cjs_createRequire(import.meta.url);
4
-
5
- const __cjs_getProcess = typeof globalThis !== "undefined" && typeof globalThis.process !== "undefined" ? globalThis.process : process;
6
-
7
- const __cjs_getBuiltinModule = (module) => {
8
- // Check if we're in Node.js and version supports getBuiltinModule
9
- if (typeof __cjs_getProcess !== "undefined" && __cjs_getProcess.versions && __cjs_getProcess.versions.node) {
10
- const [major, minor] = __cjs_getProcess.versions.node.split(".").map(Number);
11
- // Node.js 20.16.0+ and 22.3.0+
12
- if (major > 22 || (major === 22 && minor >= 3) || (major === 20 && minor >= 16)) {
13
- return __cjs_getProcess.getBuiltinModule(module);
14
- }
15
- }
16
- // Fallback to createRequire
17
- return __cjs_require(module);
18
- };
19
-
20
- const {
21
- existsSync
22
- } = __cjs_getBuiltinModule("node:fs");
23
- import { findUp, findUpSync, readJsonSync } from '@visulima/fs';
24
- import { dirname, join } from '@visulima/path';
25
- import { findLockFile, findLockFileSync } from './package-manager.js';
26
-
27
- const packageJsonMatcher = (directory) => {
28
- if (existsSync(join(directory, "package.json"))) {
29
- const packageJson = readJsonSync(join(directory, "package.json"));
30
- if (packageJson.name && packageJson.private !== true) {
31
- return "package.json";
32
- }
33
- }
34
- return void 0;
35
- };
36
- const findPackageRoot = async (cwd) => {
37
- try {
38
- const lockFile = await findLockFile(cwd);
39
- return dirname(lockFile);
40
- } catch {
41
- }
42
- const gitConfig = await findUp(".git/config", {
43
- ...cwd && { cwd },
44
- type: "file"
45
- });
46
- if (gitConfig) {
47
- return dirname(dirname(gitConfig));
48
- }
49
- const filePath = await findUp(packageJsonMatcher, {
50
- ...cwd && { cwd },
51
- type: "file"
52
- });
53
- if (filePath) {
54
- return dirname(filePath);
55
- }
56
- throw new Error("Could not find root directory");
57
- };
58
- const findPackageRootSync = (cwd) => {
59
- try {
60
- const lockFile = findLockFileSync(cwd);
61
- return dirname(lockFile);
62
- } catch {
63
- }
64
- const gitConfig = findUpSync(".git/config", {
65
- ...cwd && { cwd },
66
- type: "file"
67
- });
68
- if (gitConfig) {
69
- return dirname(dirname(gitConfig));
70
- }
71
- const filePath = findUpSync(packageJsonMatcher, {
72
- ...cwd && { cwd },
73
- type: "file"
74
- });
75
- if (filePath) {
76
- return dirname(filePath);
77
- }
78
- throw new Error("Could not find root directory");
79
- };
80
-
81
- export { findPackageRoot, findPackageRootSync };
1
+ var u=Object.defineProperty;var c=(e,o)=>u(e,"name",{value:o,configurable:!0});import{createRequire as l}from"node:module";import{findUp as a,findUpSync as f,readJsonSync as _}from"@visulima/fs";import{dirname as r,join as d}from"@visulima/path";import{findLockFile as w,findLockFileSync as j}from"./package-manager.js";const g=l(import.meta.url),n=typeof globalThis<"u"&&typeof globalThis.process<"u"?globalThis.process:process,y=c(e=>{if(typeof n<"u"&&n.versions&&n.versions.node){const[o,t]=n.versions.node.split(".").map(Number);if(o>22||o===22&&t>=3||o===20&&t>=16)return n.getBuiltinModule(e)}return g(e)},"__cjs_getBuiltinModule"),{existsSync:m}=y("node:fs");var k=Object.defineProperty,s=c((e,o)=>k(e,"name",{value:o,configurable:!0}),"r");const p=s(e=>{if(m(d(e,"package.json"))){const o=_(d(e,"package.json"));if(o.name&&o.private!==!0)return"package.json"}},"packageJsonMatcher"),S=s(async e=>{try{const i=await w(e);return r(i)}catch{}const o=await a(".git/config",{...e&&{cwd:e},type:"file"});if(o)return r(r(o));const t=await a(p,{...e&&{cwd:e},type:"file"});if(t)return r(t);throw new Error("Could not find root directory")},"findPackageRoot"),q=s(e=>{try{const i=j(e);return r(i)}catch{}const o=f(".git/config",{...e&&{cwd:e},type:"file"});if(o)return r(r(o));const t=f(p,{...e&&{cwd:e},type:"file"});if(t)return r(t);throw new Error("Could not find root directory")},"findPackageRootSync");export{S as findPackageRoot,q as findPackageRootSync};
@@ -0,0 +1 @@
1
+ var o=Object.defineProperty;var a=(r,e)=>o(r,"name",{value:e,configurable:!0});import{findPackageManagerSync as n}from"../package-manager.js";var c=Object.defineProperty,s=a((r,e)=>c(r,"name",{value:e,configurable:!0}),"t");class g extends Error{static{a(this,"s")}static{s(this,"PackageNotFoundError")}constructor(e,t){if(typeof e=="string"&&(e=[e]),e.length===0){super("Package was not found.");return}if(t===void 0)try{t=n().packageManager}catch{}t??="npm",super(`Package '${e.join(" ")}' was not found. Please install it using '${t} install ${e.join(" ")}'`)}get code(){return"PACKAGE_NOT_FOUND"}set code(e){throw new Error("Cannot overwrite code PACKAGE_NOT_FOUND")}get name(){return"PackageNotFoundError"}set name(e){throw new Error("Cannot overwrite name of PackageNotFoundError")}}export{g as default};
@@ -0,0 +1,173 @@
1
+ import { WriteJsonOptions } from '@visulima/fs';
2
+ import { PackageJson as PackageJson$1, Paths, JsonObject } from 'type-fest';
3
+ import { InstallPackageOptions } from '@antfu/install-pkg';
4
+ import { Theme } from '@inquirer/core';
5
+ import { PartialDeep } from '@inquirer/type';
6
+ import { Package } from 'normalize-package-data';
7
+ type NormalizedPackageJson = Package & PackageJson;
8
+ type PackageJson = PackageJson$1;
9
+ type Cache<T = unknown> = Map<string, T>;
10
+ type EnsurePackagesOptions = {
11
+ /** Configuration for user confirmation prompts when installing packages */
12
+ confirm?: {
13
+ /** Default value for the confirmation prompt */
14
+ default?: boolean;
15
+ /** Message to display in the confirmation prompt, or a function that receives packages array */
16
+ message: string | ((packages: string[]) => string);
17
+ /** Theme configuration for the prompt interface */
18
+ theme?: PartialDeep<Theme>;
19
+ /** Function to transform the boolean value for display */
20
+ transformer?: (value: boolean) => string;
21
+ };
22
+ /** Current working directory for package operations */
23
+ cwd?: URL | string;
24
+ /** Whether to include regular dependencies in the operation */
25
+ deps?: boolean;
26
+ /** Whether to include development dependencies in the operation */
27
+ devDeps?: boolean;
28
+ /** Additional options for package installation (excluding cwd and dev which are handled separately) */
29
+ installPackage?: Omit<InstallPackageOptions, "cwd" | "dev">;
30
+ /** Custom logger interface for warning messages */
31
+ logger?: {
32
+ warn: (message: string) => void;
33
+ };
34
+ /** Whether to include peer dependencies in the operation */
35
+ peerDeps?: boolean;
36
+ /** Whether to throw an error when warnings are logged instead of just logging them */
37
+ throwOnWarn?: boolean;
38
+ };
39
+ type ReadOptions = {
40
+ cache?: FindPackageJsonCache | boolean;
41
+ ignoreWarnings?: (RegExp | string)[];
42
+ json5?: boolean;
43
+ resolveCatalogs?: boolean;
44
+ strict?: boolean;
45
+ yaml?: boolean;
46
+ };
47
+ type FindPackageJsonCache = Cache<NormalizedReadResult>;
48
+ type NormalizedReadResult = {
49
+ packageJson: NormalizedPackageJson;
50
+ path: string;
51
+ };
52
+ /**
53
+ * An asynchronous function to find the package.json, package.yaml, or package.json5 file in the specified directory or its parent directories.
54
+ * @param cwd The current working directory.
55
+ * @param options Configuration options including yaml, json5, and resolveCatalogs flags.
56
+ * @returns A `Promise` that resolves to an object containing the parsed package data and the file path.
57
+ * The type of the returned promise is `Promise&lt;NormalizedReadResult>`.
58
+ * @throws {Error} If no package file can be found or if strict mode is enabled and normalize warnings are thrown.
59
+ */
60
+ declare const findPackageJson: (cwd?: URL | string, options?: ReadOptions) => Promise<NormalizedReadResult>;
61
+ /**
62
+ * A synchronous function to find the package.json, package.yaml, or package.json5 file in the specified directory or its parent directories.
63
+ * @param cwd The current working directory.
64
+ * @param options Configuration options including yaml, json5, and resolveCatalogs flags.
65
+ * @returns An object containing the parsed package data and the file path.
66
+ * @throws {Error} If no package file can be found or if strict mode is enabled and normalize warnings are thrown.
67
+ */
68
+ declare const findPackageJsonSync: (cwd?: URL | string, options?: ReadOptions) => NormalizedReadResult;
69
+ /**
70
+ * An asynchronous function to write the package.json file with the given data.
71
+ * @param data The package.json data to write. The data is an intersection type of `PackageJson` and a record where keys are `string` and values can be any type.
72
+ * @param options Optional. The options for writing the package.json. If not provided, an empty object will be used `{}`.
73
+ * This is an intersection type of `WriteJsonOptions` and a record with an optional `cwd` key which type is `Options["cwd"]`.
74
+ * `cwd` represents the current working directory. If not specified, the default working directory will be used.
75
+ * @returns A `Promise` that resolves once the package.json file has been written. The type of the returned promise is `Promise&lt;void>`.
76
+ */
77
+ declare const writePackageJson: (data: PackageJson, options?: WriteJsonOptions & {
78
+ cwd?: URL | string;
79
+ }) => Promise<void>;
80
+ declare const writePackageJsonSync: (data: PackageJson, options?: WriteJsonOptions & {
81
+ cwd?: URL | string;
82
+ }) => void;
83
+ /**
84
+ * A synchronous function to parse the package.json, package.yaml, or package.json5 file/object/string and return normalize the data.
85
+ * @param packageFile
86
+ * @param options
87
+ * @param options.cache Cache for parsed results (only applies to file paths)
88
+ * @param options.ignoreWarnings List of warning messages or patterns to skip in strict mode
89
+ * @param options.resolveCatalogs Whether to resolve pnpm catalog references
90
+ * @param options.strict Whether to throw errors on normalization warnings
91
+ * @param options.yaml Whether to enable package.yaml parsing (default: true)
92
+ * @param options.json5 Whether to enable package.json5 parsing (default: true)
93
+ * @returns
94
+ * @throws {Error} If the packageFile parameter is not an object or a string or if strict mode is enabled and normalize warnings are thrown.
95
+ */
96
+ declare const parsePackageJsonSync: (packageFile: JsonObject | string, options?: {
97
+ cache?: Cache<NormalizedPackageJson> | boolean;
98
+ ignoreWarnings?: (RegExp | string)[];
99
+ json5?: boolean;
100
+ resolveCatalogs?: boolean;
101
+ strict?: boolean;
102
+ yaml?: boolean;
103
+ }) => NormalizedPackageJson;
104
+ /**
105
+ * An asynchronous function to parse the package.json, package.yaml, or package.json5 file/object/string and return normalize the data.
106
+ * @param packageFile
107
+ * @param options
108
+ * @param options.cache Cache for parsed results (only applies to file paths)
109
+ * @param options.ignoreWarnings List of warning messages or patterns to skip in strict mode
110
+ * @param options.strict Whether to throw errors on normalization warnings
111
+ * @param options.resolveCatalogs Whether to resolve pnpm catalog references
112
+ * @param options.yaml Whether to enable package.yaml parsing (default: true)
113
+ * @param options.json5 Whether to enable package.json5 parsing (default: true)
114
+ * @returns
115
+ * @throws {Error} If the packageFile parameter is not an object or a string or if strict mode is enabled and normalize warnings are thrown.
116
+ */
117
+ declare const parsePackageJson: (packageFile: JsonObject | string, options?: {
118
+ cache?: Cache<NormalizedPackageJson> | boolean;
119
+ ignoreWarnings?: (RegExp | string)[];
120
+ json5?: boolean;
121
+ resolveCatalogs?: boolean;
122
+ strict?: boolean;
123
+ yaml?: boolean;
124
+ }) => Promise<NormalizedPackageJson>;
125
+ /**
126
+ * An asynchronous function to get the value of a property from the package.json file.
127
+ * @param packageJson
128
+ * @param property
129
+ * @param defaultValue
130
+ * @returns
131
+ */
132
+ declare const getPackageJsonProperty: <T = unknown>(packageJson: NormalizedPackageJson, property: Paths<NormalizedPackageJson>, defaultValue?: T) => T;
133
+ /**
134
+ * An asynchronous function to check if a property exists in the package.json file.
135
+ * @param packageJson
136
+ * @param property
137
+ * @returns
138
+ */
139
+ declare const hasPackageJsonProperty: (packageJson: NormalizedPackageJson, property: Paths<NormalizedPackageJson>) => boolean;
140
+ /**
141
+ * An asynchronous function to check if any of the specified dependencies exist in the package.json file.
142
+ * @param packageJson
143
+ * @param arguments_
144
+ * @param options
145
+ * @param options.peerDeps Whether to include peer dependencies
146
+ * @returns
147
+ */
148
+ declare const hasPackageJsonAnyDependency: (packageJson: NormalizedPackageJson, arguments_: string[], options?: {
149
+ peerDeps?: boolean;
150
+ }) => boolean;
151
+ /**
152
+ * An asynchronous function to ensure that the specified packages are installed in the package.json file.
153
+ * If the packages are not installed, the user will be prompted to install them.
154
+ * If the user agrees, the packages will be installed.
155
+ * If the user declines, the function will return without installing the packages.
156
+ * If the user does not respond, the function will return without installing the packages.
157
+ * @param packageJson
158
+ * @param packages
159
+ * @param installKey
160
+ * @param options
161
+ * @param options.deps Whether to include regular dependencies
162
+ * @param options.devDeps Whether to include development dependencies
163
+ * @param options.peerDeps Whether to include peer dependencies
164
+ * @param options.throwOnWarn Whether to throw an error when warnings are logged instead of just logging them
165
+ * @param options.logger Whether to use a custom logger
166
+ * @param options.confirm Whether to use a custom confirmation prompt
167
+ * @param options.installPackage Whether to use a custom installation package
168
+ * @param options.cwd Whether to use a custom current working directory
169
+ * @param options.dev Whether to use a custom installation key
170
+ * @returns
171
+ */
172
+ declare const ensurePackages: (packageJson: NormalizedPackageJson, packages: string[], installKey?: "dependencies" | "devDependencies", options?: EnsurePackagesOptions) => Promise<void>;
173
+ export { EnsurePackagesOptions as E, FindPackageJsonCache as F, NormalizedPackageJson as N, PackageJson as P, NormalizedReadResult as a, findPackageJsonSync as b, hasPackageJsonProperty as c, parsePackageJsonSync as d, ensurePackages as e, findPackageJson as f, getPackageJsonProperty as g, hasPackageJsonAnyDependency as h, writePackageJsonSync as i, parsePackageJson as p, writePackageJson as w };