File tree Expand file tree Collapse file tree 3 files changed +74
-7
lines changed Expand file tree Collapse file tree 3 files changed +74
-7
lines changed Original file line number Diff line number Diff line change @@ -23,6 +23,7 @@ import {
2323 InvalidChangelogError ,
2424 validateChangelog ,
2525} from './validate-changelog' ;
26+ import { getRepositoryUrl } from './repo' ;
2627
2728const updateEpilog = `New commits will be added to the "${ unreleased } " section (or \
2829to 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
4041const 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 ( / \. g i t $ / u, '' )
46- : null ;
4742
4843function 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 } )
Original file line number Diff line number Diff line change 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+ } ) ;
Original file line number Diff line number Diff line change 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 ( / \. g i t $ / 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 ( / \. g i t $ / u, '' ) ;
31+ }
32+
33+ if ( typeof packageJsonContent . repository ?. url === 'string' ) {
34+ return packageJsonContent . repository . url . replace ( / \. g i t $ / u, '' ) ;
35+ }
36+ }
37+
38+ return null ;
39+ }
You can’t perform that action at this time.
0 commit comments