Skip to content

Commit

Permalink
feat(bootstrap): Support --force-local option (#1807)
Browse files Browse the repository at this point in the history
Fixes #1763
  • Loading branch information
doug-wade authored and evocateur committed Dec 6, 2018
1 parent 7a5a7c2 commit 25572af
Show file tree
Hide file tree
Showing 14 changed files with 125 additions and 6 deletions.
8 changes: 8 additions & 0 deletions commands/bootstrap/README.md
Expand Up @@ -169,6 +169,14 @@ $ lerna bootstrap --ci

This can be useful for "clean" re-installs, or initial installations after fresh cloning.

### `--force-local`

```sh
$ lerna bootstrap --force-local
```

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

## How It Works

Let's use `babel` as an example.
Expand Down
@@ -0,0 +1,4 @@
{
"ci": false,
"version": "1.0.0"
}
@@ -0,0 +1,8 @@
{
"name": "basic",
"version": "monorepo",
"private": true,
"dependencies": {
"bar": "^2.0.0"
}
}
@@ -0,0 +1,4 @@
{
"name": "@test/package-1",
"version": "1.0.0"
}
@@ -0,0 +1,2 @@
#!/usr/bin/env node
console.log("Hello, world!");
@@ -0,0 +1,8 @@
{
"name": "@test/package-2",
"version": "1.0.0",
"bin": "cli.js",
"dependencies": {
"@test/package-1": "^10.0.0"
}
}
@@ -0,0 +1,2 @@
#!/usr/bin/env node
console.log("Hello, world!");
@@ -0,0 +1,2 @@
#!/usr/bin/env node
console.log("Hello, world!");
@@ -0,0 +1,12 @@
{
"name": "@test/package-3",
"version": "1.0.0",
"bin": {
"package3cli1": "cli1.js",
"package3cli2": "cli2.js"
},
"devDependencies": {
"@test/package-1": "^10.0.0",
"@test/package-2": "^10.0.0"
}
}
@@ -0,0 +1,8 @@
{
"name": "package-4",
"version": "1.0.0",
"dependencies": {
"@test/package-1": "^10.0.0",
"@test/package-3": "^10.0.0"
}
}
Expand Up @@ -99,6 +99,51 @@ Object {
}
`;

exports[`BootstrapCommand with force-local links all packages 1`] = `
Array [
Object {
"_src": "packages/package-1",
"dest": "packages/package-2/node_modules/@test/package-1",
"type": "junction",
},
Object {
"_src": "packages/package-1",
"dest": "packages/package-3/node_modules/@test/package-1",
"type": "junction",
},
Object {
"_src": "packages/package-1",
"dest": "packages/package-4/node_modules/@test/package-1",
"type": "junction",
},
Object {
"_src": "packages/package-2",
"dest": "packages/package-3/node_modules/@test/package-2",
"type": "junction",
},
Object {
"_src": "packages/package-2/cli.js",
"dest": "packages/package-3/node_modules/.bin/package-2",
"type": "exec",
},
Object {
"_src": "packages/package-3",
"dest": "packages/package-4/node_modules/@test/package-3",
"type": "junction",
},
Object {
"_src": "packages/package-3/cli1.js",
"dest": "packages/package-4/node_modules/.bin/package3cli1",
"type": "exec",
},
Object {
"_src": "packages/package-3/cli2.js",
"dest": "packages/package-4/node_modules/.bin/package3cli2",
"type": "exec",
},
]
`;

exports[`BootstrapCommand with hoisting should hoist 1`] = `
Object {
"ROOT": Array [
Expand Down
10 changes: 10 additions & 0 deletions commands/bootstrap/__tests__/bootstrap-command.test.js
Expand Up @@ -584,4 +584,14 @@ describe("BootstrapCommand", () => {
expect(err.message).toMatch("this is not a git repository");
}
});

describe("with force-local", () => {
it("links all packages", async () => {
const testDir = await initFixture("force-local");

await lernaBootstrap(testDir)("--force-local");

expect(symlinkedDirectories(testDir)).toMatchSnapshot();
});
});
});
17 changes: 11 additions & 6 deletions commands/bootstrap/index.js
Expand Up @@ -21,6 +21,7 @@ const symlinkBinary = require("@lerna/symlink-binary");
const symlinkDependencies = require("@lerna/symlink-dependencies");
const ValidationError = require("@lerna/validation-error");
const { getFilteredPackages } = require("@lerna/filter-options");
const PackageGraph = require("@lerna/package-graph");
const hasDependencyInstalled = require("./lib/has-dependency-installed");
const isHoistedPackage = require("./lib/is-hoisted-package");

Expand Down Expand Up @@ -80,9 +81,13 @@ class BootstrapCommand extends Command {
this.npmConfig.npmClientArgs = [...(npmClientArgs || []), ...doubleDashArgs];
}

this.targetGraph = this.options.forceLocal
? new PackageGraph(this.packageGraph.rawPackageList, "allDependencies", "forceLocal")
: this.packageGraph;

let chain = Promise.resolve();

chain = chain.then(() => getFilteredPackages(this.packageGraph, this.execOpts, this.options));
chain = chain.then(() => getFilteredPackages(this.targetGraph, this.execOpts, this.options));
chain = chain.then(filteredPackages => {
this.filteredPackages = filteredPackages;
});
Expand Down Expand Up @@ -159,7 +164,7 @@ class BootstrapCommand extends Command {

return Object.keys(rootDependencies).some(
name =>
this.packageGraph.has(name) &&
this.targetGraph.has(name) &&
npa.resolve(name, rootDependencies[name], this.project.rootPath).type === "directory"
);
}
Expand Down Expand Up @@ -301,7 +306,7 @@ class BootstrapCommand extends Command {
*/
const depsToInstall = new Map();
const filteredNodes = new Map(
this.filteredPackages.map(pkg => [pkg.name, this.packageGraph.get(pkg.name)])
this.filteredPackages.map(pkg => [pkg.name, this.targetGraph.get(pkg.name)])
);

// collect root dependency versions
Expand Down Expand Up @@ -363,7 +368,7 @@ class BootstrapCommand extends Command {
}

const dependents = Array.from(externalDependents.get(rootVersion)).map(
leafName => this.packageGraph.get(leafName).pkg
leafName => this.targetGraph.get(leafName).pkg
);

// remove collection so leaves don't repeat it
Expand Down Expand Up @@ -395,7 +400,7 @@ class BootstrapCommand extends Command {
);
}

const leafNode = this.packageGraph.get(leafName);
const leafNode = this.targetGraph.get(leafName);
const leafRecord = leaves.get(leafNode) || leaves.set(leafNode, new Set()).get(leafNode);

// only install dependency if it's not already installed
Expand Down Expand Up @@ -546,7 +551,7 @@ class BootstrapCommand extends Command {
* @returns {Promise}
*/
symlinkPackages() {
return symlinkDependencies(this.filteredPackages, this.packageGraph, this.logger);
return symlinkDependencies(this.filteredPackages, this.targetGraph, this.logger);
}
}

Expand Down
1 change: 1 addition & 0 deletions commands/bootstrap/package.json
Expand Up @@ -41,6 +41,7 @@
"@lerna/has-npm-version": "file:../../utils/has-npm-version",
"@lerna/npm-conf": "file:../../utils/npm-conf",
"@lerna/npm-install": "file:../../utils/npm-install",
"@lerna/package-graph": "file:../../core/package-graph",
"@lerna/rimraf-dir": "file:../../utils/rimraf-dir",
"@lerna/run-lifecycle": "file:../../utils/run-lifecycle",
"@lerna/run-parallel-batches": "file:../../utils/run-parallel-batches",
Expand Down

0 comments on commit 25572af

Please sign in to comment.