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
25 changes: 19 additions & 6 deletions commands/link.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
const semver = require('semver');
const executeSyncExit = require('roc').executeSyncExit;

const previous = [];

// We will link all previous with the next one, this might not be needed but we do it
// because it's easy and it does not really hurt in any way.
const linkPrevious = (name, yarn) => {
const linkPrevious = (extension, yarn, useInstall) => {
const pkg = yarn ? 'yarn' : 'npm';

const links = previous.map((prev) => `${pkg} link ${prev}`);
previous.push(name);
const operation = useInstall ? 'install' : 'link';
const links = previous
.map(({ path, name }) => `${pkg} ${operation} ${useInstall ? path : name}`);
previous.push(extension);
if (yarn) {
links.push('yarn install');
}
Expand All @@ -29,15 +32,25 @@ const linkExtra = (extra, yarn) => {
}`;
};

const link = (extension, extra, yarn) =>
`cd ${extension.path}${linkExtra(extra, yarn)} && ${linkPrevious(extension.name, yarn)}`;
const link = (extension, extra, yarn, useInstall) =>
`cd ${extension.path}${linkExtra(extra, yarn)} && ${linkPrevious(extension, yarn, useInstall)}`;

module.exports = (extensions) => (commandObject) => {
const extra = commandObject.arguments.managed.modules || [];
const yarn = commandObject.options.managed.yarn;

// We want to use "npm install" over "npm link" when running with npm 5+
// This since the behaviour seems to have changed when using "link" and
// in general direct install seems to be the recommended way for monorepos
// We only want to do this when using npm, not when using yarn
const useInstall = semver.satisfies(
executeSyncExit('npm -v', { silent: true }),
'>=5'
) && !yarn;

executeSyncExit(
extensions
.map((extension) => link(extension, extra, yarn))
.map((extension) => link(extension, extra, yarn, useInstall))
.join(' && ')
);
};
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"eslint-plugin-import": "~1.14.0",
"readline-sync": "~1.4.4",
"rimraf": "~2.5.4",
"roc": "^1.0.0-rc.11"
"roc": "^1.0.0-rc.11",
"semver": "^5.3.0"
}
}