Skip to content

feat: added init-private option #8246

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

Open
wants to merge 2 commits into
base: latest
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions docs/lib/content/commands/npm-init.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ Generate it without having it ask any questions:
$ npm init -y
```

Set the private flag to `true` in package.json:
```bash
$ npm init --init-private -y
```

### Workspaces support

It's possible to create a new workspace within your project by using the
Expand Down
9 changes: 8 additions & 1 deletion lib/commands/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class Init extends BaseCommand {
'init-module',
'init-type',
'init-version',
'init-private',
'yes',
'force',
'scope',
Expand Down Expand Up @@ -133,8 +134,14 @@ class Init extends BaseCommand {
const scriptShell = this.npm.config.get('script-shell') || undefined
const yes = this.npm.config.get('yes')

// only send the init-private flag if it is set
const opts = { ...flatOptions }
if (this.npm.config.isDefault('init-private')) {
delete opts['init-private']
}

await libexec({
...flatOptions,
...opts,
args: newArgs,
localBin,
globalBin,
Expand Down
18 changes: 17 additions & 1 deletion node_modules/init-package-json/lib/default-input.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,17 @@ const npa = require('npm-package-arg')
const semver = require('semver')

// more popular packages should go here, maybe?
const isTestPkg = (p) => !!p.match(/^(expresso|mocha|tap|coffee-script|coco|streamline)$/)
const testPkgs = [
'coco',
'coffee-script',
'expresso',
'jasmine',
'jest',
'mocha',
'streamline',
'tap',
]
const isTestPkg = p => testPkgs.includes(p)

const invalid = (msg) => Object.assign(new Error(msg), { notValid: true })

Expand Down Expand Up @@ -266,3 +276,9 @@ const type = package.type || getConfig('type') || 'commonjs'
exports.type = yes ? type : prompt('type', type, (data) => {
return data
})

// Only include private field if it already exists or if explicitly set in config
const configPrivate = getConfig('private')
if (package.private !== undefined || configPrivate !== undefined) {
exports.private = package.private !== undefined ? package.private : configPrivate
}
4 changes: 2 additions & 2 deletions node_modules/init-package-json/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "init-package-json",
"version": "8.0.0",
"version": "8.2.0",
"main": "lib/init-package-json.js",
"scripts": {
"test": "tap",
Expand Down Expand Up @@ -29,7 +29,7 @@
"validate-npm-package-name": "^6.0.0"
},
"devDependencies": {
"@npmcli/config": "^9.0.0",
"@npmcli/config": "^10.0.0",
"@npmcli/eslint-config": "^5.0.0",
"@npmcli/template-oss": "4.23.4",
"tap": "^16.0.1"
Expand Down
8 changes: 4 additions & 4 deletions package-lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@
"graceful-fs": "^4.2.11",
"hosted-git-info": "^8.1.0",
"ini": "^5.0.0",
"init-package-json": "^8.0.0",
"init-package-json": "^8.2.0",
"is-cidr": "^5.1.1",
"json-parse-even-better-errors": "^4.0.0",
"libnpmaccess": "^10.0.0",
Expand Down Expand Up @@ -9493,9 +9493,9 @@
}
},
"node_modules/init-package-json": {
"version": "8.0.0",
"resolved": "https://registry.npmjs.org/init-package-json/-/init-package-json-8.0.0.tgz",
"integrity": "sha512-zKgxfaGt6Zzi8VBSInOK0CYDigA9gzDCWPnSzGIoUlTU/5w7qIyi+6MyJYX96mMlxDGrIR85FhQszVyodYfB9g==",
"version": "8.2.0",
"resolved": "https://registry.npmjs.org/init-package-json/-/init-package-json-8.2.0.tgz",
"integrity": "sha512-eG4WzSLsMMoESLm5y5X1M7EVm1EGNOnL2T4BZ5YPN/Rw91ZxmxqGAqeh07p3Y056pklqnLA5Pks5+2TCB2jpZA==",
"inBundle": true,
"license": "ISC",
"dependencies": {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
"graceful-fs": "^4.2.11",
"hosted-git-info": "^8.1.0",
"ini": "^5.0.0",
"init-package-json": "^8.0.0",
"init-package-json": "^8.2.0",
"is-cidr": "^5.1.1",
"json-parse-even-better-errors": "^4.0.0",
"libnpmaccess": "^10.0.0",
Expand Down
2 changes: 2 additions & 0 deletions tap-snapshots/test/lib/commands/config.js.test.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ exports[`test/lib/commands/config.js TAP config list --json > output matches sna
"init-module": "{CWD}/home/.npm-init.js",
"init-type": "commonjs",
"init-version": "1.0.0",
"init-private": false,
"init.author.email": "",
"init.author.name": "",
"init.author.url": "",
Expand Down Expand Up @@ -239,6 +240,7 @@ init-author-name = ""
init-author-url = ""
init-license = "ISC"
init-module = "{CWD}/home/.npm-init.js"
init-private = false
init-type = "commonjs"
init-version = "1.0.0"
init.author.email = ""
Expand Down
14 changes: 13 additions & 1 deletion tap-snapshots/test/lib/docs.js.test.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -854,6 +854,15 @@ more information, or [npm init](/commands/npm-init).



#### \`init-private\`

* Default: false
* Type: Boolean

The value \`npm init\` should use by default for the package's private flag.



#### \`init-type\`

* Default: "commonjs"
Expand Down Expand Up @@ -2148,6 +2157,7 @@ Array [
"init-module",
"init-type",
"init-version",
"init-private",
"init.author.email",
"init.author.name",
"init.author.url",
Expand Down Expand Up @@ -2385,6 +2395,7 @@ Array [
"init-module",
"init-type",
"init-version",
"init-private",
"init.author.email",
"init.author.name",
"init.author.url",
Expand Down Expand Up @@ -3245,7 +3256,7 @@ npm init <@scope> (same as \`npx <@scope>/create\`)
Options:
[--init-author-name <name>] [--init-author-url <url>] [--init-license <license>]
[--init-module <module>] [--init-type <type>] [--init-version <version>]
[-y|--yes] [-f|--force] [--scope <@scope>]
[--init-private] [-y|--yes] [-f|--force] [--scope <@scope>]
[-w|--workspace <workspace-name> [-w|--workspace <workspace-name> ...]]
[--workspaces] [--no-workspaces-update] [--include-workspace-root]

Expand All @@ -3266,6 +3277,7 @@ aliases: create, innit
#### \`init-module\`
#### \`init-type\`
#### \`init-version\`
#### \`init-private\`
#### \`yes\`
#### \`force\`
#### \`scope\`
Expand Down
55 changes: 55 additions & 0 deletions test/lib/commands/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -459,3 +459,58 @@
t.ok(exists.isFile(), 'bin ran, creating file inside workspace')
})
})

t.test('npm init with init-private config set', async t => {
const { npm, prefix } = await mockNpm(t, {
config: { yes: true, 'init-private': true },
noLog: true,
})

await npm.exec('init', [])

const pkg = require(resolve(prefix, 'package.json'))
t.equal(pkg.private, true, 'should set private to true when init-private is set')
})

t.test('npm init does not set private by default', async t => {
const { npm, prefix } = await mockNpm(t, {
config: { yes: true },
noLog: true,
})

await npm.exec('init', [])

const pkg = require(resolve(prefix, 'package.json'))
t.strictSame(Object.prototype.hasOwnProperty.call(pkg, 'private'), false, 'should not set private by default')

Check failure on line 484 in test/lib/commands/init.js

View workflow job for this annotation

GitHub Actions / Test - Linux - 20.x

should not set private by default

Check failure on line 484 in test/lib/commands/init.js

View workflow job for this annotation

GitHub Actions / Test - macOS - 20.17.0

should not set private by default

Check failure on line 484 in test/lib/commands/init.js

View workflow job for this annotation

GitHub Actions / Test - macOS - 22.9.0

should not set private by default

Check failure on line 484 in test/lib/commands/init.js

View workflow job for this annotation

GitHub Actions / Test - macOS - 20.x

should not set private by default

Check failure on line 484 in test/lib/commands/init.js

View workflow job for this annotation

GitHub Actions / Test - Linux - 22.x

should not set private by default

Check failure on line 484 in test/lib/commands/init.js

View workflow job for this annotation

GitHub Actions / Test - Linux - 20.17.0

should not set private by default

Check failure on line 484 in test/lib/commands/init.js

View workflow job for this annotation

GitHub Actions / Test - Linux - 22.9.0

should not set private by default

Check failure on line 484 in test/lib/commands/init.js

View workflow job for this annotation

GitHub Actions / Test - macOS - 22.x

should not set private by default
})

t.test('create‑initializer path: default init-private is NOT forwarded', async t => {
const calls = []
const libexecStub = async opts => calls.push(opts)

const { npm } = await mockNpm(t, {
libnpmexec: libexecStub,
config: { yes: true },
noLog: true,
})

await npm.exec('init', ['foo'])

t.notOk(Object.prototype.hasOwnProperty.call(calls[0], 'init-private'), 'flag not present')
})

t.test('create‑initializer path: user‑set init-private IS forwarded', async t => {
const calls = []
const libexecStub = async opts => calls.push(opts)

const { npm } = await mockNpm(t, {
libnpmexec: libexecStub,
// user set the flag in their config
config: { yes: true, 'init-private': true },
noLog: true,
})

await npm.exec('init', ['bar'])

t.equal(calls[0]['init-private'], true, 'flag forwarded with true value')

Check failure on line 515 in test/lib/commands/init.js

View workflow job for this annotation

GitHub Actions / Test - Linux - 20.x

flag forwarded with true value

Check failure on line 515 in test/lib/commands/init.js

View workflow job for this annotation

GitHub Actions / Test - macOS - 20.17.0

flag forwarded with true value

Check failure on line 515 in test/lib/commands/init.js

View workflow job for this annotation

GitHub Actions / Test - macOS - 22.9.0

flag forwarded with true value

Check failure on line 515 in test/lib/commands/init.js

View workflow job for this annotation

GitHub Actions / Test - macOS - 20.x

flag forwarded with true value

Check failure on line 515 in test/lib/commands/init.js

View workflow job for this annotation

GitHub Actions / Test - Linux - 22.x

flag forwarded with true value

Check failure on line 515 in test/lib/commands/init.js

View workflow job for this annotation

GitHub Actions / Test - Linux - 20.17.0

flag forwarded with true value

Check failure on line 515 in test/lib/commands/init.js

View workflow job for this annotation

GitHub Actions / Test - Linux - 22.9.0

flag forwarded with true value

Check failure on line 515 in test/lib/commands/init.js

View workflow job for this annotation

GitHub Actions / Test - macOS - 22.x

flag forwarded with true value
})
7 changes: 7 additions & 0 deletions workspaces/config/lib/definitions/definitions.js
Original file line number Diff line number Diff line change
Expand Up @@ -971,6 +971,13 @@ const definitions = {
version number, if not already set in package.json.
`,
}),
'init-private': new Definition('init-private', {
default: false,
type: Boolean,
description: `
The value \`npm init\` should use by default for the package's private flag.
`,
}),
// these "aliases" are historically supported in .npmrc files, unfortunately
// They should be removed in a future npm version.
'init.author.email': new Definition('init.author.email', {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,9 @@ Object {
"init-module": Array [
"valid filesystem path",
],
"init-private": Array [
"boolean value (true or false)",
],
"init-type": Array [
Function String(),
],
Expand Down
Loading