$ lerna publish -m "chore(release): publish %s"

# commit message = "chore(release): publish v1.0.0"

$ lerna publish -m "chore(release): publish" --independent

$ lerna publish -m "chore(release): publish %v"

# commit message = "chore(release): publish 1.0.0"

import chalk from "chalk";
import log from "npmlog";
import normalizeNewline from "normalize-newline";
import path from "path";

// mocked or stubbed modules
import writeJsonFile from "write-json-file";
import writePkg from "write-pkg";
import ConventionalCommitUtilities from "../src/ConventionalCommitUtilities";
import GitUtilities from "../src/GitUtilities";
import NpmUtilities from "../src/NpmUtilities";
import PromptUtilities from "../src/PromptUtilities";
import output from "../src/utils/output";

// helpers
import callsBack from "./helpers/callsBack";
import initFixture from "./helpers/initFixture";
import normalizeRelativeDir from "./helpers/normalizeRelativeDir";
import yargsRunner from "./helpers/yargsRunner";

// file under test
import * as commandModule from "../src/commands/PublishCommand";

const PublishCommand = commandModule.default;
const run = yargsRunner(commandModule);

jest.mock("write-json-file");
jest.mock("write-pkg");
jest.mock("../src/GitUtilities");
jest.mock("../src/NpmUtilities");
jest.mock("../src/PromptUtilities");
jest.mock("../src/utils/output");

// silence logs
log.level = "silent";

// keep snapshots stable cross-platform
chalk.enabled = false;

const execOpts = testDir =>
  expect.objectContaining({
    cwd: testDir,
  });

const consoleOutput = () => output.mock.calls.map(args => normalizeNewline(args[0]));

const publishedTagInDirectories = testDir =>
  NpmUtilities.publishTaggedInDir.mock.calls.reduce((arr, args) => {
    const tag = args[0];
    const dir = normalizeRelativeDir(testDir, args[1].location);
    arr.push({ dir, tag });
    return arr;
  }, []);

const removedDistTagInDirectories = testDir =>
  NpmUtilities.removeDistTag.mock.calls.reduce((obj, args) => {
    const location = normalizeRelativeDir(testDir, args[0]);
    const tag = args[2];
    obj[location] = tag;
    return obj;
  }, {});

const addedDistTagInDirectories = testDir =>
  NpmUtilities.addDistTag.mock.calls.reduce((obj, args) => {
    const location = normalizeRelativeDir(testDir, args[0]);
    const pkg = args[1];
    const version = args[2];
    const tag = args[3];
    obj[location] = `${pkg}@${version} ${tag}`;
    return obj;
  }, {});

const gitAddedFiles = testDir =>
  GitUtilities.addFile.mock.calls.map(args => normalizeRelativeDir(testDir, args[0]));

const gitCommitMessage = () => normalizeNewline(GitUtilities.commit.mock.calls[0][0]);

const gitTagsAdded = () => GitUtilities.addTag.mock.calls.map(args => args[0]);

const updatedLernaJson = () => writeJsonFile.sync.mock.calls[0][1];

const updatedPackageVersions = testDir =>
  writePkg.sync.mock.calls.reduce((obj, args) => {
    const location = normalizeRelativeDir(testDir, path.dirname(args[0]));
    obj[location] = args[1].version;
    return obj;
  }, {});

const updatedPackageJSON = name =>
  writePkg.sync.mock.calls
    .reduce((arr, args) => {
      if (args[1].name === name) {
        arr.push(args[1]);
      }
      return arr;
    }, [])
    .pop();

describe("PublishCommand", () => {
  beforeEach(() => {
    // we've already tested these utilities elsewhere
    GitUtilities.isInitialized = jest.fn(() => true);
    GitUtilities.getCurrentBranch = jest.fn(() => "master");
    GitUtilities.getCurrentSHA = jest.fn(() => "deadbeefcafe");

    NpmUtilities.publishTaggedInDir = jest.fn(callsBack());
    NpmUtilities.checkDistTag = jest.fn(() => true);

    PromptUtilities.select = jest.fn(callsBack("1.0.1"));
    PromptUtilities.confirm = jest.fn(callsBack(true));
  });

  afterEach(() => jest.resetAllMocks());

  /** =========================================================================
   * NORMAL
   * ======================================================================= */

  describe("normal mode", () => {
    let testDir;

    beforeEach(() =>
      initFixture("PublishCommand/normal").then(dir => {
        testDir = dir;
      }),
    );

    it("should publish the changed packages", () =>
      run(testDir)().then(() => {
        expect(PromptUtilities.select.mock.calls).toMatchSnapshot("[normal] prompt");
        expect(PromptUtilities.confirm).toBeCalled();

        expect(updatedLernaJson()).toMatchObject({ version: "1.0.1" });
        expect(updatedPackageVersions(testDir)).toMatchSnapshot("[normal] bumps package versions");

        expect(updatedPackageJSON("package-2").dependencies).toMatchObject({
          "package-1": "^1.0.1",
        });
        expect(updatedPackageJSON("package-3").devDependencies).toMatchObject({
          "package-2": "^1.0.1",
        });
        // peerDependencies are _never_ modified automatically
        expect(updatedPackageJSON("package-3").peerDependencies).toMatchObject({
          "package-2": "^1.0.0",
        });
        expect(updatedPackageJSON("package-4").dependencies).toMatchObject({
          "package-1": "^0.0.0",
        });
        expect(updatedPackageJSON("package-5").dependencies).toMatchObject({
          "package-1": "^1.0.1",
        });

        expect(gitAddedFiles(testDir)).toMatchSnapshot("[normal] git adds changed files");
        expect(gitCommitMessage()).toEqual("v1.0.1");
        expect(gitTagsAdded()).toEqual(["v1.0.1"]);

        expect(publishedTagInDirectories(testDir)).toMatchSnapshot("[normal] npm publish --tag");

        expect(GitUtilities.pushWithTags).lastCalledWith("origin", gitTagsAdded(), execOpts(testDir));
        expect(consoleOutput()).toMatchSnapshot("[normal] console output");
      }));

    it("throws an error when --independent is passed", () =>
      run(testDir)("--independent").catch(error => {
        expect(error.exitCode).toBe(1);
      }));
  });

  /** =========================================================================
   * INDEPENDENT
   * ======================================================================= */

  describe("independent mode", () => {
    let testDir;

    beforeEach(() =>
      initFixture("PublishCommand/independent").then(dir => {
        testDir = dir;
      }),
    );

    it("should publish the changed packages in independent mode", () => {
      const promptReplies = ["1.0.1", "1.1.0", "2.0.0", "1.1.0", "1.0.1"];
      PromptUtilities.select = jest.fn((...args) => {
        const reply = promptReplies.shift();
        return callsBack(reply)(...args);
      });

      return run(testDir)(
        "--independent", // not required due to lerna.json config, but here to assert it doesn't error
      ).then(() => {
        expect(PromptUtilities.confirm).toBeCalled();

        expect(writeJsonFile.sync).not.toBeCalled();
        expect(updatedPackageVersions(testDir)).toMatchSnapshot("[independent] bumps package versions");

        expect(updatedPackageJSON("package-2").dependencies).toMatchObject({
          "package-1": "^1.0.1",
        });
        expect(updatedPackageJSON("package-3").devDependencies).toMatchObject({
          "package-2": "^1.1.0",
        });
        expect(updatedPackageJSON("package-4").dependencies).toMatchObject({
          "package-1": "^0.0.0",
        });
        expect(updatedPackageJSON("package-5").dependencies).toMatchObject({
          "package-3": "^2.0.0",
        });

        expect(gitAddedFiles(testDir)).toMatchSnapshot("[independent] git adds changed files");
        expect(gitCommitMessage()).toMatchSnapshot("[independent] git commit message");
        expect(gitTagsAdded()).toMatchSnapshot("[independent] git tags added");
        expect(GitUtilities.checkoutChanges).not.toBeCalled();

        expect(publishedTagInDirectories(testDir)).toMatchSnapshot("[independent] npm publish --tag");

        expect(GitUtilities.pushWithTags).lastCalledWith("origin", gitTagsAdded(), execOpts(testDir));
        expect(consoleOutput()).toMatchSnapshot("[independent] console output");
      });
    });
  });

  /** =========================================================================
   * NORMAL - CANARY
   * ======================================================================= */

  describe("normal mode as canary", () => {
    let testDir;

    beforeEach(() =>
      initFixture("PublishCommand/normal").then(dir => {
        testDir = dir;
      }),
    );

    it("should publish the changed packages", () =>
      run(testDir)("--canary").then(() => {
        expect(PromptUtilities.select).not.toBeCalled();

        expect(writeJsonFile.sync).not.toBeCalled();
        expect(updatedPackageVersions(testDir)).toMatchSnapshot("[normal --canary] bumps package versions");

        expect(updatedPackageJSON("package-2").dependencies).toMatchObject({
          "package-1": "^1.1.0-alpha.deadbeef",
        });
        expect(updatedPackageJSON("package-3").devDependencies).toMatchObject({
          "package-2": "^1.1.0-alpha.deadbeef",
        });
        expect(updatedPackageJSON("package-4").dependencies).toMatchObject({
          "package-1": "^0.0.0",
        });

        expect(GitUtilities.addFile).not.toBeCalled();
        expect(GitUtilities.commit).not.toBeCalled();
        expect(GitUtilities.addTag).not.toBeCalled();
        expect(GitUtilities.checkoutChanges).lastCalledWith(
          expect.stringContaining("packages/*/package.json"),
          execOpts(testDir),
        );

        expect(GitUtilities.pushWithTags).not.toBeCalled();
        expect(publishedTagInDirectories(testDir)).toMatchSnapshot("[normal --canary] npm publish --tag");
      }));

    it("should use the provided value as the meta suffix", () =>
      run(testDir)("--canary", "beta").then(() => {
        expect(updatedPackageVersions(testDir)).toMatchSnapshot(
          "[normal --canary=beta] bumps package versions",
        );

        expect(updatedPackageJSON("package-2").dependencies).toMatchObject({
          "package-1": "^1.1.0-beta.deadbeef",
        });
        expect(updatedPackageJSON("package-3").devDependencies).toMatchObject({
          "package-2": "^1.1.0-beta.deadbeef",
        });
        expect(updatedPackageJSON("package-4").dependencies).toMatchObject({
          "package-1": "^0.0.0",
        });
      }));

    it("should work with --canary and --cd-version=patch", () =>
      run(testDir)("--canary", "--cd-version", "patch").then(() => {
        expect(updatedPackageVersions(testDir)).toMatchSnapshot(
          "[normal --canary --cd-version=patch] bumps package versions",
        );

        expect(updatedPackageJSON("package-2").dependencies).toMatchObject({
          "package-1": "^1.0.1-alpha.deadbeef",
        });
        expect(updatedPackageJSON("package-3").devDependencies).toMatchObject({
          "package-2": "^1.0.1-alpha.deadbeef",
        });
        expect(updatedPackageJSON("package-4").dependencies).toMatchObject({
          "package-1": "^0.0.0",
        });
      }));
  });

  /** =========================================================================
   * INDEPENDENT - CANARY
   * ======================================================================= */

  describe("independent mode as canary", () => {
    let testDir;

    beforeEach(() =>
      initFixture("PublishCommand/independent").then(dir => {
        testDir = dir;
      }),
    );

    it("should publish the changed packages", () =>
      run(testDir)("--canary").then(() => {
        expect(PromptUtilities.select).not.toBeCalled();

        expect(writeJsonFile.sync).not.toBeCalled();
        expect(updatedPackageVersions(testDir)).toMatchSnapshot(
          "[independent --canary] bumps package versions",
        );

        expect(updatedPackageJSON("package-2").dependencies).toMatchObject({
          "package-1": "^1.1.0-alpha.deadbeef",
        });
        expect(updatedPackageJSON("package-3").devDependencies).toMatchObject({
          "package-2": "^2.1.0-alpha.deadbeef",
        });
        expect(updatedPackageJSON("package-4").dependencies).toMatchObject({
          "package-1": "^0.0.0",
        });

        expect(publishedTagInDirectories(testDir)).toMatchSnapshot(
          "[independent --canary] npm publish --tag",
        );
      }));

    it("should use the provided value as the meta suffix", () =>
      run(testDir)("--canary", "beta").then(() => {
        expect(updatedPackageJSON("package-2").dependencies).toMatchObject({
          "package-1": "^1.1.0-beta.deadbeef",
        });
        expect(updatedPackageJSON("package-3").devDependencies).toMatchObject({
          "package-2": "^2.1.0-beta.deadbeef",
        });
        expect(updatedPackageJSON("package-4").dependencies).toMatchObject({
          "package-1": "^0.0.0",
        });
      }));
  });

  /** =========================================================================
   * NORMAL - SKIP GIT
   * ======================================================================= */

  describe("normal mode with --skip-git", () => {
    let testDir;

    beforeEach(() =>
      initFixture("PublishCommand/normal").then(dir => {
        testDir = dir;
      }),
    );

    it("should publish the changed packages", () =>
      run(testDir)("--skip-git").then(() => {
        expect(GitUtilities.addFile).not.toBeCalled();
        expect(GitUtilities.commit).not.toBeCalled();
        expect(GitUtilities.addTag).not.toBeCalled();
        expect(GitUtilities.pushWithTags).not.toBeCalled();

        expect(publishedTagInDirectories(testDir)).toMatchSnapshot("[normal --skip-git] npm publish --tag");
      }));
  });

  /** =========================================================================
   * NORMAL - SKIP NPM
   * ======================================================================= */

  describe("normal mode with --skip-npm", () => {
    let testDir;

    beforeEach(() =>
      initFixture("PublishCommand/normal").then(dir => {
        testDir = dir;
      }),
    );

    it("should update versions and push changes but not publish", () =>
      run(testDir)("--skip-npm").then(() => {
        expect(NpmUtilities.publishTaggedInDir).not.toBeCalled();
        expect(NpmUtilities.checkDistTag).not.toBeCalled();
        expect(NpmUtilities.removeDistTag).not.toBeCalled();
        expect(NpmUtilities.addDistTag).not.toBeCalled();

        expect(gitCommitMessage()).toEqual("v1.0.1");
        // FIXME
        // expect(GitUtilities.pushWithTags).lastCalledWith("origin", ["v1.0.1"]);
      }));
  });

  /** =========================================================================
   * NORMAL - SKIP GIT AND SKIP NPM
   * ======================================================================= */

  describe("normal mode with --skip-git and --skip-npm", () => {
    let testDir;

    beforeEach(() =>
      initFixture("PublishCommand/normal").then(dir => {
        testDir = dir;
      }),
    );

    it("should update versions but not push changes or publish", () =>
      run(testDir)("--skip-git", "--skip-npm").then(() => {
        expect(updatedLernaJson()).toMatchObject({ version: "1.0.1" });
        expect(updatedPackageVersions(testDir)).toMatchSnapshot(
          "[normal --skip-git --skip-npm] bumps package versions",
        );

        expect(updatedPackageJSON("package-2").dependencies).toMatchObject({
          "package-1": "^1.0.1",
        });
        expect(updatedPackageJSON("package-3").devDependencies).toMatchObject({
          "package-2": "^1.0.1",
        });
        expect(updatedPackageJSON("package-4").dependencies).toMatchObject({
          "package-1": "^0.0.0",
        });
        expect(updatedPackageJSON("package-5").dependencies).toMatchObject({
          "package-1": "^1.0.1",
        });

        expect(GitUtilities.addFile).not.toBeCalled();
        expect(GitUtilities.commit).not.toBeCalled();
        expect(GitUtilities.addTag).not.toBeCalled();
        expect(GitUtilities.pushWithTags).not.toBeCalled();

        expect(NpmUtilities.publishTaggedInDir).not.toBeCalled();
        expect(NpmUtilities.checkDistTag).not.toBeCalled();
        expect(NpmUtilities.removeDistTag).not.toBeCalled();
        expect(NpmUtilities.addDistTag).not.toBeCalled();
      }));
  });

  /** =========================================================================
   * NORMAL - TEMP TAG
   * ======================================================================= */

  describe("normal mode with --temp-tag", () => {
    let testDir;

    beforeEach(() =>
      initFixture("PublishCommand/normal").then(dir => {
        testDir = dir;
      }),
    );

    it("should publish the changed packages with a temp tag", () =>
      run(testDir)("--temp-tag").then(() => {
        expect(publishedTagInDirectories(testDir)).toMatchSnapshot("[normal --temp-tag] npm publish --tag");
        expect(removedDistTagInDirectories(testDir)).toMatchSnapshot("[normal --temp-tag] npm dist-tag rm");
        expect(addedDistTagInDirectories(testDir)).toMatchSnapshot("[normal --temp-tag] npm dist-tag add");

        expect(GitUtilities.pushWithTags).lastCalledWith("origin", ["v1.0.1"], execOpts(testDir));
      }));
  });

  /** =========================================================================
   * NORMAL - NPM TAG
   * ======================================================================= */

  describe("normal mode with --npm-tag", () => {
    let testDir;

    beforeEach(() =>
      initFixture("PublishCommand/normal").then(dir => {
        testDir = dir;
      }),
    );

    it("should publish the changed packages with npm tag", () =>
      run(testDir)("--npm-tag", "custom").then(() => {
        expect(publishedTagInDirectories(testDir)).toMatchSnapshot("[normal --npm-tag] npm publish --tag");
      }));
  });

  /** =========================================================================
   * AUTO-ACCEPT
   * ======================================================================= */

  describe("with --yes", () => {
    it("skips confirmation prompt", done => {
      const publishCommand = new PublishCommand([], {
        yes: true,
      });
      publishCommand.updates = [];
      publishCommand.confirmVersions(err => {
        if (err) {
          return done.fail(err);
        }

        try {
          expect(PromptUtilities.confirm).not.toBeCalled();

          done();
        } catch (ex) {
          done.fail(ex);
        }
      });
    });
  });

  /** =========================================================================
   * NORMAL - REGISTRY
   * ======================================================================= */

  describe("normal mode with --registry", () => {
    let testDir;

    beforeEach(() =>
      initFixture("PublishCommand/normal").then(dir => {
        testDir = dir;
      }),
    );

    it("passes registry to npm commands", () => {
      const registry = "https://my-private-registry";

      return run(testDir)("--registry", registry).then(() => {
        expect(NpmUtilities.checkDistTag).not.toBeCalled();
        expect(NpmUtilities.removeDistTag).not.toBeCalled();
        expect(NpmUtilities.addDistTag).not.toBeCalled();
        // FIXME: this isn't actually asserting anything about --registry
        expect(publishedTagInDirectories(testDir)).toMatchSnapshot("[normal --registry] npm publish --tag");
      });
    });
  });

  /** =========================================================================
   * NORMAL - REPO VERSION
   * ======================================================================= */

  describe("normal mode with --repo-version", () => {
    let testDir;

    beforeEach(() =>
      initFixture("PublishCommand/normal").then(dir => {
        testDir = dir;
      }),
    );

    it("skips version prompt and publishes changed packages with designated version", () =>
      run(testDir)("--repo-version", "1.0.1-beta").then(() => {
        expect(PromptUtilities.select).not.toBeCalled();
        expect(updatedLernaJson()).toMatchObject({ version: "1.0.1-beta" });
      }));
  });

  /** =========================================================================
   * NORMAL - EXACT
   * ======================================================================= */

  describe("normal mode with --exact", () => {
    let testDir;

    beforeEach(() =>
      initFixture("PublishCommand/normal").then(dir => {
        testDir = dir;
      }),
    );

    it("updates matching local dependencies of published packages with exact versions", () =>
      run(testDir)("--exact").then(() => {
        expect(updatedPackageJSON("package-2").dependencies).toMatchObject({
          "package-1": "1.0.1",
        });
        expect(updatedPackageJSON("package-3").devDependencies).toMatchObject({
          "package-2": "1.0.1",
        });
        // package-4's dependency on package-1 remains semver because
        // it does not match the version of package-1 being published
        expect(updatedPackageJSON("package-4").dependencies).toMatchObject({
          "package-1": "^0.0.0",
        });
        expect(updatedPackageJSON("package-5").dependencies).toMatchObject({
          "package-1": "1.0.1",
        });
      }));
  });

  /** =========================================================================
   * NORMAL MODE - CD VERSION
   * ======================================================================= */

  describe("normal mode with --cd-version", () => {
    let testDir;

    beforeEach(() =>
      initFixture("PublishCommand/normal").then(dir => {
        testDir = dir;
      }),
    );

    it("should use semver increments when passed to cdVersion flag", () =>
      run(testDir)("--cd-version", "minor").then(() => {
        expect(PromptUtilities.select).not.toBeCalled();
        expect(gitCommitMessage()).toBe("v1.1.0");
      }));

    it("throws an error when an invalid semver keyword is used", async () => {
      expect.assertions(1);
      try {
        await run(testDir)("--cd-version", "poopypants");
      } catch (err) {
        expect(err.message).toBe(
          "--cd-version must be one of: " +
            "'major', 'minor', 'patch', 'premajor', 'preminor', 'prepatch', or 'prerelease'.",
        );
      }
    });
  });

  /** =========================================================================
   * CD VERSION - REPUBLISH PRERELEASED
   * ======================================================================= */

  describe("CD VERSION - REPUBLISH PRERELEASED ", () => {
    let testDir;

    beforeEach(async () => {
      testDir = await initFixture("PublishCommand/republish-prereleased");

      GitUtilities.hasTags.mockReturnValue(true);
      GitUtilities.getLastTag.mockReturnValue("v1.0.1-beta.3");
      GitUtilities.diffSinceIn.mockImplementation((since, location) => {
        if (location.endsWith("package-3")) {
          return "packages/package-3/newfile.json";
        }
        return "";
      });
    });

    it("publishes changed & prereleased packages if --cd-version is non-prerelease", async () => {
      // should republish 3, 4, and 5 because:
      // package 3 changed
      // package 5 has a prerelease version
      // package 4 depends on package 5
      await run(testDir)("--cd-version", "patch");
      expect(gitCommitMessage()).toBe("v1.0.1");
      expect(updatedPackageVersions(testDir)).toMatchSnapshot("[republish prereleased] patch");
      expect(updatedPackageJSON("package-4").dependencies).toMatchObject({
        "package-5": "^1.0.1",
      });
    });

    it("should not publish prereleased packages if --cd-version is a pre-* increment", async () => {
      // should republish only package 3, because it changed
      await run(testDir)("--cd-version", "prerelease", "---preid", "beta");
      expect(gitCommitMessage()).toBe("v1.0.1-beta.4");
      expect(updatedPackageVersions(testDir)).toMatchSnapshot("[republish prereleased] prerelease");
    });
  });

  /** =========================================================================
   * INDEPENDENT - CD VERSION
   * ======================================================================= */

  describe("indepdendent mode with --cd-version", () => {
    let testDir;

    beforeEach(() =>
      initFixture("PublishCommand/independent").then(dir => {
        testDir = dir;
      }),
    );

    it("should use semver increments when passed to cdVersion flag", () =>
      run(testDir)("--cd-version", "patch").then(() => {
        expect(PromptUtilities.select).not.toBeCalled();
        expect(gitCommitMessage()).toMatchSnapshot("[independent --cd-version] git commit message");
      }));

    /** =========================================================================
     * INDEPENDENT - CD VERSION - PRERELEASE
     * ======================================================================= */

    it("should bump to prerelease versions with --cd-version=prerelease --preid=foo", () =>
      run(testDir)("--cd-version", "prerelease", "--preid", "foo").then(() => {
        expect(updatedPackageVersions(testDir)).toMatchSnapshot(
          "[independent --cd-version=prerelease --preid=foo] bumps package versions",
        );

        expect(updatedPackageJSON("package-2").dependencies).toMatchObject({
          "package-1": "^1.0.1-foo.0",
        });
        expect(updatedPackageJSON("package-3").devDependencies).toMatchObject({
          "package-2": "^2.0.1-foo.0",
        });
        expect(updatedPackageJSON("package-4").dependencies).toMatchObject({
          "package-1": "^0.0.0",
        });
      }));

    it("should bump to prerelease versions with --cd-version prerelease (no --preid)", () =>
      run(testDir)("--cd-version", "prerelease").then(() => {
        expect(updatedPackageVersions(testDir)).toMatchSnapshot(
          "[independent --cd-version=prerelease] bumps package versions",
        );

        expect(updatedPackageJSON("package-2").dependencies).toMatchObject({
          "package-1": "^1.0.1-0",
        });
        expect(updatedPackageJSON("package-3").devDependencies).toMatchObject({
          "package-2": "^2.0.1-0",
        });
        expect(updatedPackageJSON("package-4").dependencies).toMatchObject({
          "package-1": "^0.0.0",
        });
      }));
  });

  /** =========================================================================
   * NORMAL - GIT REMOTE
   * ======================================================================= */

  describe("normal mode with --git-remote", () => {
    let testDir;

    beforeEach(() =>
      initFixture("PublishCommand/normal").then(dir => {
        testDir = dir;
      }),
    );

    it("pushes tags to specified remote", () =>
      run(testDir)("--git-remote", "upstream").then(() => {
        expect(GitUtilities.pushWithTags).lastCalledWith("upstream", ["v1.0.1"], execOpts(testDir));
      }));
  });

  /** =========================================================================
   * NORMAL - GIT REMOTE
   * ======================================================================= */

  describe("normal mode with --ignore", () => {
    let testDir;

    beforeEach(() =>
      initFixture("PublishCommand/normal").then(dir => {
        testDir = dir;
      }),
    );

    it("does not publish ignored packages", () =>
      run(testDir)("--ignore", "package-2", "--ignore", "package-3", "--ignore", "package-4").then(() => {
        expect(gitAddedFiles(testDir)).toMatchSnapshot("[normal --ignore] git adds changed files");
        expect(publishedTagInDirectories(testDir)).toMatchSnapshot("[normal --ignore] npm publish --tag");
      }));
  });

  /** =========================================================================
   * NORMAL - MESSAGE
   * ======================================================================= */

  describe("normal mode with --message", () => {
    let testDir;

    beforeEach(() =>
      initFixture("PublishCommand/normal").then(dir => {
        testDir = dir;
      }),
    );

    it("commits changes with a custom message", () =>
      run(testDir)("--message", "chore: Release %s :rocket:").then(() => {
        expect(GitUtilities.commit).lastCalledWith("chore: Release v1.0.1 :rocket:", execOpts(testDir));
      }));
  });

  /** =========================================================================
   * INDEPENDENT - MESSAGE
   * ======================================================================= */

  describe("independent mode with --message", () => {
    let testDir;

    beforeEach(() =>
      initFixture("PublishCommand/independent").then(dir => {
        testDir = dir;
      }),
    );

    it("commits changes with a custom message", () =>
      run(testDir)("-m", "chore: Custom publish message").then(() => {
        expect(GitUtilities.commit).lastCalledWith(expect.stringContaining("chore:"), execOpts(testDir));
        expect(gitCommitMessage()).toMatchSnapshot("[independent --message] git commit message");
      }));
  });

  /** =========================================================================
   * CONVENTIONAL COMMITS
   * ======================================================================= */

  describe("--conventional-commits", () => {
    let testDir;

    describe("independent mode", () => {
      const recommendIndependentVersionOriginal = ConventionalCommitUtilities.recommendIndependentVersion;
      const updateIndependentChangelogOriginal = ConventionalCommitUtilities.updateIndependentChangelog;

      beforeEach(() =>
        initFixture("PublishCommand/independent").then(dir => {
          testDir = dir;

          const reccomendReplies = ["1.0.1", "1.1.0", "2.0.0", "1.1.0", "5.1.1"];
          ConventionalCommitUtilities.recommendIndependentVersion = jest.fn(() => reccomendReplies.shift());
          ConventionalCommitUtilities.updateIndependentChangelog = jest.fn();
        }),
      );

      afterEach(() => {
        ConventionalCommitUtilities.recommendIndependentVersion = recommendIndependentVersionOriginal;
        ConventionalCommitUtilities.updateIndependentChangelog = updateIndependentChangelogOriginal;
      });

      it("should use conventional-commits utility to guess version bump and generate CHANGELOG", () =>
        run(testDir)("--conventional-commits").then(() => {
          expect(gitAddedFiles(testDir)).toMatchSnapshot(
            "[independent --conventional-commits] git adds changed files",
          );
          expect(gitCommitMessage()).toMatchSnapshot(
            "[independent --conventional-commits] git commit message",
          );

          [
            ["package-1", "1.0.0"],
            ["package-2", "2.0.0"],
            ["package-3", "3.0.0"],
            ["package-4", "4.0.0"],
            ["package-5", "5.0.0"],
          ].forEach(([name, version]) => {
            const location = path.join(testDir, "packages", name);

            expect(ConventionalCommitUtilities.recommendIndependentVersion).toBeCalledWith(
              expect.objectContaining({ name, version }),
              execOpts(testDir),
            );
            expect(ConventionalCommitUtilities.updateIndependentChangelog).toBeCalledWith(
              expect.objectContaining({ name, location }),
              execOpts(testDir),
            );
          });
        }));

      it("accepts --changelog-preset option", () =>
        run(testDir)("--conventional-commits", "--changelog-preset", "foo-bar").then(() => {
          const name = "package-3";
          const version = "3.0.0";
          const location = path.join(testDir, "packages", name);

          expect(ConventionalCommitUtilities.recommendIndependentVersion).toBeCalledWith(
            expect.objectContaining({ name, version }),
            expect.objectContaining({
              cwd: testDir,
              changelogPreset: "foo-bar",
            }),
          );
          expect(ConventionalCommitUtilities.updateIndependentChangelog).toBeCalledWith(
            expect.objectContaining({ name, location }),
            expect.objectContaining({
              cwd: testDir,
              changelogPreset: "foo-bar",
            }),
          );
        }));
    });

    describe("fixed mode", () => {
      const recommendFixedVersionOriginal = ConventionalCommitUtilities.recommendFixedVersion;
      const updateFixedRootChangelogOriginal = ConventionalCommitUtilities.updateFixedRootChangelog;
      const updateFixedChangelogOriginal = ConventionalCommitUtilities.updateFixedChangelog;

      beforeEach(() =>
        initFixture("PublishCommand/normal").then(dir => {
          testDir = dir;

          const reccomendReplies = ["1.0.1", "1.1.0", "2.0.0", "1.1.0", "5.1.1"];
          ConventionalCommitUtilities.recommendFixedVersion = jest.fn(() => reccomendReplies.shift());
          ConventionalCommitUtilities.updateFixedRootChangelog = jest.fn();
          ConventionalCommitUtilities.updateFixedChangelog = jest.fn();
        }),
      );

      afterEach(() => {
        ConventionalCommitUtilities.recommendFixedVersion = recommendFixedVersionOriginal;
        ConventionalCommitUtilities.updateFixedRootChangelog = updateFixedRootChangelogOriginal;
        ConventionalCommitUtilities.updateFixedChangelog = updateFixedChangelogOriginal;
      });

      it("should use conventional-commits utility to guess version bump and generate CHANGELOG", () =>
        run(testDir)("--conventional-commits").then(() => {
          expect(gitAddedFiles(testDir)).toMatchSnapshot(
            "[fixed --conventional-commits] git adds changed files",
          );
          expect(gitCommitMessage()).toMatchSnapshot("[fixed --conventional-commits] git commit message");

          [
            ["package-1", "1.0.0"],
            ["package-2", "1.0.0"],
            ["package-3", "1.0.0"],
            ["package-4", "1.0.0"],
            ["package-5", "1.0.0"],
          ].forEach(([name, version]) => {
            const location = path.join(testDir, "packages", name);

            expect(ConventionalCommitUtilities.recommendFixedVersion).toBeCalledWith(
              expect.objectContaining({ name, version, location }),
              execOpts(testDir),
            );

            expect(ConventionalCommitUtilities.updateFixedChangelog).toBeCalledWith(
              expect.objectContaining({ name, location }),
              execOpts(testDir),
            );
          });

          expect(ConventionalCommitUtilities.updateFixedRootChangelog).toBeCalledWith(
            expect.objectContaining({
              name: "normal",
              location: path.join(testDir),
            }),
            execOpts(testDir),
          );
        }));

      it("accepts --changelog-preset option", () =>
        run(testDir)("--conventional-commits", "--changelog-preset", "baz-qux").then(() => {
          const name = "package-5";
          const version = "1.0.0";
          const location = path.join(testDir, "packages", name);

          expect(ConventionalCommitUtilities.recommendFixedVersion).toBeCalledWith(
            expect.objectContaining({ name, version, location }),
            expect.objectContaining({
              cwd: testDir,
              changelogPreset: "baz-qux",
            }),
          );
          expect(ConventionalCommitUtilities.updateFixedChangelog).toBeCalledWith(
            expect.objectContaining({ name, location }),
            expect.objectContaining({
              cwd: testDir,
              changelogPreset: "baz-qux",
            }),
          );
        }));
    });
  });

  /** =========================================================================
   * INDEPENDENT - CANARY + NPMTAG + YES + EXACT
   * ======================================================================= */

  describe("independent mode --canary --npm-tag=next --yes --exact", () => {
    let testDir;

    beforeEach(() =>
      initFixture("PublishCommand/independent").then(dir => {
        testDir = dir;
      }),
    );

    it("should publish the changed packages", () =>
      run(testDir)("--canary", "--npm-tag", "next", "--yes", "--exact").then(() => {
        expect(publishedTagInDirectories(testDir)).toMatchSnapshot(
          "[independent --canary --npm-tag=next --yes --exact] npm publish --tag",
        );
      }));
  });

  describe("--allow-branch", () => {
    describe("cli", () => {
      let testDir;

      beforeEach(async () => {
        testDir = await initFixture("PublishCommand/normal");
      });

      it("should reject a non matching branch", async () => {
        GitUtilities.getCurrentBranch.mockReturnValueOnce("unmatched");

        try {
          await run(testDir)("--allow-branch", "master");
        } catch (err) {
          expect(err.message).toMatch("Branch 'unmatched' is restricted from publishing");
        }
      });

      it("should accept an exactly matching branch", async () => {
        GitUtilities.getCurrentBranch.mockReturnValueOnce("exact-match");

        const { exitCode } = await run(testDir)("--allow-branch", "exact-match");
        expect(exitCode).toBe(0);
      });

      it("should accept a branch that matches by wildcard", async () => {
        GitUtilities.getCurrentBranch.mockReturnValueOnce("feature/awesome");

        const { exitCode } = await run(testDir)("--allow-branch", "feature/*");
        expect(exitCode).toBe(0);
      });

      it("should accept a branch that matches one of the items passed", async () => {
        GitUtilities.getCurrentBranch.mockReturnValueOnce("feature/awesome");

        const { exitCode } = await run(testDir)("--allow-branch", "master", "feature/*");
        expect(exitCode).toBe(0);
      });
    });

    describe("lerna.json", () => {
      let testDir;

      beforeEach(async () => {
        testDir = await initFixture("PublishCommand/allow-branch-lerna");
      });

      it("should reject a non matching branch", async () => {
        GitUtilities.getCurrentBranch.mockReturnValueOnce("unmatched");

        try {
          await run(testDir)();
        } catch (err) {
          expect(err.message).toMatch("Branch 'unmatched' is restricted from publishing");
        }
      });

      it("should accept a matching branch", async () => {
        GitUtilities.getCurrentBranch.mockReturnValueOnce("lerna");

        const { exitCode } = await run(testDir)();
        expect(exitCode).toBe(0);
      });

      it("should prioritize cli over defaults", async () => {
        GitUtilities.getCurrentBranch.mockReturnValueOnce("cli-override");

        const { exitCode } = await run(testDir)("--allow-branch", "cli-override");
        expect(exitCode).toBe(0);
      });
    });

    describe("with --canary", () => {
      it("does not restrict publishing canary versions", async () => {
        const testDir = await initFixture("PublishCommand/normal");
        GitUtilities.getCurrentBranch.mockReturnValueOnce("other");

        const { exitCode } = await run(testDir)("--allow-branch", "master", "--canary");
        expect(exitCode).toBe(0);
        expect(updatedPackageVersions(testDir)).toMatchSnapshot();
      });
    });
  });

  /** =========================================================================
   * VERSION LIFECYCLE SCRIPTS
   * ======================================================================= */

  describe("lifecycle scripts", () => {
    let testDir;

    const scripts = ["preversion", "version", "postversion"];

    beforeEach(async () => {
      testDir = await initFixture("PublishCommand/lifecycle");
    });

    it("should call version lifecycle scripts for a package", async () => {
      await run(testDir)();
      scripts.forEach(script => {
        expect(NpmUtilities.runScriptInDirSync).toHaveBeenCalledWith(
          script,
          {
            args: [],
            directory: path.resolve(testDir, "packages", "package-1"),
            npmClient: "npm",
          },
          expect.any(Function),
        );
      });
    });

    it("should not call version lifecycle scripts for a package missing them", async () => {
      await run(testDir)();
      scripts.forEach(script => {
        expect(NpmUtilities.runScriptInDirSync).not.toHaveBeenCalledWith(
          script,
          {
            args: [],
            directory: path.resolve(testDir, "packages", "package-2"),
            npmClient: "npm",
          },
          expect.any(Function),
        );
      });
    });

    it("should call version lifecycle scripts in the correct order", async () => {
      await run(testDir)();
      expect(NpmUtilities.runScriptInDirSync.mock.calls.map(args => args[0])).toEqual(scripts);
    });
  });
});


package-cycle-extraneous

This package is used to break ties between package-cycle-{1,2}.


If the message contains `%v`, it will be replaced with the new global version version number without the leading "v".

  A tool for managing JavaScript projects with multiple packages.

- [About](#about)
- [Getting Started](#getting-started)
- [How It Works](#how-it-works)
- [Troubleshooting](#troubleshooting)
- [Commands](#commands)
- [Misc](#misc)
- [Lerna.json](#lernajson)
- [Flags](#flags)

## About

Splitting up large codebases into separate independently versioned packages
is extremely useful for code sharing. However, making changes across many
repositories is *messy* and difficult to track, and testing across repositories
gets complicated really fast.

To solve these (and many other) problems, some projects will organize their
codebases into multi-package repositories (sometimes called [monorepos](https://github.com/babel/babel/blob/master/doc/design/monorepo.md)). Projects like [Babel](https://github.com/babel/babel/tree/master/packages), [React](https://github.com/facebook/react/tree/master/packages), [Angular](https://github.com/angular/angular/tree/master/modules),
[Ember](https://github.com/emberjs/ember.js/tree/master/packages), [Meteor](https://github.com/meteor/meteor/tree/devel/packages), [Jest](https://github.com/facebook/jest/tree/master/packages), and many others develop all of their packages within a
single repository.

**Lerna is a tool that optimizes the workflow around managing multi-package
repositories with git and npm.**

Lerna can also reduce the time and space requirements for numerous
copies of packages in development and build environments - normally a
downside of dividing a project into many separate NPM package. See the
[hoist documentation](doc/hoist.md) for details.

### What does a Lerna repo look like?

There's actually very little to it. You have a file system that looks like this:

```
my-lerna-repo/
  package.json
  packages/
    package-1/
      package.json
    package-2/
      package.json
```

### What can Lerna do?

The two primary commands in Lerna are `lerna bootstrap` and `lerna publish`.

`bootstrap` will link dependencies in the repo together.
`publish` will help publish any updated packages.

## Getting Started

> The instructions below are for Lerna 2.x.
> We recommend using it instead of 1.x for a new Lerna project. Check the [wiki](https://github.com/lerna/lerna/wiki/1.x-Docs) if you need to see the 1.x README.

Let's start by installing Lerna globally with [npm](https://www.npmjs.com/).

```sh
$ npm install --global lerna
```

Next we'll create a new folder:

```sh
$ mkdir lerna-repo
$ cd lerna-repo
```

And now let's turn it into a Lerna repo:

```sh
$ lerna init
```

This will create a `lerna.json` configuration file as well as a `packages` folder, so your folder should now look like this:

```
lerna-repo/
  packages/
  package.json
  lerna.json
```

## How It Works

Lerna allows you to manage your project using one of two modes: Fixed or Independent.

### Fixed/Locked mode (default)

Fixed mode Lerna projects operate on a single version line. The version is kept in the `lerna.json` file at the root of your project under the `version` key. When you run `lerna publish`, if a module has been updated since the last time a release was made, it will be updated to the new version you're releasing. This means that you only publish a new version of a package when you need to.

This is the mode that [Babel](https://github.com/babel/babel) is currently using. Use this if you want to automatically tie all package versions together. One issue with this approach is that a major change in any package will result in all packages having a new major version.

### Independent mode (`--independent`)

Independent mode Lerna projects allows maintainers to increment package versions independently of each other. Each time you publish, you will get a prompt for each package that has changed to specify if it's a patch, minor, major or custom change.

Independent mode allows you to more specifically update versions for each package and makes sense for a group of components. Combining this mode with something like [semantic-release](https://github.com/semantic-release/semantic-release) would make it less painful. (There is work on this already at [atlassian/lerna-semantic-release](https://github.com/atlassian/lerna-semantic-release)).

> The `version` key in `lerna.json` is ignored in independent mode.

## Troubleshooting

If you encounter any issues while using Lerna please check out our [Troubleshooting](doc/troubleshooting.md)
document where you might find the answer to your problem.

## Frequently asked questions

See [FAQ.md](FAQ.md).

## Commands

### init

```sh
$ lerna init
```

Create a new Lerna repo or upgrade an existing repo to the current version of Lerna.

> Lerna assumes the repo has already been initialized with `git init`.

When run, this command will:

1. Add `lerna` as a [`devDependency`](https://docs.npmjs.com/files/package.json#devdependencies) in `package.json` if it doesn't already exist.
2. Create a `lerna.json` config file to store the `version` number.

Example output on a new git repo:

```sh
$ lerna init
lerna info version v2.0.0
lerna info Updating package.json
lerna info Creating lerna.json
lerna success Initialized Lerna files
```

#### --independent, -i

```sh
$ lerna init --independent
```

This flag tells Lerna to use independent versioning mode.

#### --exact

```sh
$ lerna init --exact
```

By default, `lerna init` will use a caret range when adding or updating
the local version of `lerna`, just like `npm install --save-dev lerna`.

To retain the `lerna` 1.x behavior of "exact" comparison, pass this flag.
It will configure `lerna.json` to enforce exact match for all subsequent executions.

```json
{
  "lerna": "2.0.0",
  "commands": {
    "init": {
      "exact": true
    }
  },
  "version": "0.0.0"
}
```

### bootstrap

```sh
$ lerna bootstrap
```

Bootstrap the packages in the current Lerna repo.
Installs all of their dependencies and links any cross-dependencies.

When run, this command will:

1. `npm install` all external dependencies of each package.
2. Symlink together all Lerna `packages` that are dependencies of each other.
3. `npm run prepublish` in all bootstrapped packages.
4. `npm run prepare` in all bootstrapped packages.

`lerna bootstrap` respects the `--ignore`, `--scope` and `--include-filtered-dependencies` flags (see [Flags](#flags)).

Pass extra arguments to npm client by placing them after `--`:

```sh
$ lerna bootstrap -- --production --no-optional
```

May also be configured in `lerna.json`:

```js
{
  ...
  "npmClient": "yarn",
  "npmClientArgs": ["--production", "--no-optional"]
}
```

#### How `bootstrap` works

Let's use `babel` as an example.

- `babel-generator` and `source-map` (among others) are dependencies of `babel-core`.
-  `babel-core`'s [`package.json`](https://github.com/babel/babel/blob/13c961d29d76ccd38b1fc61333a874072e9a8d6a/packages/babel-core/package.json#L28-L47) lists both these packages as keys in `dependencies`, as shown below.

### add

```sh
$ lerna add <package>[@version] [--dev]
```

Add local or remote `package` as dependency to packages in the current Lerna repo.

When run, this command will:

1. Add `package` to each applicable package. Applicable are packages that are not `package` and are in scope
2. Bootstrap packages with changes to their manifest file (`package.json`)

`lerna add` respects the `--ignore`, `--scope` and `--include-filtered-dependencies` flags (see [Flags](#flags)).

#### Examples

```
lerna add module-1 --scope=module-2 # Install module-1 to module-2
lerna add module-1 --scope=module-2 --dev # Install module-1 to module-2 in devDependencies
lerna add module-1 # Install module-1 in all modules except module-1
lerna add babel-core # Install babel-core in all modules
```

```js
// babel-core package.json
{
  "name": "babel-core",
  ...
  "dependencies": {
    ...
    "babel-generator": "^6.9.0",
    ...
    "source-map": "^0.5.0"
  }
}
```

- Lerna checks if each dependency is also part of the Lerna repo.
  - In this example, `babel-generator` can be an internal dependency, while `source-map` is always an external dependency.
  - The version of `babel-generator` in the `package.json` of `babel-core` is satisfied by `packages/babel-generator`, passing for an internal dependency.
  - `source-map` is `npm install`ed (or `yarn`ed) like normal.
- `packages/babel-core/node_modules/babel-generator` symlinks to `packages/babel-generator`
- This allows nested directory imports

**Notes:**
- When a dependency version in a package is not satisfied by a package of the same name in the repo, it will be `npm install`ed (or `yarn`ed) like normal.
- Dist-tags, like `latest`, do not satisfy [semver](https://semver.npmjs.com/) ranges.
- Circular dependencies result in circular symlinks which *may* impact your editor/IDE.

[Webstorm](https://www.jetbrains.com/webstorm/) locks up when circular symlinks are present. To prevent this, add `node_modules` to the list of ignored files and folders in `Preferences | Editor | File Types | Ignored files and folders`.

### publish

```sh
$ lerna publish
```

Publish packages in the current Lerna project. When run, this command does the following:

Creates a new release of the packages that have been updated.
Prompts for a new version.
Creates a new git commit/tag in the process of publishing to npm.

More specifically, this command will:

1. Run the equivalent of `lerna updated` to determine which packages need to be published.
2. If necessary, increment the `version` key in `lerna.json`.
3. Update the `package.json` of all updated packages to their new versions.
4. Update all dependencies of the updated packages with the new versions, specified with a [caret (^)](https://docs.npmjs.com/files/package.json#dependencies).
5. Create a new git commit and tag for the new version.
6. Publish updated packages to npm.

> Lerna won't publish packages which are marked as private (`"private": true` in the `package.json`).

**Note:** to publish scoped packages, you need to add the following to each `package.json`:

```js
"publishConfig": {
  "access": "public"
}
```

#### --exact

```sh
$ lerna publish --exact
```

When run with this flag, `publish` will specify updated dependencies in updated packages exactly (with no punctuation), instead of as semver compatible (with a `^`).

For more information, see the package.json [dependencies](https://docs.npmjs.com/files/package.json#dependencies) documentation.

#### --npm-tag [tagname]

```sh
$ lerna publish --npm-tag=next
```

When run with this flag, `publish` will publish to npm with the given npm [dist-tag](https://docs.npmjs.com/cli/dist-tag) (defaults to `latest`).

This option can be used to publish a [`prerelease`](http://carrot.is/coding/npm_prerelease) or `beta` version.

> Note: the `latest` tag is the one that is used when a user runs `npm install my-package`.
> To install a different tag, a user can run `npm install my-package@prerelease`.

#### --canary, -c

```sh
$ lerna publish --canary
$ lerna publish --canary=beta
```

When run with this flag, `publish` publishes packages in a more granular way (per commit). Before publishing to npm, it creates the new `version` tag by taking the current `version`, bumping it to the next *minor* version, adding the provided meta suffix (defaults to `alpha`) and appending the current git sha (ex: `1.0.0` becomes `1.1.0-alpha.81e3b443`).

> The intended use case for this flag is a per commit level release or nightly release.

#### --conventional-commits

```sh
$ lerna publish --conventional-commits
```

When run with this flag, `publish` will use the [Conventional Commits Specification](https://conventionalcommits.org/) to [determine the version bump](https://github.com/conventional-changelog/conventional-changelog/tree/master/packages/conventional-recommended-bump) and [generate CHANGELOG](https://github.com/conventional-changelog/conventional-changelog/tree/master/packages/conventional-changelog-cli)

#### --changelog-preset

```sh
$ lerna publish --conventional-commits --changelog-preset=angular-bitbucket
```

By default, the changelog preset is set to `angular`. In some cases you might want to change either use a another preset or a custom one.

Presets are names of built-in or installable configuration for conventional changelog.

#### --git-remote [remote]

```sh
$ lerna publish --git-remote upstream
```

When run with this flag, `publish` will push the git changes to the specified remote instead of `origin`.

#### --skip-git

```sh
$ lerna publish --skip-git
```

When run with this flag, `publish` will publish to npm without running any of the git commands.

> Only publish to npm; skip committing, tagging, and pushing git changes (this only affects publish).

#### --skip-npm

```sh
$ lerna publish --skip-npm
```

When run with this flag, `publish` will update all `package.json` package
versions and dependency versions, but it will not actually publish the
packages to npm.

> This was useful as a workaround for an [npm
issue](https://github.com/npm/registry/issues/42) which has since been fixed.  When publishing with
README changes, use `--skip-npm` and do the final `npm publish` by hand for
each package.

This flag can be combined with `--skip-git` to _just_ update versions and
dependencies, without committing, tagging, pushing or publishing.

> Only update versions and dependencies; don't actually publish (this only affects publish).

#### --force-publish [packages]

```sh
$ lerna publish --force-publish=package-2,package-4
# force publish all packages
$ lerna publish --force-publish=*
```

When run with this flag, `publish` will force publish the specified packages (comma-separated) or all packages using `*`.

> This will skip the `lerna updated` check for changed packages and forces a package that didn't have a `git diff` change to be updated.

#### --yes

```sh
$ lerna publish --canary --yes
# skips `Are you sure you want to publish the above changes?`
```

When run with this flag, `publish` will skip all confirmation prompts.
Useful in [Continuous integration (CI)](https://en.wikipedia.org/wiki/Continuous_integration) to automatically answer the publish confirmation prompt.

#### --cd-version

```sh
$ lerna publish --cd-version (major | minor | patch | premajor | preminor | prepatch | prerelease)
# uses the next semantic version(s) value and this skips `Select a new version for...` prompt
```

When run with this flag, `publish` will skip the version selection prompt (in independent mode) and use the next specified semantic version.
You must still use the `--yes` flag to avoid all prompts. This is useful when build systems need to publish without command prompts. Works in both normal and independent modes.

If you have any packages with a prerelease version number (e.g. `2.0.0-beta.3`) and you run `lerna publish` with `--cd-version` and a non-prerelease version increment (major / minor / patch), it will publish those packages in addition to the packages that have changed since the last release.

#### --preid

```sh
$ lerna publish --cd-version=prerelease
# uses the next semantic prerelease version, e.g.
# 1.0.0 => 1.0.0-0

$ lerna publish --cd-version=prepatch --preid=next
# uses the next semantic prerelease version with a specific prerelease identifier, e.g.
# 1.0.0 => 1.0.1-next.0
```

When run with this flag, `lerna publish --cd-version` will
increment `premajor`, `preminor`, `prepatch`, or `prerelease`
versions using the specified [prerelease identifier](http://semver.org/#spec-item-9).

#### --repo-version

```sh
$ lerna publish --repo-version 1.0.1
# applies version and skips `Select a new version for...` prompt
```

When run with this flag, `publish` will skip the version selection prompt and use the specified version.
Useful for bypassing the user input prompt if you already know which version to publish.

#### --message, -m [msg]

```sh
$ lerna publish -m "chore(release): publish %s"
# commit message = "chore(release): publish v1.0.0"

$ lerna publish -m "chore(release): publish" --independent
# commit message = "chore(release): publish
#
# - package-1@3.0.1
# - package-2@1.5.4"
```

When run with this flag, `publish` will use the provided message when committing the version updates
for publication. Useful for integrating lerna into projects that expect commit messages to adhere
to certain guidelines, such as projects which use [commitizen](https://github.com/commitizen/cz-cli) and/or [semantic-release](https://github.com/semantic-release/semantic-release).

If the message contains `%s`, it will be replaced with the new global version version number prefixed with a "v".
Note that this only applies when using the default "fixed" versioning mode, as there is no "global" version when using `--independent`.

This can be configured in lerna.json, as well:
```json
{
  "commands": {
    "publish": {
      "message": "chore(release): publish %s"
    }
  }
}
```

#### --allow-branch [glob]

Lerna allows you to specify a glob or an array of globs in your `lerna.json` that your current branch needs to match to be publishable.
You can use this flag to override this setting.
If your `lerna.json` contains something like this:

```json
{
  "commands": {
    "publish": {
      "allowBranch": "master"
    }
  }
}
```

```json
{
  "command": {
    "publish": {
      "allowBranch": [
        "master",
        "feature/*"
      ]
    }
  }
}
```

and you are not on the branch `master` lerna will prevent you from publishing. To force a publish despite this config, pass the `--allow-branch` flag:

```sh
$ lerna publish --allow-branch my-new-feature
```

### updated

```sh
$ lerna updated
```

Check which `packages` have changed since the last release (the last git tag).

Lerna determines the last git tag created and runs `git diff --name-only v6.8.1` to get all files changed since that tag. It then returns an array of packages that have an updated file.

**Note that configuration for the `publish` command _also_ affects the
`updated` command.  For example `config.publish.ignore`**

#### --json

```sh
$ lerna updated --json
```

When run with this flag, `updated` will return an array of objects in the following format:

```json
[
  {
    "name": "package",
    "version": "1.0.0",
    "private": false
  }
]
```

### clean

```sh
$ lerna clean
```

Remove the `node_modules` directory from all packages.

`lerna clean` respects the `--ignore`, `--scope`, and `--yes` flags (see [Flags](#flags)).

### diff

```sh
$ lerna diff [package?]

$ lerna diff
# diff a specific package
$ lerna diff package-name
```

Diff all packages or a single package since the last release.

> Similar to `lerna updated`. This command runs `git diff`.

### ls

```sh
$ lerna ls
```

List all of the public packages in the current Lerna repo.

`lerna ls` respects the `--ignore` and `--scope` flags (see [Flags](#flags)).

#### --json

```sh
$ lerna ls --json
```

When run with this flag, `ls` will return an array of objects in the following format:

```json
[
  {
    "name": "package",
    "version": "1.0.0",
    "private": false
  }
]
```

### run

```sh
$ lerna run <script> -- [..args] # runs npm run my-script in all packages that have it
$ lerna run test
$ lerna run build

# watch all packages and transpile on change, streaming prefixed output
$ lerna run --parallel watch
```

Run an [npm script](https://docs.npmjs.com/misc/scripts) in each package that contains that script. A double-dash (`--`) is necessary to pass dashed arguments to the script execution.

`lerna run` respects the `--concurrency`, `--scope`, `--ignore`, `--stream`, and `--parallel` flags (see [Flags](#flags)).

```sh
$ lerna run --scope my-component test
```

> Note: It is advised to constrain the scope of this command (and `lerna exec`,
> below) when using the `--parallel` flag, as spawning dozens of subprocesses
> may be harmful to your shell's equanimity (or maximum file descriptor limit,
> for example). YMMV

### exec

```sh
$ lerna exec -- <command> [..args] # runs the command in all packages
$ lerna exec -- rm -rf ./node_modules
$ lerna exec -- protractor conf.js
```

Run an arbitrary command in each package.
A double-dash (`--`) is necessary to pass dashed flags to the spawned command, but is not necessary when all the arguments are positional.

`lerna exec` respects the `--concurrency`, `--scope`, `--ignore`, `--stream` and `--parallel` flags (see [Flags](#flags)).

```sh
$ lerna exec --scope my-component -- ls -la
```

To spawn long-running processes, pass the `--parallel` flag:
```sh
# transpile all modules as they change in every package
$ lerna exec --parallel -- babel src -d lib -w
```

You may also get the name of the current package through the environment variable `LERNA_PACKAGE_NAME`:

```sh
$ lerna exec -- npm view \$LERNA_PACKAGE_NAME
```

You may also run a script located in the root dir, in a complicated dir structure through the environment variable `LERNA_ROOT_PATH`:

```sh
$ lerna exec -- node \$LERNA_ROOT_PATH/scripts/some-script.js
```

> Hint: The commands are spawned in parallel, using the concurrency given (except with `--parallel`).
> The output is piped through, so not deterministic.
> If you want to run the command in one package after another, use it like this:

```sh
$ lerna exec --concurrency 1 -- ls -la
```

#### --bail

```sh
$ lerna exec --bail=<boolean> <command>
```

This flag signifies whether or not the `exec` command should halt execution upon encountering an error thrown by one of the spawned subprocesses. Its default value is `true`.

### import

```sh
$ lerna import <path-to-external-repository>
```

Import the package at `<path-to-external-repository>`, with commit history,
into `packages/<directory-name>`.  Original commit authors, dates and messages
are preserved.  Commits are applied to the current branch.

This is useful for gathering pre-existing standalone packages into a Lerna
repo.  Each commit is modified to make changes relative to the package
directory.  So, for example, the commit that added `package.json` will
instead add `packages/<directory-name>/package.json`.

### link

```sh
$ lerna link
```

Symlink together all Lerna `packages` that are dependencies of each other in the current Lerna repo.

#### --force-local

```sh
$ lerna link --force-local
```

When passed, this flag causes the `link` command to always symlink local dependencies regardless of matching version range.

## Misc

Lerna will log to a `lerna-debug.log` file (same as `npm-debug.log`) when it encounters an error running a command.

Lerna also has support for [scoped packages](https://docs.npmjs.com/misc/scope).

Running `lerna` without arguments will show all commands/options.

### lerna.json

```js
{
  "lerna": "2.0.0",
  "version": "1.1.3",
  "commands": {
    "publish": {
      "ignore": [
        "ignored-file",
        "*.md"
      ]
    },
    "bootstrap": {
      "ignore": "component-*"
    }
  },
  "packages": ["packages/*"]
}
```

- `lerna`: the current version of Lerna being used.
- `version`: the current version of the repository.
- `commands.publish.ignore`: an array of globs that won't be included in `lerna updated/publish`. Use this to prevent publishing a new version unnecessarily for changes, such as fixing a `README.md` typo.
- `commands.bootstrap.ignore`: an array of globs that won't be bootstrapped when running the `lerna bootstrap` command.
- `commands.bootstrap.scope`: an array of globs that restricts which packages will be bootstrapped when running the `lerna bootstrap` command.
- `packages`: Array of globs to use as package locations.

### Common `devDependencies`

Most `devDependencies` can be pulled up to the root of a Lerna repo.

This has a few benefits:

- All packages use the same version of a given dependency
- Can keep dependencies at the root up-to-date with an automated tool such as [GreenKeeper](https://greenkeeper.io/)
- Dependency installation time is reduced
- Less storage is needed

Note that `devDependencies` providing "binary" executables that are used by
npm scripts still need to be installed directly in each package where they're
used.

For example the `nsp` dependency is necessary in this case for `lerna run nsp`
(and `npm run nsp` within the package's directory) to work correctly:

```json

{
  "scripts": {
    "nsp": "nsp"
  },
  "devDependencies": {
    "nsp": "^2.3.3"
  }
}
```

### Flags

Options to Lerna can come from configuration (`lerna.json`) or on the command
line.  Additionally options in config can live at the top level or may be
applied to specific commands.

Example:

```json
{
  "lerna": "x.x.x",
  "version": "1.2.0",
  "exampleOption": "foo",
  "commands": {
    "init": {
      "exampleOption": "bar",
    }
  },
}
```

In this case `exampleOption` will be "foo" for all commands except `init`,
where it will be "bar".  In all cases it may be overridden to "baz" on the
command-line with `--example-option=baz`.

#### --concurrency

How many threads to use when Lerna parallelizes the tasks (defaults to `4`)

```sh
$ lerna publish --concurrency 1
```

#### --scope [glob]

Scopes a command to a subset of packages.

```sh
$ lerna exec --scope my-component -- ls -la
```

```sh
$ lerna run --scope toolbar-* test
```

#### --since [ref]

When executing a script or command, scope the operation to packages that have been updated since the specified `ref`. If `ref` is not specified, it defaults to the latest tag.

List the contents of packages that have changed since the latest tag:

```sh
$ lerna exec --since -- ls -la
```

Run the tests for all packages that have changed since `master`:

```
$ lerna run test --since master
```

List all packages that have changed since `some-branch`:

```
$ lerna ls --since some-branch
```

*This can be particularly useful when used in CI, if you can obtain the target branch a PR will be going into, because you can use that as the `ref` to the `--since` option. This works well for PRs going into master as well as feature branches.*

#### --flatten

When importing repositories with merge commits with conflicts, the import command will fail trying to apply all commits. The user can use this flag to ask for import of "flat" history, i.e. with each merge commit as a single change the merge introduced.

```
$ lerna import ~/Product --flatten
```

#### --ignore [glob]

Excludes a subset of packages when running a command.

```sh
$ lerna bootstrap --ignore component-*
```

The `ignore` flag, when used with the `bootstrap` command, can also be set in `lerna.json` under the `commands.bootstrap` key. The command-line flag will take precedence over this option.

**Example**

```javascript
{
  "lerna": "2.0.0",
  "version": "0.0.0",
  "commands": {
    "bootstrap": {
      "ignore": "component-*"
    }
  }
}
```

> Hint: The glob is matched against the package name defined in `package.json`,
> not the directory name the package lives in.

#### --include-filtered-dependencies

Used in combination with any command that accepts `--scope` (`bootstrap`, `clean`, `ls`, `run`, `exec`). Ensures that all dependencies (and dev dependencies) of any scoped packages (either through `--scope` or `--ignore`) are operated on as well.

> Note: This will override the `--scope` and `--ignore` flags.
> > i.e. A package matched by the `--ignore` flag will still be bootstrapped if it is depended on by another package that is being bootstrapped.

This is useful for situations where you want to "set up" a single package that relies on other packages being set up.

```sh
$ lerna bootstrap --scope my-component --include-filtered-dependencies
# my-component and all of its dependencies will be bootstrapped
```

```sh
$ lerna bootstrap --scope "package-*" --ignore "package-util-*" --include-filtered-dependencies
# all package-util's will be ignored unless they are depended upon by a
# package matched by "package-*"
```

#### --loglevel [silent|error|warn|success|info|verbose|silly]

What level of logs to report.  On failure, all logs are written to lerna-debug.log in the current working directory.

Any logs of a higher level than the setting are shown.  The default is "info".

#### --max-buffer [in-bytes]

Set a max buffer length for each underlying process call. Useful for example
when someone wants to import a repo with a larger amount of commits while
running `lerna import`. In that case the built-in buffer length might not
be sufficient.

#### --no-sort

By default, all tasks execute on packages in topologically sorted order as to respect the dependency relationships of the packages in question. Cycles are broken on a best-effort basis in a way not guaranteed to be consistent across Lerna invocations.

Topological sorting can cause concurrency bottlenecks if there are a small number of packages with many dependents or if some packages take a disproportionately long time to execute. The `--no-sort` option disables sorting, instead executing tasks in an arbitrary order with maximum concurrency.

This option can also help if you run multiple "watch" commands. Since `lerna run` will execute commands in topologically sorted order, it can end up waiting for a command before moving on. This will block execution when you run "watch" commands, since they typically never end. An example of a "watch" command is [running `babel` with the `--watch` CLI flag](https://babeljs.io/docs/usage/cli/#babel-compile-files).

#### --hoist [glob]

Install external dependencies matching `glob` at the repo root so they're
available to all packages.  Any binaries from these dependencies will be
linked into dependent package `node_modules/.bin/` directories so they're
available for npm scripts.  If the option is present but no `glob` is given
the default is `**` (hoist everything).  This option only affects the
`bootstrap` command.

```sh
$ lerna bootstrap --hoist
```

For background on `--hoist`, see the [hoist documentation](doc/hoist.md).

Note: If packages depend on different _versions_ of an external dependency,
the most commonly used version will be hoisted, and a warning will be emitted.

#### --nohoist [glob]

Do _not_ install external dependencies matching `glob` at the repo root.  This
can be used to opt out of hoisting for certain dependencies.

```sh
$ lerna bootstrap --hoist --nohoist=babel-*
```

#### --npm-client [client]

Install external dependencies using `[client] install`.  Must be an executable
that knows how to install npm dependencies.

```sh
$ lerna bootstrap --npm-client=yarn
```

May also be configured in `lerna.json`:

```js
{
  ...
  "npmClient": "yarn"
}
```

#### --reject-cycles

Fail immediately if a cycle is found (in `bootstrap`, `exec`, `publish` or `run`).

```sh
$ lerna bootstrap --reject-cycles
```

#### --use-workspaces

Enables integration with [Yarn Workspaces](https://github.com/yarnpkg/rfcs/blob/master/implemented/0000-workspaces-install-phase-1.md) (available since yarn@0.27+).
The values in the array are the commands in which Lerna will delegate operation to Yarn (currently only bootstrapping).
If `--use-workspaces` is true then `packages` will be overridden by the value from `package.json/workspaces.`
May also be configured in `lerna.json`:

```js
{
  ...
  "npmClient": "yarn",
  "useWorkspaces": true
}
```

The root-level package.json must also include a `workspaces` array:
```json
{
  "private": true,
  "devDependencies": {
    "lerna": "^2.2.0"
  },
  "workspaces": [
    "packages/*"
  ]
}
```
This list is broadly similar to lerna's `packages` config (a list of globs matching directories with a package.json),
except it does not support recursive globs (`"**"`, a.k.a. "globstars").

#### --use-git-version

Allow target versions of dependent packages to be written as [git hosted urls](https://github.com/npm/hosted-git-info) instead of a plain version number.
If enabled, Lerna will attempt to extract and save the interpackage dependency versions from `package.json` files using git url-aware parser.

Eg. assuming monorepo with 2 packages where `my-package-1` depends on `my-package-2`, `package.json` of `my-package-1` could be:
```
// packages/my-package-1/package.json
{
  name: "my-package-1",
  version: "1.0.0",
  bin: "bin.js",
  dependencies: {
    "my-package-2": "github:example-user/my-package-2#v1.0.0"
  },
  devDependencies: {
    "my-dev-dependency": "^1.0.0"
  },
  peerDependencies: {
    "my-peer-dependency": "^1.0.0"
  }
}
```
For the case above Lerna will read the version of `my-package-2` dependency as `1.0.0`.

This allows packages to be distributed via git repos if eg. packages are private and [private npm repo is not an option](https://www.dotconferences.com/2016/05/fabien-potencier-monolithic-repositories-vs-many-repositories).

Please note that using `--use-git-version`
- is limited to urls with [`committish`](https://docs.npmjs.com/files/package.json#git-urls-as-dependencies) part present (ie. `github:example-user/my-package-2` is invalid)
- requires `publish` command to be used with `--exact`

May also be configured in `lerna.json`:
```js
{
  ...
  "useGitVersion": true
}
```

#### --git-version-prefix

Defines version prefix string (defaults to 'v') ignored when extracting version number from a commitish part of git url.
Everything after the prefix will be considered a version.

Eg. given `github:example-user/my-package-2#v1.0.0` and `gitVersionPrefix: 'v'` version will be read as `1.0.0`.

Only used if `--use-git-version` is set to `true`.

May also be configured in `lerna.json`:
```js
{
  ...
  "gitVersionPrefix": "v"
}
```

#### --stream

Stream output from child processes immediately, prefixed with the originating
package name. This allows output from different packages to be interleaved.

```sh
$ lerna run watch --stream
```

#### --parallel

Similar to `--stream`, but completely disregards concurrency and topological sorting, running a given command or script immediately in all matching packages with prefixed streaming output. This is the preferred flag for long-running processes such as `babel src -d lib -w` run over many packages.

```sh
$ lerna exec --parallel -- babel src -d lib -w
```

#### --registry [registry]

When run with this flag, forwarded npm commands will use the specified registry for your package(s).

This is useful if you do not want to explicitly set up your registry
configuration in all of your package.json files individually when e.g. using
private registries.

#### --temp-tag

When passed, this flag will alter the default publish process by first publishing
all changed packages to a temporary dist-tag (`lerna-temp`) and then moving the
new version(s) to the default [dist-tag](https://docs.npmjs.com/cli/dist-tag) (`latest`).

This is not generally necessary, as Lerna will publish packages in topological
order (all dependencies before dependents) by default.

### README Badge
Using Lerna? Add a README badge to show it off: [![lerna](https://img.shields.io/badge/maintained%20with-lerna-cc00ff.svg)](https://lernajs.io/)
```
[![lerna](https://img.shields.io/badge/maintained%20with-lerna-cc00ff.svg)](https://lernajs.io/)
```

### Wizard

If you prefer some guidance for cli (in case you're about to start using lerna or introducing it to a new team), you might like [lerna-wizard](https://github.com/szarouski/lerna-wizard). It will lead you through a series of well-defined steps:

![lerna-wizard demo image](https://raw.githubusercontent.com/szarouski/lerna-wizard/2e269fb5a3af7100397a1f874cea3fa78089486e/demo.png)


// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`stderr: global --yes 1`] = `
"lerna info version __TEST_VERSION__
lerna info clean removing __TEST_ROOTDIR__/packages/package-1/node_modules
lerna info clean removing __TEST_ROOTDIR__/packages/package-2/node_modules
lerna info clean removing __TEST_ROOTDIR__/packages/package-3/node_modules
lerna success clean finished"
`;

exports[`stderr: local npm 1`] = `
"lerna info version __TEST_VERSION__
lerna info clean removing __TEST_ROOTDIR__/packages/package-1/node_modules
lerna info clean removing __TEST_ROOTDIR__/packages/package-2/node_modules
lerna info clean removing __TEST_ROOTDIR__/packages/package-3/node_modules
lerna success clean finished"
`;

exports[`stdout: global --yes 1`] = `""`;

exports[`stdout: local npm 1`] = `""`;


import _ from "lodash";
import async from "async";
import dedent from "dedent";
import getPort from "get-port";
import path from "path";
import semver from "semver";

import Command from "../Command";
import FileSystemUtilities from "../FileSystemUtilities";
import NpmUtilities from "../NpmUtilities";
import PackageUtilities from "../PackageUtilities";
import ValidationError from "../utils/ValidationError";

export function handler(argv) {
  // eslint-disable-next-line no-use-before-define
  const cmd = new BootstrapCommand([...argv.args], argv, argv._cwd);
  return cmd.run().then(argv._onResolved, argv._onRejected);
}

export const command = "bootstrap [args..]";

export const describe = "Link local packages together and install remaining package dependencies";

export const builder = {
  hoist: {
    group: "Command Options:",
    describe: "Install external dependencies matching [glob] to the repo root",
    defaultDescription: "'**'",
    coerce: arg =>
      // `--hoist` is equivalent to `--hoist=**`.
      arg === true ? "**" : arg,
  },
  nohoist: {
    group: "Command Options:",
    describe: "Don't hoist external dependencies matching [glob] to the repo root",
    type: "string",
  },
  "npm-client": {
    group: "Command Options:",
    describe: "Executable used to install dependencies (npm, yarn, pnpm, ...)",
    type: "string",
    requiresArg: true,
  },
};

export default class BootstrapCommand extends Command {
  get requiresGit() {
    return false;
  }

  initialize(callback) {
    const { registry, rejectCycles, npmClient, npmClientArgs, mutex, hoist } = this.options;

    if (npmClient === "yarn" && typeof hoist === "string") {
      return callback(
        new ValidationError(
          "EWORKSPACES",
          dedent`
            --hoist is not supported with --npm-client=yarn, use yarn workspaces instead
            A guide is available at https://yarnpkg.com/blog/2017/08/02/introducing-workspaces/
          `,
        ),
      );
    }

    if (
      npmClient === "yarn" &&
      this.repository.packageJson.workspaces &&
      this.options.useWorkspaces !== true
    ) {
      return callback(
        new ValidationError(
          "EWORKSPACES",
          dedent`
            Yarn workspaces are configured in package.json, but not enabled in lerna.json!
            Please choose one: useWorkspaces = true in lerna.json, or remove package.json workspaces config
          `,
        ),
      );
    }

    this.npmConfig = {
      registry,
      npmClient,
      npmClientArgs,
      mutex,
    };

    // lerna bootstrap ... -- <input>
    if (this.input.length) {
      this.npmConfig.npmClientArgs = [...(npmClientArgs || []), ...this.input];
    }

    try {
      this.batchedPackages = this.toposort
        ? PackageUtilities.topologicallyBatchPackages(this.filteredPackages, {
            rejectCycles,
          })
        : [this.filteredPackages];
    } catch (e) {
      return callback(e);
    }

    if (npmClient === "yarn" && !mutex) {
      return getPort({ port: 42424, host: "0.0.0.0" })
        .then(port => {
          this.npmConfig.mutex = `network:${port}`;
          this.logger.silly("npmConfig", this.npmConfig);
          callback(null, true);
        })
        .catch(callback);
    }

    PackageUtilities.validatePackageNames(this.filteredPackages);

    this.logger.silly("npmConfig", this.npmConfig);
    callback(null, true);
  }

  execute(callback) {
    this.bootstrapPackages(err => {
      if (err) {
        callback(err);
      } else {
        this.logger.success("", `Bootstrapped ${this.filteredPackages.length} packages`);
        callback(null, true);
      }
    });
  }

  /**
   * Bootstrap packages
   * @param {Function} callback
   */
  bootstrapPackages(callback) {
    this.logger.info("", `Bootstrapping ${this.filteredPackages.length} packages`);

    if (this.options.useWorkspaces) {
      this.installRootPackageOnly(callback);
    } else {
      async.series(
        [
          // preinstall bootstrapped packages
          cb => this.preinstallPackages(cb),
          // install external dependencies
          cb => this.installExternalDependencies(cb),
          // symlink packages and their binaries
          cb => this.symlinkPackages(cb),
          // postinstall bootstrapped packages
          cb => this.postinstallPackages(cb),
          // prepublish bootstrapped packages
          cb => this.prepublishPackages(cb),
          // prepare bootstrapped packages
          cb => this.preparePackages(cb),
        ],
        callback,
      );
    }
  }

  installRootPackageOnly(callback) {
    const tracker = this.logger.newItem("install dependencies");

    NpmUtilities.installInDirOriginalPackageJson(this.repository.rootPath, this.npmConfig, err => {
      if (err) {
        return callback(err);
      }
      tracker.info("hoist", "Finished installing in root");
      tracker.completeWork(1);
      callback(err);
    });
  }

  runScriptInPackages(scriptName, callback) {
    if (!this.filteredPackages.length) {
      return callback(null, true);
    }

    const tracker = this.logger.newItem(scriptName);
    tracker.addWork(this.filteredPackages.length);

    PackageUtilities.runParallelBatches(
      this.batchedPackages,
      pkg => done => {
        pkg.runScript(scriptName, err => {
          tracker.silly(pkg.name);
          tracker.completeWork(1);
          if (err) {
            err.pkg = pkg;
          }
          done(err);
        });
      },
      this.concurrency,
      err => {
        tracker.finish();
        callback(err);
      },
    );
  }

  /**
   * Run the "preinstall" NPM script in all bootstrapped packages
   * @param callback
   */
  preinstallPackages(callback) {
    this.logger.info("lifecycle", "preinstall");
    this.runScriptInPackages("preinstall", callback);
  }

  /**
   * Run the "postinstall" NPM script in all bootstrapped packages
   * @param callback
   */
  postinstallPackages(callback) {
    this.logger.info("lifecycle", "postinstall");
    this.runScriptInPackages("postinstall", callback);
  }

  /**
   * Run the "prepublish" NPM script in all bootstrapped packages
   * @param callback
   */
  prepublishPackages(callback) {
    this.logger.info("lifecycle", "prepublish");
    this.runScriptInPackages("prepublish", callback);
  }

  /**
   * Run the "prepublish" NPM script in all bootstrapped packages
   * @param callback
   */
  preparePackages(callback) {
    this.logger.info("lifecycle", "prepare");
    this.runScriptInPackages("prepare", callback);
  }

  hoistedDirectory(dependency) {
    return path.join(this.repository.rootPath, "node_modules", dependency);
  }

  hoistedPackageJson(dependency) {
    try {
      // eslint-disable-next-line import/no-dynamic-require, global-require
      return require(path.join(this.hoistedDirectory(dependency), "package.json"));
    } catch (e) {
      // Pass.
    }
  }

  /**
   * Determine if a dependency installed at the root satifies the requirements of the passed packages
   * This helps to optimize the bootstrap process and skip dependencies that are already installed
   * @param {String} dependency
   * @param {Array.<String>} packages
   */
  dependencySatisfiesPackages(dependency, packages) {
    const { version } = this.hoistedPackageJson(dependency) || {};
    return packages.every(pkg => semver.satisfies(version, pkg.allDependencies[dependency]));
  }

  /**
   * Return a object of root and leaf dependencies to install
   * @returns {Object}
   */
  getDependenciesToInstall(tracker) {
    // find package by name
    const findPackage = (name, version) =>
      _.find(this.packages, pkg => pkg.name === name && (!version || semver.satisfies(pkg.version, version)));

    const hasPackage = (name, version) => Boolean(findPackage(name, version));

    // Configuration for what packages to hoist may be in lerna.json or it may
    // come in as command line options.
    const { hoist, nohoist } = this.options;

    if (hoist) {
      tracker.verbose("hoist", "enabled for %j", hoist);
    }

    // This will contain entries for each hoistable dependency.
    const root = [];

    // This will map packages to lists of unhoistable dependencies
    const leaves = {};

    /**
     * Map of dependencies to install
     * {
     *   <name>: {
     *     versions: {
     *       <version>: <# of dependents>
     *     },
     *     dependents: {
     *       <version>: [<dependent1>, <dependent2>, ...]
     *     }
     *   }
     * }
     *
     * Example:
     *
     * {
     *   react: {
     *     versions: {
     *       "15.x": 3,
     *       "^0.14.0": 1
     *     },
     *     dependents: {
     *       "15.x": ["my-component1", "my-component2", "my-component3"],
     *       "^0.14.0": ["my-component4"],
     *     }
     *   }
     * }
     */
    const depsToInstall = {};

    Object.keys(this.repository.package.allDependencies).forEach(name => {
      const version = this.repository.package.allDependencies[name];
      depsToInstall[name] = {
        versions: { [version]: 0 },
        dependents: { [version]: [] },
      };
    });

    // get the map of external dependencies to install
    this.filteredPackages.forEach(pkg => {
      // for all package dependencies
      Object.keys(pkg.allDependencies)

        // map to package or normalized external dependency
        .map(
          name =>
            findPackage(name, pkg.allDependencies[name]) || { name, version: pkg.allDependencies[name] },
        )

        // match external and version mismatched local packages
        .filter(dep => !hasPackage(dep.name, dep.version) || !pkg.hasMatchingDependency(dep, true))

        .forEach(({ name, version }) => {
          // Get the object for this package, auto-vivifying.
          const dep =
            depsToInstall[name] ||
            (depsToInstall[name] = {
              versions: {},
              dependents: {},
            });

          // Add this version if it's the first time we've seen it.
          if (!dep.versions[version]) {
            dep.versions[version] = 0;
            dep.dependents[version] = [];
          }

          // Record the dependency on this version.
          dep.versions[version] += 1;
          dep.dependents[version].push(pkg.name);
        });
    });

    // determine where each dependency will be installed
    Object.keys(depsToInstall).forEach(name => {
      const { versions, dependents } = depsToInstall[name];

      let rootVersion;

      if (hoist && PackageUtilities.isHoistedPackage(name, hoist, nohoist)) {
        // Get the most common version.
        const commonVersion = Object.keys(versions).reduce((a, b) => (versions[a] > versions[b] ? a : b));

        // Get the version required by the repo root (if any).
        // If the root doesn't have a dependency on this package then we'll
        // install the most common dependency there.
        rootVersion = this.repository.package.allDependencies[name] || commonVersion;

        if (rootVersion !== commonVersion) {
          tracker.warn(
            "EHOIST_ROOT_VERSION",
            `The repository root depends on ${name}@${rootVersion}, ` +
              `which differs from the more common ${name}@${commonVersion}.`,
          );
        }

        // Install the best version we can in the repo root.
        // Even if it's already installed there we still need to make sure any
        // binaries are linked to the packages that depend on them.
        root.push({
          name,
          dependents: (dependents[rootVersion] || []).map(dep => this.packageGraph.get(dep).package),
          dependency: `${name}@${rootVersion}`,
          isSatisfied: this.repository.hasDependencyInstalled(name, rootVersion),
        });
      }

      // Add less common versions to package installs.
      Object.keys(versions).forEach(version => {
        // Only install deps that can't be hoisted in the leaves.
        if (version === rootVersion) {
          return;
        }

        dependents[version].forEach(pkg => {
          if (rootVersion) {
            tracker.warn(
              "EHOIST_PKG_VERSION",
              `"${pkg}" package depends on ${name}@${version}, ` +
                `which differs from the hoisted ${name}@${rootVersion}.`,
            );
          }

          // only install dependency if it's not already installed
          (leaves[pkg] || (leaves[pkg] = [])).push({
            dependency: `${name}@${version}`,
            isSatisfied: findPackage(pkg).hasDependencyInstalled(name),
          });
        });
      });
    });

    tracker.silly("root dependencies", JSON.stringify(root, null, 2));
    tracker.silly("leaf dependencies", JSON.stringify(leaves, null, 2));

    return { root, leaves };
  }

  /**
   * Install external dependencies for all packages
   * @param {Function} callback
   */
  installExternalDependencies(callback) {
    const tracker = this.logger.newItem("install dependencies");

    const { leaves, root } = this.getDependenciesToInstall(tracker);
    const actions = [];

    // Start root install first, if any, since it's likely to take the longest.
    if (Object.keys(root).length) {
      // If we have anything to install in the root then we'll install
      // _everything_ that needs to go there.  This is important for
      // consistent behavior across npm clients.
      const depsToInstallInRoot = root.some(({ isSatisfied }) => !isSatisfied)
        ? root.map(({ dependency }) => dependency)
        : [];

      actions.push(actionDone => {
        if (depsToInstallInRoot.length) {
          tracker.info("hoist", "Installing hoisted dependencies into root");
        }

        NpmUtilities.installInDir(
          this.repository.rootPath,
          depsToInstallInRoot,
          this.npmConfig,
          installError => {
            if (installError) {
              return actionDone(installError);
            }

            // Link binaries into dependent packages so npm scripts will have
            // access to them.
            async.series(
              root.map(({ name, dependents }) => itemDone => {
                const { bin } = this.hoistedPackageJson(name) || {};
                if (bin) {
                  async.series(
                    dependents.map(pkg => linkDone => {
                      const src = this.hoistedDirectory(name);
                      PackageUtilities.createBinaryLink(src, pkg, linkDone);
                    }),
                    itemDone,
                  );
                } else {
                  itemDone();
                }
              }),
              err => {
                tracker.info("hoist", "Finished installing in root");
                tracker.completeWork(1);
                actionDone(err);
              },
            );
          },
        );
      });

      // Remove any hoisted dependencies that may have previously been
      // installed in package directories.
      actions.push(actionDone => {
        // Compute the list of candidate directories synchronously
        const candidates = root.filter(pkg => pkg.dependents.length).reduce((list, { name, dependents }) => {
          const dirs = dependents
            .filter(pkg => pkg.nodeModulesLocation !== this.repository.nodeModulesLocation)
            .map(pkg => path.join(pkg.nodeModulesLocation, name));

          return list.concat(dirs);
        }, []);

        if (!candidates.length) {
          tracker.verbose("hoist", "nothing to prune");
          tracker.completeWork(1); // the action "work"
          return actionDone();
        }

        tracker.info("hoist", "Pruning hoisted dependencies");
        tracker.silly("prune", candidates);
        tracker.addWork(candidates.length);

        async.series(
          candidates.map(dirPath => done => {
            FileSystemUtilities.rimraf(dirPath, err => {
              tracker.verbose("prune", dirPath);
              tracker.completeWork(1);
              done(err);
            });
          }),
          err => {
            tracker.info("hoist", "Finished pruning hoisted dependencies");
            tracker.completeWork(1); // the action "work"
            actionDone(err);
          },
        );
      });
    }

    const leafNpmConfig = Object.assign({}, this.npmConfig, {
      // Use `npm install --global-style` for leaves when hoisting is enabled
      npmGlobalStyle: !!this.options.hoist,
    });

    // Install anything that needs to go into the leaves.
    Object.keys(leaves)
      .map(pkgName => ({ pkg: this.packageGraph.get(pkgName).package, deps: leaves[pkgName] }))
      .forEach(({ pkg, deps }) => {
        // If we have any unsatisfied deps then we need to install everything.
        // This is important for consistent behavior across npm clients.
        if (deps.some(({ isSatisfied }) => !isSatisfied)) {
          actions.push(cb => {
            NpmUtilities.installInDir(
              pkg.location,
              deps.map(({ dependency }) => dependency),
              leafNpmConfig,
              err => {
                tracker.verbose("installed leaf", pkg.name);
                tracker.completeWork(1);
                cb(err);
              },
            );
          });
        }
      });

    if (actions.length) {
      tracker.info("", "Installing external dependencies");
      tracker.verbose("actions", "%d actions, concurrency %d", actions.length, this.concurrency);
      tracker.addWork(actions.length);
    }

    async.parallelLimit(actions, this.concurrency, err => {
      tracker.finish();
      callback(err);
    });
  }

  /**
   * Symlink all packages to the packages/node_modules directory
   * Symlink package binaries to dependent packages' node_modules/.bin directory
   * @param {Function} callback
   */
  symlinkPackages(callback) {
    const forceLocal = false;
    const { filteredPackages, packageGraph, logger } = this;
    PackageUtilities.symlinkPackages(filteredPackages, packageGraph, logger, forceLocal, callback);
  }
}


const message = (this.options.message && this.options.message.replace(/%s/g, tag)) || tag;

keep keep keep keep keep replace keep keep keep keep keep

? this.options.message.replace(/%s/g, tag).replace(/%v/g, version)


# Contributor Code of Conduct

As contributors and maintainers of this project, we pledge to respect all
people who contribute through reporting issues, posting feature requests,
updating documentation, submitting pull requests or patches, and other
activities.

We are committed to making participation in this project a harassment-free
experience for everyone, regardless of level of experience, gender, gender
identity and expression, sexual orientation, disability, personal appearance,
body size, race, age, or religion.

Examples of unacceptable behavior by participants include the use of sexual
language or imagery, derogatory comments or personal attacks, trolling, public
or private harassment, insults, or other unprofessional conduct.

Project maintainers have the right and responsibility to remove, edit, or
reject comments, commits, code, wiki edits, issues, and other contributions
that are not aligned to this Code of Conduct. Project maintainers who do not
follow the Code of Conduct may be removed from the project team.

Instances of abusive, harassing, or otherwise unacceptable behavior may be
reported by opening an issue or contacting one or more of the project
maintainers.

This Code of Conduct is adapted from the
[Contributor Covenant](http://contributor-covenant.org), version 1.0.0,
available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)