Skip to content

Commit

Permalink
feat: support feature cli update tip
Browse files Browse the repository at this point in the history
* feat: support feature cliUpdateTip

* feat: fix style

* refactor: refactor code

* feat: 修改包名

* feat: update cli name
  • Loading branch information
nikewu authored and cpselvis committed Nov 26, 2019
1 parent 3cd0463 commit 90ab127
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 2 deletions.
60 changes: 60 additions & 0 deletions packages/feflow-cli/src/cli/upgrade.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import chalk from 'chalk';
import packageJson from '../shared/packageJson'
import { getRegistryUrl } from '../shared/npm';
import semver from 'semver';
import { safeDump } from '../shared/yaml';
import spawn from 'cross-spawn';
import inquirer from 'inquirer';

async function updateCli(packageManager: string) {
return new Promise((resolve, reject) => {
const args = [
'install',
'feflow-cli@latest',
'--color=always',
'--save',
'--save-exact',
'--loglevel',
'error',
'-g'
];

const child = spawn(packageManager, args, { stdio: 'inherit' });
child.on('close', code => {
if (code !== 0) {
reject({
command: `${packageManager} ${args.join(' ')}`,
});
return;
}
resolve();
});
})
}

export default async function checkCliUpdate(ctx: any) {
const { version, config, configPath } = ctx;
if (config.lastUpdateCheck && (+new Date() - parseInt(config.lastUpdateCheck, 10)) <= 1000 * 3600 * 24) {
return;
}
const packageManager = config.packageManager;
const registryUrl = await getRegistryUrl(packageManager);
const latestVersion: any = await packageJson('feflow-cli', 'latest', registryUrl)
if (semver.gt(latestVersion, version)) {
const askIfUpdateCli = [{
type: "confirm",
name: "ifUpdate",
message: `${chalk.yellow(`feflow-cli's latest version is ${chalk.green(`${latestVersion}`)} but your version is ${chalk.red(`${version}`)}, Do your want to update it?`)}`,
default: true
}]
const answer = await inquirer.prompt(askIfUpdateCli)
if (answer.ifUpdate) {
await updateCli(packageManager)
} else {
safeDump({
...config,
'lastUpdateCheck': +new Date()
}, configPath);
}
}
}
59 changes: 59 additions & 0 deletions packages/feflow-cli/src/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import { FEFLOW_ROOT } from '../shared/constant';
import { safeDump, parseYaml } from '../shared/yaml';
import packageJson from '../shared/packageJson';
import { getRegistryUrl, install } from '../shared/npm';
import chalk from 'chalk';
import semver from 'semver';
const pkg = require('../../package.json');

export default class Feflow {
Expand Down Expand Up @@ -45,9 +47,11 @@ export default class Feflow {
async init(cmd: string) {
if (cmd === 'config') {
await this.initClient();
await this.checkCliUpdate()
await this.loadNative();
} else {
await this.initClient();
await this.checkCliUpdate()
await this.initPackageManager();
await this.checkUpdate();
await this.loadNative();
Expand Down Expand Up @@ -261,4 +265,59 @@ export default class Feflow {
}
});
}

async updateCli(packageManager: string) {

return new Promise((resolve, reject) => {
const args = [
'install',
'@feflow/cli@latest',
'--color=always',
'--save',
'--save-exact',
'--loglevel',
'error',
'-g'
];

const child = spawn(packageManager, args, { stdio: 'inherit' });
child.on('close', code => {
if (code !== 0) {
reject({
command: `${packageManager} ${args.join(' ')}`,
});
return;
}
resolve();
});
})
}

async checkCliUpdate() {
const { version, config, configPath } = this;
if (config.lastUpdateCheck && (+new Date() - parseInt(config.lastUpdateCheck, 10)) <= 1000 * 3600 * 24) {
return;
}
const packageManager = config.packageManager;
const registryUrl = await getRegistryUrl(packageManager);
const latestVersion: any = await packageJson('@feflow/cli', 'latest', registryUrl)
if (semver.gt(latestVersion, version)) {
const askIfUpdateCli = [{
type: "confirm",
name: "ifUpdate",
message: `${chalk.yellow(`@feflow/cli's latest version is ${chalk.green(`${latestVersion}`)} but your version is ${chalk.red(`${version}`)}, Do your want to update it?`)}`,
default: true
}]
const answer = await inquirer.prompt(askIfUpdateCli)
if (answer.ifUpdate) {
await this.updateCli(packageManager)
} else {
safeDump({
...config,
'lastUpdateCheck': +new Date()
}, configPath);
}
}
}

}
4 changes: 2 additions & 2 deletions packages/feflow-cli/src/shared/npm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ export function getRegistryUrl(packageManager: string) {

child.stdout!.on('data', (data) => {
output += data;
}).pipe(process.stdout);
});

child.stderr!.on('data', (data) => {
output += data;
}).pipe(process.stderr);
});

child.on('close', code => {
if (code !== 0) {
Expand Down

0 comments on commit 90ab127

Please sign in to comment.