@pnpm/workspace.project-manifest-reader 1001.1.4

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,25 @@
1
+ # @pnpm/read-project-manifest
2
+
3
+ > Read a project manifest (called package.json in most cases)
4
+
5
+ <!--@shields('npm')-->
6
+ [![npm version](https://img.shields.io/npm/v/@pnpm/read-project-manifest.svg)](https://www.npmjs.com/package/@pnpm/read-project-manifest)
7
+ <!--/@-->
8
+
9
+ ## Installation
10
+
11
+ ```sh
12
+ pnpm add @pnpm/read-project-manifest
13
+ ```
14
+
15
+ ## Usage
16
+
17
+ ```ts
18
+ import { readProjectManifest } from '@pnpm/read-project-manifest'
19
+
20
+ const { manifest, fileName } = await readProjectManifest(process.cwd())
21
+ ```
22
+
23
+ ## License
24
+
25
+ MIT
package/lib/index.d.ts ADDED
@@ -0,0 +1,20 @@
1
+ import type { ProjectManifest } from '@pnpm/types';
2
+ export type WriteProjectManifest = (manifest: ProjectManifest, force?: boolean) => Promise<void>;
3
+ export declare function safeReadProjectManifestOnly(projectDir: string): Promise<ProjectManifest | null>;
4
+ export declare function readProjectManifest(projectDir: string): Promise<{
5
+ fileName: string;
6
+ manifest: ProjectManifest;
7
+ writeProjectManifest: WriteProjectManifest;
8
+ }>;
9
+ export declare function readProjectManifestOnly(projectDir: string): Promise<ProjectManifest>;
10
+ export declare function tryReadProjectManifest(projectDir: string): Promise<{
11
+ fileName: string;
12
+ manifest: ProjectManifest | null;
13
+ writeProjectManifest: WriteProjectManifest;
14
+ }>;
15
+ interface ReadExactProjectManifestResult {
16
+ manifest: ProjectManifest;
17
+ writeProjectManifest: WriteProjectManifest;
18
+ }
19
+ export declare function readExactProjectManifest(manifestPath: string): Promise<ReadExactProjectManifestResult>;
20
+ export {};
package/lib/index.js ADDED
@@ -0,0 +1,261 @@
1
+ import { promises as fs } from 'node:fs';
2
+ import path from 'node:path';
3
+ import { PnpmError } from '@pnpm/error';
4
+ import { convertEnginesRuntimeToDependencies } from '@pnpm/pkg-manifest.utils';
5
+ import { extractComments } from '@pnpm/text.comments-parser';
6
+ import { writeProjectManifest } from '@pnpm/workspace.project-manifest-writer';
7
+ import detectIndent from 'detect-indent';
8
+ import equal from 'fast-deep-equal';
9
+ import isWindows from 'is-windows';
10
+ import { readYamlFile } from 'read-yaml-file';
11
+ import { readJson5File, readJsonFile, } from './readFile.js';
12
+ export async function safeReadProjectManifestOnly(projectDir) {
13
+ try {
14
+ return await readProjectManifestOnly(projectDir);
15
+ }
16
+ catch (err) { // eslint-disable-line
17
+ if (err.code === 'ERR_PNPM_NO_IMPORTER_MANIFEST_FOUND') {
18
+ return null;
19
+ }
20
+ throw err;
21
+ }
22
+ }
23
+ export async function readProjectManifest(projectDir) {
24
+ const result = await tryReadProjectManifest(projectDir);
25
+ if (result.manifest !== null) {
26
+ return result;
27
+ }
28
+ throw new PnpmError('NO_IMPORTER_MANIFEST_FOUND', `No package.json (or package.yaml, or package.json5) was found in "${projectDir}".`);
29
+ }
30
+ export async function readProjectManifestOnly(projectDir) {
31
+ const { manifest } = await readProjectManifest(projectDir);
32
+ return manifest;
33
+ }
34
+ export async function tryReadProjectManifest(projectDir) {
35
+ try {
36
+ const manifestPath = path.join(projectDir, 'package.json');
37
+ const { data, text } = await readJsonFile(manifestPath);
38
+ return {
39
+ fileName: 'package.json',
40
+ manifest: convertManifestAfterRead(data),
41
+ writeProjectManifest: createManifestWriter({
42
+ ...detectFileFormatting(text),
43
+ initialManifest: data,
44
+ manifestPath,
45
+ }),
46
+ };
47
+ }
48
+ catch (err) { // eslint-disable-line
49
+ if (err.code !== 'ENOENT')
50
+ throw err;
51
+ }
52
+ try {
53
+ const manifestPath = path.join(projectDir, 'package.json5');
54
+ const { data, text } = await readJson5File(manifestPath);
55
+ return {
56
+ fileName: 'package.json5',
57
+ manifest: convertManifestAfterRead(data),
58
+ writeProjectManifest: createManifestWriter({
59
+ ...detectFileFormattingAndComments(text),
60
+ initialManifest: data,
61
+ manifestPath,
62
+ }),
63
+ };
64
+ }
65
+ catch (err) { // eslint-disable-line
66
+ if (err.code !== 'ENOENT')
67
+ throw err;
68
+ }
69
+ try {
70
+ const manifestPath = path.join(projectDir, 'package.yaml');
71
+ const manifest = await readPackageYaml(manifestPath);
72
+ return {
73
+ fileName: 'package.yaml',
74
+ manifest: convertManifestAfterRead(manifest),
75
+ writeProjectManifest: createManifestWriter({ initialManifest: manifest, manifestPath }),
76
+ };
77
+ }
78
+ catch (err) { // eslint-disable-line
79
+ if (err.code !== 'ENOENT')
80
+ throw err;
81
+ }
82
+ if (isWindows()) {
83
+ // ENOTDIR isn't used on Windows, but pnpm expects it.
84
+ let s;
85
+ try {
86
+ s = await fs.stat(projectDir);
87
+ }
88
+ catch (err) { // eslint-disable-line
89
+ // Ignore
90
+ }
91
+ if ((s != null) && !s.isDirectory()) {
92
+ const err = new Error(`"${projectDir}" is not a directory`);
93
+ // @ts-expect-error
94
+ err['code'] = 'ENOTDIR';
95
+ throw err;
96
+ }
97
+ }
98
+ const filePath = path.join(projectDir, 'package.json');
99
+ return {
100
+ fileName: 'package.json',
101
+ manifest: null,
102
+ writeProjectManifest: async (manifest) => writeProjectManifest(filePath, manifest),
103
+ };
104
+ }
105
+ function detectFileFormattingAndComments(text) {
106
+ const { comments, text: newText, hasFinalNewline } = extractComments(text);
107
+ return {
108
+ comments,
109
+ indent: detectIndent(newText).indent,
110
+ insertFinalNewline: hasFinalNewline,
111
+ };
112
+ }
113
+ function detectFileFormatting(text) {
114
+ return {
115
+ indent: detectIndent(text).indent,
116
+ insertFinalNewline: text.endsWith('\n'),
117
+ };
118
+ }
119
+ export async function readExactProjectManifest(manifestPath) {
120
+ const base = path.basename(manifestPath).toLowerCase();
121
+ switch (base) {
122
+ case 'package.json': {
123
+ const { data, text } = await readJsonFile(manifestPath);
124
+ return {
125
+ manifest: convertManifestAfterRead(data),
126
+ writeProjectManifest: createManifestWriter({
127
+ ...detectFileFormatting(text),
128
+ initialManifest: data,
129
+ manifestPath,
130
+ }),
131
+ };
132
+ }
133
+ case 'package.json5': {
134
+ const { data, text } = await readJson5File(manifestPath);
135
+ return {
136
+ manifest: convertManifestAfterRead(data),
137
+ writeProjectManifest: createManifestWriter({
138
+ ...detectFileFormattingAndComments(text),
139
+ initialManifest: data,
140
+ manifestPath,
141
+ }),
142
+ };
143
+ }
144
+ case 'package.yaml': {
145
+ const manifest = await readPackageYaml(manifestPath);
146
+ return {
147
+ manifest: convertManifestAfterRead(manifest),
148
+ writeProjectManifest: createManifestWriter({ initialManifest: manifest, manifestPath }),
149
+ };
150
+ }
151
+ }
152
+ throw new Error(`Not supported manifest name "${base}"`);
153
+ }
154
+ async function readPackageYaml(filePath) {
155
+ try {
156
+ return await readYamlFile(filePath);
157
+ }
158
+ catch (err) { // eslint-disable-line
159
+ if (err.name !== 'YAMLException')
160
+ throw err;
161
+ err.message = `${err.message}\nin ${filePath}`;
162
+ err.code = 'ERR_PNPM_YAML_PARSE';
163
+ throw err;
164
+ }
165
+ }
166
+ function createManifestWriter(opts) {
167
+ let initialManifest = normalize(opts.initialManifest);
168
+ return async (updatedManifest, force) => {
169
+ updatedManifest = convertManifestBeforeWrite(normalize(updatedManifest));
170
+ if (force === true || !equal(initialManifest, updatedManifest)) {
171
+ await writeProjectManifest(opts.manifestPath, updatedManifest, {
172
+ comments: opts.comments,
173
+ indent: opts.indent,
174
+ insertFinalNewline: opts.insertFinalNewline,
175
+ });
176
+ initialManifest = normalize(updatedManifest);
177
+ return Promise.resolve(undefined);
178
+ }
179
+ return Promise.resolve(undefined);
180
+ };
181
+ }
182
+ function convertManifestAfterRead(manifest) {
183
+ convertEnginesRuntimeToDependencies(manifest, 'devEngines', 'devDependencies');
184
+ convertEnginesRuntimeToDependencies(manifest, 'engines', 'dependencies');
185
+ return manifest;
186
+ }
187
+ function convertManifestBeforeWrite(manifest) {
188
+ convertDependenciesToEnginesRuntime(manifest, 'devDependencies', 'devEngines');
189
+ convertDependenciesToEnginesRuntime(manifest, 'dependencies', 'engines');
190
+ return manifest;
191
+ }
192
+ function convertDependenciesToEnginesRuntime(manifest, dependenciesFieldName, enginesFieldName) {
193
+ for (const runtimeName of ['node', 'deno', 'bun']) {
194
+ const dep = manifest[dependenciesFieldName]?.[runtimeName];
195
+ if (typeof dep === 'string' && dep.startsWith('runtime:')) {
196
+ const version = dep.replace(/^runtime:/, '');
197
+ manifest[enginesFieldName] ??= {};
198
+ const runtimeEntry = {
199
+ name: runtimeName,
200
+ version,
201
+ onFail: 'download',
202
+ };
203
+ const enginesField = manifest[enginesFieldName];
204
+ if (!enginesField.runtime) {
205
+ enginesField.runtime = runtimeEntry;
206
+ }
207
+ else if (Array.isArray(enginesField.runtime)) {
208
+ const existing = enginesField.runtime.find(({ name }) => name === runtimeName);
209
+ if (existing) {
210
+ Object.assign(existing, runtimeEntry);
211
+ }
212
+ else {
213
+ enginesField.runtime.push(runtimeEntry);
214
+ }
215
+ }
216
+ else if (enginesField.runtime.name === runtimeName) {
217
+ Object.assign(enginesField.runtime, runtimeEntry);
218
+ }
219
+ else {
220
+ enginesField.runtime = [
221
+ enginesField.runtime,
222
+ runtimeEntry,
223
+ ];
224
+ }
225
+ if (manifest[dependenciesFieldName]) {
226
+ delete manifest[dependenciesFieldName][runtimeName];
227
+ }
228
+ }
229
+ }
230
+ }
231
+ const dependencyKeys = new Set([
232
+ 'dependencies',
233
+ 'devDependencies',
234
+ 'optionalDependencies',
235
+ 'peerDependencies',
236
+ ]);
237
+ function normalize(manifest) {
238
+ const result = {};
239
+ for (const key in manifest) {
240
+ if (Object.hasOwn(manifest, key)) {
241
+ const value = manifest[key];
242
+ if (typeof value !== 'object' || !dependencyKeys.has(key)) {
243
+ result[key] = structuredClone(value);
244
+ }
245
+ else {
246
+ const keys = Object.keys(value);
247
+ if (keys.length !== 0) {
248
+ keys.sort();
249
+ const sortedValue = {};
250
+ for (const k of keys) {
251
+ // @ts-expect-error this is fine
252
+ sortedValue[k] = value[k];
253
+ }
254
+ result[key] = sortedValue;
255
+ }
256
+ }
257
+ }
258
+ }
259
+ return result;
260
+ }
261
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1,9 @@
1
+ import type { ProjectManifest } from '@pnpm/types';
2
+ export declare function readJson5File(filePath: string): Promise<{
3
+ data: ProjectManifest;
4
+ text: string;
5
+ }>;
6
+ export declare function readJsonFile(filePath: string): Promise<{
7
+ data: ProjectManifest;
8
+ text: string;
9
+ }>;
@@ -0,0 +1,35 @@
1
+ import gfs from '@pnpm/fs.graceful-fs';
2
+ import JSON5 from 'json5';
3
+ import parseJson from 'parse-json';
4
+ import stripBom from 'strip-bom';
5
+ export async function readJson5File(filePath) {
6
+ const text = await readFileWithoutBom(filePath);
7
+ try {
8
+ return {
9
+ data: JSON5.parse(text),
10
+ text,
11
+ };
12
+ }
13
+ catch (err) { // eslint-disable-line
14
+ err.message = `${err.message} in ${filePath}`;
15
+ err['code'] = 'ERR_PNPM_JSON5_PARSE';
16
+ throw err;
17
+ }
18
+ }
19
+ export async function readJsonFile(filePath) {
20
+ const text = await readFileWithoutBom(filePath);
21
+ try {
22
+ return {
23
+ data: parseJson(text, filePath),
24
+ text,
25
+ };
26
+ }
27
+ catch (err) { // eslint-disable-line
28
+ err['code'] = 'ERR_PNPM_JSON_PARSE';
29
+ throw err;
30
+ }
31
+ }
32
+ async function readFileWithoutBom(path) {
33
+ return stripBom(await gfs.readFile(path, 'utf8'));
34
+ }
35
+ //# sourceMappingURL=readFile.js.map
package/package.json ADDED
@@ -0,0 +1,63 @@
1
+ {
2
+ "name": "@pnpm/workspace.project-manifest-reader",
3
+ "version": "1001.1.4",
4
+ "description": "Read a project manifest (called package.json in most cases)",
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/project-manifest-reader",
12
+ "homepage": "https://github.com/pnpm/pnpm/tree/main/workspace/project-manifest-reader#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
+ "detect-indent": "7.0.1",
28
+ "fast-deep-equal": "^3.1.3",
29
+ "is-windows": "^1.0.2",
30
+ "json5": "^2.2.3",
31
+ "parse-json": "^8.3.0",
32
+ "read-yaml-file": "^3.0.0",
33
+ "strip-bom": "^5.0.0",
34
+ "@pnpm/error": "1000.0.5",
35
+ "@pnpm/pkg-manifest.utils": "1001.0.6",
36
+ "@pnpm/fs.graceful-fs": "1000.0.1",
37
+ "@pnpm/text.comments-parser": "1000.0.0",
38
+ "@pnpm/types": "1000.9.0",
39
+ "@pnpm/workspace.project-manifest-writer": "1000.0.11"
40
+ },
41
+ "peerDependencies": {
42
+ "@pnpm/logger": ">=1001.0.0 <1002.0.0"
43
+ },
44
+ "devDependencies": {
45
+ "@types/is-windows": "^1.0.2",
46
+ "@types/parse-json": "^4.0.2",
47
+ "tempy": "3.0.0",
48
+ "@pnpm/test-fixtures": "1000.0.0",
49
+ "@pnpm/workspace.project-manifest-reader": "1001.1.4"
50
+ },
51
+ "engines": {
52
+ "node": ">=22.13"
53
+ },
54
+ "jest": {
55
+ "preset": "@pnpm/jest-config"
56
+ },
57
+ "scripts": {
58
+ "lint": "eslint \"src/**/*.ts\" \"test/**/*.ts\"",
59
+ "_test": "cross-env NODE_OPTIONS=\"$NODE_OPTIONS --experimental-vm-modules --disable-warning=ExperimentalWarning --disable-warning=DEP0169\" jest",
60
+ "test": "pnpm run compile && pnpm run _test",
61
+ "compile": "tsgo --build && pnpm run lint --fix"
62
+ }
63
+ }