Skip to content

Commit 1e4a74d

Browse files
bdr99mcous
andauthored
fix: include registry URL pathname in npm config (#186)
Co-authored-by: Michael Cousins <[email protected]>
1 parent 79051c0 commit 1e4a74d

File tree

4 files changed

+80
-62
lines changed

4 files changed

+80
-62
lines changed

dist/main.js

+9-10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/main.js.map

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/npm/__tests__/use-npm-environment.test.ts

+58-38
Original file line numberDiff line numberDiff line change
@@ -19,42 +19,62 @@ describe("useNpmEnvironment", () => {
1919
await fs.rm(directory, { recursive: true, force: true });
2020
});
2121

22-
it("create an npmrc file ", async () => {
23-
const inputManifest = { name: "fizzbuzz" } as PackageManifest;
24-
const inputOptions = {
25-
token: "abc123",
26-
registry: new URL("http://example.com/cool-registry/"),
27-
temporaryDirectory: directory,
28-
} as NormalizedOptions;
29-
30-
let npmrcPath: string | undefined;
31-
let npmrcContents: string | undefined;
32-
33-
const result = await subject.useNpmEnvironment(
34-
inputManifest,
35-
inputOptions,
36-
async (manifest, options, environment) => {
37-
npmrcPath = environment["npm_config_userconfig"]!;
38-
npmrcContents = await fs.readFile(npmrcPath, "utf8");
39-
return { manifest, options, environment };
40-
}
41-
);
42-
43-
expect(result).toEqual({
44-
manifest: inputManifest,
45-
options: inputOptions,
46-
environment: {
47-
NODE_AUTH_TOKEN: "abc123",
48-
npm_config_userconfig: npmrcPath,
49-
},
50-
});
51-
expect(npmrcContents).toContain(
52-
"//example.com/:_authToken=${NODE_AUTH_TOKEN}"
53-
);
54-
expect(npmrcContents).toContain(
55-
"registry=http://example.com/cool-registry/"
56-
);
57-
58-
await expect(fs.access(npmrcPath!)).rejects.toThrow(/ENOENT/);
59-
});
22+
it.each([
23+
{
24+
registryUrl: "http://example.com/",
25+
expectedAuthConfig: "//example.com/:_authToken=${NODE_AUTH_TOKEN}",
26+
expectedRegistryConfig: "registry=http://example.com/",
27+
},
28+
{
29+
registryUrl: "http://example.com",
30+
expectedAuthConfig: "//example.com/:_authToken=${NODE_AUTH_TOKEN}",
31+
expectedRegistryConfig: "registry=http://example.com/",
32+
},
33+
{
34+
registryUrl: "http://example.com/hello/",
35+
expectedAuthConfig: "//example.com/hello/:_authToken=${NODE_AUTH_TOKEN}",
36+
expectedRegistryConfig: "registry=http://example.com/hello/",
37+
},
38+
{
39+
registryUrl: "http://example.com/hello",
40+
expectedAuthConfig: "//example.com/hello/:_authToken=${NODE_AUTH_TOKEN}",
41+
expectedRegistryConfig: "registry=http://example.com/hello/",
42+
},
43+
])(
44+
"creates an npmrc file for $registryUrl",
45+
async ({ registryUrl, expectedAuthConfig, expectedRegistryConfig }) => {
46+
const inputManifest = { name: "fizzbuzz" } as PackageManifest;
47+
const inputOptions = {
48+
token: "abc123",
49+
registry: new URL(registryUrl),
50+
temporaryDirectory: directory,
51+
} as NormalizedOptions;
52+
53+
let npmrcPath: string | undefined;
54+
let npmrcContents: string | undefined;
55+
56+
const result = await subject.useNpmEnvironment(
57+
inputManifest,
58+
inputOptions,
59+
async (manifest, options, environment) => {
60+
npmrcPath = environment["npm_config_userconfig"]!;
61+
npmrcContents = await fs.readFile(npmrcPath, "utf8");
62+
return { manifest, options, environment };
63+
}
64+
);
65+
66+
expect(result).toEqual({
67+
manifest: inputManifest,
68+
options: inputOptions,
69+
environment: {
70+
NODE_AUTH_TOKEN: "abc123",
71+
npm_config_userconfig: npmrcPath,
72+
},
73+
});
74+
expect(npmrcContents).toContain(expectedAuthConfig);
75+
expect(npmrcContents).toContain(expectedRegistryConfig);
76+
77+
await expect(fs.access(npmrcPath!)).rejects.toThrow(/ENOENT/);
78+
}
79+
);
6080
});

src/npm/use-npm-environment.ts

+10-11
Original file line numberDiff line numberDiff line change
@@ -29,22 +29,21 @@ export async function useNpmEnvironment<TReturn>(
2929
task: NpmCliTask<TReturn>
3030
): Promise<TReturn> {
3131
const { registry, token, logger, temporaryDirectory } = options;
32-
const npmrcDirectory = await fs.mkdtemp(
33-
path.join(temporaryDirectory, "npm-publish-")
34-
);
35-
const npmrc = path.join(npmrcDirectory, ".npmrc");
36-
const environment = {
37-
NODE_AUTH_TOKEN: token,
38-
npm_config_userconfig: npmrc,
39-
};
40-
32+
const { host, origin, pathname } = registry;
33+
const pathnameWithSlash = pathname.endsWith("/") ? pathname : `${pathname}/`;
4134
const config = [
4235
"; created by jsdevtools/npm-publish",
43-
`//${registry.host}/:_authToken=\${NODE_AUTH_TOKEN}`,
44-
`registry=${registry.href}`,
36+
`//${host}${pathnameWithSlash}:_authToken=\${NODE_AUTH_TOKEN}`,
37+
`registry=${origin}${pathnameWithSlash}`,
4538
"",
4639
].join(os.EOL);
4740

41+
const npmrcDirectory = await fs.mkdtemp(
42+
path.join(temporaryDirectory, "npm-publish-")
43+
);
44+
const npmrc = path.join(npmrcDirectory, ".npmrc");
45+
const environment = { NODE_AUTH_TOKEN: token, npm_config_userconfig: npmrc };
46+
4847
await fs.writeFile(npmrc, config, "utf8");
4948

5049
logger?.debug?.(`Temporary .npmrc created at ${npmrc}\n${config}`);

0 commit comments

Comments
 (0)