Skip to content

Commit

Permalink
Merge pull request #513 from chromaui/ghengeveld/ch-1486-cli-custom-n…
Browse files Browse the repository at this point in the history
…pm-registry-support

Add support for custom npm registry url
  • Loading branch information
ghengeveld authored Jan 31, 2022
2 parents a93662e + 19f88f1 commit ae23b1c
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 7 deletions.
18 changes: 12 additions & 6 deletions bin-src/lib/checkForUpdates.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import semver from 'semver';
import yon from 'yarn-or-npm';
import { hasYarn } from 'yarn-or-npm';

import spawn from './spawn';
import { Context } from '../types';

import outdatedPackage from '../ui/messages/warnings/outdatedPackage';

const { hasYarn } = yon;

const rejectIn = (ms: number) => new Promise<any>((_, reject) => setTimeout(reject, ms));
const withTimeout = <T>(promise: Promise<T>, ms: number): Promise<T> =>
Promise.race([promise, rejectIn(ms)]);
Expand All @@ -18,9 +18,15 @@ export default async function checkForUpdates(ctx: Context) {

let latestVersion: string;
try {
const pkgUrl = `https://registry.npmjs.org/${ctx.pkg.name}`;
// If not fetched within 5 seconds, nevermind.
const res = await withTimeout(ctx.http.fetch(pkgUrl), 5000);
const registryUrl = await spawn(['config', 'get', 'registry']).catch(
() => 'https://registry.npmjs.org/'
);
if (!['https://registry.npmjs.org/', 'https://registry.yarnpkg.com'].includes(registryUrl)) {
ctx.log.info(`Using custom npm registry: ${registryUrl}`);
}

const pkgUrl = new URL(ctx.pkg.name, registryUrl).href;
const res = await withTimeout(ctx.http.fetch(pkgUrl), 5000); // If not fetched within 5 seconds, nevermind.
const { 'dist-tags': distTags = {} } = (await res.json()) as any;
if (!semver.valid(distTags.latest)) {
ctx.log.warn(`Invalid dist-tag 'latest' returned from registry; skipping update check`);
Expand Down
19 changes: 19 additions & 0 deletions bin-src/lib/spawn.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { spawn } from 'yarn-or-npm';

export default (args: Parameters<typeof spawn>[0], options: Parameters<typeof spawn>[1] = {}) =>
new Promise<string>((resolve, reject) => {
let stdout = '';
let stderr = '';
const child = spawn(args, options);
child.stdout.on('data', (chunk) => {
stdout += chunk;
});
child.stderr.on('data', (chunk) => {
stderr += chunk;
});
child.on('error', reject);
child.on('close', (code) => {
if (code === 0) resolve(stdout.trim());
else reject(new Error(stderr));
});
});
4 changes: 3 additions & 1 deletion bin-src/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,8 @@ const openTunnel = <jest.MockedFunction<typeof tunnel>>tunnel;

jest.mock('./lib/uploadFiles');

jest.mock('./lib/spawn', () => () => Promise.resolve('https://npm.example.com'));

let processEnv;
beforeEach(() => {
processEnv = process.env;
Expand Down Expand Up @@ -614,7 +616,7 @@ describe('runAll', () => {
} as any);
await runAll(ctx);
expect(ctx.exitCode).toBe(1);
expect(ctx.http.fetch).toHaveBeenCalledWith('https://registry.npmjs.org/chromatic');
expect(ctx.http.fetch).toHaveBeenCalledWith('https://npm.example.com/chromatic');
expect(ctx.testLogger.warnings[0]).toMatch('Using outdated package');
});

Expand Down

0 comments on commit ae23b1c

Please sign in to comment.