@vlandoss/clibuddy 0.3.1-git-1558746.0 → 0.4.1-git-53b6b02.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 +33 -29
- package/dist/index.mjs +78 -66
- package/package.json +7 -6
- package/src/colors.ts +16 -14
- package/src/env.ts +1 -0
- package/src/index.ts +3 -2
- package/src/pkg.ts +117 -0
- package/src/run.ts +1 -1
- package/src/services/index.ts +0 -2
- package/src/services/pkg.ts +0 -93
- package/src/version.ts +0 -5
- /package/src/{services/shell → shell}/create.ts +0 -0
- /package/src/{services/shell → shell}/index.ts +0 -0
- /package/src/{services/shell → shell}/shell.ts +0 -0
- /package/src/{services/shell → shell}/types.ts +0 -0
- /package/src/{services/shell → shell}/utils.ts +0 -0
package/dist/index.d.mts
CHANGED
|
@@ -1,53 +1,60 @@
|
|
|
1
|
-
import
|
|
1
|
+
import * as _$ansis from "ansis";
|
|
2
|
+
import { hasTTY, isCI } from "std-env";
|
|
3
|
+
import { PackageJson } from "pkg-types";
|
|
2
4
|
import * as _$zx from "zx";
|
|
3
5
|
import { Options, ProcessOutput, Shell as Shell$1 } from "zx";
|
|
4
|
-
import { Project } from "@pnpm/
|
|
5
|
-
import { NormalizedPackageJson } from "read-package-up";
|
|
6
|
+
import { Project } from "@pnpm/types";
|
|
6
7
|
|
|
7
8
|
//#region src/colors.d.ts
|
|
8
|
-
declare const
|
|
9
|
-
declare const colorize: (hex: string) => ChalkInstance | (<T>(x: T) => T);
|
|
9
|
+
declare const colorize: (hex: string) => _$ansis.Ansis;
|
|
10
10
|
declare const palette: {
|
|
11
|
-
bold:
|
|
12
|
-
italic:
|
|
13
|
-
link:
|
|
14
|
-
muted:
|
|
15
|
-
primary:
|
|
11
|
+
bold: _$ansis.Ansis;
|
|
12
|
+
italic: _$ansis.Ansis;
|
|
13
|
+
link: _$ansis.Ansis;
|
|
14
|
+
muted: _$ansis.Ansis;
|
|
15
|
+
primary: _$ansis.Ansis;
|
|
16
|
+
dim: _$ansis.Ansis;
|
|
17
|
+
highlight: _$ansis.Ansis;
|
|
18
|
+
success: _$ansis.Ansis;
|
|
19
|
+
label: (s: string) => string;
|
|
16
20
|
};
|
|
17
21
|
//#endregion
|
|
18
22
|
//#region src/meta.d.ts
|
|
19
23
|
declare function dirnameOf(meta: ImportMeta): string;
|
|
20
24
|
declare function filenameOf(meta: ImportMeta): string;
|
|
21
25
|
//#endregion
|
|
22
|
-
//#region src/
|
|
23
|
-
declare
|
|
24
|
-
error: (...args: unknown[]) => void;
|
|
25
|
-
}): Promise<void>;
|
|
26
|
-
//#endregion
|
|
27
|
-
//#region src/services/pkg.d.ts
|
|
28
|
-
declare class PkgService {
|
|
26
|
+
//#region src/pkg.d.ts
|
|
27
|
+
declare class Pkg {
|
|
29
28
|
#private;
|
|
30
29
|
get dirPath(): string;
|
|
31
|
-
get
|
|
32
|
-
|
|
30
|
+
get pkgPath(): string;
|
|
31
|
+
get packageJson(): PackageJson;
|
|
32
|
+
get version(): string;
|
|
33
|
+
constructor(packageJson: PackageJson, pkgPath: string);
|
|
33
34
|
info(): {
|
|
34
|
-
packageJson:
|
|
35
|
+
packageJson: PackageJson;
|
|
35
36
|
dirPath: string;
|
|
36
37
|
};
|
|
37
38
|
hasFile(name: string, dir?: string): boolean;
|
|
38
39
|
isMonorepo(): boolean;
|
|
39
40
|
getWorkspaceProjects(): Promise<Project[]>;
|
|
41
|
+
write(packageJson: PackageJson): Promise<void>;
|
|
40
42
|
}
|
|
41
|
-
declare function
|
|
43
|
+
declare function createPkg(cwd: string): Promise<Pkg | null>;
|
|
42
44
|
//#endregion
|
|
43
|
-
//#region src/
|
|
45
|
+
//#region src/run.d.ts
|
|
46
|
+
declare function run(fn: () => Promise<void>, logger: {
|
|
47
|
+
error: (...args: unknown[]) => void;
|
|
48
|
+
}): Promise<void>;
|
|
49
|
+
//#endregion
|
|
50
|
+
//#region src/shell/types.d.ts
|
|
44
51
|
type Shell = Shell$1;
|
|
45
52
|
type ShellOptions = Partial<Options>;
|
|
46
53
|
type CreateOptions = ShellOptions & {
|
|
47
54
|
localBaseBinPath?: string | Array<string>;
|
|
48
55
|
};
|
|
49
56
|
//#endregion
|
|
50
|
-
//#region src/
|
|
57
|
+
//#region src/shell/shell.d.ts
|
|
51
58
|
declare class ShellService {
|
|
52
59
|
#private;
|
|
53
60
|
constructor(options: ShellOptions);
|
|
@@ -59,7 +66,7 @@ declare class ShellService {
|
|
|
59
66
|
at(cwd: string, options?: ShellOptions): ShellService;
|
|
60
67
|
}
|
|
61
68
|
//#endregion
|
|
62
|
-
//#region src/
|
|
69
|
+
//#region src/shell/create.d.ts
|
|
63
70
|
declare const cwd: string;
|
|
64
71
|
declare function quote(arg: string): string;
|
|
65
72
|
declare const isRaw: (arg: unknown) => arg is {
|
|
@@ -67,7 +74,7 @@ declare const isRaw: (arg: unknown) => arg is {
|
|
|
67
74
|
};
|
|
68
75
|
declare function createShellService(options?: CreateOptions): ShellService;
|
|
69
76
|
//#endregion
|
|
70
|
-
//#region src/
|
|
77
|
+
//#region src/shell/utils.d.ts
|
|
71
78
|
declare function isProcessOutput(value: unknown): value is ProcessOutput;
|
|
72
79
|
declare function getPreferLocal(localBaseBinPath: string | Array<string> | undefined): string[] | undefined;
|
|
73
80
|
//#endregion
|
|
@@ -77,7 +84,4 @@ declare const text: {
|
|
|
77
84
|
version: (version: string) => string;
|
|
78
85
|
};
|
|
79
86
|
//#endregion
|
|
80
|
-
|
|
81
|
-
declare function getVersion(pkg: PkgService): string;
|
|
82
|
-
//#endregion
|
|
83
|
-
export { CreateOptions, PkgService, type Project, Shell, ShellOptions, ShellService, colorIsSupported, colorize, createPkgService, createShellService, cwd, dirnameOf, filenameOf, getPreferLocal, getVersion, isProcessOutput, isRaw, palette, quote, run, text };
|
|
87
|
+
export { CreateOptions, Pkg, type Project, Shell, ShellOptions, ShellService, colorize, createPkg, createShellService, cwd, dirnameOf, filenameOf, getPreferLocal, hasTTY, isCI, isProcessOutput, isRaw, palette, quote, run, text };
|
package/dist/index.mjs
CHANGED
|
@@ -1,23 +1,25 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
1
|
+
import ansis, { bold, cyan, dim, green, italic, underline } from "ansis";
|
|
2
|
+
import { hasTTY, isCI } from "std-env";
|
|
3
3
|
import path, { dirname } from "node:path";
|
|
4
4
|
import { fileURLToPath } from "node:url";
|
|
5
|
+
import fs, { existsSync } from "node:fs";
|
|
6
|
+
import { readFile } from "node:fs/promises";
|
|
7
|
+
import { findPackages } from "@pnpm/fs.find-packages";
|
|
8
|
+
import { readPackageJSON, resolvePackageJSON, writePackageJSON } from "pkg-types";
|
|
9
|
+
import { parse } from "yaml";
|
|
5
10
|
import { $, ProcessOutput } from "zx";
|
|
6
|
-
import fs from "node:fs";
|
|
7
|
-
import { findWorkspacePackages } from "@pnpm/workspace.find-packages";
|
|
8
|
-
import { readWorkspaceManifest } from "@pnpm/workspace.read-manifest";
|
|
9
|
-
import { readPackageUp } from "read-package-up";
|
|
10
11
|
//#region src/colors.ts
|
|
11
|
-
const
|
|
12
|
-
const identity = (x) => x;
|
|
13
|
-
const safe = (style) => colorIsSupported() ? style : identity;
|
|
14
|
-
const colorize = (hex) => safe(chalk.hex(hex));
|
|
12
|
+
const colorize = (hex) => ansis.hex(hex);
|
|
15
13
|
const palette = {
|
|
16
|
-
bold
|
|
17
|
-
italic
|
|
18
|
-
link:
|
|
19
|
-
muted:
|
|
20
|
-
primary:
|
|
14
|
+
bold,
|
|
15
|
+
italic,
|
|
16
|
+
link: underline,
|
|
17
|
+
muted: ansis.hex("#a8afb5"),
|
|
18
|
+
primary: ansis.hex("#36d399"),
|
|
19
|
+
dim,
|
|
20
|
+
highlight: cyan,
|
|
21
|
+
success: green,
|
|
22
|
+
label: (s) => ansis.bgMagenta.black(s)
|
|
21
23
|
};
|
|
22
24
|
//#endregion
|
|
23
25
|
//#region src/meta.ts
|
|
@@ -28,44 +30,26 @@ function filenameOf(meta) {
|
|
|
28
30
|
return meta.filename ?? fileURLToPath(meta.url);
|
|
29
31
|
}
|
|
30
32
|
//#endregion
|
|
31
|
-
//#region src/
|
|
32
|
-
|
|
33
|
-
return value instanceof ProcessOutput;
|
|
34
|
-
}
|
|
35
|
-
const getLocalBinPath = (dirPath) => path.join(dirPath, "node_modules", ".bin");
|
|
36
|
-
function getPreferLocal(localBaseBinPath) {
|
|
37
|
-
return !localBaseBinPath ? void 0 : Array.isArray(localBaseBinPath) ? localBaseBinPath.map(getLocalBinPath) : [localBaseBinPath].map(getLocalBinPath);
|
|
38
|
-
}
|
|
39
|
-
//#endregion
|
|
40
|
-
//#region src/run.ts
|
|
41
|
-
function hasMessage(error) {
|
|
42
|
-
return typeof error === "object" && error !== null && "message" in error && typeof error.message === "string";
|
|
43
|
-
}
|
|
44
|
-
function formatError(error) {
|
|
45
|
-
if (hasMessage(error)) return error.message;
|
|
46
|
-
return String(error);
|
|
47
|
-
}
|
|
48
|
-
async function run(fn, logger) {
|
|
49
|
-
try {
|
|
50
|
-
await fn();
|
|
51
|
-
} catch (error) {
|
|
52
|
-
if (!isProcessOutput(error)) logger.error(formatError(error));
|
|
53
|
-
process.exit(1);
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
//#endregion
|
|
57
|
-
//#region src/services/pkg.ts
|
|
58
|
-
var PkgService = class {
|
|
33
|
+
//#region src/pkg.ts
|
|
34
|
+
var Pkg = class {
|
|
59
35
|
#packageJson;
|
|
60
36
|
#dirPath;
|
|
37
|
+
#pkgPath;
|
|
61
38
|
get dirPath() {
|
|
62
39
|
return this.#dirPath;
|
|
63
40
|
}
|
|
41
|
+
get pkgPath() {
|
|
42
|
+
return this.#pkgPath;
|
|
43
|
+
}
|
|
64
44
|
get packageJson() {
|
|
65
45
|
return this.#packageJson;
|
|
66
46
|
}
|
|
47
|
+
get version() {
|
|
48
|
+
return process.env.VERSION || this.#packageJson.version || "0.0.0";
|
|
49
|
+
}
|
|
67
50
|
constructor(packageJson, pkgPath) {
|
|
68
51
|
this.#packageJson = packageJson;
|
|
52
|
+
this.#pkgPath = pkgPath;
|
|
69
53
|
this.#dirPath = path.dirname(pkgPath);
|
|
70
54
|
}
|
|
71
55
|
info() {
|
|
@@ -75,41 +59,74 @@ var PkgService = class {
|
|
|
75
59
|
};
|
|
76
60
|
}
|
|
77
61
|
hasFile(name, dir) {
|
|
78
|
-
|
|
79
|
-
return fs.existsSync(filepath);
|
|
62
|
+
return existsSync(dir ? path.join(dir, name) : this.#fromPkgDir(name));
|
|
80
63
|
}
|
|
81
64
|
isMonorepo() {
|
|
82
|
-
return this.#packageJson.workspaces
|
|
65
|
+
return !!this.#packageJson.workspaces || this.#hasPnpmWorkspace();
|
|
83
66
|
}
|
|
84
67
|
async getWorkspaceProjects() {
|
|
85
68
|
let patterns;
|
|
86
|
-
if (this.#hasPnpmWorkspace())
|
|
87
|
-
|
|
88
|
-
if (!manifest) throw new Error("Can't read pnpm workspace manifest");
|
|
89
|
-
patterns = manifest.packages;
|
|
90
|
-
} else patterns = Array.isArray(this.#packageJson.workspaces) ? this.#packageJson.workspaces : this.#packageJson.workspaces?.packages ?? [];
|
|
69
|
+
if (this.#hasPnpmWorkspace()) patterns = (await this.#readPnpmWorkspaceManifest()).packages;
|
|
70
|
+
else patterns = Array.isArray(this.#packageJson.workspaces) ? this.#packageJson.workspaces : this.#packageJson.workspaces?.packages ?? [];
|
|
91
71
|
if (!Array.isArray(patterns) || patterns.some((p) => typeof p !== "string")) throw new Error("Invalid workspace patterns");
|
|
92
|
-
const projects = await
|
|
72
|
+
const projects = await findPackages(this.#dirPath, { patterns });
|
|
93
73
|
const excludeRoot = (projects) => {
|
|
94
74
|
return projects.filter((project) => project.rootDir !== this.#dirPath);
|
|
95
75
|
};
|
|
96
76
|
return excludeRoot(projects);
|
|
97
77
|
}
|
|
78
|
+
async write(packageJson) {
|
|
79
|
+
await writePackageJSON(this.#pkgPath, packageJson);
|
|
80
|
+
}
|
|
98
81
|
#hasPnpmWorkspace() {
|
|
99
82
|
return this.hasFile("pnpm-workspace.yaml");
|
|
100
83
|
}
|
|
101
|
-
#
|
|
84
|
+
async #readPnpmWorkspaceManifest() {
|
|
85
|
+
const manifest = parse(await readFile(this.#fromPkgDir("pnpm-workspace.yaml"), "utf-8"));
|
|
86
|
+
if (!manifest) throw new Error("Can't read pnpm workspace manifest");
|
|
87
|
+
return manifest;
|
|
88
|
+
}
|
|
89
|
+
#fromPkgDir(...args) {
|
|
102
90
|
return path.join(this.#dirPath, ...args);
|
|
103
91
|
}
|
|
104
92
|
};
|
|
105
|
-
async function
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
93
|
+
async function createPkg(cwd) {
|
|
94
|
+
let pkgPath;
|
|
95
|
+
try {
|
|
96
|
+
pkgPath = await resolvePackageJSON(cwd);
|
|
97
|
+
} catch {
|
|
98
|
+
return null;
|
|
99
|
+
}
|
|
100
|
+
return new Pkg(await readPackageJSON(pkgPath), pkgPath);
|
|
110
101
|
}
|
|
111
102
|
//#endregion
|
|
112
|
-
//#region src/
|
|
103
|
+
//#region src/shell/utils.ts
|
|
104
|
+
function isProcessOutput(value) {
|
|
105
|
+
return value instanceof ProcessOutput;
|
|
106
|
+
}
|
|
107
|
+
const getLocalBinPath = (dirPath) => path.join(dirPath, "node_modules", ".bin");
|
|
108
|
+
function getPreferLocal(localBaseBinPath) {
|
|
109
|
+
return !localBaseBinPath ? void 0 : Array.isArray(localBaseBinPath) ? localBaseBinPath.map(getLocalBinPath) : [localBaseBinPath].map(getLocalBinPath);
|
|
110
|
+
}
|
|
111
|
+
//#endregion
|
|
112
|
+
//#region src/run.ts
|
|
113
|
+
function hasMessage(error) {
|
|
114
|
+
return typeof error === "object" && error !== null && "message" in error && typeof error.message === "string";
|
|
115
|
+
}
|
|
116
|
+
function formatError(error) {
|
|
117
|
+
if (hasMessage(error)) return error.message;
|
|
118
|
+
return String(error);
|
|
119
|
+
}
|
|
120
|
+
async function run(fn, logger) {
|
|
121
|
+
try {
|
|
122
|
+
await fn();
|
|
123
|
+
} catch (error) {
|
|
124
|
+
if (!isProcessOutput(error)) logger.error(formatError(error));
|
|
125
|
+
process.exit(1);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
//#endregion
|
|
129
|
+
//#region src/shell/shell.ts
|
|
113
130
|
var ShellService = class ShellService {
|
|
114
131
|
#shell;
|
|
115
132
|
#options;
|
|
@@ -157,7 +174,7 @@ var ShellService = class ShellService {
|
|
|
157
174
|
}
|
|
158
175
|
};
|
|
159
176
|
//#endregion
|
|
160
|
-
//#region src/
|
|
177
|
+
//#region src/shell/create.ts
|
|
161
178
|
const cwd = fs.realpathSync(process.cwd());
|
|
162
179
|
function quote(arg) {
|
|
163
180
|
if (/^[\w./:=@-]+$/i.test(arg) || arg === "") return arg;
|
|
@@ -185,9 +202,4 @@ const text = {
|
|
|
185
202
|
version: (version) => palette.muted(`v${version}`)
|
|
186
203
|
};
|
|
187
204
|
//#endregion
|
|
188
|
-
|
|
189
|
-
function getVersion(pkg) {
|
|
190
|
-
return process.env.VERSION || pkg.packageJson.version;
|
|
191
|
-
}
|
|
192
|
-
//#endregion
|
|
193
|
-
export { PkgService, ShellService, colorIsSupported, colorize, createPkgService, createShellService, cwd, dirnameOf, filenameOf, getPreferLocal, getVersion, isProcessOutput, isRaw, palette, quote, run, text };
|
|
205
|
+
export { Pkg, ShellService, colorize, createPkg, createShellService, cwd, dirnameOf, filenameOf, getPreferLocal, hasTTY, isCI, isProcessOutput, isRaw, palette, quote, run, text };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vlandoss/clibuddy",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.1-git-53b6b02.0",
|
|
4
4
|
"description": "A helper library to create CLIs in Variable Land",
|
|
5
5
|
"homepage": "https://github.com/variableland/dx/tree/main/packages/clibuddy#readme",
|
|
6
6
|
"bugs": {
|
|
@@ -27,11 +27,12 @@
|
|
|
27
27
|
"!src/**/*.test.*"
|
|
28
28
|
],
|
|
29
29
|
"dependencies": {
|
|
30
|
-
"@pnpm/
|
|
31
|
-
"@pnpm/
|
|
32
|
-
"
|
|
33
|
-
"
|
|
34
|
-
"
|
|
30
|
+
"@pnpm/fs.find-packages": "1000.0.24",
|
|
31
|
+
"@pnpm/types": "1001.3.0",
|
|
32
|
+
"ansis": "4.2.0",
|
|
33
|
+
"pkg-types": "2.3.0",
|
|
34
|
+
"std-env": "3.9.0",
|
|
35
|
+
"yaml": "2.8.4",
|
|
35
36
|
"zx": "8.8.5"
|
|
36
37
|
},
|
|
37
38
|
"publishConfig": {
|
package/src/colors.ts
CHANGED
|
@@ -1,18 +1,20 @@
|
|
|
1
|
-
import
|
|
2
|
-
import supportsColor from "supports-color";
|
|
1
|
+
import ansis, { bold, cyan, dim, green, italic, underline } from "ansis";
|
|
3
2
|
|
|
4
|
-
//
|
|
5
|
-
export const
|
|
6
|
-
|
|
7
|
-
const identity = <T>(x: T) => x;
|
|
8
|
-
const safe = (style: ChalkInstance) => (colorIsSupported() ? style : identity);
|
|
9
|
-
|
|
10
|
-
export const colorize = (hex: string) => safe(chalk.hex(hex));
|
|
3
|
+
// hex-from-string factory; matches the previous public API.
|
|
4
|
+
export const colorize = (hex: string) => ansis.hex(hex);
|
|
11
5
|
|
|
6
|
+
// Curated semantic palette used across our CLIs.
|
|
12
7
|
export const palette = {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
8
|
+
// structural
|
|
9
|
+
bold,
|
|
10
|
+
italic,
|
|
11
|
+
link: underline,
|
|
12
|
+
// tones
|
|
13
|
+
muted: ansis.hex("#a8afb5"),
|
|
14
|
+
primary: ansis.hex("#36d399"),
|
|
15
|
+
dim,
|
|
16
|
+
// semantic
|
|
17
|
+
highlight: cyan,
|
|
18
|
+
success: green,
|
|
19
|
+
label: (s: string) => ansis.bgMagenta.black(s),
|
|
18
20
|
};
|
package/src/env.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { hasTTY, isCI } from "std-env";
|
package/src/index.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
export * from "./colors.ts";
|
|
2
|
+
export * from "./env.ts";
|
|
2
3
|
export * from "./meta.ts";
|
|
4
|
+
export * from "./pkg.ts";
|
|
3
5
|
export * from "./run.ts";
|
|
4
|
-
export * from "./
|
|
6
|
+
export * from "./shell/index.ts";
|
|
5
7
|
export * from "./text.ts";
|
|
6
|
-
export * from "./version.ts";
|
package/src/pkg.ts
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import { existsSync } from "node:fs";
|
|
2
|
+
import { readFile } from "node:fs/promises";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import { findPackages } from "@pnpm/fs.find-packages";
|
|
5
|
+
import type { Project } from "@pnpm/types";
|
|
6
|
+
import { type PackageJson, readPackageJSON, resolvePackageJSON, writePackageJSON } from "pkg-types";
|
|
7
|
+
import { parse as yamlParse } from "yaml";
|
|
8
|
+
|
|
9
|
+
export type { Project };
|
|
10
|
+
|
|
11
|
+
export class Pkg {
|
|
12
|
+
#packageJson: PackageJson;
|
|
13
|
+
#dirPath: string;
|
|
14
|
+
#pkgPath: string;
|
|
15
|
+
|
|
16
|
+
get dirPath() {
|
|
17
|
+
return this.#dirPath;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
get pkgPath() {
|
|
21
|
+
return this.#pkgPath;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
get packageJson() {
|
|
25
|
+
return this.#packageJson;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
get version() {
|
|
29
|
+
return process.env.VERSION || this.#packageJson.version || "0.0.0";
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
constructor(packageJson: PackageJson, pkgPath: string) {
|
|
33
|
+
this.#packageJson = packageJson;
|
|
34
|
+
this.#pkgPath = pkgPath;
|
|
35
|
+
this.#dirPath = path.dirname(pkgPath);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
info() {
|
|
39
|
+
return {
|
|
40
|
+
packageJson: this.#packageJson,
|
|
41
|
+
dirPath: this.#dirPath,
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
hasFile(name: string, dir?: string) {
|
|
46
|
+
const filepath = dir ? path.join(dir, name) : this.#fromPkgDir(name);
|
|
47
|
+
return existsSync(filepath);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
isMonorepo() {
|
|
51
|
+
return !!this.#packageJson.workspaces || this.#hasPnpmWorkspace();
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
async getWorkspaceProjects() {
|
|
55
|
+
let patterns: string[];
|
|
56
|
+
|
|
57
|
+
if (this.#hasPnpmWorkspace()) {
|
|
58
|
+
const manifest = await this.#readPnpmWorkspaceManifest();
|
|
59
|
+
patterns = manifest.packages;
|
|
60
|
+
} else {
|
|
61
|
+
patterns = Array.isArray(this.#packageJson.workspaces)
|
|
62
|
+
? this.#packageJson.workspaces
|
|
63
|
+
: (this.#packageJson.workspaces?.packages ?? []);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
if (!Array.isArray(patterns) || patterns.some((p) => typeof p !== "string")) {
|
|
67
|
+
throw new Error("Invalid workspace patterns");
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const projects = await findPackages(this.#dirPath, {
|
|
71
|
+
patterns,
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
const excludeRoot = (projects: Project[]) => {
|
|
75
|
+
return projects.filter((project) => project.rootDir !== this.#dirPath);
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
return excludeRoot(projects);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
async write(packageJson: PackageJson) {
|
|
82
|
+
await writePackageJSON(this.#pkgPath, packageJson);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
#hasPnpmWorkspace() {
|
|
86
|
+
return this.hasFile("pnpm-workspace.yaml");
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
async #readPnpmWorkspaceManifest() {
|
|
90
|
+
const manifestPath = this.#fromPkgDir("pnpm-workspace.yaml");
|
|
91
|
+
const manifestContent = await readFile(manifestPath, "utf-8");
|
|
92
|
+
const manifest = yamlParse(manifestContent);
|
|
93
|
+
|
|
94
|
+
if (!manifest) {
|
|
95
|
+
throw new Error("Can't read pnpm workspace manifest");
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
return manifest;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
#fromPkgDir(...args: string[]) {
|
|
102
|
+
return path.join(this.#dirPath, ...args);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
export async function createPkg(cwd: string): Promise<Pkg | null> {
|
|
107
|
+
let pkgPath: string;
|
|
108
|
+
try {
|
|
109
|
+
pkgPath = await resolvePackageJSON(cwd);
|
|
110
|
+
} catch {
|
|
111
|
+
return null;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
const packageJson = await readPackageJSON(pkgPath);
|
|
115
|
+
|
|
116
|
+
return new Pkg(packageJson, pkgPath);
|
|
117
|
+
}
|
package/src/run.ts
CHANGED
package/src/services/index.ts
DELETED
package/src/services/pkg.ts
DELETED
|
@@ -1,93 +0,0 @@
|
|
|
1
|
-
import fs from "node:fs";
|
|
2
|
-
import path from "node:path";
|
|
3
|
-
import { findWorkspacePackages, type Project } from "@pnpm/workspace.find-packages";
|
|
4
|
-
import { readWorkspaceManifest } from "@pnpm/workspace.read-manifest";
|
|
5
|
-
import { type NormalizedPackageJson, readPackageUp } from "read-package-up";
|
|
6
|
-
|
|
7
|
-
export type { Project };
|
|
8
|
-
|
|
9
|
-
export class PkgService {
|
|
10
|
-
#packageJson: NormalizedPackageJson;
|
|
11
|
-
#dirPath: string;
|
|
12
|
-
|
|
13
|
-
get dirPath() {
|
|
14
|
-
return this.#dirPath;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
get packageJson() {
|
|
18
|
-
return this.#packageJson;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
constructor(packageJson: NormalizedPackageJson, pkgPath: string) {
|
|
22
|
-
this.#packageJson = packageJson;
|
|
23
|
-
this.#dirPath = path.dirname(pkgPath);
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
info() {
|
|
27
|
-
return {
|
|
28
|
-
packageJson: this.#packageJson,
|
|
29
|
-
dirPath: this.#dirPath,
|
|
30
|
-
};
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
hasFile(name: string, dir?: string) {
|
|
34
|
-
const filepath = dir ? path.join(dir, name) : this.#fromApp(name);
|
|
35
|
-
return fs.existsSync(filepath);
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
isMonorepo() {
|
|
39
|
-
return this.#packageJson.workspaces !== undefined || this.#hasPnpmWorkspace();
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
async getWorkspaceProjects() {
|
|
43
|
-
let patterns: string[];
|
|
44
|
-
|
|
45
|
-
if (this.#hasPnpmWorkspace()) {
|
|
46
|
-
const manifest = await readWorkspaceManifest(this.#dirPath);
|
|
47
|
-
|
|
48
|
-
if (!manifest) {
|
|
49
|
-
throw new Error("Can't read pnpm workspace manifest");
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
patterns = manifest.packages;
|
|
53
|
-
} else {
|
|
54
|
-
patterns = Array.isArray(this.#packageJson.workspaces)
|
|
55
|
-
? this.#packageJson.workspaces
|
|
56
|
-
: (this.#packageJson.workspaces?.packages ?? []);
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
if (!Array.isArray(patterns) || patterns.some((p) => typeof p !== "string")) {
|
|
60
|
-
throw new Error("Invalid workspace patterns");
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
const projects = await findWorkspacePackages(this.#dirPath, {
|
|
64
|
-
patterns,
|
|
65
|
-
});
|
|
66
|
-
|
|
67
|
-
const excludeRoot = (projects: Project[]) => {
|
|
68
|
-
return projects.filter((project) => project.rootDir !== this.#dirPath);
|
|
69
|
-
};
|
|
70
|
-
|
|
71
|
-
return excludeRoot(projects);
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
#hasPnpmWorkspace() {
|
|
75
|
-
return this.hasFile("pnpm-workspace.yaml");
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
#fromApp(...args: string[]) {
|
|
79
|
-
return path.join(this.#dirPath, ...args);
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
export async function createPkgService(cwd: string): Promise<PkgService | null> {
|
|
84
|
-
const searchResult = await readPackageUp({ cwd });
|
|
85
|
-
|
|
86
|
-
if (!searchResult) {
|
|
87
|
-
return null;
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
const { packageJson, path } = searchResult;
|
|
91
|
-
|
|
92
|
-
return new PkgService(packageJson, path);
|
|
93
|
-
}
|
package/src/version.ts
DELETED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|