Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Include registry URL pathname in npmrc auth token config #186

Merged
merged 4 commits into from
Mar 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading