Skip to content

Commit

Permalink
Read repository from package.json if npm_package_repository_url is …
Browse files Browse the repository at this point in the history
…not set (#111)

* Read repository from package.json if npm_package_repository_url is not set

* Remove console.log
  • Loading branch information
Mrtenz authored May 16, 2022
1 parent 9435465 commit e48ffd0
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 7 deletions.
9 changes: 2 additions & 7 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
InvalidChangelogError,
validateChangelog,
} from './validate-changelog';
import { getRepositoryUrl } from './repo';

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

// eslint-disable-next-line node/no-process-env
const npmPackageVersion = process.env.npm_package_version;
// eslint-disable-next-line node/no-process-env
const npmPackageRepositoryUrl = process.env.npm_package_repository_url;

const githubRepositoryUrl = npmPackageRepositoryUrl
? npmPackageRepositoryUrl.replace(/\.git$/u, '')
: null;

function isValidUrl(proposedUrl: string) {
try {
Expand Down Expand Up @@ -165,7 +160,7 @@ function configureCommonCommandOptions(_yargs: Argv) {
type: 'string',
})
.option('repo', {
default: githubRepositoryUrl,
default: getRepositoryUrl(),
description: `The GitHub repository URL`,
type: 'string',
})
Expand Down
33 changes: 33 additions & 0 deletions src/repo.test.ts
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',
);
});
});
39 changes: 39 additions & 0 deletions src/repo.ts
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;
}

0 comments on commit e48ffd0

Please sign in to comment.