diff --git a/index.js b/index.js index 4fd5a0d..d674986 100644 --- a/index.js +++ b/index.js @@ -82,7 +82,7 @@ function initOpts () { prompt: { message: 'Author:', default: (promptInput, allInput) => { - return allInput.author || git.author({ cwd: allInput.cwd }) + return allInput.author } } }, @@ -91,7 +91,7 @@ function initOpts () { prompt: { message: 'Repository:', default: (promptInput, allInput) => { - return allInput.repository || git.remote({ cwd: allInput.cwd }) + return allInput.repository } } }, @@ -217,7 +217,7 @@ async function main (input, _opts = {}) { })() opts = options.values() - return write(path.resolve(opts.cwd, 'package.json'), opts, await format(opts, pkg), { log }) + return write(opts, await format(opts, pkg), { log }) } module.exports.options = initOpts().options @@ -239,14 +239,38 @@ async function readPackageJson (options, { log } = {}) { // ignore if missing or unreadable } + let author + if (!pkg || !pkg.author) { + const gitAuthor = await git.author({ cwd: opts.cwd }) + if (gitAuthor) { + author = gitAuthor + } + } else if (pkg && typeof pkg.author === 'string') { + author = pkg.author + } else if (pkg && typeof pkg.author !== 'undefined') { + author = `${pkg.author.name}${pkg.author.email ? ` <${pkg.author.email}>` : ''}` + } + + let repo + if (!pkg || !pkg.repository) { + const gitRemote = await git.remote({ cwd: opts.cwd }) + if (gitRemote) { + repo = gitRemote + } + } else if (pkg && typeof pkg.repository === 'string') { + repo = pkg.repository + } else if (pkg && typeof pkg.repository !== 'undefined' && pkg.repository.url) { + repo = pkg.repository.url + } + // Set defaults from the package.json options.defaults({ version: pkg.version, name: pkg.name, type: pkg.type, - author: pkg.author, + author: author, description: pkg.description, - repository: pkg.repository, + repository: repo, keywords: pkg.keywords, scripts: pkg.scripts, license: pkg.license @@ -302,6 +326,8 @@ async function format (opts, pkg = {}) { pkg.peerDependencies = {} await Promise.all(opts.peerDependencies.map(async (name) => { + // @TODO we should align peer deps with the associated + // devDep or prodDep semver range const spec = await npm.normalizePackageName(name) let ver switch (spec.type) { @@ -320,7 +346,8 @@ async function format (opts, pkg = {}) { module.exports.write = write // TODO: look at https://npm.im/json-file-plus for writing -async function write (pkgPath, opts, pkg, { log } = {}) { +async function write (opts, pkg, { log } = {}) { + const pkgPath = path.resolve(opts.cwd, 'package.json') // Write package json log.info(`Writing package.json\n${pkgPath}`) await fs.outputJSON(pkgPath, pkg, {