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

feat: migrate to @npmcli/package-json #225

Merged
merged 1 commit into from
Dec 11, 2023
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
36 changes: 14 additions & 22 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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
Expand All @@ -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;
Copy link
Member Author

Choose a reason for hiding this comment

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

Not sure if there was a better way, but to reduce the surface of this change I do this so we can just modify the object and leave all those other lines alone.

log.debug('Read existing package.json', pkg);
} catch (e) {
// @TODO log this?
// ignore if missing or unreadable
log.error(e);
}

let author;
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -349,7 +343,7 @@ async function format (opts, pkg = {}) {
pkg.peerDependencies[spec.name] = ver;
}));
}
return pkg;
return packageInstance.update(pkg);
}

module.exports.write = write;
Expand All @@ -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
wesleytodd marked this conversation as resolved.
Show resolved Hide resolved
});
await pkg.save();

// Run installs
if (opts.dependencies && opts.dependencies.length) {
Expand Down
18 changes: 17 additions & 1 deletion lib/git.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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;
}
};
Copy link
Member Author

Choose a reason for hiding this comment

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

I am wondering now if we can delete all this since @npmcli/package-json does more normalization?

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down