Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: @npmcli/package-json refactor #3455

Merged
merged 1 commit into from Jun 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
37 changes: 9 additions & 28 deletions lib/init.js
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
@@ -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.