Skip to content

Commit

Permalink
fix: add support for pulling registry from yarnrc file
Browse files Browse the repository at this point in the history
  • Loading branch information
dmeents committed Mar 7, 2023
1 parent 80cad06 commit 8d788eb
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 7 deletions.
15 changes: 8 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import { PluginConfig } from './config/plugin';
import { error, ErrorTypes } from './utils/error';
import { getChannel, getNpmAuthIdent, getNpmToken, getPackage } from './utils';
import {
getChannel,
getNpmAuthIdent,
getNpmToken,
getPackage,
getYarnRc,
} from './utils';
import { Context, PrepareContext } from './types';
import { Yarn } from './utils/yarn';
import yaml from 'js-yaml';
import * as fs from 'fs';

let verified = false;
let prepared = false;
Expand All @@ -20,10 +24,7 @@ export async function verifyConditions(
const packageJson = await getPackage(ctx.cwd);

ctx.logger.log(`read ${ctx.cwd}/yarnrc.yml`);

const yarnrc = yaml.load(
fs.readFileSync(`${ctx.cwd}/.yarnrc.yml`, 'utf8'),
) as Record<string, string>;
const yarnrc = await getYarnRc(ctx.cwd);

const registryFromPackage = packageJson?.publishConfig?.registry as string;
const registryFromYarnrc = yarnrc?.npmPublishRegistry;
Expand Down
18 changes: 18 additions & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { error, ErrorTypes } from './error';
import semver from 'semver';
import readPackage, { NormalizedPackageJson } from 'read-pkg';
import yaml from 'js-yaml';
import fs from 'fs';

export async function getPackage(cwd: string) {
let packageJson: NormalizedPackageJson;
Expand All @@ -17,6 +19,22 @@ export async function getPackage(cwd: string) {
return packageJson;
}

export async function getYarnRc(cwd: string) {
let yarnRc: Record<string, string>;

try {
yarnRc = (await yaml.load(
fs.readFileSync(`${cwd}/.yarnrc.yml`, 'utf8'),
)) as Record<string, string>;
} catch (err) {
const { code } = err as { code?: string };
if (code === 'ENOENT') throw error(ErrorTypes.MISSING_PACKAGE);
throw new AggregateError([err]);
}

return yarnRc;
}

export function getNpmToken(env: NodeJS.ProcessEnv): string {
const token = env['NPM_TOKEN'];
if (typeof token !== 'string') throw error(ErrorTypes.INVALID_NPM_TOKEN);
Expand Down
11 changes: 11 additions & 0 deletions src/utils/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,16 @@ describe('utils', () => {

expect(result.name).toEqual(mockPackage.name);
});

it('should throw an error if the package.json does not exist', async () => {
const cwd = tempy.directory();
await expect(getPackage(cwd)).rejects.toThrow();
});

it('should throw an error if the package.json does not include a name', async () => {
const cwd = tempy.directory();
fs.writeFileSync(`${cwd}/package.json`, JSON.stringify({}));
await expect(getPackage(cwd)).rejects.toThrow();
});
});
});

0 comments on commit 8d788eb

Please sign in to comment.