Skip to content

Commit

Permalink
Fix 4263: Make upgrade and upgrade-interactive compatible with worksp…
Browse files Browse the repository at this point in the history
…aces (yarnpkg#4278)

* make upgrade and upgrade-interactive compatible with workspaces

* lint

* flow

* feedback from @arcanis

* fix flow

* remove lockfile pattern for normal upgrade interactive
  • Loading branch information
kaylieEB authored and joaolucasl committed Oct 27, 2017
1 parent d697a4c commit 3159074
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 4 deletions.
8 changes: 8 additions & 0 deletions __tests__/commands/upgrade.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,14 @@ test.concurrent('upgrades from fixed version to latest', (): Promise<void> => {
});
});

test.concurrent('upgrades from fixed version to latest with workspaces', (): Promise<void> => {
return runUpgrade(['max-safe-integer'], {latest: true}, 'fixed-to-latest-workspaces', async (config): ?Promise<
void,
> => {
await expectInstalledDevDependency(config, 'max-safe-integer', '1.0.1', '1.0.1');
});
});

test.concurrent('upgrades to latest matching package.json semver when no package name passed', (): Promise<void> => {
return runUpgrade([], {}, 'range-to-latest', async (config): ?Promise<void> => {
await expectInstalledDependency(config, 'left-pad', '<=1.1.1', '1.1.1');
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"private": true,
"devDependencies": {
"max-safe-integer": "1.0.0"
},
"workspaces": [
"packages"
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "workspace-1",
"version": "1.0.0",
"scripts": {
"prescript": "echo workspace-1 prescript",
"script": "echo workspace-1 script",
"postscript": "echo workspace-1 postscript"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1


max-safe-integer@1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/max-safe-integer/-/max-safe-integer-1.0.0.tgz#4662073a02c7e02d38153e25795489b20be6f01a"
10 changes: 9 additions & 1 deletion src/cli/commands/add.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ export class Add extends Install {
constructor(args: Array<string>, flags: Object, config: Config, reporter: Reporter, lockfile: Lockfile) {
super(flags, config, reporter, lockfile);
this.args = args;

if (this.config.workspaceRootFolder && this.config.cwd === this.config.workspaceRootFolder) {
this.setIgnoreWorkspaces(true);
}
// only one flag is supported, so we can figure out which one was passed to `yarn add`
this.flagToOrigin = [
flags.dev && 'devDependencies',
Expand All @@ -32,6 +36,10 @@ export class Add extends Install {
]
.filter(Boolean)
.shift();

if (flags.existing) {
this.flagToOrigin = '';
}
}

args: Array<string>;
Expand Down Expand Up @@ -120,7 +128,7 @@ export class Add extends Install {
*/

async init(): Promise<Array<string>> {
if (this.config.workspaceRootFolder && this.config.cwd === this.config.workspaceRootFolder) {
if (this.ignoreWorkspaces) {
if (this.flagToOrigin === 'dependencies') {
throw new MessageError(this.reporter.lang('workspacesPreferDevDependencies'));
}
Expand Down
11 changes: 10 additions & 1 deletion src/cli/commands/install.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ export class Install {
this.integrityChecker = new InstallationIntegrityChecker(config);
this.linker = new PackageLinker(config, this.resolver);
this.scripts = new PackageInstallScripts(config, this.resolver, this.flags.force);
this.ignoreWorkspaces = false;
}

flags: Flags;
Expand All @@ -191,6 +192,7 @@ export class Install {
rootPatternsToOrigin: {[pattern: string]: string};
integrityChecker: InstallationIntegrityChecker;
resolutionMap: ResolutionMap;
ignoreWorkspaces: boolean;

/**
* Create a list of dependency requests from the current directories manifests.
Expand Down Expand Up @@ -284,7 +286,7 @@ export class Install {
pushDeps('devDependencies', projectManifestJson, {hint: 'dev', optional: false}, !this.config.production);
pushDeps('optionalDependencies', projectManifestJson, {hint: 'optional', optional: true}, true);

if (this.config.workspaceRootFolder) {
if (this.config.workspaceRootFolder && !this.ignoreWorkspaces) {
const workspacesRoot = path.dirname(loc);
const workspaces = await this.config.resolveWorkspaces(workspacesRoot, projectManifestJson);
workspaceLayout = new WorkspaceLayout(workspaces, this.config);
Expand Down Expand Up @@ -327,6 +329,13 @@ export class Install {
};
}

/**
* Sets the value of `ignoreWorkspaces` for install commands that should skip workspaces
*/
setIgnoreWorkspaces(ignoreWorkspaces: boolean) {
this.ignoreWorkspaces = ignoreWorkspaces;
}

/**
* TODO description
*/
Expand Down
4 changes: 3 additions & 1 deletion src/cli/commands/upgrade-interactive.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,11 @@ export async function run(config: Config, reporter: Reporter, flags: Object, arg
flags.dev = hint === 'dev';
flags.peer = hint === 'peer';
flags.optional = hint === 'optional';

const deps = answers.filter(isHint(hint)).map(getPattern);
if (deps.length) {
for (const pattern of deps) {
lockfile.removePattern(pattern);
}
reporter.info(reporter.lang('updateInstalling', getNameFromHint(hint)));
const add = new Add(deps, flags, config, reporter, lockfile);
return add.init();
Expand Down
2 changes: 1 addition & 1 deletion src/cli/commands/upgrade.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export async function run(config: Config, reporter: Reporter, flags: Object, arg
const deps = await getOutdated(config, reporter, flags, lockfile, args);

// do not pass the --latest flag to add, otherwise it may ignore the version ranges we already determined.
const addFlags = Object.assign({}, flags, {force: true, latest: false});
const addFlags = Object.assign({}, flags, {force: true, latest: false, existing: true});

setUserRequestedPackageVersions(deps, args);

Expand Down

0 comments on commit 3159074

Please sign in to comment.