@pnpm/workspace.workspace-manifest-writer 1001.0.3

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/README.md ADDED
@@ -0,0 +1,13 @@
1
+ # @pnpm/workspace.workspace-manifest-writer
2
+
3
+ > Updates the workspace manifest file
4
+
5
+ ## Install
6
+
7
+ ```
8
+ pnpm add @pnpm/workspace.workspace-manifest-writer
9
+ ```
10
+
11
+ ## LICENSE
12
+
13
+ MIT
package/lib/index.d.ts ADDED
@@ -0,0 +1,19 @@
1
+ import type { Catalogs } from '@pnpm/catalogs.types';
2
+ import { type GLOBAL_CONFIG_YAML_FILENAME, WORKSPACE_MANIFEST_FILENAME } from '@pnpm/constants';
3
+ import type { ResolvedCatalogEntry } from '@pnpm/lockfile.types';
4
+ import type { Project } from '@pnpm/types';
5
+ import { type WorkspaceManifest } from '@pnpm/workspace.workspace-manifest-reader';
6
+ export type FileName = typeof GLOBAL_CONFIG_YAML_FILENAME | typeof WORKSPACE_MANIFEST_FILENAME;
7
+ export declare function updateWorkspaceManifest(dir: string, opts: {
8
+ updatedFields?: Partial<WorkspaceManifest>;
9
+ updatedCatalogs?: Catalogs;
10
+ updatedOverrides?: Record<string, string>;
11
+ fileName?: FileName;
12
+ cleanupUnusedCatalogs?: boolean;
13
+ allProjects?: Project[];
14
+ }): Promise<void>;
15
+ export interface NewCatalogs {
16
+ [catalogName: string]: {
17
+ [dependencyName: string]: Pick<ResolvedCatalogEntry, 'specifier'>;
18
+ };
19
+ }
package/lib/index.js ADDED
@@ -0,0 +1,174 @@
1
+ import fs from 'node:fs';
2
+ import path from 'node:path';
3
+ import util from 'node:util';
4
+ import { WORKSPACE_MANIFEST_FILENAME } from '@pnpm/constants';
5
+ import { sortKeysByPriority } from '@pnpm/object.key-sorting';
6
+ import { validateWorkspaceManifest } from '@pnpm/workspace.workspace-manifest-reader';
7
+ import { patchDocument } from '@pnpm/yaml.document-sync';
8
+ import { equals } from 'ramda';
9
+ import writeFileAtomic from 'write-file-atomic';
10
+ import yaml from 'yaml';
11
+ const DEFAULT_FILENAME = WORKSPACE_MANIFEST_FILENAME;
12
+ async function writeManifestFile(dir, fileName, manifest) {
13
+ const manifestStr = manifest.toString({
14
+ lineWidth: 0, // This is setting line width to never wrap
15
+ singleQuote: true, // Prefer single quotes over double quotes
16
+ });
17
+ await fs.promises.mkdir(dir, { recursive: true });
18
+ await writeFileAtomic(path.join(dir, fileName), manifestStr);
19
+ }
20
+ async function readManifestRaw(file) {
21
+ try {
22
+ return (await fs.promises.readFile(file)).toString();
23
+ }
24
+ catch (err) {
25
+ if (util.types.isNativeError(err) && 'code' in err && err.code === 'ENOENT') {
26
+ return undefined;
27
+ }
28
+ throw err;
29
+ }
30
+ }
31
+ export async function updateWorkspaceManifest(dir, opts) {
32
+ const fileName = opts.fileName ?? DEFAULT_FILENAME;
33
+ const workspaceManifestStr = await readManifestRaw(path.join(dir, fileName));
34
+ const document = workspaceManifestStr != null
35
+ ? yaml.parseDocument(workspaceManifestStr)
36
+ : new yaml.Document();
37
+ let manifest = document.toJSON();
38
+ validateWorkspaceManifest(manifest);
39
+ manifest ??= {};
40
+ let shouldBeUpdated = opts.updatedCatalogs != null && addCatalogs(manifest, opts.updatedCatalogs);
41
+ if (opts.cleanupUnusedCatalogs) {
42
+ shouldBeUpdated = removePackagesFromWorkspaceCatalog(manifest, opts.allProjects ?? []) || shouldBeUpdated;
43
+ }
44
+ const updatedFields = { ...opts.updatedFields };
45
+ for (const [key, value] of Object.entries(updatedFields)) {
46
+ if (!equals(manifest[key], value)) {
47
+ shouldBeUpdated = true;
48
+ if (value == null) {
49
+ delete manifest[key];
50
+ }
51
+ else {
52
+ manifest[key] = value;
53
+ }
54
+ }
55
+ }
56
+ if (opts.updatedOverrides) {
57
+ manifest.overrides ??= {};
58
+ for (const [key, value] of Object.entries(opts.updatedOverrides)) {
59
+ if (!equals(manifest.overrides[key], value)) {
60
+ shouldBeUpdated = true;
61
+ manifest.overrides[key] = value;
62
+ }
63
+ }
64
+ }
65
+ if (!shouldBeUpdated) {
66
+ return;
67
+ }
68
+ if (Object.keys(manifest).length === 0) {
69
+ await fs.promises.rm(path.join(dir, fileName));
70
+ return;
71
+ }
72
+ manifest = sortKeysByPriority({
73
+ priority: { packages: 0 },
74
+ deep: true,
75
+ }, manifest);
76
+ patchDocument(document, manifest);
77
+ await writeManifestFile(dir, fileName, document);
78
+ }
79
+ function addCatalogs(manifest, newCatalogs) {
80
+ let shouldBeUpdated = false;
81
+ for (const catalogName in newCatalogs) {
82
+ let targetCatalog = catalogName === 'default'
83
+ ? manifest.catalog ?? manifest.catalogs?.default
84
+ : manifest.catalogs?.[catalogName];
85
+ const targetCatalogWasNil = targetCatalog == null;
86
+ for (const [dependencyName, specifier] of Object.entries(newCatalogs[catalogName] ?? {})) {
87
+ if (specifier == null) {
88
+ continue;
89
+ }
90
+ targetCatalog ??= {};
91
+ if (targetCatalog[dependencyName] !== specifier) {
92
+ targetCatalog[dependencyName] = specifier;
93
+ shouldBeUpdated = true;
94
+ }
95
+ }
96
+ if (targetCatalog == null)
97
+ continue;
98
+ if (targetCatalogWasNil) {
99
+ if (catalogName === 'default') {
100
+ manifest.catalog = targetCatalog;
101
+ }
102
+ else {
103
+ manifest.catalogs ??= {};
104
+ manifest.catalogs[catalogName] = targetCatalog;
105
+ }
106
+ }
107
+ }
108
+ return shouldBeUpdated;
109
+ }
110
+ function removePackagesFromWorkspaceCatalog(manifest, packagesJson) {
111
+ let shouldBeUpdated = false;
112
+ if (packagesJson.length === 0 || (manifest.catalog == null && manifest.catalogs == null)) {
113
+ return shouldBeUpdated;
114
+ }
115
+ const packageReferences = {};
116
+ for (const pkg of packagesJson) {
117
+ const pkgManifest = pkg.manifest;
118
+ const dependencyTypes = [
119
+ pkgManifest.dependencies,
120
+ pkgManifest.devDependencies,
121
+ pkgManifest.optionalDependencies,
122
+ pkgManifest.peerDependencies,
123
+ ];
124
+ for (const deps of dependencyTypes) {
125
+ if (!deps)
126
+ continue;
127
+ for (const [pkgName, version] of Object.entries(deps)) {
128
+ if (!packageReferences[pkgName]) {
129
+ packageReferences[pkgName] = new Set();
130
+ }
131
+ packageReferences[pkgName].add(version);
132
+ }
133
+ }
134
+ }
135
+ if (manifest.catalog) {
136
+ const packagesToRemove = Object.keys(manifest.catalog).filter(pkg => !packageReferences[pkg]?.has('catalog:'));
137
+ for (const pkg of packagesToRemove) {
138
+ delete manifest.catalog[pkg];
139
+ shouldBeUpdated = true;
140
+ }
141
+ if (Object.keys(manifest.catalog).length === 0) {
142
+ delete manifest.catalog;
143
+ shouldBeUpdated = true;
144
+ }
145
+ }
146
+ if (manifest.catalogs) {
147
+ const catalogsToRemove = [];
148
+ for (const [catalogName, catalog] of Object.entries(manifest.catalogs)) {
149
+ if (!catalog)
150
+ continue;
151
+ const packagesToRemove = Object.keys(catalog).filter(pkg => {
152
+ const references = packageReferences[pkg];
153
+ return !references?.has(`catalog:${catalogName}`) && !references?.has('catalog:');
154
+ });
155
+ for (const pkg of packagesToRemove) {
156
+ delete catalog[pkg];
157
+ shouldBeUpdated = true;
158
+ }
159
+ if (Object.keys(catalog).length === 0) {
160
+ catalogsToRemove.push(catalogName);
161
+ shouldBeUpdated = true;
162
+ }
163
+ }
164
+ for (const catalogName of catalogsToRemove) {
165
+ delete manifest.catalogs[catalogName];
166
+ }
167
+ if (Object.keys(manifest.catalogs).length === 0) {
168
+ delete manifest.catalogs;
169
+ shouldBeUpdated = true;
170
+ }
171
+ }
172
+ return shouldBeUpdated;
173
+ }
174
+ //# sourceMappingURL=index.js.map
package/package.json ADDED
@@ -0,0 +1,60 @@
1
+ {
2
+ "name": "@pnpm/workspace.workspace-manifest-writer",
3
+ "version": "1001.0.3",
4
+ "description": "Updates the workspace manifest file",
5
+ "keywords": [
6
+ "pnpm",
7
+ "pnpm11"
8
+ ],
9
+ "license": "MIT",
10
+ "funding": "https://opencollective.com/pnpm",
11
+ "repository": "https://github.com/pnpm/pnpm/tree/main/workspace/workspace-manifest-writer",
12
+ "homepage": "https://github.com/pnpm/pnpm/tree/main/workspace/workspace-manifest-writer#readme",
13
+ "bugs": {
14
+ "url": "https://github.com/pnpm/pnpm/issues"
15
+ },
16
+ "type": "module",
17
+ "main": "lib/index.js",
18
+ "types": "lib/index.d.ts",
19
+ "exports": {
20
+ ".": "./lib/index.js"
21
+ },
22
+ "files": [
23
+ "lib",
24
+ "!*.map"
25
+ ],
26
+ "dependencies": {
27
+ "ramda": "npm:@pnpm/ramda@0.28.1",
28
+ "write-file-atomic": "^7.0.0",
29
+ "yaml": "^2.8.1",
30
+ "@pnpm/catalogs.types": "1000.0.0",
31
+ "@pnpm/constants": "1001.3.1",
32
+ "@pnpm/lockfile.types": "1002.0.2",
33
+ "@pnpm/object.key-sorting": "1000.0.1",
34
+ "@pnpm/workspace.workspace-manifest-reader": "1000.2.5",
35
+ "@pnpm/yaml.document-sync": "1000.0.0-0",
36
+ "@pnpm/types": "1000.9.0"
37
+ },
38
+ "devDependencies": {
39
+ "@types/ramda": "0.29.12",
40
+ "@types/write-file-atomic": "^4.0.3",
41
+ "read-yaml-file": "^3.0.0",
42
+ "write-yaml-file": "^6.0.0",
43
+ "@pnpm/prepare": "1000.0.4",
44
+ "@pnpm/prepare-temp-dir": "1000.0.0",
45
+ "@pnpm/workspace.workspace-manifest-writer": "1001.0.3",
46
+ "@pnpm/workspace.projects-reader": "1000.0.43"
47
+ },
48
+ "engines": {
49
+ "node": ">=22.13"
50
+ },
51
+ "jest": {
52
+ "preset": "@pnpm/jest-config"
53
+ },
54
+ "scripts": {
55
+ "lint": "eslint \"src/**/*.ts\" \"test/**/*.ts\"",
56
+ "test": "pnpm run compile && pnpm run _test",
57
+ "compile": "tsgo --build && pnpm run lint --fix",
58
+ "_test": "cross-env NODE_OPTIONS=\"$NODE_OPTIONS --experimental-vm-modules --disable-warning=ExperimentalWarning --disable-warning=DEP0169\" jest"
59
+ }
60
+ }