Skip to content

Commit

Permalink
chore: @npmcli/package-json refactor
Browse files Browse the repository at this point in the history
Refactor set-script and init to use @npmcli/package-json as a uniformed
way to update and save package.json files.

Fixes: npm#3234
Relates to: npm/statusboard#368

PR-URL: npm#3455
Credit: @ruyadorno
Close: npm#3455
Reviewed-by: @nlf
  • Loading branch information
ruyadorno authored and wraithgar committed Jun 23, 2021
1 parent f3dce09 commit 6254b6f
Show file tree
Hide file tree
Showing 12 changed files with 341 additions and 102 deletions.
37 changes: 9 additions & 28 deletions lib/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ const initJson = require('init-package-json')
const npa = require('npm-package-arg')
const rpj = require('read-package-json-fast')
const libexec = require('libnpmexec')
const parseJSON = require('json-parse-even-better-errors')
const mapWorkspaces = require('@npmcli/map-workspaces')
const PackageJson = require('@npmcli/package-json')

const getLocationMsg = require('./exec/get-workspace-location-msg.js')
const BaseCommand = require('./base-command.js')
Expand Down Expand Up @@ -199,35 +199,16 @@ class Init extends BaseCommand {
return
}

let manifest
try {
manifest =
fs.readFileSync(resolve(this.npm.localPrefix, 'package.json'), 'utf-8')
} catch (error) {
throw new Error('package.json not found')
}

try {
manifest = parseJSON(manifest)
} catch (error) {
throw new Error(`Invalid package.json: ${error}`)
}
const pkgJson = await PackageJson.load(this.npm.localPrefix)

if (!manifest.workspaces)
manifest.workspaces = []

manifest.workspaces.push(relative(this.npm.localPrefix, workspacePath))

// format content
const {
[Symbol.for('indent')]: indent,
[Symbol.for('newline')]: newline,
} = manifest

const content = (JSON.stringify(manifest, null, indent) + '\n')
.replace(/\n/g, newline)
pkgJson.update({
workspaces: [
...(pkgJson.content.workspaces || []),
relative(this.npm.localPrefix, workspacePath),
],
})

fs.writeFileSync(resolve(this.npm.localPrefix, 'package.json'), content)
await pkgJson.save()
}
}

Expand Down
50 changes: 19 additions & 31 deletions lib/set-script.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
const { resolve } = require('path')
const log = require('npmlog')
const fs = require('fs')
const parseJSON = require('json-parse-even-better-errors')
const rpj = require('read-package-json-fast')
const { resolve } = require('path')
const PackageJson = require('@npmcli/package-json')

const BaseCommand = require('./base-command.js')
class SetScript extends BaseCommand {
Expand Down Expand Up @@ -51,7 +50,7 @@ class SetScript extends BaseCommand {

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

for (const [name, path] of this.workspaces) {
try {
const warn = this.doSetScript(path, args[0], args[1])
const warn = await this.doSetScript(path, args[0], args[1])
if (warn) {
log.warn('set-script', `Script "${args[0]}" was overwritten`)
log.warn(` in workspace: ${name}`)
Expand All @@ -84,39 +83,28 @@ class SetScript extends BaseCommand {
// returns a Boolean that will be true if
// the requested script was overwritten
// and false if it was set as a new script
doSetScript (path, name, value) {
// Set the script
let manifest
async doSetScript (path, name, value) {
let warn = false

try {
manifest = fs.readFileSync(resolve(path, 'package.json'), 'utf-8')
} catch (error) {
throw new Error('package.json not found')
}

try {
manifest = parseJSON(manifest)
} catch (error) {
throw new Error(`Invalid package.json: ${error}`)
}
const pkgJson = await PackageJson.load(path)
const { scripts } = pkgJson.content

if (!manifest.scripts)
manifest.scripts = {}
const overwriting =
scripts
&& scripts[name]
&& scripts[name] !== value

if (manifest.scripts[name] && manifest.scripts[name] !== value)
if (overwriting)
warn = true
manifest.scripts[name] = value

// format content
const {
[Symbol.for('indent')]: indent,
[Symbol.for('newline')]: newline,
} = manifest
pkgJson.update({
scripts: {
...scripts,
[name]: value,
},
})

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

return warn
}
Expand Down
18 changes: 18 additions & 0 deletions node_modules/@npmcli/package-json/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

106 changes: 106 additions & 0 deletions node_modules/@npmcli/package-json/lib/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

72 changes: 72 additions & 0 deletions node_modules/@npmcli/package-json/lib/update-dependencies.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions node_modules/@npmcli/package-json/lib/update-scripts.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions node_modules/@npmcli/package-json/lib/update-workspaces.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 6254b6f

Please sign in to comment.