From ce51568ca386b25034f8454e31b549e26a9e1a4a Mon Sep 17 00:00:00 2001 From: Wes Todd Date: Thu, 7 Dec 2023 17:51:05 -0600 Subject: [PATCH] feat: migrate to @npmcli/package-json --- index.js | 36 ++++++++++++++---------------------- lib/git.js | 18 +++++++++++++++++- package.json | 2 +- 3 files changed, 32 insertions(+), 24 deletions(-) diff --git a/index.js b/index.js index 145e77d..34f85eb 100644 --- a/index.js +++ b/index.js @@ -3,8 +3,7 @@ const path = require('path'); const fs = require('fs-extra'); const opta = require('opta'); const parseList = require('safe-parse-list'); -const { promisify } = require('util'); -const readPkg = promisify(require('read-package-json')); +const { create, load } = require('@npmcli/package-json'); const { Loggerr } = require('loggerr'); const packageName = require('./lib/package-name'); const git = require('./lib/git'); @@ -215,7 +214,7 @@ async function main (input, _opts = {}) { }); // Read current state and set defaults - const pkg = opts.ignoreExisting ? {} : await readPackageJson(options, { log }); + const pkg = opts.ignoreExisting ? await create(opts.cwd) : await readPackageJson(options, { log }); await options.prompt({ promptor: _opts.promptor @@ -235,13 +234,17 @@ module.exports.cli = function () { module.exports.readPackageJson = readPackageJson; async function readPackageJson (options, { log } = {}) { const opts = options.values(); + let packageInstance; let pkg = {}; try { - pkg = await readPkg(path.resolve(opts.cwd, 'package.json')); + packageInstance = await load(opts.cwd, { + create: true + }); + pkg = packageInstance.content; log.debug('Read existing package.json', pkg); } catch (e) { - // @TODO log this? // ignore if missing or unreadable + log.error(e); } let author; @@ -256,17 +259,7 @@ async function readPackageJson (options, { log } = {}) { 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; - } + const repo = await git.repository(opts.cwd, pkg); // Remove some of the extras that don't make sense here delete pkg.gitHead; @@ -286,11 +279,12 @@ async function readPackageJson (options, { log } = {}) { license: pkg.license }); - return pkg; + return packageInstance.update(pkg); } module.exports.format = format; -async function format (opts, pkg = {}) { +async function format (opts, packageInstance) { + const pkg = packageInstance.content; // The order here matters pkg.name = opts.name; pkg.version = opts.version; @@ -349,7 +343,7 @@ async function format (opts, pkg = {}) { pkg.peerDependencies[spec.name] = ver; })); } - return pkg; + return packageInstance.update(pkg); } module.exports.write = write; @@ -358,9 +352,7 @@ 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, { - spaces: opts.spacer || 2 - }); + await pkg.save(); // Run installs if (opts.dependencies && opts.dependencies.length) { diff --git a/lib/git.js b/lib/git.js index 666870d..d7637c2 100644 --- a/lib/git.js +++ b/lib/git.js @@ -21,7 +21,7 @@ module.exports.author = async function author (opts = {}) { }; // Taken from npm: https://github.com/npm/init-package-json/blob/latest/default-input.js#L188-L208 -module.exports.remote = async function remote (opts = {}) { +async function gitRemote (opts = {}) { try { const cwd = opts.cwd || process.cwd(); let conf = await fs.readFile(path.join(cwd, '.git', 'config'), 'utf8'); @@ -51,4 +51,20 @@ module.exports.remote = async function remote (opts = {}) { } catch (e) { // ignore error } +} +module.exports.remote = gitRemote; + +module.exports.repository = async function (cwd, pkg) { + if (!pkg || !pkg.repository) { + const remote = await gitRemote({ cwd }); + if (remote) { + return remote; + } + } + if (pkg && typeof pkg.repository === 'string') { + return pkg.repository; + } + if (pkg && typeof pkg.repository !== 'undefined' && pkg.repository.url) { + return pkg.repository.url; + } }; diff --git a/package.json b/package.json index 28ef6ca..180a5ab 100644 --- a/package.json +++ b/package.json @@ -35,11 +35,11 @@ "license": "MIT", "dependencies": { "@npmcli/name-from-folder": "^2.0.0", + "@npmcli/package-json": "^5.0.0", "fs-extra": "^11.1.1", "loggerr": "^3.0.0", "npm-package-arg": "^11.0.1", "opta": "^1.0.0", - "read-package-json": "^7.0.0", "safe-parse-list": "^0.1.1", "validate-npm-package-name": "^5.0.0" },