Skip to content

Commit 74b1453

Browse files
feat: populate package.json author from npm info (#343)
## PR Checklist - [x] Addresses an existing open issue: fixes #154 - [x] That issue was marked as [`status: accepting prs`](https://github.com/JoshuaKGoldberg/template-typescript-node-package/issues?q=is%3Aopen+is%3Aissue+label%3A%22status%3A+accepting+prs%22) - [x] Steps in [CONTRIBUTING.md](https://github.com/JoshuaKGoldberg/template-typescript-node-package/blob/main/.github/CONTRIBUTING.md) were taken ## Overview Uses `npm whoami` and [`npm-user`](https://github.com/sindresorhus/npm-user) to try to retrieve data on the currently logged in npm user. Bolsters the end-to-end testing too!
1 parent f3f756e commit 74b1453

File tree

4 files changed

+200
-2
lines changed

4 files changed

+200
-2
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
"markdownlint-cli": "^0.33.0",
6666
"npm-package-json-lint": "^6.4.0",
6767
"npm-package-json-lint-config-default": "^5.0.0",
68+
"npm-user": "^5.0.1",
6869
"octokit": "^2.0.11",
6970
"pnpm-deduplicate": "^0.4.1",
7071
"prettier": "^2.8.3",

pnpm-lock.yaml

Lines changed: 149 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

script/setup-test-e2e.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,12 @@ const repository = "new-repository-test";
1111

1212
const result =
1313
await $`pnpm run setup --description ${description} --owner ${owner} --title ${title} --repository ${repository} --skip-api`;
14-
console.log({ result });
14+
console.log("Result from pnpm run setup:", result);
1515

1616
const newPackageJson = JSON.parse(
1717
(await fs.readFile("./package.json")).toString()
1818
);
19+
console.log("New package JSON:", newPackageJson);
1920

2021
assert.equal(newPackageJson.description, description);
2122
assert.equal(newPackageJson.name, repository);
@@ -31,3 +32,10 @@ for (const search of [
3132
`README.md:> 💙 This package is based on [@JoshuaKGoldberg](https://github.com/JoshuaKGoldberg)'s [template-typescript-node-package](https://github.com/JoshuaKGoldberg/template-typescript-node-package).`
3233
);
3334
}
35+
36+
try {
37+
await $`pnpm lint:knip`;
38+
} catch (error) {
39+
console.error("Error running lint:knip:", error);
40+
process.exitCode = 1;
41+
}

script/setup.js

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { parseArgs } from "node:util";
55
import { cancel, isCancel, text } from "@clack/prompts";
66
import chalk from "chalk";
77
import { promises as fs } from "fs";
8+
import npmUser from "npm-user";
89
import { Octokit } from "octokit";
910
import prettier from "prettier";
1011
import replace from "replace-in-file";
@@ -147,13 +148,45 @@ try {
147148
)
148149
);
149150

151+
async function getNpmAuthor() {
152+
let username;
153+
154+
try {
155+
username = await $`npm whoami`;
156+
} catch {
157+
console.log(
158+
chalk.gray("Could not populate npm user. Failed to run npm whoami. ")
159+
);
160+
return owner;
161+
}
162+
163+
let npmUserInfo;
164+
165+
try {
166+
npmUserInfo = await npmUser(username.stdout.trim());
167+
} catch {
168+
console.log(
169+
chalk.gray(
170+
"Could not populate npm user. Failed to retrieve user info from npm. "
171+
)
172+
);
173+
return owner;
174+
}
175+
176+
const { name = owner, email } = npmUserInfo;
177+
return email ? `${name} <${email}>` : name;
178+
}
179+
180+
const npmAuthor = await getNpmAuthor();
181+
150182
for (const [from, to, files = ["./.github/**/*", "./*.*"]] of [
151183
[new RegExp(existingPackage.description, "g"), description],
152184
[/Template TypeScript Node Package/g, title],
153185
[/JoshuaKGoldberg/g, owner],
154186
[/template-typescript-node-package/g, repository],
155187
[/"setup": ".*",/g, ``, "./package.json"],
156188
[/"setup:test": ".*",/g, ``, "./package.json"],
189+
[/"author": ".+"/g, `"author": "${npmAuthor}"`, "./package.json"],
157190
[
158191
new RegExp(`"version": "${existingPackage.version}"`, "g"),
159192
`"version": "0.0.0"`,
@@ -328,7 +361,14 @@ try {
328361
console.log(
329362
chalk.gray`Removing devDependency packages only used for setup...`
330363
);
331-
await $`pnpm remove @clack/prompts all-contributors-cli chalk octokit replace-in-file -D`;
364+
365+
try {
366+
await $`pnpm remove @clack/prompts all-contributors-cli chalk octokit npm-user replace-in-file title-case -D`;
367+
} catch (error) {
368+
console.log("Error uninstalling packages:", error);
369+
caughtError = error;
370+
}
371+
332372
console.log(chalk.gray`✔️ Done.`);
333373
}
334374

0 commit comments

Comments
 (0)