Skip to content

Commit

Permalink
refactor: Decompose PromptUtilities namespace import
Browse files Browse the repository at this point in the history
  • Loading branch information
evocateur committed Dec 10, 2020
1 parent a08bafb commit 69bb2e4
Show file tree
Hide file tree
Showing 17 changed files with 71 additions and 81 deletions.
10 changes: 5 additions & 5 deletions commands/clean/__tests__/clean-command.test.js
Expand Up @@ -8,7 +8,7 @@ const path = require("path");

// mocked or stubbed modules
const { rimrafDir } = require("@lerna/rimraf-dir");
const PromptUtilities = require("@lerna/prompt");
const { promptConfirmation } = require("@lerna/prompt");

// helpers
const initFixture = require("@lerna-test/init-fixture")(__dirname);
Expand All @@ -26,15 +26,15 @@ describe("CleanCommand", () => {
// .mockResolvedValue() doesn't work when you want to reject it later
rimrafDir.mockImplementation(() => Promise.resolve());

PromptUtilities.promptConfirmation.mockResolvedValue(true);
promptConfirmation.mockResolvedValue(true);

describe("basic tests", () => {
it("should rm -rf the node_modules", async () => {
const testDir = await initFixture("basic");

await lernaClean(testDir)();

expect(PromptUtilities.promptConfirmation).toHaveBeenCalled();
expect(promptConfirmation).toHaveBeenCalled();
expect(removedDirectories(testDir)).toEqual([
"packages/package-1/node_modules",
"packages/package-2/node_modules",
Expand All @@ -45,7 +45,7 @@ describe("CleanCommand", () => {
it("exits early when confirmation is rejected", async () => {
const testDir = await initFixture("basic");

PromptUtilities.promptConfirmation.mockResolvedValueOnce(false);
promptConfirmation.mockResolvedValueOnce(false);

await lernaClean(testDir)();

Expand All @@ -57,7 +57,7 @@ describe("CleanCommand", () => {

await lernaClean(testDir)("--yes");

expect(PromptUtilities.promptConfirmation).not.toHaveBeenCalled();
expect(promptConfirmation).not.toHaveBeenCalled();
});

it("should only clean scoped packages", async () => {
Expand Down
4 changes: 2 additions & 2 deletions commands/clean/index.js
Expand Up @@ -5,7 +5,7 @@ const pMap = require("p-map");

const { Command } = require("@lerna/command");
const { rimrafDir } = require("@lerna/rimraf-dir");
const PromptUtilities = require("@lerna/prompt");
const { promptConfirmation } = require("@lerna/prompt");
const { getFilteredPackages } = require("@lerna/filter-options");
const { pulseTillDone } = require("@lerna/pulse-till-done");

Expand Down Expand Up @@ -39,7 +39,7 @@ class CleanCommand extends Command {
this.directoriesToDelete.map((dir) => path.relative(this.project.rootPath, dir)).join("\n")
);

return PromptUtilities.promptConfirmation("Proceed?");
return promptConfirmation("Proceed?");
});
}

Expand Down
8 changes: 4 additions & 4 deletions commands/import/__tests__/import-command.test.js
Expand Up @@ -8,7 +8,7 @@ const path = require("path");
const pathExists = require("path-exists");

// mocked or stubbed modules
const PromptUtilities = require("@lerna/prompt");
const { promptConfirmation } = require("@lerna/prompt");

// helpers
const initNamedFixture = require("@lerna-test/init-named-fixture")(__dirname);
Expand All @@ -25,7 +25,7 @@ const lastCommitInDir = (cwd) =>
execa("git", ["log", "-1", "--format=%s"], { cwd }).then((result) => result.stdout);

describe("ImportCommand", () => {
PromptUtilities.promptConfirmation.mockResolvedValue(true);
promptConfirmation.mockResolvedValue(true);

describe("import", () => {
const initBasicFixtures = async () => {
Expand Down Expand Up @@ -178,7 +178,7 @@ describe("ImportCommand", () => {
it("exits early when confirmation is rejected", async () => {
const [testDir, externalDir] = await initBasicFixtures();

PromptUtilities.promptConfirmation.mockResolvedValueOnce(false);
promptConfirmation.mockResolvedValueOnce(false);

await lernaImport(testDir)(externalDir);

Expand Down Expand Up @@ -222,7 +222,7 @@ describe("ImportCommand", () => {
const [testDir, externalDir] = await initBasicFixtures();
await lernaImport(testDir)(externalDir, "--yes");

expect(PromptUtilities.promptConfirmation).not.toHaveBeenCalled();
expect(promptConfirmation).not.toHaveBeenCalled();
});

it("errors without an argument", async () => {
Expand Down
4 changes: 2 additions & 2 deletions commands/import/index.js
Expand Up @@ -7,7 +7,7 @@ const pMapSeries = require("p-map-series");

const ChildProcessUtilities = require("@lerna/child-process");
const { Command } = require("@lerna/command");
const PromptUtilities = require("@lerna/prompt");
const { promptConfirmation } = require("@lerna/prompt");
const { ValidationError } = require("@lerna/validation-error");
const { pulseTillDone } = require("@lerna/pulse-till-done");

Expand Down Expand Up @@ -114,7 +114,7 @@ class ImportCommand extends Command {
return true;
}

return PromptUtilities.promptConfirmation("Are you sure you want to import these commits onto the current branch?");
return promptConfirmation("Are you sure you want to import these commits onto the current branch?");
}

getPackageDirectories() {
Expand Down
6 changes: 2 additions & 4 deletions commands/publish/__tests__/publish-canary.test.js
Expand Up @@ -16,7 +16,7 @@ const childProcess = require("@lerna/child-process");
// mocked modules
const writePkg = require("write-pkg");
const { npmPublish } = require("@lerna/npm-publish");
const PromptUtilities = require("@lerna/prompt");
const { promptConfirmation } = require("@lerna/prompt");
const { throwIfUncommitted } = require("@lerna/check-working-tree");

// helpers
Expand Down Expand Up @@ -75,9 +75,7 @@ test("publish --canary", async () => {
);
await lernaPublish(cwd)("--canary");

expect(PromptUtilities.promptConfirmation).toHaveBeenLastCalledWith(
"Are you sure you want to publish these packages?"
);
expect(promptConfirmation).toHaveBeenLastCalledWith("Are you sure you want to publish these packages?");
expect(npmPublish.registry).toMatchInlineSnapshot(`
Map {
"package-1" => "canary",
Expand Down
6 changes: 2 additions & 4 deletions commands/publish/__tests__/publish-command.test.js
Expand Up @@ -20,7 +20,7 @@ const { getOneTimePassword } = require("@lerna/otplease");
const npmDistTag = require("@lerna/npm-dist-tag");
const { npmPublish } = require("@lerna/npm-publish");
const { packDirectory } = require("@lerna/pack-directory");
const PromptUtilities = require("@lerna/prompt");
const { promptConfirmation } = require("@lerna/prompt");
const { collectUpdates } = require("@lerna/collect-updates");
const { getNpmUsername } = require("../lib/get-npm-username");
const { verifyNpmPackageAccess } = require("../lib/verify-npm-package-access");
Expand Down Expand Up @@ -97,9 +97,7 @@ describe("PublishCommand", () => {

await lernaPublish(testDir)();

expect(PromptUtilities.promptConfirmation).toHaveBeenLastCalledWith(
"Are you sure you want to publish these packages?"
);
expect(promptConfirmation).toHaveBeenLastCalledWith("Are you sure you want to publish these packages?");
expect(packDirectory.registry).toMatchInlineSnapshot(`
Set {
"package-1",
Expand Down
6 changes: 2 additions & 4 deletions commands/publish/__tests__/publish-from-git.test.js
Expand Up @@ -17,7 +17,7 @@ jest.mock("../../version/lib/remote-branch-exists");

// mocked or stubbed modules
const { npmPublish } = require("@lerna/npm-publish");
const PromptUtilities = require("@lerna/prompt");
const { promptConfirmation } = require("@lerna/prompt");
const { output } = require("@lerna/output");
const { throwIfUncommitted } = require("@lerna/check-working-tree");

Expand All @@ -39,9 +39,7 @@ describe("publish from-git", () => {
// called from chained describeRef()
expect(throwIfUncommitted).toHaveBeenCalled();

expect(PromptUtilities.promptConfirmation).toHaveBeenLastCalledWith(
"Are you sure you want to publish these packages?"
);
expect(promptConfirmation).toHaveBeenLastCalledWith("Are you sure you want to publish these packages?");
expect(output.logged()).toMatch("Found 4 packages to publish:");
expect(npmPublish.order()).toEqual([
"package-1",
Expand Down
6 changes: 2 additions & 4 deletions commands/publish/__tests__/publish-from-package.test.js
Expand Up @@ -18,7 +18,7 @@ const path = require("path");
// mocked or stubbed modules
const writePkg = require("write-pkg");
const { npmPublish } = require("@lerna/npm-publish");
const PromptUtilities = require("@lerna/prompt");
const { promptConfirmation } = require("@lerna/prompt");
const { output } = require("@lerna/output");
const { throwIfUncommitted } = require("@lerna/check-working-tree");
const { getUnpublishedPackages } = require("../lib/get-unpublished-packages");
Expand All @@ -41,9 +41,7 @@ describe("publish from-package", () => {

await lernaPublish(cwd)("from-package");

expect(PromptUtilities.promptConfirmation).toHaveBeenLastCalledWith(
"Are you sure you want to publish these packages?"
);
expect(promptConfirmation).toHaveBeenLastCalledWith("Are you sure you want to publish these packages?");
expect(output.logged()).toMatch("Found 2 packages to publish:");
expect(npmPublish.order()).toEqual(["package-2", "package-3"]);
});
Expand Down
4 changes: 2 additions & 2 deletions commands/publish/index.js
Expand Up @@ -11,7 +11,7 @@ const { Command } = require("@lerna/command");
const { ValidationError } = require("@lerna/validation-error");
const { describeRef } = require("@lerna/describe-ref");
const { throwIfUncommitted } = require("@lerna/check-working-tree");
const PromptUtilities = require("@lerna/prompt");
const { promptConfirmation } = require("@lerna/prompt");
const { output } = require("@lerna/output");
const { collectUpdates } = require("@lerna/collect-updates");
const npmConf = require("@lerna/npm-conf");
Expand Down Expand Up @@ -426,7 +426,7 @@ class PublishCommand extends Command {
return true;
}

return PromptUtilities.promptConfirmation("Are you sure you want to publish these packages?");
return promptConfirmation("Are you sure you want to publish these packages?");
}

prepareLicenseActions() {
Expand Down
16 changes: 8 additions & 8 deletions commands/version/__tests__/prompt-version.test.js
Expand Up @@ -3,7 +3,7 @@
jest.mock("@lerna/prompt");

const semver = require("semver");
const prompt = require("@lerna/prompt");
const { promptSelectOne, promptTextInput, mockPromptChoices } = require("@lerna/prompt");
const { prereleaseIdFromVersion } = require("@lerna/prerelease-id-from-version");
const { makePromptVersion } = require("../lib/prompt-version");

Expand All @@ -17,7 +17,7 @@ describe("select", () => {
version: "1.2.3",
});

expect(prompt.promptSelectOne).toHaveBeenLastCalledWith(
expect(promptSelectOne).toHaveBeenLastCalledWith(
"Select a new version (currently 1.2.3)",
expect.objectContaining({
choices: expect.any(Array),
Expand All @@ -31,7 +31,7 @@ describe("select", () => {
name: "my-package",
});

expect(prompt.promptSelectOne).toHaveBeenLastCalledWith(
expect(promptSelectOne).toHaveBeenLastCalledWith(
"Select a new version for my-package (currently 3.2.1)",
expect.any(Object)
);
Expand All @@ -47,7 +47,7 @@ describe("select", () => {
["preminor", "1.1.0-alpha.0"],
["premajor", "2.0.0-alpha.0"],
])("bump %s", async (bump, result) => {
prompt.mockPromptChoices(bump);
mockPromptChoices(bump);

const choice = await versionPrompt({
version: "1.0.0",
Expand All @@ -64,9 +64,9 @@ describe("custom version", () => {
let inputValidate;

beforeEach(() => {
prompt.mockPromptChoices("CUSTOM");
mockPromptChoices("CUSTOM");

prompt.promptTextInput.mockImplementationOnce((msg, cfg) => {
promptTextInput.mockImplementationOnce((msg, cfg) => {
inputFilter = cfg.filter;
inputValidate = cfg.validate;

Expand Down Expand Up @@ -102,9 +102,9 @@ describe("custom prerelease", () => {
let inputFilter;

beforeEach(() => {
prompt.mockPromptChoices("PRERELEASE");
mockPromptChoices("PRERELEASE");

prompt.promptTextInput.mockImplementationOnce((msg, cfg) => {
promptTextInput.mockImplementationOnce((msg, cfg) => {
inputFilter = cfg.filter;

return Promise.resolve(msg);
Expand Down
6 changes: 3 additions & 3 deletions commands/version/__tests__/version-bump-prerelease.test.js
Expand Up @@ -14,7 +14,7 @@ const fs = require("fs-extra");
const path = require("path");

// mocked modules
const prompt = require("@lerna/prompt");
const { promptTextInput, mockPromptChoices } = require("@lerna/prompt");

// helpers
const initFixture = require("@lerna-test/init-fixture")(path.resolve(__dirname, "../../publish/__tests__"));
Expand Down Expand Up @@ -152,8 +152,8 @@ test("independent version prerelease does not bump on every unrelated change", a
await gitCommit(cwd, "init");

// simulate choices for pkg-a then pkg-b
prompt.mockPromptChoices("patch", "PRERELEASE");
prompt.promptTextInput.mockImplementationOnce((msg, cfg) =>
mockPromptChoices("patch", "PRERELEASE");
promptTextInput.mockImplementationOnce((msg, cfg) =>
// the _existing_ "bumps" prerelease ID should be preserved
Promise.resolve(cfg.filter())
);
Expand Down
6 changes: 3 additions & 3 deletions commands/version/__tests__/version-bump.test.js
Expand Up @@ -9,7 +9,7 @@ jest.mock("../lib/remote-branch-exists");
const path = require("path");

// mocked modules
const PromptUtilities = require("@lerna/prompt");
const { promptSelectOne } = require("@lerna/prompt");

// helpers
const initFixture = require("@lerna-test/init-fixture")(path.resolve(__dirname, "../../publish/__tests__"));
Expand All @@ -23,7 +23,7 @@ describe("version bump", () => {
const testDir = await initFixture("normal");
await lernaVersion(testDir)("1.0.1-beta.25");

expect(PromptUtilities.promptSelectOne).not.toHaveBeenCalled();
expect(promptSelectOne).not.toHaveBeenCalled();

const message = await getCommitMessage(testDir);
expect(message).toBe("v1.0.1-beta.25");
Expand Down Expand Up @@ -56,7 +56,7 @@ describe("version bump", () => {
const testDir = await initFixture("normal");
await lernaVersion(testDir)("minor");

expect(PromptUtilities.promptSelectOne).not.toHaveBeenCalled();
expect(promptSelectOne).not.toHaveBeenCalled();

const message = await getCommitMessage(testDir);
expect(message).toBe("v1.1.0");
Expand Down

0 comments on commit 69bb2e4

Please sign in to comment.