Skip to content
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

fix!: refactor writePackageJson to not require path #50

Merged
merged 3 commits into from
Oct 2, 2023
Merged
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
39 changes: 33 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ function initOpts () {
prompt: {
message: 'Author:',
default: (promptInput, allInput) => {
return allInput.author || git.author({ cwd: allInput.cwd })
return allInput.author
}
}
},
Expand All @@ -91,7 +91,7 @@ function initOpts () {
prompt: {
message: 'Repository:',
default: (promptInput, allInput) => {
return allInput.repository || git.remote({ cwd: allInput.cwd })
return allInput.repository
}
}
},
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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) {
Expand All @@ -320,7 +346,8 @@ async function format (opts, pkg = {}) {

module.exports.write = write
// TODO: look at https://npm.im/json-file-plus for writing
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should look at these as well:

They are used in standard-version and that has appeared to work well for me

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, {
Expand Down