@sencha/cmd
Version:
Productivity and performance optimization tool for building applications with Sencha Ext JS
149 lines (136 loc) • 4.39 kB
JavaScript
const pkg = require('./package.json');
const platform = {"darwin": "macos", "win32": "windows", "win64": "windows", "linux": "linux" }[process.platform] || process.platform;
const arch = process.arch || (process.env.hasOwnProperty('PROCESSOR_ARCHITEW6432') ? "x64" : "x32");
const which = require('which');
const path = require('path');
let javaFound = false;
var greenbold = `\x1b[32m\x1b[1m`
var end = `\x1b[0m`
var prefix = ``
if (require('os').platform() == 'darwin') {
prefix = `ℹ 「ext」:`
}
else {
prefix = `i [ext]:`
}
var app =(`${greenbold}${prefix}${end} platform-install:`)
function boldRed(s) {
var boldredcolor = `\x1b[31m\x1b[1m`
var endmarker = `\x1b[0m`
return (`${boldredcolor}${s}${endmarker}`)
}
let getPackageName = () => {
let name = `${pkg.name}-${platform}`;
if (!javaFound) {
if (platform === "windows") {
name += `-${arch === "x64" ? "64" : "32"}`;
}
name += `-jre`;
} else {
if (platform === "linux") {
name += `-${arch === "x64" ? "64" : "32" }`;
}
}
return name;
}
let validateForJava = () => {
if (!javaFound && platform === "linux") {
console.error('A JRE is required to download and run Sencha Cmd in linux.');
process.exit(126);
}
}
let proceedWithInstall = () => {
// console.log(`${app} ${pkg.name} v${pkg.sencha.version} post-install`)
console.log(`${app} ${pkg.name} v${pkg.version} installed`)
let pkgName = getPackageName()
// console.log(`${app} Sencha Cmd package name: ${pkgName}`)
// var npmConfigLogLevel = process.env.npm_config_loglevel
// console.log(`${app} npm config loglevel: ${npmConfigLogLevel}`)
// let loglevel = ["silent", "error", "warn", "http", "info", "verbose", "silly"].indexOf(process.env.npm_config_loglevel);
// if (loglevel > -1) {
// args.push('--loglevel');
// args.push(process.env.npm_config_loglevel);
// }
// console.log(`${app} args: ${args}`)
validateForJava();
var package = `${pkgName}@${pkg.version}`
var command = `npm${/^win/.test(require('os').platform()) ? ".cmd" : ""}`
var args = []
if (process.env.EXTGEN_VERBOSE == 'true') {
args = ['install',package]
}
else {
args = ['install','-s',package]
// if (require('os').platform() == 'win32') {
// args = ['install','-s','>','NUL',package]
// }
// else {
// args = ['install','-s','>','/dev/null',package]
// }
}
let options = {cwd: path.join(__dirname), stdio: 'inherit', encoding: 'utf-8'}
if (process.env.EXTGEN_VERBOSE == 'true') {
console.log(`${app} npm ${args.toString().replace(/,/g, " ")} started`)
}
let child = require('child_process').spawn(
command,
args,
options
)
child.on('close', (code) => {
if (code===0) {
console.log(`${app} npm ${args.toString().replace(/,/g, " ")} installed`)
require(`${pkgName}/install.js`)(__dirname);
}
else {
if (process.env.EXTGEN_VERBOSE == 'true') {
console.log(`${app} ${boldRed('[ERR]')} Closed with code ${code}`)
}
}
})
child.on('error', (error) => {
var s = error.message ? error.message : error
s = s.replace(/(\r\n\t|\n|\r\t)/gm,"");
console.log(`${app} ${boldRed('[ERR]')} ${s}`)
})
child.on('data', (error) => {
var s = error.message ? error.message : error
s = s.replace(/(\r\n\t|\n|\r\t)/gm,"");
console.log(`${app} ${boldRed('[DATA]')} ${s}`)
})
if (child.stdout) {
child.stdout.on('data', (stdout) => {
var s = stdout.toString()
s = s.replace(/(\r\n\t|\n|\r\t)/gm,"");
console.log(`${app} ${s}`)
})
}
else {
// var s = `no stdout`
// console.log(`${app} ${boldRed("[ERR]")} ${s}`)
}
if (child.stderr) {
child.stderr.on('data', (stderr) => {
var s = stderr.toString()
s = s.replace(/(\r\n\t|\n|\r\t)/gm,"");
console.log(`${app} ${boldRed('[ERR]')} ${s}`)
})
}
else {
// var s = `no stderr`
// console.log(`${app} ${boldRed("[ERR]")} ${s}`)
}
}
try {
which('java', (err, path) => {
javaFound = !err && path //to deal with java 9 and above issue
//javaFound = false
//console.log(`${app} Found java at: ${javaFound}`)
proceedWithInstall()
})
} catch (error) {
var s = error.message ? error.message : error
s = s.replace(/(\r\n\t|\n|\r\t)/gm,"");
console.log(`${app} ${boldRed('[ERR]')} ${s}`)
proceedWithInstall()
}