Skip to content

make add command working with latest tag if offline or preferOffline is setted #2521

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
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
29 changes: 29 additions & 0 deletions __tests__/commands/add.js
Original file line number Diff line number Diff line change
Expand Up @@ -630,9 +630,38 @@ test.skip('add asks for correct package version if user passes an incorrect one'

test.concurrent('install with latest tag', (): Promise<void> => {
return runAdd(['left-pad@latest'], {}, 'latest-version-in-package', async (config) => {
const lockfile = explodeLockfile(await fs.readFile(path.join(config.cwd, 'yarn.lock')));
const pkg = await fs.readJson(path.join(config.cwd, 'package.json'));
const version = await getPackageVersion(config, 'left-pad');

assert.deepEqual(pkg.dependencies, {'left-pad': `^${version}`});
assert(lockfile.indexOf(`left-pad@^${version}:`) === 0);
});
});

test.concurrent('install with latest tag and --offline flag', (): Promise<void> => {
return runAdd(['left-pad@latest'], {}, 'latest-version-in-package', async (config, reporter, previousAdd) => {
config.offline = true;
const add = new Add(['left-pad@latest'], {}, config, reporter, previousAdd.lockfile);
await add.init();

const pkg = await fs.readJson(path.join(config.cwd, 'package.json'));
const version = await getPackageVersion(config, 'left-pad');

assert.deepEqual(pkg.dependencies, {'left-pad': `^${version}`});
});
});

test.concurrent('install with latest tag and --prefer-offline flag', (): Promise<void> => {
return runAdd(['left-pad@1.1.0'], {}, 'latest-version-in-package', async (config, reporter, previousAdd) => {
config.preferOffline = true;
const add = new Add(['left-pad@latest'], {}, config, reporter, previousAdd.lockfile);
await add.init();

const pkg = await fs.readJson(path.join(config.cwd, 'package.json'));
const version = await getPackageVersion(config, 'left-pad');

assert.deepEqual(pkg.dependencies, {'left-pad': `^${version}`});
assert.notEqual(version, '1.1.0');
});
});
7 changes: 6 additions & 1 deletion src/package-constraint-resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ export default class PackageConstraintResolver {
config: Config;

reduce(versions: Array<string>, range: string): Promise<?string> {
return Promise.resolve(semver.maxSatisfying(versions, range, this.config.looseSemver));
if (range === 'latest') {
// Usually versions are already ordered and the last one is the latest
return Promise.resolve(versions[versions.length - 1]);
} else {
return Promise.resolve(semver.maxSatisfying(versions, range, this.config.looseSemver));
}
}
}