Skip to content

Commit 6254b6f

Browse files
ruyadornowraithgar
authored andcommitted
chore: @npmcli/package-json refactor
Refactor set-script and init to use @npmcli/package-json as a uniformed way to update and save package.json files. Fixes: #3234 Relates to: npm/statusboard#368 PR-URL: #3455 Credit: @ruyadorno Close: #3455 Reviewed-by: @nlf
1 parent f3dce09 commit 6254b6f

12 files changed

+341
-102
lines changed

lib/init.js

+9-28
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ const initJson = require('init-package-json')
55
const npa = require('npm-package-arg')
66
const rpj = require('read-package-json-fast')
77
const libexec = require('libnpmexec')
8-
const parseJSON = require('json-parse-even-better-errors')
98
const mapWorkspaces = require('@npmcli/map-workspaces')
9+
const PackageJson = require('@npmcli/package-json')
1010

1111
const getLocationMsg = require('./exec/get-workspace-location-msg.js')
1212
const BaseCommand = require('./base-command.js')
@@ -199,35 +199,16 @@ class Init extends BaseCommand {
199199
return
200200
}
201201

202-
let manifest
203-
try {
204-
manifest =
205-
fs.readFileSync(resolve(this.npm.localPrefix, 'package.json'), 'utf-8')
206-
} catch (error) {
207-
throw new Error('package.json not found')
208-
}
209-
210-
try {
211-
manifest = parseJSON(manifest)
212-
} catch (error) {
213-
throw new Error(`Invalid package.json: ${error}`)
214-
}
202+
const pkgJson = await PackageJson.load(this.npm.localPrefix)
215203

216-
if (!manifest.workspaces)
217-
manifest.workspaces = []
218-
219-
manifest.workspaces.push(relative(this.npm.localPrefix, workspacePath))
220-
221-
// format content
222-
const {
223-
[Symbol.for('indent')]: indent,
224-
[Symbol.for('newline')]: newline,
225-
} = manifest
226-
227-
const content = (JSON.stringify(manifest, null, indent) + '\n')
228-
.replace(/\n/g, newline)
204+
pkgJson.update({
205+
workspaces: [
206+
...(pkgJson.content.workspaces || []),
207+
relative(this.npm.localPrefix, workspacePath),
208+
],
209+
})
229210

230-
fs.writeFileSync(resolve(this.npm.localPrefix, 'package.json'), content)
211+
await pkgJson.save()
231212
}
232213
}
233214

lib/set-script.js

+19-31
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1+
const { resolve } = require('path')
12
const log = require('npmlog')
2-
const fs = require('fs')
3-
const parseJSON = require('json-parse-even-better-errors')
43
const rpj = require('read-package-json-fast')
5-
const { resolve } = require('path')
4+
const PackageJson = require('@npmcli/package-json')
65

76
const BaseCommand = require('./base-command.js')
87
class SetScript extends BaseCommand {
@@ -51,7 +50,7 @@ class SetScript extends BaseCommand {
5150

5251
async setScript (args) {
5352
this.validate(args)
54-
const warn = this.doSetScript(this.npm.localPrefix, args[0], args[1])
53+
const warn = await this.doSetScript(this.npm.localPrefix, args[0], args[1])
5554
if (warn)
5655
log.warn('set-script', `Script "${args[0]}" was overwritten`)
5756
}
@@ -66,7 +65,7 @@ class SetScript extends BaseCommand {
6665

6766
for (const [name, path] of this.workspaces) {
6867
try {
69-
const warn = this.doSetScript(path, args[0], args[1])
68+
const warn = await this.doSetScript(path, args[0], args[1])
7069
if (warn) {
7170
log.warn('set-script', `Script "${args[0]}" was overwritten`)
7271
log.warn(` in workspace: ${name}`)
@@ -84,39 +83,28 @@ class SetScript extends BaseCommand {
8483
// returns a Boolean that will be true if
8584
// the requested script was overwritten
8685
// and false if it was set as a new script
87-
doSetScript (path, name, value) {
88-
// Set the script
89-
let manifest
86+
async doSetScript (path, name, value) {
9087
let warn = false
9188

92-
try {
93-
manifest = fs.readFileSync(resolve(path, 'package.json'), 'utf-8')
94-
} catch (error) {
95-
throw new Error('package.json not found')
96-
}
97-
98-
try {
99-
manifest = parseJSON(manifest)
100-
} catch (error) {
101-
throw new Error(`Invalid package.json: ${error}`)
102-
}
89+
const pkgJson = await PackageJson.load(path)
90+
const { scripts } = pkgJson.content
10391

104-
if (!manifest.scripts)
105-
manifest.scripts = {}
92+
const overwriting =
93+
scripts
94+
&& scripts[name]
95+
&& scripts[name] !== value
10696

107-
if (manifest.scripts[name] && manifest.scripts[name] !== value)
97+
if (overwriting)
10898
warn = true
109-
manifest.scripts[name] = value
11099

111-
// format content
112-
const {
113-
[Symbol.for('indent')]: indent,
114-
[Symbol.for('newline')]: newline,
115-
} = manifest
100+
pkgJson.update({
101+
scripts: {
102+
...scripts,
103+
[name]: value,
104+
},
105+
})
116106

117-
const content = (JSON.stringify(manifest, null, indent) + '\n')
118-
.replace(/\n/g, newline)
119-
fs.writeFileSync(resolve(path, 'package.json'), content)
107+
await pkgJson.save()
120108

121109
return warn
122110
}

node_modules/@npmcli/package-json/LICENSE

+18
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

node_modules/@npmcli/package-json/lib/index.js

+106
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

node_modules/@npmcli/package-json/lib/update-dependencies.js

+72
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

node_modules/@npmcli/package-json/lib/update-scripts.js

+28
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

node_modules/@npmcli/package-json/lib/update-workspaces.js

+25
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)