Skip to content

Commit a0e60fb

Browse files
owlstronautwraithgar
authored andcommitted
feat: added init-private option
1 parent d2498df commit a0e60fb

File tree

7 files changed

+162
-2
lines changed

7 files changed

+162
-2
lines changed

docs/lib/content/commands/npm-init.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,11 @@ Generate it without having it ask any questions:
8787
$ npm init -y
8888
```
8989

90+
Set the private flag to `true` in package.json:
91+
```bash
92+
$ npm init --init-private -y
93+
```
94+
9095
### Workspaces support
9196

9297
It's possible to create a new workspace within your project by using the

lib/commands/init.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class Init extends BaseCommand {
2121
'init-module',
2222
'init-type',
2323
'init-version',
24+
'init-private',
2425
'yes',
2526
'force',
2627
'scope',
@@ -133,8 +134,14 @@ class Init extends BaseCommand {
133134
const scriptShell = this.npm.config.get('script-shell') || undefined
134135
const yes = this.npm.config.get('yes')
135136

137+
// only send the init-private flag if it is set
138+
const opts = { ...flatOptions }
139+
if (this.npm.config.isDefault('init-private')) {
140+
delete opts.initPrivate
141+
}
142+
136143
await libexec({
137-
...flatOptions,
144+
...opts,
138145
args: newArgs,
139146
localBin,
140147
globalBin,

tap-snapshots/test/lib/commands/config.js.test.cjs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ exports[`test/lib/commands/config.js TAP config list --json > output matches sna
7676
"init-module": "{CWD}/home/.npm-init.js",
7777
"init-type": "commonjs",
7878
"init-version": "1.0.0",
79+
"init-private": false,
7980
"init.author.email": "",
8081
"init.author.name": "",
8182
"init.author.url": "",
@@ -239,6 +240,7 @@ init-author-name = ""
239240
init-author-url = ""
240241
init-license = "ISC"
241242
init-module = "{CWD}/home/.npm-init.js"
243+
init-private = false
242244
init-type = "commonjs"
243245
init-version = "1.0.0"
244246
init.author.email = ""

tap-snapshots/test/lib/docs.js.test.cjs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -854,6 +854,15 @@ more information, or [npm init](/commands/npm-init).
854854
855855
856856
857+
#### \`init-private\`
858+
859+
* Default: false
860+
* Type: Boolean
861+
862+
The value \`npm init\` should use by default for the package's private flag.
863+
864+
865+
857866
#### \`init-type\`
858867
859868
* Default: "commonjs"
@@ -2148,6 +2157,7 @@ Array [
21482157
"init-module",
21492158
"init-type",
21502159
"init-version",
2160+
"init-private",
21512161
"init.author.email",
21522162
"init.author.name",
21532163
"init.author.url",
@@ -2301,6 +2311,7 @@ Array [
23012311
"include",
23022312
"include-staged",
23032313
"include-workspace-root",
2314+
"init-private",
23042315
"install-links",
23052316
"install-strategy",
23062317
"json",
@@ -2456,6 +2467,7 @@ Object {
24562467
"ignoreScripts": false,
24572468
"includeStaged": false,
24582469
"includeWorkspaceRoot": false,
2470+
"initPrivate": false,
24592471
"installLinks": false,
24602472
"installStrategy": "hoisted",
24612473
"json": false,
@@ -3245,7 +3257,7 @@ npm init <@scope> (same as \`npx <@scope>/create\`)
32453257
Options:
32463258
[--init-author-name <name>] [--init-author-url <url>] [--init-license <license>]
32473259
[--init-module <module>] [--init-type <type>] [--init-version <version>]
3248-
[-y|--yes] [-f|--force] [--scope <@scope>]
3260+
[--init-private] [-y|--yes] [-f|--force] [--scope <@scope>]
32493261
[-w|--workspace <workspace-name> [-w|--workspace <workspace-name> ...]]
32503262
[--workspaces] [--no-workspaces-update] [--include-workspace-root]
32513263
@@ -3266,6 +3278,7 @@ aliases: create, innit
32663278
#### \`init-module\`
32673279
#### \`init-type\`
32683280
#### \`init-version\`
3281+
#### \`init-private\`
32693282
#### \`yes\`
32703283
#### \`force\`
32713284
#### \`scope\`

test/lib/commands/init.js

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -459,3 +459,125 @@ t.test('workspaces', async t => {
459459
t.ok(exists.isFile(), 'bin ran, creating file inside workspace')
460460
})
461461
})
462+
463+
t.test('npm init with init-private config set', async t => {
464+
const { npm, prefix } = await mockNpm(t, {
465+
config: { yes: true, 'init-private': true },
466+
noLog: true,
467+
})
468+
469+
await npm.exec('init', [])
470+
471+
const pkg = require(resolve(prefix, 'package.json'))
472+
t.equal(pkg.private, true, 'should set private to true when init-private is set')
473+
})
474+
475+
t.test('npm init does not set private by default', async t => {
476+
const { npm, prefix } = await mockNpm(t, {
477+
config: { yes: true },
478+
noLog: true,
479+
})
480+
481+
await npm.exec('init', [])
482+
483+
const pkg = require(resolve(prefix, 'package.json'))
484+
t.strictSame(pkg.private, undefined, 'should not set private by default')
485+
})
486+
487+
t.test('user‑set init-private IS forwarded', async t => {
488+
const { npm, prefix } = await mockNpm(t, {
489+
config: { yes: true, 'init-private': true },
490+
noLog: true,
491+
})
492+
493+
await npm.exec('init', [])
494+
495+
const pkg = require(resolve(prefix, 'package.json'))
496+
t.strictSame(pkg.private, true, 'should set private to true when init-private is set')
497+
})
498+
499+
t.test('user‑set init-private IS forwarded when false', async t => {
500+
const { npm, prefix } = await mockNpm(t, {
501+
config: { yes: true, 'init-private': false },
502+
noLog: true,
503+
})
504+
505+
await npm.exec('init', [])
506+
507+
const pkg = require(resolve(prefix, 'package.json'))
508+
t.strictSame(pkg.private, false, 'should set private to false when init-private is false')
509+
})
510+
511+
t.test('No init-private is respected in workspaces', async t => {
512+
const { npm, prefix } = await mockNpm(t, {
513+
prefixDir: {
514+
'package.json': JSON.stringify({
515+
name: 'top-level',
516+
}),
517+
},
518+
config: { workspace: 'a', yes: true },
519+
noLog: true,
520+
})
521+
522+
await npm.exec('init', [])
523+
524+
const pkg = require(resolve(prefix, 'a/package.json'))
525+
t.strictSame(pkg.private, undefined, 'workspace package.json has no private field set')
526+
})
527+
528+
t.test('init-private is respected in workspaces', async t => {
529+
const { npm, prefix } = await mockNpm(t, {
530+
prefixDir: {
531+
'package.json': JSON.stringify({
532+
name: 'top-level',
533+
}),
534+
},
535+
config: { workspace: 'a', yes: true, 'init-private': true },
536+
noLog: true,
537+
})
538+
539+
await npm.exec('init', [])
540+
541+
const pkg = require(resolve(prefix, 'a/package.json'))
542+
t.equal(pkg.private, true, 'workspace package.json has private field set')
543+
})
544+
545+
t.test('create‑initializer path: init-private flag is forwarded via args', async t => {
546+
const calls = []
547+
const libexecStub = async opts => calls.push(opts)
548+
549+
const { npm } = await mockNpm(t, {
550+
libnpmexec: libexecStub,
551+
// user set the flag in their config
552+
config: { yes: true, 'init-private': true },
553+
noLog: true,
554+
})
555+
556+
await npm.exec('init', ['create-bar'])
557+
558+
t.ok(calls[0].initPrivate, 'init-private included in options')
559+
560+
// Also verify the test for when isDefault returns true
561+
calls.length = 0
562+
npm.config.isDefault = () => true
563+
564+
await npm.exec('init', ['create-bar'])
565+
566+
t.equal(calls[0].initPrivate, undefined, 'init-private not included when using default')
567+
})
568+
569+
t.test('create‑initializer path: false init-private is forwarded', async t => {
570+
const calls = []
571+
const libexecStub = async opts => calls.push(opts)
572+
573+
const { npm } = await mockNpm(t, {
574+
libnpmexec: libexecStub,
575+
// explicitly set to false
576+
config: { yes: true, 'init-private': false },
577+
noLog: true,
578+
})
579+
580+
await npm.exec('init', ['create-baz'])
581+
582+
t.equal(calls[0].initPrivate, false, 'false init-private value is properly forwarded')
583+
})

workspaces/config/lib/definitions/definitions.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -971,6 +971,14 @@ const definitions = {
971971
version number, if not already set in package.json.
972972
`,
973973
}),
974+
'init-private': new Definition('init-private', {
975+
default: false,
976+
type: Boolean,
977+
description: `
978+
The value \`npm init\` should use by default for the package's private flag.
979+
`,
980+
flatten,
981+
}),
974982
// these "aliases" are historically supported in .npmrc files, unfortunately
975983
// They should be removed in a future npm version.
976984
'init.author.email': new Definition('init.author.email', {

workspaces/config/tap-snapshots/test/type-description.js.test.cjs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,9 @@ Object {
231231
"init-module": Array [
232232
"valid filesystem path",
233233
],
234+
"init-private": Array [
235+
"boolean value (true or false)",
236+
],
234237
"init-type": Array [
235238
Function String(),
236239
],

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy