Skip to content

Commit

Permalink
feat(link): Add 'convert' subcommand to aid migration to local file: …
Browse files Browse the repository at this point in the history
…specifiers
  • Loading branch information
evocateur committed Apr 13, 2018
1 parent ec2b8f5 commit f59bf3c
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 12 deletions.
30 changes: 20 additions & 10 deletions commands/link/command.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,27 @@
*/
exports.command = "link";

exports.describe = "Symlink together all packages which are dependencies of each other";
exports.describe = "Symlink together all packages that are dependencies of each other";

exports.builder = {
"force-local": {
group: "Command Options:",
describe: "Force local",
type: "boolean",
default: undefined,
},
exports.builder = yargs => {
yargs.options({
"force-local": {
group: "Command Options:",
describe: "Force local sibling links regardless of version range match",
type: "boolean",
default: undefined,
},
});

return yargs.command(
"convert",
"Replace local sibling version ranges with relative file: specifiers",
() => {},
handler
);
};

exports.handler = function handler(argv) {
exports.handler = handler;
function handler(argv) {
return require(".")(argv);
};
}
43 changes: 43 additions & 0 deletions commands/link/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
"use strict";

const path = require("path");
const pMap = require("p-map");
const slash = require("slash");
const Command = require("@lerna/command");
const PackageGraph = require("@lerna/package-graph");
const symlinkDependencies = require("@lerna/symlink-dependencies");
Expand Down Expand Up @@ -32,8 +35,48 @@ class LinkCommand extends Command {
}

execute() {
if (this.options._.pop() === "convert") {
return this.convertLinksToFileSpecs();
}

return symlinkDependencies(this.packages, this.targetGraph, this.logger);
}

convertLinksToFileSpecs() {
const rootPkg = this.project.manifest;
const rootDependencies = {};
const hoisted = {};
const changed = new Set();
const savePrefix = "file:";

for (const targetNode of this.targetGraph.values()) {
const resolved = { name: targetNode.name, type: "directory" };

// install root file: specifiers to avoid bootstrap
rootDependencies[targetNode.name] = targetNode.pkg.resolved.saveSpec;

for (const depNode of targetNode.localDependents.values()) {
const depVersion = slash(path.relative(depNode.pkg.location, targetNode.pkg.location));
// console.log("\n%s\n %j: %j", depNode.name, name, `${savePrefix}${depVersion}`);

depNode.pkg.updateLocalDependency(resolved, depVersion, savePrefix);
changed.add(depNode);
}

if (targetNode.pkg.devDependencies) {
// hoist _all_ devDependencies to the root
Object.assign(hoisted, targetNode.pkg.devDependencies);
targetNode.pkg.set("devDependencies", {});
changed.add(targetNode);
}
}

// mutate project manifest, completely overwriting existing dependencies
rootPkg.set("dependencies", rootDependencies);
rootPkg.set("devDependencies", Object.assign(rootPkg.get("devDependencies") || {}, hoisted));

return pMap(changed, node => node.pkg.serialize()).then(() => rootPkg.serialize());
}
}

module.exports.LinkCommand = LinkCommand;
4 changes: 3 additions & 1 deletion commands/link/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
"dependencies": {
"@lerna/command": "file:../../core/command",
"@lerna/package-graph": "file:../../core/package-graph",
"@lerna/symlink-dependencies": "file:../../utils/symlink-dependencies"
"@lerna/symlink-dependencies": "file:../../utils/symlink-dependencies",
"p-map": "^1.2.0",
"slash": "^1.0.0"
}
}
4 changes: 3 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit f59bf3c

Please sign in to comment.