Skip to content

Commit

Permalink
feat(add): Add --peer option to save target in peerDependencies (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
icrosil authored and evocateur committed Nov 20, 2019
1 parent e2b23f0 commit e12bf6a
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 3 deletions.
9 changes: 8 additions & 1 deletion commands/add/README.md
Expand Up @@ -7,7 +7,7 @@ Install [lerna](https://www.npmjs.com/package/lerna) for access to the `lerna` C
## Usage

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

Add local or remote `package` as dependency to packages in the current Lerna repo. Note that only a single package can be added at a time compared to `yarn add` or `npm install`.
Expand Down Expand Up @@ -35,6 +35,10 @@ $ lerna add --exact

Add the new package with an exact version (e.g., `1.0.1`) rather than the default `^` semver range (e.g., `^1.0.1`).

### `--peer`

Add the new package to `peerDependencies` instead of `dependencies`.

### `--registry <url>`

Use a custom registry to install the targeted package.
Expand All @@ -55,6 +59,9 @@ lerna add module-1 --scope=module-2
# Install module-1 to module-2 in devDependencies
lerna add module-1 --scope=module-2 --dev

# Install module-1 to module-2 in peerDependencies
lerna add module-1 --scope=module-2 --peer

# Install module-1 in all modules except module-1
lerna add module-1

Expand Down
22 changes: 22 additions & 0 deletions commands/add/__tests__/add-command.test.js
Expand Up @@ -154,6 +154,28 @@ describe("AddCommand", () => {
expect(pkg2).toDevDependOn("@test/package-1");
});

describe("peerDependencies", () => {
it("should add target package to peerDependencies", async () => {
const testDir = await initFixture("basic");

await lernaAdd(testDir)("@test/package-1", "--peer");
const [, pkg2, pkg3, pkg4] = await getPackages(testDir);

expect(pkg2).toPeerDependOn("@test/package-1");
expect(pkg3).toPeerDependOn("@test/package-1");
expect(pkg4).toPeerDependOn("@test/package-1");
});

it("should add target package to peerDependencies with alias", async () => {
const testDir = await initFixture("basic");

await lernaAdd(testDir)("-P", "@test/package-1");
const [, pkg2] = await getPackages(testDir);

expect(pkg2).toPeerDependOn("@test/package-1");
});
});

it("should not reference packages to themeselves", async () => {
const testDir = await initFixture("basic");

Expand Down
7 changes: 7 additions & 0 deletions commands/add/command.js
Expand Up @@ -32,6 +32,12 @@ exports.builder = yargs => {
alias: "exact",
describe: "Save version exactly",
},
P: {
group: "Command Options:",
type: "boolean",
alias: "peer",
describe: "Save to peerDependencies",
},
registry: {
group: "Command Options:",
describe: "Use the specified registry for all npm client operations.",
Expand All @@ -55,6 +61,7 @@ exports.builder = yargs => {
)
.example("$0 add module-1 --scope=module-2", "Install module-1 to module-2")
.example("$0 add module-1 --scope=module-2 --dev", "Install module-1 to module-2 in devDependencies")
.example("$0 add module-1 --scope=module-2 --peer", "Install module-1 to module-2 in peerDependencies")
.example("$0 add module-1", "Install module-1 in all modules except module-1")
.example("$0 add module-1 --no-bootstrap", "Skip automatic `lerna bootstrap`")
.example("$0 add babel-core", "Install babel-core in all modules");
Expand Down
11 changes: 10 additions & 1 deletion commands/add/index.js
Expand Up @@ -25,8 +25,17 @@ class AddCommand extends Command {
return false;
}

get dependencyType() {
if (this.options.dev) {
return "devDependencies";
}
if (this.options.peer) {
return "peerDependencies";
}
return "dependencies";
}

initialize() {
this.dependencyType = this.options.dev ? "devDependencies" : "dependencies";
this.spec = npa(this.options.pkg);
this.dirs = new Set(this.options.globs.map(fp => path.resolve(this.project.rootPath, fp)));
this.selfSatisfied = this.packageSatisfied();
Expand Down
8 changes: 7 additions & 1 deletion helpers/pkg-matchers/index.js
Expand Up @@ -7,11 +7,17 @@ const Package = require("@lerna/package");

exports.toDependOn = createDependencyMatcher("dependencies");
exports.toDevDependOn = createDependencyMatcher("devDependencies");
exports.toPeerDependOn = createDependencyMatcher("peerDependencies");
exports.toHaveBinaryLinks = toHaveBinaryLinks;
exports.toHaveExecutables = toHaveExecutables;

function createDependencyMatcher(dependencyType) {
const verb = dependencyType === "dependencies" ? "depend" : "dev-depend";
const verbMap = {
dependencies: "depend",
devDependencies: "dev-depend",
peerDependencies: "peer-depend",
};
const verb = verbMap[dependencyType] || "dev-depend";

return (received, name, range, options) => {
const pkg = Package.lazy(received);
Expand Down

0 comments on commit e12bf6a

Please sign in to comment.