25 lines
917 B
TypeScript
25 lines
917 B
TypeScript
import { execSync } from 'node:child_process'
|
|
import path from 'node:path';
|
|
|
|
export const getPathnameFromGitUrl = (url: string): { pathname: string, filename: string } => {
|
|
const _url = new URL(url.replace(/\.git$/, ''));
|
|
const _pathname = _url.pathname;
|
|
const pathname = _pathname.replace(/^\/+/, '').replace(/\/+$/, ''); // 去除开头和结尾的斜杠
|
|
const filename = path.basename(_pathname);
|
|
return { pathname, filename };
|
|
}
|
|
export const getGitPathname = (repoPath: string): { url: string, pathname: string, filename: string } | null => {
|
|
try {
|
|
const url = execSync('git config --get remote.origin.url', { cwd: repoPath, encoding: 'utf-8' }).trim();
|
|
if (url) {
|
|
return {
|
|
url,
|
|
...getPathnameFromGitUrl(url),
|
|
}
|
|
}
|
|
return null;
|
|
} catch (err) {
|
|
console.warn(`[getGitPathname] Failed to get git remote url for ${repoPath}:`, err);
|
|
return null;
|
|
}
|
|
}; |