Skip to content
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

Add self update #6736

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions src/cli/aliases.js
Expand Up @@ -3,4 +3,5 @@
export default ({
'upgrade-interactive': 'upgradeInteractive',
'generate-lock-entry': 'generateLockEntry',
'self-upgrade': 'selfUpgrade',
}: {[key: string]: string});
2 changes: 2 additions & 0 deletions src/cli/commands/index.js
Expand Up @@ -36,6 +36,7 @@ import * as policies from './policies.js';
import * as publish from './publish.js';
import * as remove from './remove.js';
import * as run from './run.js';
import * as selfUpgrade from './self-upgrade.js';
import * as tag from './tag.js';
import * as team from './team.js';
import * as unplug from './unplug.js';
Expand Down Expand Up @@ -84,6 +85,7 @@ const commands = {
publish,
remove,
run,
selfUpgrade,
tag,
team,
unplug,
Expand Down
2 changes: 1 addition & 1 deletion src/cli/commands/install.js
Expand Up @@ -87,7 +87,7 @@ type Flags = {
* Try and detect the installation method for Yarn and provide a command to update it with.
*/

function getUpdateCommand(installationMethod: InstallationMethod): ?string {
export function getUpdateCommand(installationMethod: InstallationMethod): ?string {
if (installationMethod === 'tar') {
return `curl --compressed -o- -L ${constants.YARN_INSTALLER_SH} | bash`;
}
Expand Down
62 changes: 62 additions & 0 deletions src/cli/commands/self-upgrade.js
@@ -0,0 +1,62 @@
/* @flow */

import semver from 'semver';
import type Config from '../../config.js';
import {SELF_UPDATE_VERSION_URL} from '../../constants.js';
import type {Reporter} from '../../reporters/index.js';
import {getInstallationMethod} from '../../util/yarn-version.js';
import {getUpdateCommand} from './install';
import {execCommand} from '../../util/execute-lifecycle-script.js';

export const noArguments = true;
export const requireLockfile = false;

export function setFlags(commander: Object) {
commander.description('Upgrades yarn according to the installation method');
}

export function hasWrapper(commander: Object, args: Array<string>): boolean {
return true;
}

export async function run(config: Config, reporter: Reporter, flags: Object, args: Array<string>): Promise<void> {
const currentVersion = flags.version();
const latestVersion = await config.requestManager.request({
url: SELF_UPDATE_VERSION_URL,
headers: {
Accept: 'text/plain',
},
});

// Check if we already use the latest or a newer version
if (semver.compare(currentVersion, latestVersion) >= 0) {
reporter.info('already on latest version or higher');
return;
}

const installationMethod = await getInstallationMethod();

reporter.info(installationMethod);

if (installationMethod === 'unknown') {
reporter.info('installation method could not be identified');
return;
}

const command = getUpdateCommand(installationMethod);
if (command) {
reporter.info(`installation method: , ${installationMethod}`);

reporter.info('running');

await execCommand({
stage: 'self-upgrade',
config,
cmd: command,
cwd: config.cwd,
isInteractive: true,
});
} else {
reporter.info('could not find command for instalation method');
}
}
2 changes: 1 addition & 1 deletion src/lockfile/parse.js
Expand Up @@ -186,7 +186,7 @@ class Parser {
if (version > LOCKFILE_VERSION) {
throw new MessageError(
`Can't install from a lockfile of version ${version} as you're on an old yarn version that only supports ` +
`versions up to ${LOCKFILE_VERSION}. Run \`$ yarn self-update\` to upgrade to the latest version.`,
`versions up to ${LOCKFILE_VERSION}. Run \`$ yarn self-upgrade\` to upgrade to the latest version.`,
);
}
}
Expand Down