+54
-20
lines changedFilter options
+54
-20
lines changed Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
1
-
const { readFile, writeFile } = require('fs/promises')
2
-
const { resolve } = require('path')
1
+
const { readFile, writeFile } = require('node:fs/promises')
2
+
const { resolve } = require('node:path')
3
+
const parseJSON = require('json-parse-even-better-errors')
4
+
3
5
const updateDeps = require('./update-dependencies.js')
4
6
const updateScripts = require('./update-scripts.js')
5
7
const updateWorkspaces = require('./update-workspaces.js')
6
8
const normalize = require('./normalize.js')
7
-
8
-
const parseJSON = require('json-parse-even-better-errors')
9
+
const { read, parse } = require('./read-package.js')
9
10
10
11
// a list of handy specialized helper functions that take
11
12
// care of special cases that are handled by the npm cli
@@ -126,9 +127,8 @@ class PackageJson {
126
127
this.#path = path
127
128
let parseErr
128
129
try {
129
-
this.#readFileContent = await readFile(this.filename, 'utf8')
130
+
this.#readFileContent = await read(this.filename)
130
131
} catch (err) {
131
-
err.message = `Could not read package.json: ${err}`
132
132
if (!parseIndex) {
133
133
throw err
134
134
}
@@ -158,12 +158,7 @@ class PackageJson {
158
158
159
159
// Load data from a JSON string/buffer
160
160
fromJSON (data) {
161
-
try {
162
-
this.#manifest = parseJSON(data)
163
-
} catch (err) {
164
-
err.message = `Invalid package.json: ${err}`
165
-
throw err
166
-
}
161
+
this.#manifest = parse(data)
167
162
return this
168
163
}
169
164
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
1
1
const valid = require('semver/functions/valid')
2
2
const clean = require('semver/functions/clean')
3
-
const fs = require('fs/promises')
4
-
const path = require('path')
3
+
const fs = require('node:fs/promises')
4
+
const path = require('node:path')
5
5
const { log } = require('proc-log')
6
6
7
7
/**
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
1
+
// This is JUST the code needed to open a package.json file and parse it.
2
+
// It's isolated out so that code needing to parse a package.json file can do so in the same way as this module does, without needing to require the whole module, or needing to require the underlying parsing library.
3
+
4
+
const { readFile } = require('fs/promises')
5
+
const parseJSON = require('json-parse-even-better-errors')
6
+
7
+
async function read (filename) {
8
+
try {
9
+
const data = await readFile(filename, 'utf8')
10
+
return data
11
+
} catch (err) {
12
+
err.message = `Could not read package.json: ${err}`
13
+
throw err
14
+
}
15
+
}
16
+
17
+
function parse (data) {
18
+
try {
19
+
const content = parseJSON(data)
20
+
return content
21
+
} catch (err) {
22
+
err.message = `Invalid package.json: ${err}`
23
+
throw err
24
+
}
25
+
}
26
+
27
+
// This is what most external libs will use.
28
+
// PackageJson will call read and parse separately
29
+
async function readPackage (filename) {
30
+
const data = await read(filename)
31
+
const content = parse(data)
32
+
return content
33
+
}
34
+
35
+
module.exports = {
36
+
read,
37
+
parse,
38
+
readPackage,
39
+
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
1
1
{
2
2
"name": "@npmcli/package-json",
3
-
"version": "5.1.1",
3
+
"version": "5.2.0",
4
4
"description": "Programmatic API to update package.json",
5
5
"main": "lib/index.js",
6
6
"files": [
Original file line number Diff line number Diff line change
@@ -91,7 +91,7 @@
91
91
"@npmcli/config": "^8.3.3",
92
92
"@npmcli/fs": "^3.1.1",
93
93
"@npmcli/map-workspaces": "^3.0.6",
94
-
"@npmcli/package-json": "^5.1.1",
94
+
"@npmcli/package-json": "^5.2.0",
95
95
"@npmcli/promise-spawn": "^7.0.2",
96
96
"@npmcli/redact": "^2.0.0",
97
97
"@npmcli/run-script": "^8.1.0",
@@ -1716,9 +1716,9 @@
1716
1716
}
1717
1717
},
1718
1718
"node_modules/@npmcli/package-json": {
1719
-
"version": "5.1.1",
1720
-
"resolved": "https://registry.npmjs.org/@npmcli/package-json/-/package-json-5.1.1.tgz",
1721
-
"integrity": "sha512-uTq5j/UqUzbOaOxVy+osfOhpqOiLfUZ0Ut33UbcyyAPJbZcJsf4Mrsyb8r58FoIFlofw0iOFsuCA/oDK14VDJQ==",
1719
+
"version": "5.2.0",
1720
+
"resolved": "https://registry.npmjs.org/@npmcli/package-json/-/package-json-5.2.0.tgz",
1721
+
"integrity": "sha512-qe/kiqqkW0AGtvBjL8TJKZk/eBBSpnJkUWvHdQ9jM2lKHXRYYJuyNpJPlJw3c8QjC2ow6NZYiLExhUaeJelbxQ==",
1722
1722
"inBundle": true,
1723
1723
"license": "ISC",
1724
1724
"dependencies": {
Original file line number Diff line number Diff line change
@@ -56,7 +56,7 @@
56
56
"@npmcli/config": "^8.3.3",
57
57
"@npmcli/fs": "^3.1.1",
58
58
"@npmcli/map-workspaces": "^3.0.6",
59
-
"@npmcli/package-json": "^5.1.1",
59
+
"@npmcli/package-json": "^5.2.0",
60
60
"@npmcli/promise-spawn": "^7.0.2",
61
61
"@npmcli/redact": "^2.0.0",
62
62
"@npmcli/run-script": "^8.1.0",
You can’t perform that action at this time.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4