Skip to content

Commit

Permalink
feat: iceworks-cli dynamic install for iceworks-server (#2441)
Browse files Browse the repository at this point in the history
* feat: dynamic install for iceworks-server

* chore: lint rule

* chore: code optim

* chore: fix lint

* chore: code optim

* chore: fix lint

* fix: wording change
  • Loading branch information
chenbin92 authored and imsobear committed Jul 15, 2019
1 parent 5a9103d commit 7cdbbf9
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 46 deletions.
46 changes: 33 additions & 13 deletions tools/iceworks-cli/bin/iceworks.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
#!/usr/bin/env node

const chalk = require('chalk');
const program = require('commander');
const pkgData = require('../package');
const semver = require('semver');
const packageConfig = require('../package');
const checkVersion = require('../lib/checkVersion');

async function check() {
await checkVersion();
}
// check node version
checkNodeVersion();

// check node version and iceworks version
check();
// check iceworks version
checkIceworksVersion();

program.version(pkgData.version).usage('<command> [options]');
program.version(packageConfig.version).usage('<command> [options]');

// output help information on unknown commands
program.arguments('<command>').action((cmd) => {
program.outputHelp();
// eslint-disable-next-line prefer-template
console.log(` ` + chalk.red(`Unknown command ${chalk.yellow(cmd)}.`));
console.log(` chalk.red('Unknown command ${chalk.yellow(cmd)}.`);
console.log();
});

Expand Down Expand Up @@ -62,9 +60,7 @@ program
program.on('--help', () => {
console.log();
console.log(
` Run ${chalk.cyan(
`iceworks <command> --help`
)} for detailed usage of given command.`
` Run ${chalk.cyan('iceworks <command> --help')} for detailed usage of given command.`
);
console.log();
});
Expand Down Expand Up @@ -103,3 +99,27 @@ function cleanArgs(cmd) {
}
return args;
}

function checkNodeVersion() {
if (!semver.satisfies(process.version, packageConfig.engines.node)) {
console.log();
console.log(
chalk.red(
`You must upgrade node to ${packageConfig.engines.node} to use iceworks`
)
);
console.log();
process.exit(1);
}
}

async function checkIceworksVersion() {
const packageName = 'iceworks';
const packageVersion = packageConfig.version;
const latestVersion = await checkVersion(packageName, packageVersion);
if (latestVersion) {
console.log(chalk.yellow('\n A newer version of iceworks-cli is available.\n'));
console.log(` latest: + ${chalk.green(latestVersion)}`);
console.log(` installed: + ${chalk.red(packageVersion)} \n`);
}
}
56 changes: 55 additions & 1 deletion tools/iceworks-cli/command/start.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,41 @@ const portfinder = require('portfinder');
const chalk = require('chalk');
const ora = require('ora');
const open = require('open');
const inquirer = require('inquirer');
const goldlog = require('../lib/goldlog');
const checkVersion = require('../lib/checkVersion');

const SERVER_PATH = path.join(__dirname, '../', 'server');
// eslint-disable-next-line import/no-dynamic-require
const serverPackageConfig = require(path.join(SERVER_PATH, 'package.json'));

async function start(options = {}) {
const answers = await checkServerVersion();
if (answers && answers.update) {
const child = spawn('node', ['./lib/downloadServer.js'], {
stdio: ['pipe'],
cwd: path.join(__dirname, '../'),
});

child.stdout.on('data', (data) => {
console.log(data.toString());
});

child.on('error', (err) => {
console.log('update failed:', err);
});

child.on('close', (code) => {
if (code === 0) {
listen(options);
}
});
} else {
listen(options);
}
}

async function listen(options) {
const host = options.host || 'http://127.0.0.1';

let port = options.port;
Expand All @@ -24,7 +56,7 @@ async function start(options = {}) {
spinner.start();
const child = spawn('npm', ['start'], {
stdio: ['pipe'],
cwd: path.join(__dirname, '../', 'server'),
cwd: SERVER_PATH,
env,
});

Expand Down Expand Up @@ -65,6 +97,28 @@ function failedMsg(error) {
console.log();
}

/**
* Get the server package version
*/
async function checkServerVersion() {
const packageName = serverPackageConfig.name;
const packageVersion = serverPackageConfig.version;

const result = await checkVersion(packageName, packageVersion);
if (result) {
const answers = await inquirer.prompt([
{
type: 'confirm',
message: 'A newer version of iceworks core is available',
name: 'update',
default: false,
},
]);

return answers;
}
}

module.exports = (...args) => {
return start(...args).catch((err) => {
console.log(err);
Expand Down
33 changes: 5 additions & 28 deletions tools/iceworks-cli/lib/checkVersion.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,11 @@
/* eslint prefer-template:0 */
const semver = require('semver');
const chalk = require('chalk');
const request = require('request-promise-native');
const packageConfig = require('../package.json');

module.exports = async () => {
if (!semver.satisfies(process.version, packageConfig.engines.node)) {
console.log();
console.log(
chalk.red(
`You must upgrade node to ${packageConfig.engines.node} to use iceworks`
)
);
console.log();
process.exit(1);
}
const { getNpmLatestSemverVersion } = require('ice-npm-utils');

module.exports = async (packageName, packageVersion) => {
try {
const body = await request({
url: 'https://registry.npmjs.org/iceworks',
timeout: 1000,
});
const latestVersion = JSON.parse(body)['dist-tags'].latest;
const localVersion = packageConfig.version;
if (semver.lt(localVersion, latestVersion)) {
console.log(chalk.yellow(' A newer version of iceworks is available.'));
console.log();
console.log(' latest: ' + chalk.green(latestVersion));
console.log(' installed: ' + chalk.red(localVersion));
console.log();
const latestVersion = await getNpmLatestSemverVersion(packageName, packageVersion);
if (semver.lt(packageVersion, latestVersion)) {
return latestVersion;
}
} catch (error) {
// ...
Expand Down
2 changes: 1 addition & 1 deletion tools/iceworks-cli/lib/downloadServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const getTarball = require('./getTarball');
const extractTarball = require('./extractTarball');

const NPM_NAME = 'iceworks-server';
const DEST_DIR = path.join(process.cwd(), 'server');
const DEST_DIR = path.join(__dirname, '../', 'server');

/**
* Download npm package content to the specified directory
Expand Down
5 changes: 2 additions & 3 deletions tools/iceworks-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,15 @@
"cross-spawn": "^6.0.5",
"execa": "^1.0.0",
"fs-extra": "^8.0.1",
"ice-npm-utils": "^1.0.3",
"inquirer": "^6.3.1",
"ice-npm-utils": "^1.1.3",
"inquirer": "^6.5.0",
"mkdirp": "^0.5.1",
"npmlog": "^4.1.2",
"open": "^6.4.0",
"ora": "^3.4.0",
"portfinder": "^1.0.20",
"request": "^2.88.0",
"request-progress": "^3.0.0",
"request-promise-native": "^1.0.7",
"rimraf": "^2.6.3",
"tar": "^2.0.0",
"zlib": "^1.0.5"
Expand Down

0 comments on commit 7cdbbf9

Please # to comment.