Skip to content

Commit

Permalink
wip: added support to npm pkg set --json
Browse files Browse the repository at this point in the history
  • Loading branch information
ruyadorno committed Jul 2, 2021
1 parent e4e480e commit 04d466d
Show file tree
Hide file tree
Showing 23 changed files with 214 additions and 46 deletions.
3 changes: 3 additions & 0 deletions docs/content/commands/npm-audit.md
Expand Up @@ -244,6 +244,9 @@ recommended that you do not use this option!

Whether or not to output JSON data, rather than the normal output.

* In `npm pkg set` it enables parsing set values with JSON.parse() before
saving them to your `package.json`.

Not supported by all npm commands.

#### `package-lock-only`
Expand Down
3 changes: 3 additions & 0 deletions docs/content/commands/npm-config.md
Expand Up @@ -104,6 +104,9 @@ global config.
Whether or not to output JSON data, rather than the normal output.
* In `npm pkg set` it enables parsing set values with JSON.parse() before
saving them to your `package.json`.
Not supported by all npm commands.
#### `global`
Expand Down
3 changes: 3 additions & 0 deletions docs/content/commands/npm-explain.md
Expand Up @@ -63,6 +63,9 @@ node_modules/nyc/node_modules/find-up

Whether or not to output JSON data, rather than the normal output.

* In `npm pkg set` it enables parsing set values with JSON.parse() before
saving them to your `package.json`.

Not supported by all npm commands.

#### `workspace`
Expand Down
3 changes: 3 additions & 0 deletions docs/content/commands/npm-fund.md
Expand Up @@ -73,6 +73,9 @@ test-workspaces-fund@1.0.0

Whether or not to output JSON data, rather than the normal output.

* In `npm pkg set` it enables parsing set values with JSON.parse() before
saving them to your `package.json`.

Not supported by all npm commands.

#### `browser`
Expand Down
3 changes: 3 additions & 0 deletions docs/content/commands/npm-ls.md
Expand Up @@ -91,6 +91,9 @@ upon by the current project.

Whether or not to output JSON data, rather than the normal output.

* In `npm pkg set` it enables parsing set values with JSON.parse() before
saving them to your `package.json`.

Not supported by all npm commands.

#### `long`
Expand Down
3 changes: 3 additions & 0 deletions docs/content/commands/npm-org.md
Expand Up @@ -87,6 +87,9 @@ password, npm will prompt on the command line for one.

Whether or not to output JSON data, rather than the normal output.

* In `npm pkg set` it enables parsing set values with JSON.parse() before
saving them to your `package.json`.

Not supported by all npm commands.

#### `parseable`
Expand Down
3 changes: 3 additions & 0 deletions docs/content/commands/npm-outdated.md
Expand Up @@ -104,6 +104,9 @@ upon by the current project.

Whether or not to output JSON data, rather than the normal output.

* In `npm pkg set` it enables parsing set values with JSON.parse() before
saving them to your `package.json`.

Not supported by all npm commands.

#### `long`
Expand Down
3 changes: 3 additions & 0 deletions docs/content/commands/npm-pack.md
Expand Up @@ -34,6 +34,9 @@ Note: This is NOT honored by other network related commands, eg `dist-tags`,

Whether or not to output JSON data, rather than the normal output.

* In `npm pkg set` it enables parsing set values with JSON.parse() before
saving them to your `package.json`.

Not supported by all npm commands.

#### `pack-destination`
Expand Down
28 changes: 20 additions & 8 deletions docs/content/commands/npm-pkg.md
Expand Up @@ -98,12 +98,18 @@ Returned values are always in **json** format.
npm pkg set contributors[0].name='Foo' contributors[0].email='foo@bar.ca'
```
It's also possible to cast the setting value to either a `number` or a
`boolean`, for example to set a `"private": true` property in your
`package.json` file:
It's also possible to parse values as json prior to saving them to your
`package.json` file, for example in order to set a `"private": true`
property:
```bash
npm pkg set private=true --pkg-type=boolean
npm pkg set private=true --json
```
It also enables saving values as numbers:
```bash
npm pkg set tap.timeout=60 --json
```
* `npm pkg delete <key>`
Expand Down Expand Up @@ -173,16 +179,22 @@ mistakes, unnecessary performance degradation, and malicious input.
* Allow unpublishing all versions of a published package.
* Allow conflicting peerDependencies to be installed in the root project.
* Implicitly set `--yes` during `npm init`.
* Allow clobbering existing values in `npm pkg`
If you don't have a clear idea of what you want to do, it is strongly
recommended that you do not use this option!
#### `pkg-type`
#### `json`
* Default: false
* Type: Boolean
Whether or not to output JSON data, rather than the normal output.
* Default: "string"
* Type: String
* In `npm pkg set` it enables parsing set values with JSON.parse() before
saving them to your `package.json`.
The type in which to convert a value set by `npm pkg`.
Not supported by all npm commands.
#### `workspace`
Expand Down
3 changes: 3 additions & 0 deletions docs/content/commands/npm-profile.md
Expand Up @@ -91,6 +91,9 @@ The base URL of the npm registry.

Whether or not to output JSON data, rather than the normal output.

* In `npm pkg set` it enables parsing set values with JSON.parse() before
saving them to your `package.json`.

Not supported by all npm commands.

#### `parseable`
Expand Down
3 changes: 3 additions & 0 deletions docs/content/commands/npm-prune.md
Expand Up @@ -75,6 +75,9 @@ Note: This is NOT honored by other network related commands, eg `dist-tags`,

Whether or not to output JSON data, rather than the normal output.

* In `npm pkg set` it enables parsing set values with JSON.parse() before
saving them to your `package.json`.

Not supported by all npm commands.

#### `workspace`
Expand Down
3 changes: 3 additions & 0 deletions docs/content/commands/npm-search.md
Expand Up @@ -55,6 +55,9 @@ Show extended information in `ls`, `search`, and `help-search`.

Whether or not to output JSON data, rather than the normal output.

* In `npm pkg set` it enables parsing set values with JSON.parse() before
saving them to your `package.json`.

Not supported by all npm commands.

#### `color`
Expand Down
3 changes: 3 additions & 0 deletions docs/content/commands/npm-team.md
Expand Up @@ -138,6 +138,9 @@ Output parseable results from commands that write to standard output. For

Whether or not to output JSON data, rather than the normal output.

* In `npm pkg set` it enables parsing set values with JSON.parse() before
saving them to your `package.json`.

Not supported by all npm commands.

<!-- AUTOGENERATED CONFIG DESCRIPTIONS END -->
Expand Down
3 changes: 3 additions & 0 deletions docs/content/commands/npm-version.md
Expand Up @@ -47,6 +47,9 @@ Tag the commit when using the `npm version` command.
Whether or not to output JSON data, rather than the normal output.
* In `npm pkg set` it enables parsing set values with JSON.parse() before
saving them to your `package.json`.
Not supported by all npm commands.
#### `preid`
Expand Down
3 changes: 3 additions & 0 deletions docs/content/commands/npm-view.md
Expand Up @@ -105,6 +105,9 @@ npm view connect versions

Whether or not to output JSON data, rather than the normal output.

* In `npm pkg set` it enables parsing set values with JSON.parse() before
saving them to your `package.json`.

Not supported by all npm commands.

#### `workspace`
Expand Down
10 changes: 3 additions & 7 deletions docs/content/using-npm/config.md
Expand Up @@ -695,6 +695,9 @@ number, if not already set in package.json.

Whether or not to output JSON data, rather than the normal output.

* In `npm pkg set` it enables parsing set values with JSON.parse() before
saving them to your `package.json`.

Not supported by all npm commands.

#### `key`
Expand Down Expand Up @@ -916,13 +919,6 @@ For `list` this means the output will be based on the tree described by the
Output parseable results from commands that write to standard output. For
`npm search`, this will be tab-separated table format.

#### `pkg-type`

* Default: "string"
* Type: String

The type in which to convert a value set by `npm pkg`.

#### `prefer-offline`

* Default: false
Expand Down
6 changes: 3 additions & 3 deletions lib/pkg.js
Expand Up @@ -25,7 +25,7 @@ class Pkg extends BaseCommand {
static get params () {
return [
'force',
'pkg-type',
'json',
'workspace',
'workspaces',
]
Expand Down Expand Up @@ -109,7 +109,7 @@ class Pkg extends BaseCommand {
throw setError()

const force = this.npm.config.get('force')
const castType = this.npm.config.get('pkg-type')
const json = this.npm.config.get('json')
const pkgJson = await PackageJson.load(this.prefix)
const q = new Queryable(pkgJson.content)
for (const arg of args) {
Expand All @@ -118,7 +118,7 @@ class Pkg extends BaseCommand {
if (!key || !value)
throw setError()

q.set(key, value, { force, castType })
q.set(key, json ? JSON.parse(value) : value, { force })
}

pkgJson.update(q.toJSON())
Expand Down
12 changes: 3 additions & 9 deletions lib/utils/config/definitions.js
Expand Up @@ -1030,6 +1030,9 @@ define('json', {
description: `
Whether or not to output JSON data, rather than the normal output.
* In \`npm pkg set\` it enables parsing set values with JSON.parse()
before saving them to your \`package.json\`.
Not supported by all npm commands.
`,
flatten,
Expand Down Expand Up @@ -1356,15 +1359,6 @@ define('parseable', {
flatten,
})

define('pkg-type', {
default: 'string',
type: String,
description: `
The type in which to convert a value set by \`npm pkg\`.
`,
flatten,
})

define('prefer-offline', {
default: false,
type: Boolean,
Expand Down
2 changes: 1 addition & 1 deletion tap-snapshots/test/lib/load-all-commands.js.test.cjs
Expand Up @@ -689,7 +689,7 @@ npm pkg get [<key> [<key> ...]]
npm pkg delete <key> [<key> ...]
Options:
[-f|--force] [--pkg-type <pkg-type>]
[-f|--force] [--json]
[-w|--workspace <workspace-name> [-w|--workspace <workspace-name> ...]]
[-ws|--workspaces]
Expand Down
13 changes: 3 additions & 10 deletions tap-snapshots/test/lib/utils/config/definitions.js.test.cjs
Expand Up @@ -100,7 +100,6 @@ Array [
"package-lock-only",
"pack-destination",
"parseable",
"pkg-type",
"prefer-offline",
"prefer-online",
"prefix",
Expand Down Expand Up @@ -951,6 +950,9 @@ exports[`test/lib/utils/config/definitions.js TAP > config description for json
Whether or not to output JSON data, rather than the normal output.
* In \`npm pkg set\` it enables parsing set values with JSON.parse() before
saving them to your \`package.json\`.
Not supported by all npm commands.
`

Expand Down Expand Up @@ -1240,15 +1242,6 @@ Output parseable results from commands that write to standard output. For
\`npm search\`, this will be tab-separated table format.
`

exports[`test/lib/utils/config/definitions.js TAP > config description for pkg-type 1`] = `
#### \`pkg-type\`
* Default: "string"
* Type: String
The type in which to convert a value set by \`npm pkg\`.
`

exports[`test/lib/utils/config/definitions.js TAP > config description for prefer-offline 1`] = `
#### \`prefer-offline\`
Expand Down
10 changes: 3 additions & 7 deletions tap-snapshots/test/lib/utils/config/describe-all.js.test.cjs
Expand Up @@ -574,6 +574,9 @@ number, if not already set in package.json.
Whether or not to output JSON data, rather than the normal output.
* In \`npm pkg set\` it enables parsing set values with JSON.parse() before
saving them to your \`package.json\`.
Not supported by all npm commands.
#### \`key\`
Expand Down Expand Up @@ -795,13 +798,6 @@ For \`list\` this means the output will be based on the tree described by the
Output parseable results from commands that write to standard output. For
\`npm search\`, this will be tab-separated table format.
#### \`pkg-type\`
* Default: "string"
* Type: String
The type in which to convert a value set by \`npm pkg\`.
#### \`prefer-offline\`
* Default: false
Expand Down
2 changes: 1 addition & 1 deletion tap-snapshots/test/lib/utils/npm-usage.js.test.cjs
Expand Up @@ -772,7 +772,7 @@ All commands:
npm pkg delete <key> [<key> ...]
Options:
[-f|--force] [--pkg-type <pkg-type>]
[-f|--force] [--json]
[-w|--workspace <workspace-name> [-w|--workspace <workspace-name> ...]]
[-ws|--workspaces]
Expand Down

0 comments on commit 04d466d

Please sign in to comment.