Skip to content

Commit b674034

Browse files
committed
refactor(registry): extract a constant rather than repeating the url string
1 parent 761d4db commit b674034

File tree

8 files changed

+16
-8
lines changed

8 files changed

+16
-8
lines changed

lib/definitions/constants.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const OFFICIAL_REGISTRY = 'https://registry.npmjs.org/';

lib/get-registry.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import path from "path";
22
import rc from "rc";
33
import getRegistryUrl from "registry-auth-token/registry-url.js";
4+
import { OFFICIAL_REGISTRY } from "./definitions/constants.js";
45

56
export default function ({ publishConfig: { registry } = {}, name }, { cwd, env }) {
67
return (
@@ -10,7 +11,7 @@ export default function ({ publishConfig: { registry } = {}, name }, { cwd, env
1011
name.split("/")[0],
1112
rc(
1213
"npm",
13-
{ registry: "https://registry.npmjs.org/" },
14+
{ registry: OFFICIAL_REGISTRY },
1415
{ config: env.NPM_CONFIG_USERCONFIG || path.resolve(cwd, ".npmrc") }
1516
)
1617
)

lib/get-release-info.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import normalizeUrl from "normalize-url";
2+
import { OFFICIAL_REGISTRY } from "./definitions/constants.js";
23

34
export default function (
45
{ name },
5-
{ env: { DEFAULT_NPM_REGISTRY = "https://registry.npmjs.org/" }, nextRelease: { version } },
6+
{ env: { DEFAULT_NPM_REGISTRY = OFFICIAL_REGISTRY }, nextRelease: { version } },
67
distTag,
78
registry
89
) {

lib/set-npmrc-auth.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@ import getAuthToken from "registry-auth-token";
55
import nerfDart from "nerf-dart";
66
import AggregateError from "aggregate-error";
77
import getError from "./get-error.js";
8+
import { OFFICIAL_REGISTRY } from "./definitions/constants.js";
89

910
export default async function (npmrc, registry, { cwd, env: { NPM_TOKEN, NPM_CONFIG_USERCONFIG }, logger }) {
1011
logger.log("Verify authentication for registry %s", registry);
1112
const { configs, ...rcConfig } = rc(
1213
"npm",
13-
{ registry: "https://registry.npmjs.org/" },
14+
{ registry: OFFICIAL_REGISTRY },
1415
{ config: NPM_CONFIG_USERCONFIG || path.resolve(cwd, ".npmrc") }
1516
);
1617

lib/verify-auth.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import AggregateError from "aggregate-error";
44
import getRegistry from "./get-registry.js";
55
import setNpmrcAuth from "./set-npmrc-auth.js";
66
import getError from "./get-error.js";
7+
import { OFFICIAL_REGISTRY } from "./definitions/constants.js";
78

89
function registryIsDefault(registry, DEFAULT_NPM_REGISTRY) {
910
return normalizeUrl(registry) === normalizeUrl(DEFAULT_NPM_REGISTRY);
@@ -16,7 +17,7 @@ function targetingDefaultRegistryButNoTokenProvided(registry, DEFAULT_NPM_REGIST
1617
export default async function (npmrc, pkg, context) {
1718
const {
1819
cwd,
19-
env: { DEFAULT_NPM_REGISTRY = "https://registry.npmjs.org/", ...env },
20+
env: { DEFAULT_NPM_REGISTRY = OFFICIAL_REGISTRY, ...env },
2021
stdout,
2122
stderr,
2223
logger,

test/get-registry.test.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@ import test from "ava";
33
import fs from "fs-extra";
44
import { temporaryDirectory } from "tempy";
55
import getRegistry from "../lib/get-registry.js";
6+
import { OFFICIAL_REGISTRY } from "../lib/definitions/constants.js";
67

78
test("Get default registry", (t) => {
89
const cwd = temporaryDirectory();
9-
t.is(getRegistry({ name: "package-name" }, { cwd, env: {} }), "https://registry.npmjs.org/");
10-
t.is(getRegistry({ name: "package-name", publishConfig: {} }, { cwd, env: {} }), "https://registry.npmjs.org/");
10+
t.is(getRegistry({ name: "package-name" }, { cwd, env: {} }), OFFICIAL_REGISTRY);
11+
t.is(getRegistry({ name: "package-name", publishConfig: {} }, { cwd, env: {} }), OFFICIAL_REGISTRY);
1112
});
1213

1314
test('Get the registry configured in ".npmrc" and normalize trailing slash', async (t) => {

test/get-release-info.test.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import test from "ava";
22
import getReleaseInfo from "../lib/get-release-info.js";
3+
import { OFFICIAL_REGISTRY } from "../lib/definitions/constants.js";
34

45
test("Default registry and scoped module", async (t) => {
56
t.deepEqual(
67
await getReleaseInfo(
78
{ name: "@scope/module" },
89
{ env: {}, nextRelease: { version: "1.0.0" } },
910
"latest",
10-
"https://registry.npmjs.org/"
11+
OFFICIAL_REGISTRY
1112
),
1213
{
1314
name: "npm package (@latest dist-tag)",

test/verify-auth.test.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import test from "ava";
22
import * as td from "testdouble";
3+
import {OFFICIAL_REGISTRY} from '../lib/definitions/constants.js';
34

45
let execa, verifyAuth, getRegistry, setNpmrcAuth;
5-
const DEFAULT_NPM_REGISTRY = "https://registry.npmjs.org/";
6+
const DEFAULT_NPM_REGISTRY = OFFICIAL_REGISTRY;
67
const npmrc = "npmrc contents";
78
const pkg = {};
89
const otherEnvVars = { foo: "bar" };

0 commit comments

Comments
 (0)