Skip to content

Commit

Permalink
fix: monorepo configurations failing when looking for a yarnrc
Browse files Browse the repository at this point in the history
  • Loading branch information
dmeents committed Mar 29, 2023
1 parent b02710e commit daae1cb
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 10 deletions.
20 changes: 14 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,23 @@ export async function verifyConditions(

ctx.logger.log(`read ${ctx.cwd}/package.json`);
const packageJson = await getPackage(ctx.cwd);
const registryFromPackage = packageJson?.publishConfig?.registry as string;

ctx.logger.log(`read ${ctx.cwd}/.yarnrc.yml`);
const yarnrc = await getYarnRc(ctx.cwd);
let yarnrc: Record<string, string> = {};
let registryFromYarnrc = '';

const registryFromPackage = packageJson?.publishConfig?.registry as string;
const registryFromYarnrc = yarnrc?.npmPublishRegistry;
if (!registryFromPackage) {
ctx.logger.log(`no registry found in package.json, checking .yarnrc.yml`);
ctx.logger.log(`read ${ctx.cwd}/.yarnrc.yml`);
yarnrc = await getYarnRc(ctx.cwd);
registryFromYarnrc = yarnrc?.npmPublishRegistry;
}

if (packageJson.private === true) {
ctx.logger.log('skipping since package is private');
if (
packageJson.private === true ||
yarnrc.npmPublishAccess === 'restricted'
) {
ctx.logger.log('skipping since registry is private');
return;
}

Expand Down
6 changes: 6 additions & 0 deletions src/utils/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import SemanticReleaseError from '@semantic-release/error';

export enum ErrorTypes {
MISSING_PACKAGE_NAME,
MISSING_YARNRC,
MISSING_PACKAGE,
INVALID_NPM_TOKEN,
INVALID_NPM_AUTH_IDENT,
Expand All @@ -15,6 +16,11 @@ export const error = (error: ErrorTypes): Error => {
'Missing `name` in property `package.json`',
'MISSING_PACKAGE_NAME',
) as Error;
case ErrorTypes.MISSING_YARNRC:
return new SemanticReleaseError(
'Missing `.yarnrc.yml`',
'MISSING_YARNRC',
) as Error;
case ErrorTypes.MISSING_PACKAGE:
return new SemanticReleaseError(
'Missing `package.json`',
Expand Down
43 changes: 42 additions & 1 deletion src/utils/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import tempy from 'tempy';
import * as fs from 'fs';
import { getPackage, getYarnRc } from './index';
import {
getChannel,
getNpmAuthIdent,
getNpmToken,
getPackage,
getYarnRc,
} from './index';

describe('utils', () => {
describe('getPackage', () => {
Expand Down Expand Up @@ -46,4 +52,39 @@ describe('utils', () => {
expect(result.npmPublishRegistry).toEqual(mockYarnRc.npmPublishRegistry);
});
});

describe('getNpmToken', () => {
it('should return the npm token if it exists in the environment', () => {
const mockEnv = { NPM_TOKEN: '1234567890' };
const result = getNpmToken(mockEnv);
expect(result).toEqual(mockEnv.NPM_TOKEN);
});
});

describe('getNpmAuthIdent', () => {
it('should return the npm auth ident if it exists in the environment', () => {
const mockEnv = { NPM_AUTH_IDENT: '1234567890' };
const result = getNpmAuthIdent(mockEnv);
expect(result).toEqual(mockEnv.NPM_AUTH_IDENT);
});
});

describe('getChannel', () => {
it('should return "latest" if there is no channel set', () => {
const result = getChannel();
expect(result).toEqual('latest');
});

it('should return a channel preceded by "release-" if there is a valid channel set', () => {
const result = getChannel('1.0.1');
expect(result).toEqual('release-1.0.1');
});

// TODO: this seems like weird behavior, if it's not a valid semver range, it should throw
it('should return the provided channel if it is not a valid semver range', () => {
const channel = '-';
const result = getChannel(channel);
expect(result).toEqual(channel);
});
});
});
9 changes: 6 additions & 3 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export async function getYarnRc(cwd: string) {
)) as Record<string, string>;
} catch (err) {
const { code } = err as { code?: string };
if (code === 'ENOENT') throw error(ErrorTypes.MISSING_PACKAGE);
if (code === 'ENOENT') throw error(ErrorTypes.MISSING_YARNRC);
throw new AggregateError([err]);
}

Expand All @@ -43,12 +43,15 @@ export function getNpmToken(env: NodeJS.ProcessEnv): string {

export function getNpmAuthIdent(env: NodeJS.ProcessEnv): string {
const authIdent = env['NPM_AUTH_IDENT'];
if (typeof authIdent !== 'string')

if (typeof authIdent !== 'string') {
throw error(ErrorTypes.INVALID_NPM_AUTH_IDENT);
}

return authIdent;
}

export const getChannel = (channel: string) => {
export const getChannel = (channel?: string) => {
if (!channel) return 'latest';
return semver.validRange(channel) ? `release-${channel}` : channel;
};

0 comments on commit daae1cb

Please sign in to comment.