Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 24 additions & 8 deletions lib/default-input.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,15 +204,31 @@ if (!package.keywords) {
})
}

if (!package.author) {
let author
if (package.author) {
if (typeof package.author === 'string') {
author = package.author
} else {
const authorName = package.author.name
const authorEmail = package.author.email || package.author.mail
const authorUrl = package.author.url || package.author.web
author = `${authorName}${authorEmail ? ` <${authorEmail}>` : ''}${authorUrl ? ` (${authorUrl})` : ''}`
}
} else {
const authorName = getConfig('author.name')
exports.author = authorName
? {
name: authorName,
email: getConfig('author.email'),
url: getConfig('author.url'),
}
: yes ? '' : prompt('author')
if (authorName) {
const authorEmail = getConfig('author.email')
const authorUrl = getConfig('author.url')
author = `${authorName}${authorEmail ? ` <${authorEmail}>` : ''}${authorUrl ? ` (${authorUrl})` : ''}`
}
}

if (yes) {
if (author) {
exports.author = author
}
} else {
exports.author = prompt('author', author || undefined)
}

const configLicense = getConfig('license')
Expand Down
5 changes: 5 additions & 0 deletions lib/init-package-json.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,11 @@ async function init (dir,
delete pkg.content.license
}

// if no author was explicitly provided, don't include one
if (!pzData.author) {
delete pkg.content.author
}

// readJson filters out empty descriptions, but init-package-json
// traditionally leaves them alone
if (!pkg.content.description) {
Expand Down
76 changes: 76 additions & 0 deletions test/author.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
const t = require('tap')
const { setup, child, isChild } = require('./fixtures/setup')

if (isChild()) {
return child()
}

t.test('author from config used as default', async (t) => {
const { data } = await setup(t, __filename, {
inputs: {
name: 'the-name',
author: [
[/author: \(npmbot <npmbot@npmjs\.com> \(https:\/\/npmjs\.com\)\) $/, ''],
],
},
config: {
'init-author-name': 'npmbot',
'init-author-email': 'npmbot@npmjs.com',
'init-author-url': 'https://npmjs.com',
},
})

t.equal(data.author, 'npmbot <npmbot@npmjs.com> (https://npmjs.com)', 'uses config author as default')
})

t.test('author from config in yes mode', async (t) => {
const { data } = await setup(t, __filename, {
config: {
yes: 'yes',
'init-author-name': 'npmbot',
'init-author-email': 'npmbot@npmjs.com',
'init-author-url': 'https://npmjs.com',
},
})

t.equal(data.author, 'npmbot <npmbot@npmjs.com> (https://npmjs.com)', 'uses config author in yes mode')
})

t.test('author omitted when left blank', async (t) => {
const { data } = await setup(t, __filename, {
inputs: {
name: 'the-name',
author: [
[/author: $/, ''],
],
},
})

t.equal(data.author, undefined, 'author is omitted when left blank')
})

t.test('author preserved from existing package.json', async (t) => {
const { data } = await setup(t, __filename, {
testdir: {
'package.json': JSON.stringify({
name: 'existing-package',
version: '1.0.0',
author: 'Existing Author <exist@npmjs.com>',
}),
},
config: { yes: 'yes' },
})

t.equal(data.author, 'Existing Author <exist@npmjs.com>', 'preserves existing author')
})

t.test('author name only from config', async (t) => {
const { data } = await setup(t, __filename, {
config: {
yes: 'yes',
'init-author-name': 'npmbot',
},
})

t.equal(data.author, 'npmbot', 'uses author name only when no email/url configured')
})
1 change: 0 additions & 1 deletion test/dependencies.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ t.test('existing dependencies', async (t) => {
version: '1.0.0',
type: 'commonjs',
description: '',
author: '',
scripts: { test: 'echo "Error: no test specified" && exit 1' },
main: 'index.js',
keywords: [],
Expand Down
1 change: 0 additions & 1 deletion test/license.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ t.test('license', async (t) => {
description: '',
scripts: { test: 'echo "Error: no test specified" && exit 1' },
license: 'Apache-2.0',
author: '',
main: 'index.js',
}
t.has(data, wanted)
Expand Down
2 changes: 0 additions & 2 deletions test/name-spaces.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ t.test('single space', async t => {
version: '1.0.0',
description: '',
scripts: { test: 'echo "Error: no test specified" && exit 1' },
author: '',
main: 'index.js',
}
t.has(data, wanted)
Expand All @@ -41,7 +40,6 @@ t.test('multiple spaces', async t => {
version: '1.0.0',
description: '',
scripts: { test: 'echo "Error: no test specified" && exit 1' },
author: '',
main: 'index.js',
}
t.has(data, wanted)
Expand Down
1 change: 0 additions & 1 deletion test/name-uppercase.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ t.test('uppercase', async (t) => {
version: '1.0.0',
description: '',
scripts: { test: 'echo "Error: no test specified" && exit 1' },
author: '',
main: 'index.js',
}
t.has(data, EXPECT)
Expand Down
1 change: 0 additions & 1 deletion test/repository.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ t.test('license', async (t) => {
version: '1.0.0',
description: '',
scripts: { test: 'echo "Error: no test specified" && exit 1' },
author: '',
repository: {
type: 'git',
url: 'git+https://github.com/npm/cli.git',
Expand Down
1 change: 0 additions & 1 deletion test/scope-in-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ t.test('--yes with scope', async (t) => {
name: '@scoped/tap-testdir-scope-in-config---yes-with-scope',
version: '1.0.0',
description: '',
author: '',
scripts: { test: 'echo "Error: no test specified" && exit 1' },
main: 'index.js',
keywords: [],
Expand Down
2 changes: 1 addition & 1 deletion test/yes-defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ t.test('--yes defaults', async (t) => {
name: 'tap-testdir-yes-defaults---yes-defaults',
version: '1.0.0',
description: '',
author: '',
scripts: { test: 'echo "Error: no test specified" && exit 1' },
main: 'index.js',
keywords: [],
Expand All @@ -21,5 +20,6 @@ t.test('--yes defaults', async (t) => {
})

t.has(data, EXPECT, 'used the default data')
t.equal(data.author, undefined, 'author is omitted by default')
t.equal(data.license, undefined, 'license is omitted by default')
})
Loading