Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/FileSystemUtilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,17 @@ export default class FileSystemUtilities {
rimraf(filePath, callback);
}

@logger.logifyAsync
static symlink(src, dest, type = "file", callback) {
fs.lstat(dest, (err) => {
if (!err) {
fs.unlink(dest, () => fs.symlink(src, dest, type, callback));
} else {
fs.symlink(src, dest, type, callback);
}
});
}

@logger.logifySync
static unlinkSync(filePath) {
fs.unlink(filePath);
Expand Down
4 changes: 4 additions & 0 deletions src/Package.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ export default class Package {
this._package.version = version;
}

get bin() {
return this._package.bin;
}

get dependencies() {
return this._package.dependencies;
}
Expand Down
86 changes: 65 additions & 21 deletions src/commands/BootstrapCommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,29 @@ export default class BootstrapCommand extends Command {
});

async.parallelLimit(batch.map(pkg => done => {
async.series([
// async actions to bootstrap each package
const actions = [
cb => FileSystemUtilities.mkdirp(pkg.nodeModulesLocation, cb),
cb => this.installExternalPackages(pkg, cb),
cb => this.linkDependenciesForPackage(pkg, cb),
cb => this.runPrepublishForPackage(pkg, cb),
], err => {
];
// array of dependencies with binaries
const depsWithBin = [];
// find matching dependencies with binaries
this.packages.forEach(dependency => {
// for each matched dependency with binaries
if (this.hasMatchingDependency(pkg, dependency) && dependency.bin) {
depsWithBin.push(dependency);
}
});
// if package has dependencies with binaries
if (depsWithBin.length) {
// symlink binaries
actions.push(cb => this.symlinkDependencyBinaries(pkg, depsWithBin, cb));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the binaries should be linked before runPrepublishForPackage.

A package may have a prepublish script that uses a binary from a dependency.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make sense.

}
// add prepublish action
actions.push(cb => this.runPrepublishForPackage(pkg, cb));
async.series(actions, err => {
this.progressBar.tick(pkg.name);
donePackages[pkg.name] = true;
todoPackages.splice(todoPackages.indexOf(pkg), 1);
Expand Down Expand Up @@ -100,19 +117,11 @@ export default class BootstrapCommand extends Command {
}

createLinkedDependency(src, dest, name, callback) {
FileSystemUtilities.rimraf(dest, err => {
if (err) {
return callback(err);
}

FileSystemUtilities.mkdirp(dest, err => {
if (err) {
return callback(err);
}

this.createLinkedDependencyFiles(src, dest, name, callback);
});
});
async.series([
cb => FileSystemUtilities.rimraf(dest, cb),
cb => FileSystemUtilities.mkdirp(dest, cb),
cb => this.createLinkedDependencyFiles(src, dest, name, cb)
], callback);
}

createLinkedDependencyFiles(src, dest, name, callback) {
Expand All @@ -128,13 +137,48 @@ export default class BootstrapCommand extends Command {
const prefix = this.repository.linkedFiles.prefix || "";
const indexJsFileContents = prefix + "module.exports = require(" + JSON.stringify(src) + ");";

FileSystemUtilities.writeFile(destPackageJsonLocation, packageJsonFileContents, err => {
if (err) {
return callback(err);
}
async.series([
cb => FileSystemUtilities.writeFile(destPackageJsonLocation, packageJsonFileContents, cb),
cb => FileSystemUtilities.writeFile(destIndexJsLocation, indexJsFileContents, cb)
], callback);
}

/**
* Symlink package dependency binaries to the package's node_modules/.bin folder
* @param {Package} pkg
* @param {Array.<Package>} dependencies
* @param {Function} callback
*/
symlinkDependencyBinaries(pkg, dependencies, callback) {
const actions = [];
dependencies.forEach(dependency => {
const { location, name, bin } = dependency;
actions.push(cb => this.createBinaryLink(location, pkg.nodeModulesLocation, name, bin, cb));
});
async.parallelLimit(actions, this.concurrency, callback);
}

FileSystemUtilities.writeFile(destIndexJsLocation, indexJsFileContents, callback);
/**
* Create a symlink to a dependency's binary in the node_modules/.bin folder
* @param {String} src Source location of package to link
* @param {String} dest Destination node_modules folder
* @param {String} name Name of source package
* @param {String|Object} bin Source package.json "bin" value
* @param {Function} callback
*/
createBinaryLink(src, dest, name, bin, callback) {
// destination folder of binaries
const destBinFolder = path.join(dest, ".bin");
const bins = typeof bin === "string" ? { [name]: bin } : bin;
const srcBinFiles = Object.keys(bins).map(name => path.join(src, bins[name]));
const destBinFiles = Object.keys(bins).map(name => path.join(destBinFolder, name));
// ensure destination folder
const actions = [cb => FileSystemUtilities.mkdirp(destBinFolder, cb)];
// create symlinks for binaries
srcBinFiles.forEach((binFile, idx) => {
actions.push(cb => FileSystemUtilities.symlink(binFile, destBinFiles[idx], "file", cb));
});
async.series(actions, callback);
}

installExternalPackages(pkg, callback) {
Expand Down
23 changes: 23 additions & 0 deletions test/BootstrapCommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,20 @@ describe("BootstrapCommand", () => {
assert.ok(pathExists.sync(path.join(testDir, "packages/package-3/node_modules/package-2/index.js")));
assert.ok(pathExists.sync(path.join(testDir, "packages/package-3/node_modules/package-2/package.json")));

// look for binary symlinks
assert.ok(pathExists.sync(path.join(testDir, "packages/package-3/node_modules/.bin/package-2")));
const package2BinLink = fs.readlinkSync(path.join(testDir, "packages/package-3/node_modules/.bin/package-2"));
assert.equal(package2BinLink, path.join(testDir, "packages/package-2/cli.js"))
assert.ok(pathExists.sync(path.join(testDir, "packages/package-4/node_modules/.bin/package3cli1")));
assert.ok(pathExists.sync(path.join(testDir, "packages/package-4/node_modules/.bin/package3cli2")));
const package3BinLink1 = fs.readlinkSync(path.join(testDir, "packages/package-4/node_modules/.bin/package3cli1"));
assert.equal(package3BinLink1, path.join(testDir, "packages/package-3/cli1.js"))
const package3BinLink2 = fs.readlinkSync(path.join(testDir, "packages/package-4/node_modules/.bin/package3cli2"));
assert.equal(package3BinLink2, path.join(testDir, "packages/package-3/cli2.js"))

// Should not exist because mis-matched version
assert.ok(!pathExists.sync(path.join(testDir, "packages/package-4/node_modules/package-1")));

// Should not exist because mis-matched version
assert.ok(!pathExists.sync(path.join(testDir, "packages/package-4/node_modules/package-1")));

Expand Down Expand Up @@ -107,6 +121,15 @@ describe("BootstrapCommand", () => {
bootstrapCommand.runValidations();
bootstrapCommand.runPreparations();

assertStubbedCalls([
[ChildProcessUtilities, "spawn", { nodeCallback: true }, [
{ args: ["npm", ["install", "package-2@^1.0.0"], { cwd: path.join(testDir, "packages/package-3"), stdio: "ignore" }] }
]],
[ChildProcessUtilities, "spawn", { nodeCallback: true }, [
{ args: ["npm", ["install", "package-1@^0.0.0"], { cwd: path.join(testDir, "packages/package-4"), stdio: "ignore" }] }
]]
]);

bootstrapCommand.runCommand(exitWithCode(0, err => {
if (err) return done(err);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#! /usr/bin/env node
console.log("Hello, world!");
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
"version": "1.0.0",
"dependencies": {
"package-1": "^1.0.0"
}
},
"bin": "cli.js"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#! /usr/bin/env node
console.log("Hello, world!");
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#! /usr/bin/env node
console.log("Hello, world!");
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,9 @@
"version": "1.0.0",
"devDependencies": {
"package-2": "^1.0.0"
},
"bin": {
"package3cli1": "cli1.js",
"package3cli2": "cli2.js"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"name": "package-4",
"version": "1.0.0",
"dependencies": {
"package-1": "^0.0.0"
"package-1": "^0.0.0",
"package-3": "^1.0.0"
}
}