newkit-cli
Version:
Newkit cli tools.
40 lines (37 loc) • 1.52 kB
JavaScript
const fs = require('fs');
const path = require('path');
const child_process = require('child_process');
const util = require('./../lib/util');
const config = require('./config');
module.exports = (version, force) => {
let cwd = process.cwd();
let pkgFilepath = path.join(cwd, 'package.json');
if (!fs.existsSync(pkgFilepath)) {
return util.error('package.json not found.');
}
let pkgOj = JSON.parse(fs.readFileSync(pkgFilepath, 'utf8'));
config.getConfig('')
.then(conf => {
if (!conf.latestVersion) {
return util.error('No published versions.');
}
let needVersion = version || conf.latestVersion;
if (!force) { // 非强制安装,检查版本
if (util.compareVersion(needVersion, pkgOj.version) <= 0) {
return util.error('Local version are less than or equal to need version, Stoped.');
}
}
util.info(`Get version ${needVersion} succeed. Downloading...`);
let packagePath = `${conf.newkit2PackageAddress}/${needVersion}.zip`;
util.downloadFile(packagePath)
.then(downloadFolder => {
util.info('Downloading zip package succeed. Unzip...');
util.unzip(downloadFolder, cwd)
.then(folderPath => {
util.info(`Init successfully. Current vesion ${conf.latestVersion}`);
util.info(`Try to install dependencies...`);
exec('npm i --registry http://10.16.75.27:7001/');
}).catch(util.error);
}).catch(util.error);
});
};