generated from MetaMask/metamask-module-template
-
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
Showing
3 changed files
with
74 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* eslint-disable node/no-process-env */ | ||
|
||
import path from 'path'; | ||
import { getRepositoryUrl } from './repo'; | ||
|
||
describe('getRepositoryUrl', () => { | ||
it('reads the repository URL from an environment variable', () => { | ||
process.env.npm_package_repository_url = | ||
'https://github.com/metamask/auto-changelog'; | ||
|
||
expect(getRepositoryUrl()).toBe( | ||
'https://github.com/metamask/auto-changelog', | ||
); | ||
}); | ||
|
||
it('reads the repository URL from an environment variable (.git suffix)', () => { | ||
process.env.npm_package_repository_url = | ||
'https://github.com/metamask/auto-changelog.git'; | ||
|
||
expect(getRepositoryUrl()).toBe( | ||
'https://github.com/metamask/auto-changelog', | ||
); | ||
}); | ||
|
||
it('reads the repository URL from the package.json', () => { | ||
process.env.npm_package_repository_url = ''; | ||
process.env.PROJECT_CWD = path.resolve(__dirname, '..'); | ||
|
||
expect(getRepositoryUrl()).toBe( | ||
'https://github.com/MetaMask/auto-changelog', | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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; | ||
} |