From 0e9bda769d944e3f0b9218ec1ecfaf63273baf11 Mon Sep 17 00:00:00 2001 From: Adam Girton Date: Sun, 15 Dec 2019 13:31:48 -0800 Subject: [PATCH] feat(publish): Add `--legacy-auth` flag (#2347) Passed as `_auth` internally because that is what NPM expects. --- commands/publish/README.md | 9 +++++ .../publish/__tests__/publish-command.test.js | 17 ++++++++ commands/publish/command.js | 4 ++ commands/publish/index.js | 1 + integration/lerna-publish-auth.test.js | 39 +++++++++++++++++++ 5 files changed, 70 insertions(+) create mode 100644 integration/lerna-publish-auth.test.js diff --git a/commands/publish/README.md b/commands/publish/README.md index 35b8ce102c..6b92b51878 100644 --- a/commands/publish/README.md +++ b/commands/publish/README.md @@ -54,6 +54,7 @@ This is useful when a previous `lerna publish` failed to publish all packages to - [`--graph-type `](#--graph-type-alldependencies) - [`--ignore-scripts`](#--ignore-scripts) - [`--ignore-prepublish`](#--ignore-prepublish) +- [`--legacy-auth`](#--legacy-auth) - [`--no-git-reset`](#--no-git-reset) - [`--no-verify-access`](#--no-verify-access) - [`--otp`](#--otp) @@ -160,6 +161,14 @@ When passed, this flag will disable running [lifecycle scripts](#lifecycle-scrip When passed, this flag will disable running [deprecated](https://docs.npmjs.com/misc/scripts#prepublish-and-prepare) [`prepublish` scripts](#lifecycle-scripts) during `lerna publish`. +### `--legacy-auth` + +When publishing packages that require authentication but you are working with an internally hosted NPM Registry that only uses the legacy Base64 version of username:password. This is the same as the NPM publish `_auth` flag. + +```sh +lerna publish --legacy-auth aGk6bW9t +``` + ### `--no-git-reset` By default, `lerna publish` ensures any changes to the working tree have been reset. diff --git a/commands/publish/__tests__/publish-command.test.js b/commands/publish/__tests__/publish-command.test.js index 73675ef8d3..4daa569189 100644 --- a/commands/publish/__tests__/publish-command.test.js +++ b/commands/publish/__tests__/publish-command.test.js @@ -241,6 +241,23 @@ Map { }); }); + describe("--legacy-auth", () => { + it("passes auth to npm commands", async () => { + const testDir = await initFixture("normal"); + const data = "hi:mom"; + const auth = Buffer.from(data).toString("base64"); + + await lernaPublish(testDir)("--legacy-auth", auth); + + expect(npmPublish).toHaveBeenCalledWith( + expect.objectContaining({ name: "package-1" }), + "/TEMP_DIR/package-1-MOCKED.tgz", + expect.objectContaining({ "auth-type": "legacy", _auth: auth }), + expect.objectContaining({ otp: undefined }) + ); + }); + }); + describe("--registry", () => { it("passes registry to npm commands", async () => { const testDir = await initFixture("normal"); diff --git a/commands/publish/command.js b/commands/publish/command.js index 0c522e602f..8d63cbc905 100644 --- a/commands/publish/command.js +++ b/commands/publish/command.js @@ -35,6 +35,10 @@ exports.builder = yargs => { type: "string", requiresArg: true, }, + "legacy-auth": { + describe: "Legacy Base64 Encoded username and password.", + type: "string", + }, "pre-dist-tag": { describe: "Publish prerelease packages with the specified npm dist-tag", type: "string", diff --git a/commands/publish/index.js b/commands/publish/index.js index 4d5466737f..07d99dd7c2 100644 --- a/commands/publish/index.js +++ b/commands/publish/index.js @@ -116,6 +116,7 @@ class PublishCommand extends Command { this.conf = npmConf({ lernaCommand: "publish", + _auth: this.options.legacyAuth, npmSession: this.npmSession, npmVersion: this.userAgent, otp: this.options.otp, diff --git a/integration/lerna-publish-auth.test.js b/integration/lerna-publish-auth.test.js new file mode 100644 index 0000000000..8a31e1bb7b --- /dev/null +++ b/integration/lerna-publish-auth.test.js @@ -0,0 +1,39 @@ +"use strict"; + +const path = require("path"); + +const cliRunner = require("@lerna-test/cli-runner"); +const cloneFixture = require("@lerna-test/clone-fixture")( + path.resolve(__dirname, "../commands/publish/__tests__") +); + +const env = { + // never actually upload when calling `npm publish` + npm_config_dry_run: true, + // skip npm package validation, none of the stubs are real + LERNA_INTEGRATION: "SKIP", +}; + +test("lerna publish --legacy-auth", async () => { + const { cwd } = await cloneFixture("normal"); + const data = "hi:mom"; + const auth = Buffer.from(data).toString("base64"); + const args = ["publish", "patch", "--yes", "--legacy-auth", auth]; + + const { stdout } = await cliRunner(cwd, env)(...args); + expect(stdout).toMatchInlineSnapshot(` + + Changes: + - package-1: 1.0.0 => 1.0.1 + - package-2: 1.0.0 => 1.0.1 + - package-3: 1.0.0 => 1.0.1 + - package-4: 1.0.0 => 1.0.1 + - package-5: 1.0.0 => 1.0.1 (private) + + Successfully published: + - package-1@1.0.1 + - package-2@1.0.1 + - package-3@1.0.1 + - package-4@1.0.1 + `); +});