[now update] Render the proper tag and improve yarn detection logic (#2595)

* [now update] Render the proper tag and improve `yarn` detection logic

Before this the suggested command would always have you install the
stable version of `now`.

With this change the `@canary` tag will be suggested if the version of
`now` is from the canary release channel.

Also updates the `isYarn` detection logic to not consider the cwd, and
instead check the installed version of now's `package.json` for clues.

* Move `getUpdateCommand` to util

* Add a unit test

* Use integration test instead
This commit is contained in:
Nathan Rajlich
2019-07-23 08:31:15 -07:00
committed by Andy Bitz
parent 50a87c4965
commit cda6a2cbb6
4 changed files with 40 additions and 15 deletions

View File

@@ -1,12 +1,11 @@
import chalk from 'chalk';
import { join } from 'path';
import { pathExists } from 'fs-extra';
import logo from '../util/output/logo';
import handleError from '../util/handle-error';
import getArgs from '../util/get-args';
import { NowContext } from '../types';
import createOutput from '../util/output';
import getUpdateCommand from '../util/get-update-command';
const help = () => {
console.log(`
@@ -32,14 +31,6 @@ const help = () => {
`);
};
export async function getUpgradeCommand() {
const isYarn = await pathExists(join(process.cwd(), 'yarn.lock'));
return isYarn
? 'Please run `yarn global upgrade now` to update Now CLI.'
: 'Please run `npm install -g now@latest` to update Now CLI.'
}
export default async function main(ctx: NowContext): Promise<number> {
let argv;
@@ -64,8 +55,6 @@ export default async function main(ctx: NowContext): Promise<number> {
const debugEnabled = argv['--debug'];
const output = createOutput({ debug: debugEnabled });
const { log } = output;
log(await getUpgradeCommand());
output.log(await getUpdateCommand());
return 0;
}

View File

@@ -36,7 +36,7 @@ import * as ERRORS from './util/errors-ts';
import { NowError } from './util/now-error';
import { SENTRY_DSN } from './util/constants.ts';
import { metrics, shouldCollectMetrics } from './util/metrics.ts';
import { getUpgradeCommand } from './commands/update';
import getUpdateCommand from './util/get-update-command';
const NOW_DIR = getNowDir();
const NOW_CONFIG_PATH = configFiles.getConfigFilePath();
@@ -145,7 +145,7 @@ const main = async argv_ => {
)
);
console.log(
info(await getUpgradeCommand())
info(await getUpdateCommand())
);
console.log(
info(

View File

@@ -0,0 +1,31 @@
import { Stats } from 'fs';
import { dirname, join, resolve } from 'path';
import { readJSON, lstat, readlink } from 'fs-extra';
import cmd from './output/cmd';
import { version } from '../../package.json';
// `npm` tacks a bunch of extra properties on the `package.json` file,
// so check for one of them to determine yarn vs. npm.
async function isYarn(): Promise<boolean> {
let s: Stats;
let binPath = process.argv[1];
while (true) {
s = await lstat(binPath);
if (s.isSymbolicLink()) {
binPath = resolve(dirname(binPath), await readlink(binPath));
} else {
break;
}
}
const pkgPath = join(dirname(binPath), '..', 'package.json');
const pkg = await readJSON(pkgPath);
return !('_id' in pkg);
}
export default async function getUpdateCommand(): Promise<string> {
const tag = version.includes('canary') ? 'canary' : 'latest';
return (await isYarn())
? `Please run ${cmd(`yarn global add now@${tag}`)} to update Now CLI.`
: `Please run ${cmd(`npm install -g now@${tag}`)} to update Now CLI.`;
}

View File

@@ -1449,6 +1449,11 @@ test('fail `now dev` dev script without now.json', async t => {
);
});
test('detect update command', async t => {
const { code, stderr } = await execute(['update']);
t.regex(stderr, /yarn global add now@/gm, `Received: "${stderr}"`);
});
test.after.always(async () => {
// Make sure the token gets revoked
await execa(binaryPath, ['logout', ...defaultArgs]);