From 04d466d13285e041d399a1cdda051356e72db97d Mon Sep 17 00:00:00 2001 From: Ruy Adorno Date: Fri, 2 Jul 2021 16:54:28 -0400 Subject: [PATCH] wip: added support to npm pkg set --json --- docs/content/commands/npm-audit.md | 3 + docs/content/commands/npm-config.md | 3 + docs/content/commands/npm-explain.md | 3 + docs/content/commands/npm-fund.md | 3 + docs/content/commands/npm-ls.md | 3 + docs/content/commands/npm-org.md | 3 + docs/content/commands/npm-outdated.md | 3 + docs/content/commands/npm-pack.md | 3 + docs/content/commands/npm-pkg.md | 28 ++-- docs/content/commands/npm-profile.md | 3 + docs/content/commands/npm-prune.md | 3 + docs/content/commands/npm-search.md | 3 + docs/content/commands/npm-team.md | 3 + docs/content/commands/npm-version.md | 3 + docs/content/commands/npm-view.md | 3 + docs/content/using-npm/config.md | 10 +- lib/pkg.js | 6 +- lib/utils/config/definitions.js | 12 +- .../test/lib/load-all-commands.js.test.cjs | 2 +- .../lib/utils/config/definitions.js.test.cjs | 13 +- .../lib/utils/config/describe-all.js.test.cjs | 10 +- .../test/lib/utils/npm-usage.js.test.cjs | 2 +- test/lib/pkg.js | 135 ++++++++++++++++++ 23 files changed, 214 insertions(+), 46 deletions(-) diff --git a/docs/content/commands/npm-audit.md b/docs/content/commands/npm-audit.md index 86bbb624a2e61..94b16b27bd7ed 100644 --- a/docs/content/commands/npm-audit.md +++ b/docs/content/commands/npm-audit.md @@ -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` diff --git a/docs/content/commands/npm-config.md b/docs/content/commands/npm-config.md index f2868cb8909a9..992e9ef70f1ba 100644 --- a/docs/content/commands/npm-config.md +++ b/docs/content/commands/npm-config.md @@ -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` diff --git a/docs/content/commands/npm-explain.md b/docs/content/commands/npm-explain.md index 0e50d7ae43343..3a87ee8e438ba 100644 --- a/docs/content/commands/npm-explain.md +++ b/docs/content/commands/npm-explain.md @@ -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` diff --git a/docs/content/commands/npm-fund.md b/docs/content/commands/npm-fund.md index 3dc5292b490a1..ec5f5a37fdb71 100644 --- a/docs/content/commands/npm-fund.md +++ b/docs/content/commands/npm-fund.md @@ -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` diff --git a/docs/content/commands/npm-ls.md b/docs/content/commands/npm-ls.md index 1f401fa956ff8..350f40a9991e5 100644 --- a/docs/content/commands/npm-ls.md +++ b/docs/content/commands/npm-ls.md @@ -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` diff --git a/docs/content/commands/npm-org.md b/docs/content/commands/npm-org.md index e6df560acfba3..269f5cc3ee5b8 100644 --- a/docs/content/commands/npm-org.md +++ b/docs/content/commands/npm-org.md @@ -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` diff --git a/docs/content/commands/npm-outdated.md b/docs/content/commands/npm-outdated.md index bc9263d7aeda7..40e5feafd4cc6 100644 --- a/docs/content/commands/npm-outdated.md +++ b/docs/content/commands/npm-outdated.md @@ -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` diff --git a/docs/content/commands/npm-pack.md b/docs/content/commands/npm-pack.md index 9507026278437..cd4a175919e7e 100644 --- a/docs/content/commands/npm-pack.md +++ b/docs/content/commands/npm-pack.md @@ -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` diff --git a/docs/content/commands/npm-pkg.md b/docs/content/commands/npm-pkg.md index df26b74932814..7ff0a4d97930f 100644 --- a/docs/content/commands/npm-pkg.md +++ b/docs/content/commands/npm-pkg.md @@ -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 ` @@ -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` diff --git a/docs/content/commands/npm-profile.md b/docs/content/commands/npm-profile.md index 63aa46540d32d..079440d785815 100644 --- a/docs/content/commands/npm-profile.md +++ b/docs/content/commands/npm-profile.md @@ -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` diff --git a/docs/content/commands/npm-prune.md b/docs/content/commands/npm-prune.md index ecb6bdcd6cb14..d9b5b068f7a4b 100644 --- a/docs/content/commands/npm-prune.md +++ b/docs/content/commands/npm-prune.md @@ -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` diff --git a/docs/content/commands/npm-search.md b/docs/content/commands/npm-search.md index 08c955e64b555..e30287635b56f 100644 --- a/docs/content/commands/npm-search.md +++ b/docs/content/commands/npm-search.md @@ -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` diff --git a/docs/content/commands/npm-team.md b/docs/content/commands/npm-team.md index 31b09c7ce22b7..c7d5defcc63c1 100644 --- a/docs/content/commands/npm-team.md +++ b/docs/content/commands/npm-team.md @@ -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. diff --git a/docs/content/commands/npm-version.md b/docs/content/commands/npm-version.md index d24207d1e7e34..a3e34153a06da 100644 --- a/docs/content/commands/npm-version.md +++ b/docs/content/commands/npm-version.md @@ -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` diff --git a/docs/content/commands/npm-view.md b/docs/content/commands/npm-view.md index ad2538eeb6c2a..b3d5df86e34a4 100644 --- a/docs/content/commands/npm-view.md +++ b/docs/content/commands/npm-view.md @@ -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` diff --git a/docs/content/using-npm/config.md b/docs/content/using-npm/config.md index 3e5d75b31f5a4..1feae3d64ac5a 100644 --- a/docs/content/using-npm/config.md +++ b/docs/content/using-npm/config.md @@ -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` @@ -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 diff --git a/lib/pkg.js b/lib/pkg.js index 3a1f40566adc9..9ba92c930e1f0 100644 --- a/lib/pkg.js +++ b/lib/pkg.js @@ -25,7 +25,7 @@ class Pkg extends BaseCommand { static get params () { return [ 'force', - 'pkg-type', + 'json', 'workspace', 'workspaces', ] @@ -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) { @@ -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()) diff --git a/lib/utils/config/definitions.js b/lib/utils/config/definitions.js index fcf4c0c82e93d..5375aeced6904 100644 --- a/lib/utils/config/definitions.js +++ b/lib/utils/config/definitions.js @@ -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, @@ -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, diff --git a/tap-snapshots/test/lib/load-all-commands.js.test.cjs b/tap-snapshots/test/lib/load-all-commands.js.test.cjs index d28d30d3cce60..3b0754c5227b5 100644 --- a/tap-snapshots/test/lib/load-all-commands.js.test.cjs +++ b/tap-snapshots/test/lib/load-all-commands.js.test.cjs @@ -689,7 +689,7 @@ npm pkg get [ [ ...]] npm pkg delete [ ...] Options: -[-f|--force] [--pkg-type ] +[-f|--force] [--json] [-w|--workspace [-w|--workspace ...]] [-ws|--workspaces] diff --git a/tap-snapshots/test/lib/utils/config/definitions.js.test.cjs b/tap-snapshots/test/lib/utils/config/definitions.js.test.cjs index d601644725044..7b2ffbd8de9b9 100644 --- a/tap-snapshots/test/lib/utils/config/definitions.js.test.cjs +++ b/tap-snapshots/test/lib/utils/config/definitions.js.test.cjs @@ -100,7 +100,6 @@ Array [ "package-lock-only", "pack-destination", "parseable", - "pkg-type", "prefer-offline", "prefer-online", "prefix", @@ -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. ` @@ -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\` diff --git a/tap-snapshots/test/lib/utils/config/describe-all.js.test.cjs b/tap-snapshots/test/lib/utils/config/describe-all.js.test.cjs index 36cb0716e7c85..babed32535fcd 100644 --- a/tap-snapshots/test/lib/utils/config/describe-all.js.test.cjs +++ b/tap-snapshots/test/lib/utils/config/describe-all.js.test.cjs @@ -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\` @@ -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 diff --git a/tap-snapshots/test/lib/utils/npm-usage.js.test.cjs b/tap-snapshots/test/lib/utils/npm-usage.js.test.cjs index 1d5c83203bef9..f417d41b6e50e 100644 --- a/tap-snapshots/test/lib/utils/npm-usage.js.test.cjs +++ b/tap-snapshots/test/lib/utils/npm-usage.js.test.cjs @@ -772,7 +772,7 @@ All commands: npm pkg delete [ ...] Options: - [-f|--force] [--pkg-type ] + [-f|--force] [--json] [-w|--workspace [-w|--workspace ...]] [-ws|--workspaces] diff --git a/test/lib/pkg.js b/test/lib/pkg.js index 18f87c9a3b606..42eb7c0cc5e9c 100644 --- a/test/lib/pkg.js +++ b/test/lib/pkg.js @@ -37,6 +37,7 @@ const readPackageJson = (path) => { t.afterEach(() => { config.global = false + config.json = false npm.localPrefix = t.testdirName OUTPUT = '' }) @@ -349,6 +350,140 @@ t.test('set = separate value', t => { }) }) +t.test('set --json', async t => { + config.json = true + npm.localPrefix = t.testdir({ + 'package.json': JSON.stringify({ + name: 'foo', + version: '1.1.1', + }), + }) + + await new Promise((res, rej) => { + pkg.exec(['set', 'private=true'], err => { + if (err) + rej(err) + + t.strictSame( + readPackageJson(), + { + name: 'foo', + version: '1.1.1', + private: true, + }, + 'should add boolean field to package.json' + ) + res() + }) + }) + + await new Promise((res, rej) => { + pkg.exec(['set', 'tap.timeout=60'], err => { + if (err) + rej(err) + + t.strictSame( + readPackageJson(), + { + name: 'foo', + version: '1.1.1', + private: true, + tap: { + timeout: 60, + }, + }, + 'should add number field to package.json' + ) + res() + }) + }) + + await new Promise((res, rej) => { + pkg.exec(['set', 'foo={ "bar": { "baz": "BAZ" } }'], err => { + if (err) + rej(err) + + t.strictSame( + readPackageJson(), + { + name: 'foo', + version: '1.1.1', + private: true, + tap: { + timeout: 60, + }, + foo: { + bar: { + baz: 'BAZ', + }, + }, + }, + 'should add object field to package.json' + ) + res() + }) + }) + + await new Promise((res, rej) => { + pkg.exec(['set', 'workspaces=["packages/*"]'], err => { + if (err) + rej(err) + + t.strictSame( + readPackageJson(), + { + name: 'foo', + version: '1.1.1', + private: true, + workspaces: [ + 'packages/*', + ], + tap: { + timeout: 60, + }, + foo: { + bar: { + baz: 'BAZ', + }, + }, + }, + 'should add object field to package.json' + ) + res() + }) + }) + + await new Promise((res, rej) => { + pkg.exec(['set', 'description="awesome"'], err => { + if (err) + rej(err) + + t.strictSame( + readPackageJson(), + { + name: 'foo', + version: '1.1.1', + description: 'awesome', + private: true, + workspaces: [ + 'packages/*', + ], + tap: { + timeout: 60, + }, + foo: { + bar: { + baz: 'BAZ', + }, + }, + }, + 'should add object field to package.json' + ) + res() + }) + }) +}) + t.test('delete no args', t => { npm.localPrefix = t.testdir({ 'package.json': JSON.stringify({ name: 'foo' }),