Skip to content

Commit

Permalink
fix: include registry URL pathname in npm config (#186)
Browse files Browse the repository at this point in the history
Co-authored-by: Michael Cousins <[email protected]>
  • Loading branch information
bdr99 and mcous authored Mar 29, 2024
1 parent 79051c0 commit 1e4a74d
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 62 deletions.
19 changes: 9 additions & 10 deletions dist/main.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions dist/main.js.map

Large diffs are not rendered by default.

96 changes: 58 additions & 38 deletions src/npm/__tests__/use-npm-environment.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,42 +19,62 @@ describe("useNpmEnvironment", () => {
await fs.rm(directory, { recursive: true, force: true });
});

it("create an npmrc file ", async () => {
const inputManifest = { name: "fizzbuzz" } as PackageManifest;
const inputOptions = {
token: "abc123",
registry: new URL("http://example.com/cool-registry/"),
temporaryDirectory: directory,
} as NormalizedOptions;

let npmrcPath: string | undefined;
let npmrcContents: string | undefined;

const result = await subject.useNpmEnvironment(
inputManifest,
inputOptions,
async (manifest, options, environment) => {
npmrcPath = environment["npm_config_userconfig"]!;
npmrcContents = await fs.readFile(npmrcPath, "utf8");
return { manifest, options, environment };
}
);

expect(result).toEqual({
manifest: inputManifest,
options: inputOptions,
environment: {
NODE_AUTH_TOKEN: "abc123",
npm_config_userconfig: npmrcPath,
},
});
expect(npmrcContents).toContain(
"//example.com/:_authToken=${NODE_AUTH_TOKEN}"
);
expect(npmrcContents).toContain(
"registry=http://example.com/cool-registry/"
);

await expect(fs.access(npmrcPath!)).rejects.toThrow(/ENOENT/);
});
it.each([
{
registryUrl: "http://example.com/",
expectedAuthConfig: "//example.com/:_authToken=${NODE_AUTH_TOKEN}",
expectedRegistryConfig: "registry=http://example.com/",
},
{
registryUrl: "http://example.com",
expectedAuthConfig: "//example.com/:_authToken=${NODE_AUTH_TOKEN}",
expectedRegistryConfig: "registry=http://example.com/",
},
{
registryUrl: "http://example.com/hello/",
expectedAuthConfig: "//example.com/hello/:_authToken=${NODE_AUTH_TOKEN}",
expectedRegistryConfig: "registry=http://example.com/hello/",
},
{
registryUrl: "http://example.com/hello",
expectedAuthConfig: "//example.com/hello/:_authToken=${NODE_AUTH_TOKEN}",
expectedRegistryConfig: "registry=http://example.com/hello/",
},
])(
"creates an npmrc file for $registryUrl",
async ({ registryUrl, expectedAuthConfig, expectedRegistryConfig }) => {
const inputManifest = { name: "fizzbuzz" } as PackageManifest;
const inputOptions = {
token: "abc123",
registry: new URL(registryUrl),
temporaryDirectory: directory,
} as NormalizedOptions;

let npmrcPath: string | undefined;
let npmrcContents: string | undefined;

const result = await subject.useNpmEnvironment(
inputManifest,
inputOptions,
async (manifest, options, environment) => {
npmrcPath = environment["npm_config_userconfig"]!;
npmrcContents = await fs.readFile(npmrcPath, "utf8");
return { manifest, options, environment };
}
);

expect(result).toEqual({
manifest: inputManifest,
options: inputOptions,
environment: {
NODE_AUTH_TOKEN: "abc123",
npm_config_userconfig: npmrcPath,
},
});
expect(npmrcContents).toContain(expectedAuthConfig);
expect(npmrcContents).toContain(expectedRegistryConfig);

await expect(fs.access(npmrcPath!)).rejects.toThrow(/ENOENT/);
}
);
});
21 changes: 10 additions & 11 deletions src/npm/use-npm-environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,21 @@ export async function useNpmEnvironment<TReturn>(
task: NpmCliTask<TReturn>
): Promise<TReturn> {
const { registry, token, logger, temporaryDirectory } = options;
const npmrcDirectory = await fs.mkdtemp(
path.join(temporaryDirectory, "npm-publish-")
);
const npmrc = path.join(npmrcDirectory, ".npmrc");
const environment = {
NODE_AUTH_TOKEN: token,
npm_config_userconfig: npmrc,
};

const { host, origin, pathname } = registry;
const pathnameWithSlash = pathname.endsWith("/") ? pathname : `${pathname}/`;
const config = [
"; created by jsdevtools/npm-publish",
`//${registry.host}/:_authToken=\${NODE_AUTH_TOKEN}`,
`registry=${registry.href}`,
`//${host}${pathnameWithSlash}:_authToken=\${NODE_AUTH_TOKEN}`,
`registry=${origin}${pathnameWithSlash}`,
"",
].join(os.EOL);

const npmrcDirectory = await fs.mkdtemp(
path.join(temporaryDirectory, "npm-publish-")
);
const npmrc = path.join(npmrcDirectory, ".npmrc");
const environment = { NODE_AUTH_TOKEN: token, npm_config_userconfig: npmrc };

await fs.writeFile(npmrc, config, "utf8");

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

0 comments on commit 1e4a74d

Please sign in to comment.