@pnpm/pkg-manifest.commands 1100.1.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 ADDED
@@ -0,0 +1,22 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015-2016 Rico Sta. Cruz and other contributors
4
+ Copyright (c) 2016-2026 Zoltan Kochan and other contributors
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ of this software and associated documentation files (the "Software"), to deal
8
+ in the Software without restriction, including without limitation the rights
9
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ copies of the Software, and to permit persons to whom the Software is
11
+ furnished to do so, subject to the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be included in all
14
+ copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ SOFTWARE.
package/lib/index.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ export * as pkg from './pkg.js';
2
+ export * as setScript from './setScript.js';
package/lib/index.js ADDED
@@ -0,0 +1,3 @@
1
+ export * as pkg from './pkg.js';
2
+ export * as setScript from './setScript.js';
3
+ //# sourceMappingURL=index.js.map
package/lib/pkg.d.ts ADDED
@@ -0,0 +1,18 @@
1
+ export declare const rcOptionsTypes: typeof cliOptionsTypes;
2
+ export declare function cliOptionsTypes(): Record<string, unknown>;
3
+ export declare const commandNames: string[];
4
+ interface PkgCommandOptions {
5
+ dir: string;
6
+ json?: boolean;
7
+ recursive?: boolean;
8
+ workspaceDir?: string;
9
+ selectedProjectsGraph?: Record<string, {
10
+ package: {
11
+ rootDir: string;
12
+ manifest: Record<string, unknown>;
13
+ };
14
+ }>;
15
+ }
16
+ export declare function handler(opts: PkgCommandOptions, params: string[]): Promise<string | void>;
17
+ export declare function help(): string;
18
+ export {};
package/lib/pkg.js ADDED
@@ -0,0 +1,205 @@
1
+ import path from 'node:path';
2
+ import { docsUrl, readProjectManifest, readProjectManifestOnly } from '@pnpm/cli.utils';
3
+ import { types as allTypes } from '@pnpm/config.reader';
4
+ import { PnpmError } from '@pnpm/error';
5
+ import { deleteObjectValueByPropertyPathString, getObjectValueByPropertyPathString, setObjectValueByPropertyPathString, } from '@pnpm/object.property-path';
6
+ import { renderHelp } from 'render-help';
7
+ export const rcOptionsTypes = cliOptionsTypes;
8
+ export function cliOptionsTypes() {
9
+ const types = allTypes;
10
+ return {
11
+ dir: types['dir'],
12
+ json: Boolean,
13
+ recursive: Boolean,
14
+ };
15
+ }
16
+ export const commandNames = ['pkg'];
17
+ export async function handler(opts, params) {
18
+ if (params.length === 0) {
19
+ throw new PnpmError('PKG_MISSING_SUBCOMMAND', 'Missing subcommand', {
20
+ hint: help(),
21
+ });
22
+ }
23
+ if (params[0] === '--help' || params[0] === '-h') {
24
+ return help();
25
+ }
26
+ const [subcmd, ...args] = params;
27
+ if (opts.recursive) {
28
+ return handleRecursiveCommand(opts, subcmd, args);
29
+ }
30
+ return runSubcommand(opts, subcmd, args);
31
+ }
32
+ async function runSubcommand(opts, subcmd, args) {
33
+ switch (subcmd) {
34
+ case 'get':
35
+ return pkgGet(opts, args);
36
+ case 'set':
37
+ return pkgSet(opts, args);
38
+ case 'delete':
39
+ return pkgDelete(opts, args);
40
+ case 'fix':
41
+ return pkgFix(opts);
42
+ default:
43
+ throw new PnpmError('PKG_UNKNOWN_SUBCOMMAND', `Unknown subcommand "${subcmd}"`, {
44
+ hint: help(),
45
+ });
46
+ }
47
+ }
48
+ async function handleRecursiveCommand(opts, subcmd, args) {
49
+ const workspaceDir = opts.workspaceDir;
50
+ if (!workspaceDir) {
51
+ throw new PnpmError('PKG_RECURSIVE_NO_ROOT', 'Cannot run recursively outside of a workspace');
52
+ }
53
+ const selectedProjects = opts.selectedProjectsGraph == null
54
+ ? []
55
+ : Object.values(opts.selectedProjectsGraph);
56
+ if (selectedProjects.length === 0) {
57
+ throw new PnpmError('PKG_RECURSIVE_NO_PACKAGES', 'No workspace packages were selected');
58
+ }
59
+ if (subcmd === 'get') {
60
+ const entries = await Promise.all(selectedProjects.map(async ({ package: pkg }) => {
61
+ const manifest = await readProjectManifestOnly(pkg.rootDir);
62
+ const pkgName = String(manifest.name ?? path.relative(workspaceDir, pkg.rootDir));
63
+ return [pkgName, selectFromManifest(manifest, args)];
64
+ }));
65
+ return JSON.stringify(Object.fromEntries(entries), undefined, 2);
66
+ }
67
+ await Promise.all(selectedProjects.map(({ package: pkg }) => runSubcommand({ ...opts, dir: pkg.rootDir }, subcmd, args)));
68
+ }
69
+ async function pkgGet(opts, args) {
70
+ const manifest = await readProjectManifestOnly(opts.dir);
71
+ if (args.length === 1) {
72
+ const value = getObjectValueByPropertyPathString(manifest, args[0]);
73
+ if (value === undefined)
74
+ return '';
75
+ if (opts.json)
76
+ return JSON.stringify(value, undefined, 2);
77
+ return typeof value === 'string' ? value : JSON.stringify(value, undefined, 2);
78
+ }
79
+ return JSON.stringify(selectFromManifest(manifest, args), undefined, 2);
80
+ }
81
+ function selectFromManifest(manifest, args) {
82
+ if (args.length === 0)
83
+ return manifest;
84
+ const result = {};
85
+ for (const key of args) {
86
+ result[key] = getObjectValueByPropertyPathString(manifest, key);
87
+ }
88
+ return result;
89
+ }
90
+ async function pkgSet(opts, args) {
91
+ if (args.length === 0) {
92
+ throw new PnpmError('PKG_SET_MISSING_ARGS', 'Missing key=value pairs', {
93
+ hint: help(),
94
+ });
95
+ }
96
+ const { manifest, writeProjectManifest } = await readProjectManifest(opts.dir);
97
+ for (const arg of args) {
98
+ const eqIndex = arg.indexOf('=');
99
+ if (eqIndex === -1) {
100
+ throw new PnpmError('PKG_SET_INVALID_ARG', `Invalid argument "${arg}". Expected key=value format`, {
101
+ hint: 'Example: pnpm pkg set name=my-package',
102
+ });
103
+ }
104
+ const key = arg.slice(0, eqIndex);
105
+ let value = arg.slice(eqIndex + 1);
106
+ if (opts.json) {
107
+ try {
108
+ value = JSON.parse(value);
109
+ }
110
+ catch {
111
+ throw new PnpmError('PKG_SET_JSON_PARSE', `Failed to parse value as JSON: "${value}"`);
112
+ }
113
+ }
114
+ setObjectValueByPropertyPathString(manifest, key, value);
115
+ }
116
+ await writeProjectManifest(manifest);
117
+ }
118
+ async function pkgDelete(opts, args) {
119
+ if (args.length === 0) {
120
+ throw new PnpmError('PKG_DELETE_MISSING_ARGS', 'Missing keys to delete', {
121
+ hint: help(),
122
+ });
123
+ }
124
+ const { manifest, writeProjectManifest } = await readProjectManifest(opts.dir);
125
+ for (const key of args) {
126
+ deleteObjectValueByPropertyPathString(manifest, key);
127
+ }
128
+ await writeProjectManifest(manifest);
129
+ }
130
+ async function pkgFix(opts) {
131
+ const { manifest, writeProjectManifest } = await readProjectManifest(opts.dir);
132
+ const m = manifest;
133
+ if ('name' in m && typeof m.name !== 'string') {
134
+ delete m.name;
135
+ }
136
+ if ('version' in m && typeof m.version !== 'string') {
137
+ delete m.version;
138
+ }
139
+ for (const field of ['dependencies', 'devDependencies', 'optionalDependencies', 'peerDependencies', 'scripts']) {
140
+ if (field in m && !isPlainObject(m[field])) {
141
+ delete m[field];
142
+ }
143
+ }
144
+ if ('bin' in m && typeof m.bin !== 'string' && !isPlainObject(m.bin)) {
145
+ delete m.bin;
146
+ }
147
+ await writeProjectManifest(manifest);
148
+ }
149
+ function isPlainObject(value) {
150
+ return typeof value === 'object' && value !== null && !Array.isArray(value);
151
+ }
152
+ export function help() {
153
+ return renderHelp({
154
+ description: 'Manages your package.json',
155
+ descriptionLists: [
156
+ {
157
+ title: 'Commands',
158
+ list: [
159
+ {
160
+ description: 'Retrieves a value from package.json',
161
+ name: 'get [<key> [<key> ...]]',
162
+ },
163
+ {
164
+ description: 'Sets a value in package.json',
165
+ name: 'set <key>=<value> [<key>=<value> ...]',
166
+ },
167
+ {
168
+ description: 'Deletes a key from package.json',
169
+ name: 'delete <key> [<key> ...]',
170
+ },
171
+ {
172
+ description: 'Auto corrects common errors in package.json',
173
+ name: 'fix',
174
+ },
175
+ ],
176
+ },
177
+ {
178
+ title: 'Options',
179
+ list: [
180
+ {
181
+ description: 'When setting, parse the value as JSON. When getting a single key, return its JSON-encoded form instead of the raw value',
182
+ name: '--json',
183
+ },
184
+ {
185
+ description: 'Run on every workspace project or every project selected by a filter',
186
+ name: '--recursive',
187
+ shortAlias: '-r',
188
+ },
189
+ ],
190
+ },
191
+ ],
192
+ url: docsUrl('pkg'),
193
+ usages: [
194
+ 'pnpm pkg get [<key> [<key> ...]]',
195
+ 'pnpm pkg set <key>=<value> [<key>=<value> ...]',
196
+ 'pnpm pkg delete <key> [<key> ...]',
197
+ 'pnpm pkg fix',
198
+ 'pnpm pkg set <key>=<value> --json',
199
+ 'pnpm -r pkg get name',
200
+ 'pnpm --filter <selector> pkg get name',
201
+ 'pnpm -r pkg set version=1.0.0',
202
+ ],
203
+ });
204
+ }
205
+ //# sourceMappingURL=pkg.js.map
@@ -0,0 +1,7 @@
1
+ export declare const rcOptionsTypes: typeof cliOptionsTypes;
2
+ export declare function cliOptionsTypes(): Record<string, unknown>;
3
+ export declare const commandNames: string[];
4
+ export declare function handler(opts: {
5
+ dir: string;
6
+ }, params: string[]): Promise<void>;
7
+ export declare function help(): string;
@@ -0,0 +1,31 @@
1
+ import { docsUrl, readProjectManifest } from '@pnpm/cli.utils';
2
+ import { types as allTypes } from '@pnpm/config.reader';
3
+ import { PnpmError } from '@pnpm/error';
4
+ import { setObjectValueByPropertyPath } from '@pnpm/object.property-path';
5
+ import { renderHelp } from 'render-help';
6
+ export const rcOptionsTypes = cliOptionsTypes;
7
+ export function cliOptionsTypes() {
8
+ const types = allTypes;
9
+ return { dir: types['dir'] };
10
+ }
11
+ export const commandNames = ['set-script', 'ss'];
12
+ export async function handler(opts, params) {
13
+ if (params.length < 2) {
14
+ throw new PnpmError('SET_SCRIPT_MISSING_ARGS', 'Missing script name or command', {
15
+ hint: help(),
16
+ });
17
+ }
18
+ const [name, ...commandParts] = params;
19
+ const command = commandParts.join(' ');
20
+ const { manifest, writeProjectManifest } = await readProjectManifest(opts.dir);
21
+ setObjectValueByPropertyPath(manifest, ['scripts', name], command);
22
+ await writeProjectManifest(manifest);
23
+ }
24
+ export function help() {
25
+ return renderHelp({
26
+ description: 'Set a script in package.json',
27
+ usages: ['pnpm set-script <name> <command>'],
28
+ url: docsUrl('set-script'),
29
+ });
30
+ }
31
+ //# sourceMappingURL=setScript.js.map
package/package.json ADDED
@@ -0,0 +1,52 @@
1
+ {
2
+ "name": "@pnpm/pkg-manifest.commands",
3
+ "version": "1100.1.0",
4
+ "description": "Commands for managing package.json",
5
+ "keywords": [
6
+ "pnpm",
7
+ "pnpm11",
8
+ "pkg"
9
+ ],
10
+ "license": "MIT",
11
+ "funding": "https://opencollective.com/pnpm",
12
+ "repository": "https://github.com/pnpm/pnpm/tree/main/pkg-manifest/commands",
13
+ "homepage": "https://github.com/pnpm/pnpm/tree/main/pkg-manifest/commands#readme",
14
+ "bugs": {
15
+ "url": "https://github.com/pnpm/pnpm/issues"
16
+ },
17
+ "type": "module",
18
+ "main": "lib/index.js",
19
+ "types": "lib/index.d.ts",
20
+ "exports": {
21
+ ".": "./lib/index.js"
22
+ },
23
+ "files": [
24
+ "lib",
25
+ "!*.map"
26
+ ],
27
+ "dependencies": {
28
+ "render-help": "^2.0.0",
29
+ "@pnpm/error": "1100.0.0",
30
+ "@pnpm/cli.utils": "1101.0.7",
31
+ "@pnpm/config.reader": "1101.4.0",
32
+ "@pnpm/object.property-path": "1100.1.0",
33
+ "@pnpm/types": "1101.1.1"
34
+ },
35
+ "devDependencies": {
36
+ "@jest/globals": "30.3.0",
37
+ "@pnpm/pkg-manifest.commands": "1100.1.0",
38
+ "@pnpm/prepare": "1100.0.10"
39
+ },
40
+ "engines": {
41
+ "node": ">=22.13"
42
+ },
43
+ "jest": {
44
+ "preset": "@pnpm/jest-config"
45
+ },
46
+ "scripts": {
47
+ "compile": "tsgo --build && pn lint --fix",
48
+ "lint": "eslint \"src/**/*.ts\" \"test/**/*.ts\"",
49
+ "test": "pn compile && pn .test",
50
+ ".test": "cross-env NODE_OPTIONS=\"$NODE_OPTIONS --experimental-vm-modules --disable-warning=ExperimentalWarning --disable-warning=DEP0169\" jest"
51
+ }
52
+ }