UNPKG

@qbyco/tjs-cli

Version:

TrafaletJS CLI Tool

310 lines (232 loc) 6.67 kB
const http = require('http'); const fs = require('fs'); const path = require('path'); const mkdirp = require('mkdirp'); const CLICommand = require("../../lib/cli/Command/CLICommand"); const utils = require("../../lib/helpers/general"); const ServerAPI = { address: 'dist.trafaletjs.io', headers: { 'Content-Type': 'application/json' }, uri: { download: "/files/download", list: "/files/list" }, }; /** * @module tasks/commands/Manager */ class Manager extends CLICommand { constructor (lib) { super('manager'); this.lib = lib; this.options = { path: './vendor', cwd: './', all: false }; this.lib.events.on("manager:install", (params) => { this.installAction.apply(this, params); }) } /** * @method installAction * @param {String} version * @param {Object} options */ installAction (version, options) { let configFile = 'trafaletjs.json', config, ver, output = this.lib.chalk.white('\nTrafaletJS CLI ----------------\n'); if (undefined === options) { output += this.lib.chalk.red('Please specify a trafaletjs version'); return console.log(output + '\n'); } // Override properties Object.assign(this.options, options); config = path.join(this.options.cwd, configFile); ver = utils.parseVersion(version); let response = function (res) { res = JSON.parse(res); if(res.error) { output += this.lib.chalk.red(res.error); return console.log(output); } Object.assign(this.options, res); let configData = JSON.parse(fs.readFileSync(config).toString()); this.serviceGetVersion({ path: res.contents, opts: this.options }, () => { configData.tjs.version = res.version; configData.tjs.path = this.options.path; fs.writeFileSync(config, JSON.stringify(configData, null, ' ')); console.log(this.lib.chalk.green(' - Download complete! Version: ' + configData.tjs.version)); this.lib.events.emit('app:finishCommand', configData.tjs); }); }; this.serviceCheckVersion({ path: ServerAPI.uri.download + '/' + ver }, response); } /** * @method updateAction * @param {String} version * @param {Object} options */ updateAction (version, options) { let config = require(path.join(this.lib.cwd, 'trafaletjs.json')), currentVersionPath = path.join(this.lib.cwd, config.tjs.path, 'trafaletjs-' + config.tjs.version + '.js'); let response = (res) => { }; console.log('Not implemented yet!!\n'); } /** * @method lsAction * @param {Object} options * @return {String|*} */ lsAction (options) { // Override properties Object.assign(this.options, options); let config = 'trafaletjs.json', output = this.lib.chalk.white('\nTrafaletJS CLI ----------------\n'); if(!fs.existsSync(config)) { console.error(this.lib.chalk.red(config + ' file was not found in path ' + this.lib.cwd)); return false; } config = JSON.parse(fs.readFileSync(config).toString()); output += this.lib.chalk.green.bold('[Local install]'); if("" === config.tjs.version) { output += this.lib.chalk.yellow('\n No local trafaletjs distribution installed'); output += this.lib.chalk.yellow('\n Run: tjs-cli manager latest or tjs-cli manager vxx.xx.xx'); } else { output += this.lib.chalk.green.bold('\n v' + config.tjs.version); } /** * Parse response and output * @param res */ let response = (res) => { if(res) { let remote = JSON.parse(res); output += this.lib.chalk.cyan.bold('\n\n[Available versions]'); remote.versions.forEach((version) => { if (false === /-beta/.test(version) && false === /-alpha/.test(version)) { output += this.lib.chalk.cyan.bold('\n ' + version); } }); } return console.log(output); }; if(this.options.all) { this.serviceGetVersions(response); } else { response(null); } } /** * @method lsBetaAction */ lsBetaAction () { console.log('List beta // Not Implemented'); } /** * @method lsAlphaAction */ lsAlphaAction () { console.log('List alpha // Not Implemented'); } /** * @method serviceGetVersions * @param {Function} cb */ serviceGetVersions (cb) { let req = http.request({ hostname: ServerAPI.address, port: 80, method: 'GET', path: ServerAPI.uri.list, headers: ServerAPI.headers }, (res) => { res.setEncoding('utf8'); res.on('data', cb.bind(this)); }); req.on('error', (e) => { console.log(`problem with request: ${e.message}`); }); req.end(); } /** * @method serviceCheckVersion * @param {Object} options * @param {Function} cb */ serviceCheckVersion (options, cb) { let req = http.request({ hostname: ServerAPI.address, port: 80, method: 'GET', path: options.path, headers: ServerAPI.headers }, (res) => { res.setEncoding('utf8'); res.on('data', cb.bind(this)); }); req.on('error', (e) => { console.log(`problem with request: ${e.message}`); }); req.end(); } /** * @method serviceGetVersion * @param {Object} options * @param {Function} cb */ serviceGetVersion (options, cb) { let appPath = path.join(options.opts.cwd, options.opts.path); if (!fs.existsSync(appPath)) { mkdirp.sync(appPath); } let installPath = path.join(appPath, options.opts.filename); let localCopy = fs.createWriteStream(installPath); let req = http.request({ hostname: ServerAPI.address, port: 80, method: 'GET', path: '/' + options.path, headers: ServerAPI.headers }, (res) => { res.setEncoding('utf8'); res.on('data', (chunk) => { localCopy.write(chunk); }); res.on('end', cb.bind(this)); }); req.on('error', (e) => { console.log(`problem with request: ${e.message}`); }); req.end(); } /** * @method help * @return {Map} */ help () { let help = new Map(); help.set('ls [--all]', 'List Trafalet JS versions'); help.set('install [version]', 'Install Trafalet JS'); return help; } } module.exports = { init: function (lib) { lib.cli.getRepository().addCommand(new Manager(lib)); }, run: function (lib) {} };