Skip to content

Commit

Permalink
fix: use npmrc
Browse files Browse the repository at this point in the history
  • Loading branch information
jpoehnelt committed Oct 27, 2021
1 parent b2b08cd commit 026a371
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 17 deletions.
30 changes: 30 additions & 0 deletions package-lock.json

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

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
},
"dependencies": {
"@semantic-release/npm": "^8.0.2",
"lodash": "^4.17.21"
"lodash": "^4.17.21",
"tempy": "<2.0.0"
},
"devDependencies": {
"@types/jest": "^27.0.1",
Expand Down
35 changes: 25 additions & 10 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@

import * as m from "./index";
import child_process from "child_process";

import tempy from "tempy";
import setNpmrcAuth from "@semantic-release/npm/lib/set-npmrc-auth";
import { Context, NextRelease } from "semantic-release";

let getPackageSpy: jest.SpyInstance;
let deprecateSpy: jest.SpyInstance;
let execSyncSpy: jest.SpyInstance;
let setNpmrcSpy: jest.SpyInstance;

beforeEach(() => {
getPackageSpy = jest
Expand All @@ -31,12 +33,15 @@ beforeEach(() => {
execSyncSpy = jest
.spyOn(child_process, "execSync")
.mockReturnValue(undefined);
setNpmrcSpy = jest.spyOn(m, "setNpmrc").mockResolvedValue(undefined);
});

const context: Context = {
logger: console,
env: {},
env: { NPM_TOKEN: "test-token" },
nextRelease: { version: "1.2.3" } as NextRelease,
// @ts-ignore incorrect typings
cwd: tempy.directory(),
};

test("should expose success", async () => {
Expand Down Expand Up @@ -74,7 +79,9 @@ test("should call deprecate", async () => {
expect(getPackageSpy).toHaveBeenCalledTimes(1);
expect(deprecateSpy).toHaveBeenCalledWith(
deprecations[0],
(await m.getPackage(context)).name
(await m.getPackage(context)).name,
expect.stringMatching(/\.npmrc$/),
expect.anything()
);
});

Expand All @@ -89,7 +96,9 @@ test("should call deprecate with rendered templates", async () => {
expect(getPackageSpy).toHaveBeenCalledTimes(1);
expect(deprecateSpy).toHaveBeenCalledWith(
{ version: "< 1.2.3", message: "Please use 1.2.3." },
(await m.getPackage(context)).name
(await m.getPackage(context)).name,
expect.stringMatching(/\.npmrc$/),
expect.anything()
);
});

Expand All @@ -104,7 +113,9 @@ test("should call deprecate with more complex templates", async () => {
expect(getPackageSpy).toHaveBeenCalledTimes(1);
expect(deprecateSpy).toHaveBeenCalledWith(
{ version: "< 1", message: "Please use ^1.0.0." },
(await m.getPackage(context)).name
(await m.getPackage(context)).name,
expect.stringMatching(/\.npmrc$/),
expect.anything()
);
});

Expand All @@ -115,10 +126,14 @@ test("should call execSync correctly", async () => {
message: "Please use ^1.",
};
const name = "test-package";
m.deprecate(deprecation, name);
m.deprecate(deprecation, name, ".npmrc", "https://registry.npmjs.org/");

expect(execSyncSpy).toHaveBeenCalledWith(
`npm deprecate ${name}@"${deprecation.version}" "${deprecation.message}"`,
{ stdio: "inherit" }
);
expect(execSyncSpy.mock.calls[0]).toMatchInlineSnapshot(`
Array [
"npm deprecate --userconfig .npmrc --registry https://registry.npmjs.org/ test-package@\\"< 1\\" \\"Please use ^1.\\"",
Object {
"stdio": "inherit",
},
]
`);
});
31 changes: 26 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,13 @@

import { Context } from "semantic-release";
import getPkg from "@semantic-release/npm/lib/get-pkg";
import getRegistry from "@semantic-release/npm/lib/get-registry";
import setNpmrcAuth from "@semantic-release/npm/lib/set-npmrc-auth";
import { template } from "lodash";
import { execSync } from "child_process";
import tempy from "tempy";

const npmrc = tempy.file({ name: ".npmrc" });

export interface Deprecation {
version: string;
Expand All @@ -40,10 +45,13 @@ export async function success(
if (deprecations.length === 0) {
return;
}
// update .npmrc file
const registry = await getRegistry(pkg, context);
await setNpmrc(npmrc, registry, context);

deprecations = renderDeprecations(deprecations, context);
deprecations.forEach((deprecation) =>
deprecate(deprecation, pkg.name)
deprecate(deprecation, pkg.name, npmrc, registry)
);
}

Expand All @@ -60,9 +68,22 @@ export const getPackage = async (context: Context) => getPkg({}, context);

export const deprecate = (
{ version, message }: Deprecation,
name: string
name: string,
npmrc: string,
registry: string
) =>
/* istanbul ignore next */
execSync(`npm deprecate ${name}@"${version}" "${message}"`, {
stdio: "inherit",
});
execSync(
`npm deprecate --userconfig ${npmrc} --registry ${registry} ${name}@"${version}" "${message}"`,
{
stdio: "inherit",
}
);

export const setNpmrc = async (
npmrc: string,
registry: string,
context: Context
) => {
await setNpmrcAuth(npmrc, registry, context);
};
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@
"declarationDir": "./dist",
"esModuleInterop": true
},
"include": ["src/**/*.ts"]
"include": ["src/**/*.ts"],
"exclude": ["src/**/*.test.ts"]
}

0 comments on commit 026a371

Please sign in to comment.