@vlandoss/clibuddy 0.0.10 → 0.0.11-git-9445806.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.
@@ -0,0 +1,72 @@
1
+ import { ChalkInstance } from "chalk";
2
+ import * as _$zx from "zx";
3
+ import { Options, ProcessOutput, Shell as Shell$1 } from "zx";
4
+ import { Project } from "@pnpm/workspace.find-packages";
5
+ import { NormalizedPackageJson } from "read-package-up";
6
+
7
+ //#region src/colors.d.ts
8
+ declare const colorIsSupported: () => boolean;
9
+ declare const colorize: (hex: string) => ChalkInstance | (<T>(x: T) => T);
10
+ declare const palette: {
11
+ bold: ChalkInstance | (<T>(x: T) => T);
12
+ italic: ChalkInstance | (<T>(x: T) => T);
13
+ link: ChalkInstance | (<T>(x: T) => T);
14
+ muted: ChalkInstance | (<T>(x: T) => T);
15
+ vland: ChalkInstance | (<T>(x: T) => T);
16
+ };
17
+ //#endregion
18
+ //#region src/run.d.ts
19
+ declare function run(fn: () => Promise<void>, logger: {
20
+ error: (...args: unknown[]) => void;
21
+ }): Promise<void>;
22
+ //#endregion
23
+ //#region src/services/pkg.d.ts
24
+ declare class PkgService {
25
+ #private;
26
+ get dirPath(): string;
27
+ get packageJson(): NormalizedPackageJson;
28
+ constructor(packageJson: NormalizedPackageJson, pkgPath: string);
29
+ info(): {
30
+ packageJson: NormalizedPackageJson;
31
+ dirPath: string;
32
+ };
33
+ hasFile(name: string, dir?: string): boolean;
34
+ isMonorepo(): boolean;
35
+ getWorkspaceProjects(): Promise<Project[]>;
36
+ }
37
+ declare function createPkgService(cwd: string): Promise<PkgService | null>;
38
+ //#endregion
39
+ //#region src/services/shell/types.d.ts
40
+ type Shell = Shell$1;
41
+ type ShellOptions = Partial<Options>;
42
+ type CreateOptions = ShellOptions & {
43
+ localBaseBinPath?: string | Array<string>;
44
+ };
45
+ //#endregion
46
+ //#region src/services/shell/shell.d.ts
47
+ declare class ShellService {
48
+ #private;
49
+ constructor(options: ShellOptions);
50
+ get options(): Partial<_$zx.Options>;
51
+ get $(): Shell;
52
+ child(options: ShellOptions): ShellService;
53
+ quiet(options?: ShellOptions): ShellService;
54
+ at(cwd: string, options?: ShellOptions): ShellService;
55
+ }
56
+ //#endregion
57
+ //#region src/services/shell/create.d.ts
58
+ declare const cwd: string;
59
+ declare function quote(arg: string): string;
60
+ declare const isRaw: (arg: unknown) => arg is {
61
+ stdout: string;
62
+ };
63
+ declare function createShellService(options?: CreateOptions): ShellService;
64
+ //#endregion
65
+ //#region src/services/shell/utils.d.ts
66
+ declare function isProcessOutput(value: unknown): value is ProcessOutput;
67
+ declare function getPreferLocal(localBaseBinPath: string | Array<string> | undefined): string[] | undefined;
68
+ //#endregion
69
+ //#region src/version.d.ts
70
+ declare function getVersion(pkg: PkgService): string;
71
+ //#endregion
72
+ export { CreateOptions, PkgService, type Project, Shell, ShellOptions, ShellService, colorIsSupported, colorize, createPkgService, createShellService, cwd, getPreferLocal, getVersion, isProcessOutput, isRaw, palette, quote, run };
package/dist/index.mjs ADDED
@@ -0,0 +1,172 @@
1
+ import chalk from "chalk";
2
+ import supportsColor from "supports-color";
3
+ import path from "node:path";
4
+ import { $, ProcessOutput } from "zx";
5
+ import fs from "node:fs";
6
+ import { findWorkspacePackages } from "@pnpm/workspace.find-packages";
7
+ import { readWorkspaceManifest } from "@pnpm/workspace.read-manifest";
8
+ import { readPackageUp } from "read-package-up";
9
+ //#region src/colors.ts
10
+ const colorIsSupported = () => supportsColor.stdout && !process.env.NO_COLOR;
11
+ const identity = (x) => x;
12
+ const safe = (style) => colorIsSupported() ? style : identity;
13
+ const colorize = (hex) => safe(chalk.hex(hex));
14
+ const palette = {
15
+ bold: safe(chalk.bold),
16
+ italic: safe(chalk.italic),
17
+ link: safe(chalk.underline),
18
+ muted: safe(chalk.dim),
19
+ vland: colorize("#36d399")
20
+ };
21
+ //#endregion
22
+ //#region src/services/shell/utils.ts
23
+ function isProcessOutput(value) {
24
+ return value instanceof ProcessOutput;
25
+ }
26
+ const getLocalBinPath = (dirPath) => path.join(dirPath, "node_modules", ".bin");
27
+ function getPreferLocal(localBaseBinPath) {
28
+ return !localBaseBinPath ? void 0 : Array.isArray(localBaseBinPath) ? localBaseBinPath.map(getLocalBinPath) : [localBaseBinPath].map(getLocalBinPath);
29
+ }
30
+ //#endregion
31
+ //#region src/run.ts
32
+ function hasMessage(error) {
33
+ return typeof error === "object" && error !== null && "message" in error && typeof error.message === "string";
34
+ }
35
+ function formatError(error) {
36
+ if (hasMessage(error)) return error.message;
37
+ return String(error);
38
+ }
39
+ async function run(fn, logger) {
40
+ try {
41
+ await fn();
42
+ } catch (error) {
43
+ if (!isProcessOutput(error)) logger.error(formatError(error));
44
+ process.exit(1);
45
+ }
46
+ }
47
+ //#endregion
48
+ //#region src/services/pkg.ts
49
+ var PkgService = class {
50
+ #packageJson;
51
+ #dirPath;
52
+ get dirPath() {
53
+ return this.#dirPath;
54
+ }
55
+ get packageJson() {
56
+ return this.#packageJson;
57
+ }
58
+ constructor(packageJson, pkgPath) {
59
+ this.#packageJson = packageJson;
60
+ this.#dirPath = path.dirname(pkgPath);
61
+ }
62
+ info() {
63
+ return {
64
+ packageJson: this.#packageJson,
65
+ dirPath: this.#dirPath
66
+ };
67
+ }
68
+ hasFile(name, dir) {
69
+ const filepath = dir ? path.join(dir, name) : this.#fromApp(name);
70
+ return fs.existsSync(filepath);
71
+ }
72
+ isMonorepo() {
73
+ return this.#packageJson.workspaces !== void 0 || this.#hasPnpmWorkspace();
74
+ }
75
+ async getWorkspaceProjects() {
76
+ let patterns;
77
+ if (this.#hasPnpmWorkspace()) {
78
+ const manifest = await readWorkspaceManifest(this.#dirPath);
79
+ if (!manifest) throw new Error("Can't read pnpm workspace manifest");
80
+ patterns = manifest.packages;
81
+ } else patterns = Array.isArray(this.#packageJson.workspaces) ? this.#packageJson.workspaces : this.#packageJson.workspaces?.packages ?? [];
82
+ if (!Array.isArray(patterns) || patterns.some((p) => typeof p !== "string")) throw new Error("Invalid workspace patterns");
83
+ const projects = await findWorkspacePackages(this.#dirPath, { patterns });
84
+ const excludeRoot = (projects) => {
85
+ return projects.filter((project) => project.rootDir !== this.#dirPath);
86
+ };
87
+ return excludeRoot(projects);
88
+ }
89
+ #hasPnpmWorkspace() {
90
+ return this.hasFile("pnpm-workspace.yaml");
91
+ }
92
+ #fromApp(...args) {
93
+ return path.join(this.#dirPath, ...args);
94
+ }
95
+ };
96
+ async function createPkgService(cwd) {
97
+ const searchResult = await readPackageUp({ cwd });
98
+ if (!searchResult) return null;
99
+ const { packageJson, path } = searchResult;
100
+ return new PkgService(packageJson, path);
101
+ }
102
+ //#endregion
103
+ //#region src/services/shell/shell.ts
104
+ var ShellService = class ShellService {
105
+ #shell;
106
+ #options;
107
+ constructor(options) {
108
+ this.#options = Object.freeze(options);
109
+ this.#shell = $(options);
110
+ }
111
+ get options() {
112
+ return this.#options;
113
+ }
114
+ get $() {
115
+ return this.#shell;
116
+ }
117
+ child(options) {
118
+ return new ShellService({
119
+ ...this.#options,
120
+ ...options
121
+ });
122
+ }
123
+ quiet(options) {
124
+ return this.child({
125
+ ...options,
126
+ verbose: false
127
+ });
128
+ }
129
+ at(cwd, options) {
130
+ const getLocals = (locals) => typeof locals === "boolean" ? [] : typeof locals === "undefined" ? [] : Array.isArray(locals) ? locals : [locals];
131
+ const cwdPreferLocal = getPreferLocal(cwd);
132
+ const preferLocal = options?.preferLocal === false ? false : [
133
+ ...getLocals(this.#options.preferLocal),
134
+ ...getLocals(options?.preferLocal),
135
+ ...cwdPreferLocal ? cwdPreferLocal : []
136
+ ];
137
+ return this.child({
138
+ ...options,
139
+ cwd,
140
+ preferLocal
141
+ });
142
+ }
143
+ };
144
+ //#endregion
145
+ //#region src/services/shell/create.ts
146
+ const cwd = fs.realpathSync(process.cwd());
147
+ function quote(arg) {
148
+ if (/^[\w./:=@-]+$/i.test(arg) || arg === "") return arg;
149
+ return arg.replace(/\\/g, "\\\\").replace(/'/g, "\\'").replace(/\f/g, "\\f").replace(/\n/g, "\\n").replace(/\r/g, "\\r").replace(/\t/g, "\\t").replace(/\v/g, "\\v").replace(/\0/g, "\\0");
150
+ }
151
+ const isRaw = (arg) => typeof arg === "object" && arg !== null && "stdout" in arg && typeof arg.stdout === "string";
152
+ function defaultQuote(arg) {
153
+ if (typeof arg === "string") return quote(arg);
154
+ if (isRaw(arg)) return arg.stdout;
155
+ throw TypeError(`Unsupported argument type: ${typeof arg}`);
156
+ }
157
+ function createShellService(options = {}) {
158
+ return new ShellService({
159
+ verbose: true,
160
+ cwd,
161
+ preferLocal: getPreferLocal(options.localBaseBinPath),
162
+ quote: defaultQuote,
163
+ ...options
164
+ });
165
+ }
166
+ //#endregion
167
+ //#region src/version.ts
168
+ function getVersion(pkg) {
169
+ return process.env.VERSION || pkg.packageJson.version;
170
+ }
171
+ //#endregion
172
+ export { PkgService, ShellService, colorIsSupported, colorize, createPkgService, createShellService, cwd, getPreferLocal, getVersion, isProcessOutput, isRaw, palette, quote, run };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vlandoss/clibuddy",
3
- "version": "0.0.10",
3
+ "version": "0.0.11-git-9445806.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": {
@@ -8,20 +8,23 @@
8
8
  },
9
9
  "repository": {
10
10
  "type": "git",
11
- "url": "git+https://github.com/variableland/dx.git"
11
+ "url": "git+https://github.com/variableland/dx.git",
12
+ "directory": "packages/clibuddy"
12
13
  },
13
14
  "license": "MIT",
14
15
  "author": "rcrd <rcrd@variable.land>",
15
16
  "type": "module",
16
17
  "exports": {
17
- ".": "./src/index.ts",
18
- "./test-helpers": {
19
- "bun": "./test-helpers/index.ts"
18
+ ".": {
19
+ "types": "./dist/index.d.mts",
20
+ "default": "./dist/index.mjs"
20
21
  }
21
22
  },
22
23
  "files": [
23
24
  "dist",
24
- "src"
25
+ "src",
26
+ "!src/**/__tests__",
27
+ "!src/**/*.test.*"
25
28
  ],
26
29
  "dependencies": {
27
30
  "@pnpm/workspace.find-packages": "1000.0.62",
@@ -35,9 +38,14 @@
35
38
  "access": "public"
36
39
  },
37
40
  "engines": {
41
+ "node": ">=20.0.0",
38
42
  "bun": ">=1.0.0"
39
43
  },
44
+ "devDependencies": {
45
+ "@vlandoss/tsdown-config": "^0.0.2-git-9445806.0"
46
+ },
40
47
  "scripts": {
48
+ "build": "tsdown",
41
49
  "test:types": "rr tsc"
42
50
  }
43
51
  }