generated from MetaMask/metamask-module-template
-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathrepo.ts
39 lines (32 loc) · 1.01 KB
/
repo.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/* eslint-disable node/no-process-env, node/no-sync */
import path from 'path';
import fs from 'fs';
interface PackageJson {
repository:
| string
| {
url: string;
};
}
export function getRepositoryUrl(): string | null {
// Set automatically by NPM or Yarn 1.x
const npmPackageRepositoryUrl = process.env.npm_package_repository_url;
if (npmPackageRepositoryUrl) {
return npmPackageRepositoryUrl.replace(/\.git$/u, '');
}
// Set automatically by Yarn 3.x
const projectCwd = process.env.PROJECT_CWD;
if (projectCwd) {
const packageJson = path.resolve(projectCwd, 'package.json');
const packageJsonContent = JSON.parse(
fs.readFileSync(packageJson, 'utf8'),
) as PackageJson;
if (typeof packageJsonContent.repository === 'string') {
return packageJsonContent.repository.replace(/\.git$/u, '');
}
if (typeof packageJsonContent.repository?.url === 'string') {
return packageJsonContent.repository.url.replace(/\.git$/u, '');
}
}
return null;
}