Skip to content

Commit b560c65

Browse files
feat: default owner and repository to Git origin (#341)
## PR Checklist - [x] Addresses an existing open issue: fixes #196 - [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 Calls to `git remote -v` to get the repo URL, and parses that as `--owner` and `--repository` field defaults. Note that this doesn't add `--description` as a default because I don't want to mess with calling to the GitHub API & potentially needing to authenticate. If someone really wants to add that in I'd encourage them to file a separate feature request issue. 🙂
1 parent 97173b3 commit b560c65

File tree

3 files changed

+38
-3
lines changed

3 files changed

+38
-3
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
"replace-in-file": "^6.3.5",
7474
"sentences-per-line": "^0.2.1",
7575
"should-semantic-release": "^0.0.4",
76+
"title-case": "^3.0.3",
7677
"typescript": "^5.0.0",
7778
"vitest": "^0.29.0",
7879
"yaml-eslint-parser": "^1.2.0"

pnpm-lock.yaml

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

script/setup.js

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { promises as fs } from "fs";
88
import { Octokit } from "octokit";
99
import prettier from "prettier";
1010
import replace from "replace-in-file";
11+
import { titleCase } from "title-case";
1112

1213
let caughtError;
1314

@@ -36,6 +37,31 @@ try {
3637
strict: false,
3738
});
3839

40+
async function getDefaultSettings() {
41+
let gitRemoteFetch;
42+
try {
43+
gitRemoteFetch = await $`git remote -v | grep fetch`;
44+
} catch {
45+
console.log(
46+
chalk.gray(
47+
"Could not populate default owner and repository. Did not detect a Git repository with an origin. "
48+
)
49+
);
50+
return {
51+
defaultOwner: "UserName",
52+
defaultRepository: "my-lovely-repository",
53+
};
54+
}
55+
56+
const [, defaultOwner, defaultRepository] = gitRemoteFetch.stdout.match(
57+
/\s.+\/([^/]+)\/([^/]+) \(fetch\)/
58+
);
59+
60+
return { defaultOwner, defaultRepository };
61+
}
62+
63+
const { defaultOwner, defaultRepository } = await getDefaultSettings();
64+
3965
async function getPrefillOrPromptedValue(key, message, placeholder) {
4066
if (values[key]) {
4167
console.log(chalk.grey(`Pre-filling ${key} to ${values[key]}.`));
@@ -63,19 +89,19 @@ try {
6389
const repository = await getPrefillOrPromptedValue(
6490
"repository",
6591
"What will the kebab-case name of the repository be?",
66-
"my-lovely-repository"
92+
defaultRepository
6793
);
6894

6995
const title = await getPrefillOrPromptedValue(
7096
"title",
7197
"What will the Title Case title of the repository be?",
72-
"My Lovely Repository"
98+
titleCase(repository).replaceAll("-", " ")
7399
);
74100

75101
const owner = await getPrefillOrPromptedValue(
76102
"owner",
77103
"What owner or user will the repository be under?",
78-
"UserName"
104+
defaultOwner
79105
);
80106

81107
const description = await getPrefillOrPromptedValue(

0 commit comments

Comments
 (0)