Skip to content

Commit

Permalink
增加serve期间检测最新版本
Browse files Browse the repository at this point in the history
  • Loading branch information
ksky521 committed Mar 4, 2019
1 parent 2fbff0b commit bf266b0
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 18 deletions.
Binary file removed packages/.DS_Store
Binary file not shown.
18 changes: 17 additions & 1 deletion packages/nodeppt-serve/commands/serve.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
* 部分代码来自 vue cli
* @file serve 主要内容
*/
const {info, prepareUrls} = require('nodeppt-shared-utils');
const {info, prepareUrls, getLatestVersion, newVersionLog} = require('nodeppt-shared-utils');
const semver = require('semver');

let newVersion = 0;

const defaults = {
host: '0.0.0.0',
Expand All @@ -26,6 +29,16 @@ module.exports = (api, options) => {
async function serve(args) {
info('Starting development server...');

const currentVersion = '2.1.0'; // args.version
// 获取版本更新
getLatestVersion()
.then(latest => {
if (semver.lt(currentVersion, latest)) {
newVersion = latest;
}
})
.catch(e => {});

const url = require('url');
const path = require('path');
const chalk = require('chalk');
Expand Down Expand Up @@ -117,6 +130,9 @@ module.exports = (api, options) => {
['SIGINT', 'SIGTERM'].forEach(signal => {
process.on(signal, () => {
server.close(() => {
if (newVersion) {
newVersionLog(currentVersion, newVersion);
}
process.exit(0);
});
});
Expand Down
1 change: 1 addition & 0 deletions packages/nodeppt-shared-utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
'get-debug',
'eval',
'path',
'new-version-log',
'download-repo',
'git-user',
'get-latest-version',
Expand Down
2 changes: 1 addition & 1 deletion packages/nodeppt-shared-utils/lib/get-latest-version.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const request = require('request');
const debug = require('./get-debug').getDebugLogger('get-lastest-version');

// 获取最新版本
function getLatestVersion(name = 'nodeppt', registry = 'http://registry.npm.taobao.org') {
function getLatestVersion(name = 'nodeppt', registry = 'https://registry.npmjs.org') {
return new Promise(resolve => {
request(
{
Expand Down
18 changes: 18 additions & 0 deletions packages/nodeppt-shared-utils/lib/new-version-log.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const chalk = require('chalk');
const boxen = require('boxen');
exports.newVersionLog = (currentVersion, newVersion) => {
if (newVersion) {
const message = `Update available ${chalk.dim(currentVersion)}${chalk.green(newVersion)}
Run ${chalk.cyan('npm i -g nodeppt')} to update`;

console.error(
boxen(message, {
padding: 1,
margin: 1,
align: 'center',
borderColor: 'yellow',
borderStyle: 'round'
})
);
}
};
1 change: 1 addition & 0 deletions packages/nodeppt-shared-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"license": "MIT",
"dependencies": {
"address": "^1.0.3",
"boxen": "^3.0.0",
"chalk": "^2.4.2",
"debug": "^4.1.1",
"fs-extra": "^7.0.1",
Expand Down
21 changes: 5 additions & 16 deletions packages/nodeppt/lib/generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/
const path = require('path');
const fs = require('fs');

const exists = fs.existsSync;

const semver = require('semver');
Expand All @@ -16,6 +17,7 @@ const {
success,
line,
getGitUser,
newVersionLog,
getLatestVersion,
logWithSpinner,
updateSpinner,
Expand Down Expand Up @@ -96,14 +98,8 @@ module.exports = async (src, dest, cmdOpts) => {
});

if (newVersion) {
/* eslint-disable*/
console.log(
chalk.green(`
┌────────────────────────────────────────────${`─`.repeat(newVersion.length)}──┐
│ A newer version of nodeppt is available: ${chalk.yellow(localVersion)}
└────────────────────────────────────────────${`─`.repeat(newVersion.length)}──┘`)
);
/* eslint-enable*/
// 存在新版本
newVersionLog(localVersion, newVersion);
}
} else {
logMessage(opts.completeMessage, data);
Expand All @@ -125,14 +121,7 @@ function logMessage(message, data) {
}

if (newVersion) {
/* eslint-disable*/
console.log(
chalk.green(`
┌────────────────────────────────────────────${`─`.repeat(newVersion.length)}──┐
│ A newer version of nodeppt is available: ${chalk.yellow(localVersion)}
└────────────────────────────────────────────${`─`.repeat(newVersion.length)}──┘`)
);
/* eslint-enable*/
newVersionLog(localVersion, newVersion);
}
}

Expand Down

0 comments on commit bf266b0

Please sign in to comment.