Skip to content

Commit e48ffd0

Browse files
authored
Read repository from package.json if npm_package_repository_url is not set (#111)
* Read repository from package.json if npm_package_repository_url is not set * Remove console.log
1 parent 9435465 commit e48ffd0

File tree

3 files changed

+74
-7
lines changed

3 files changed

+74
-7
lines changed

src/cli.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import {
2323
InvalidChangelogError,
2424
validateChangelog,
2525
} from './validate-changelog';
26+
import { getRepositoryUrl } from './repo';
2627

2728
const updateEpilog = `New commits will be added to the "${unreleased}" section (or \
2829
to the section for the current release if the '--rc' flag is used) in reverse \
@@ -38,12 +39,6 @@ formatting is correct. Verification of the contents is left for manual review.`;
3839

3940
// eslint-disable-next-line node/no-process-env
4041
const npmPackageVersion = process.env.npm_package_version;
41-
// eslint-disable-next-line node/no-process-env
42-
const npmPackageRepositoryUrl = process.env.npm_package_repository_url;
43-
44-
const githubRepositoryUrl = npmPackageRepositoryUrl
45-
? npmPackageRepositoryUrl.replace(/\.git$/u, '')
46-
: null;
4742

4843
function isValidUrl(proposedUrl: string) {
4944
try {
@@ -165,7 +160,7 @@ function configureCommonCommandOptions(_yargs: Argv) {
165160
type: 'string',
166161
})
167162
.option('repo', {
168-
default: githubRepositoryUrl,
163+
default: getRepositoryUrl(),
169164
description: `The GitHub repository URL`,
170165
type: 'string',
171166
})

src/repo.test.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/* eslint-disable node/no-process-env */
2+
3+
import path from 'path';
4+
import { getRepositoryUrl } from './repo';
5+
6+
describe('getRepositoryUrl', () => {
7+
it('reads the repository URL from an environment variable', () => {
8+
process.env.npm_package_repository_url =
9+
'https://github.com/metamask/auto-changelog';
10+
11+
expect(getRepositoryUrl()).toBe(
12+
'https://github.com/metamask/auto-changelog',
13+
);
14+
});
15+
16+
it('reads the repository URL from an environment variable (.git suffix)', () => {
17+
process.env.npm_package_repository_url =
18+
'https://github.com/metamask/auto-changelog.git';
19+
20+
expect(getRepositoryUrl()).toBe(
21+
'https://github.com/metamask/auto-changelog',
22+
);
23+
});
24+
25+
it('reads the repository URL from the package.json', () => {
26+
process.env.npm_package_repository_url = '';
27+
process.env.PROJECT_CWD = path.resolve(__dirname, '..');
28+
29+
expect(getRepositoryUrl()).toBe(
30+
'https://github.com/MetaMask/auto-changelog',
31+
);
32+
});
33+
});

src/repo.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/* eslint-disable node/no-process-env, node/no-sync */
2+
3+
import path from 'path';
4+
import fs from 'fs';
5+
6+
interface PackageJson {
7+
repository:
8+
| string
9+
| {
10+
url: string;
11+
};
12+
}
13+
14+
export function getRepositoryUrl(): string | null {
15+
// Set automatically by NPM or Yarn 1.x
16+
const npmPackageRepositoryUrl = process.env.npm_package_repository_url;
17+
if (npmPackageRepositoryUrl) {
18+
return npmPackageRepositoryUrl.replace(/\.git$/u, '');
19+
}
20+
21+
// Set automatically by Yarn 3.x
22+
const projectCwd = process.env.PROJECT_CWD;
23+
if (projectCwd) {
24+
const packageJson = path.resolve(projectCwd, 'package.json');
25+
const packageJsonContent = JSON.parse(
26+
fs.readFileSync(packageJson, 'utf8'),
27+
) as PackageJson;
28+
29+
if (typeof packageJsonContent.repository === 'string') {
30+
return packageJsonContent.repository.replace(/\.git$/u, '');
31+
}
32+
33+
if (typeof packageJsonContent.repository?.url === 'string') {
34+
return packageJsonContent.repository.url.replace(/\.git$/u, '');
35+
}
36+
}
37+
38+
return null;
39+
}

0 commit comments

Comments
 (0)