|
| 1 | +import { execFileSync } from "child_process"; |
1 | 2 | import { existsSync, mkdirSync, writeFileSync } from "fs"; |
2 | 3 | import { STATUS_CODES } from "http"; |
3 | 4 | import { get } from "https"; |
4 | | -import { homedir } from "os"; |
5 | | -import parseGitConfig = require("parse-git-config"); |
6 | 5 | import { join as joinPaths } from "path"; |
7 | | -import { parse as parseUrl } from "url"; |
| 6 | +import which = require("which"); |
8 | 7 | import { getDTName } from "./names"; |
9 | 8 |
|
10 | 9 | export default function writeDefinitelyTypedPackage( |
@@ -83,25 +82,22 @@ async function getPackageJson(dtName: string, packageName: string): Promise<{}> |
83 | 82 |
|
84 | 83 | let authorName = "My Self"; |
85 | 84 | try { |
86 | | - const globalGitConfig = parseGitConfig.sync({ cwd: homedir(), path: ".gitconfig" }); |
87 | | - if (globalGitConfig.user && globalGitConfig.user.name) { |
88 | | - authorName = globalGitConfig.user.name; |
89 | | - } |
| 85 | + authorName = |
| 86 | + execFileSync(which.sync("git"), ["config", "--global", "user.name"], { encoding: "utf-8" }).trim() || authorName; |
90 | 87 | } catch (e: any) { |
91 | 88 | console.warn(`Warning: Could not retrieve author name: ${e.message}`); |
92 | 89 | } |
93 | 90 |
|
94 | 91 | let authorUserName = "me"; |
95 | 92 | try { |
96 | | - const repoGitConfig = parseGitConfig.sync({ path: joinPaths(".git", "config") }); |
97 | | - if (repoGitConfig['remote "origin"'] && repoGitConfig['remote "origin"'].url) { |
98 | | - const url = parseUrl(repoGitConfig['remote "origin"'].url); |
99 | | - if (url.hostname === "github.com" && url.pathname) { |
100 | | - authorUserName = url.pathname.split("/")[1] || authorUserName; |
101 | | - } else if (url.pathname?.startsWith("git@github.com")) { |
102 | | - authorUserName = url.pathname.split(":")?.[1].split("/")?.[0] || authorUserName; |
103 | | - } |
104 | | - } |
| 93 | + const remoteUrl = execFileSync(which.sync("git"), ["config", "--get", "remote.origin.url"], { |
| 94 | + encoding: "utf-8", |
| 95 | + }).trim(); |
| 96 | + // Handle HTTPS URLs like https://github.com/user/repo.git |
| 97 | + const httpsMatch = remoteUrl.match(/github\.com\/([^/]+)/); |
| 98 | + // Handle SSH URLs like git@github.com:user/repo.git |
| 99 | + const sshMatch = remoteUrl.match(/github\.com:([^/]+)/); |
| 100 | + authorUserName = httpsMatch?.[1] || sshMatch?.[1] || authorUserName; |
105 | 101 | } catch (e: any) { |
106 | 102 | console.warn(`Warning: Could not retrieve author's user name: ${e.message}`); |
107 | 103 | } |
|
0 commit comments