diff --git a/lib/streamer.js b/lib/streamer.js index 2fecb21..b54efe2 100755 --- a/lib/streamer.js +++ b/lib/streamer.js @@ -1,18 +1,18 @@ #!/usr/bin/env node -const stream = require('stream'); -const got = require('got'); -const ProgressBar = require('progress'); -const {promisify} = require('util'); +import stream from 'stream'; +import got from 'got'; +import ProgressBar from 'progress'; +import { promisify } from 'util'; const pipeline = promisify(stream.pipeline); // colours -const chalk = require('chalk'); +import chalk from 'chalk'; + const red = chalk.bold.red; -const orange = chalk.keyword('orange'); const green = chalk.green; const blue = chalk.blueBright; -module.exports = class vmwClient { +export default class vmwClient { constructor(options) { this.options = options; // setup stuff @@ -35,46 +35,15 @@ module.exports = class vmwClient { .on('response', (response) => { //console.log('FIRST RESPONSE'); }) - .on('downloadProgress', (progress) => { - //let rate = Math.round(bar.curr / ((new Date - bar.start) / 1000) / 1000); - if(progress.total) { // handle no content-header - if(typeof(bar) == 'undefined') { - let totalBytes = progress.total; - let total = parseInt(totalBytes / 1024 * 100) / 100; // get KB - while(total > 99.99) { // scale up - label++; - total = parseInt(total / 1024 * 100) / 100; - } - bar = new ProgressBar(blue(fileName) + ' [' + green(':bar') + '] :percent | :etas | :curr/' + pad(total) + ' ' + labels[label], { - complete: '\u25A0', - head: '\u25A0', - incomplete: ' ', - width: 50, - renderThrottle: 500, - total: totalBytes - }); - } else { - let chunk = progress.transferred - prevTrans; - prevTrans = progress.transferred; - current = progress.transferred; - for(let i = 0; i <= label; i++) { - current = parseInt(current / 1024 * 100) / 100; - } - bar.tick(chunk, { - 'curr': pad(current) - }); - } - } + .on('downloadProgress', (progress) => {console.log(progress); }), dstStream .on('finish', () => { - if(!bar.complete) { - bar.update(1); - } + //console.log('FINISHS RESPONSE'); }) ); } -} +}; // pad/truncate zeros to left and right of number function pad(num) { @@ -86,4 +55,4 @@ function pad(num) { } return left + '.' + m[2]; } -}; +} diff --git a/lib/vmw.api.js b/lib/vmw.api.js index e523d1d..a6570f3 100755 --- a/lib/vmw.api.js +++ b/lib/vmw.api.js @@ -1,13 +1,15 @@ #!/usr/bin/env node 'use strict'; -const vmwClient = require('vmw-sdk'); -const streamer = require('./streamer'); -const fs = require('fs'); -const CookieFileStore = require('tough-cookie-file-store').FileCookieStore; -const {CookieJar} = require('tough-cookie'); +import vmwClient from 'vmw-sdk'; +import streamer from './streamer.js'; +import fs from 'fs'; +import { FileCookieStore as CookieFileStore } from 'tough-cookie-file-store'; +import { CookieJar } from 'tough-cookie'; + //import pMap from 'p-map'; // not set up yet for ESM -const pMap = require('p-map'); -const storage = require('node-persist'); +import pMap from 'p-map'; + +import storage from 'node-persist'; // provides a higher order interface on top of vmw.sdk.js // handles auth/session persistence @@ -19,13 +21,13 @@ const storage = require('node-persist'); // ignore self-signed certificate //process.env.NODE_TLS_REJECT_UNAUTHORIZED = 0; // colours -const chalk = require('chalk'); +import chalk from 'chalk'; + const red = chalk.bold.red; -const orange = chalk.keyword('orange'); const green = chalk.green; const blue = chalk.blueBright; -module.exports = class vmwApi { +export default class vmwApi { constructor(options = {}) { this.username = options.username; this.password = options.password; @@ -64,6 +66,7 @@ module.exports = class vmwApi { }); }), {concurrency: 8})).flat(1); fs.writeFileSync(this.statedir + '/fileList.json', JSON.stringify(data, null, "\t"), 'utf8'); + console.log(data) return data; } catch(error) { throw new Error(error.message); @@ -91,7 +94,7 @@ module.exports = class vmwApi { // attempt accountInfo try { if(fs.existsSync(this.statedir + '/headers.json')) { - let headers = require(this.statedir + '/headers.json'); + let headers = JSON.parse(fs.readFileSync(this.statedir + '/headers.json', 'utf8')); this.client.client = this.client.base.extend({ prefixUrl: 'https://customerconnect.vmware.com/', headers @@ -246,7 +249,7 @@ module.exports = class vmwApi { // load fileList and retrieve file details if(fs.existsSync(this.statedir + '/fileList.json')) { - let data = require(this.statedir + '/fileList.json'); + let data = JSON.parse(fs.readFileSync(this.statedir + '/fileList.json', 'utf8')); let details = data.filter((file) => { return (file.fileName == fileName); })[0]; @@ -281,4 +284,4 @@ module.exports = class vmwApi { throw new Error('[' + red('ERROR') + ']: No files cached, please use ' + blue('ls ') + ' to view'); } } -} +}; diff --git a/lib/vmw.cli.js b/lib/vmw.cli.js index 78ee068..b6c8529 100755 --- a/lib/vmw.cli.js +++ b/lib/vmw.cli.js @@ -1,10 +1,11 @@ #!/usr/bin/env node 'use strict'; -const vmwClient = require('./vmw.api'); -const streamer = require('./streamer'); -const fs = require('fs'); -const xtable = require('./xtable'); -const md5File = require('md5-file'); + +import vmwClient from './vmw.api.js'; +import streamer from './streamer.js'; +import fs from 'fs'; +import md5File from 'md5-file'; +import path from 'path'; /* Module Purpose To provide a cli interface for the vmw-api module. @@ -21,7 +22,7 @@ Handle client IO unrelated to the VMW-API interface var username = process.env.VMWUSER; var password = process.env.VMWPASS; var filesdir = process.cwd(); -var statedir = __dirname; +var statedir = process.cwd(); if(process.env.VMWFILESDIR) { filesdir = process.env.VMWFILESDIR; } @@ -30,9 +31,9 @@ if(process.env.VMWSTATEDIR) { } // colours -const chalk = require('chalk'); +import chalk from 'chalk'; + const red = chalk.bold.red; -const orange = chalk.keyword('orange'); const green = chalk.green; const blue = chalk.blueBright; @@ -84,21 +85,7 @@ if(process.argv[1].match(/vmw.cli/g)) { // main async function main(category, version, type) { - client.main(category, version, type).then((data) => { - let table = new xtable({data}); - table.run(); - table.out([ - 'fileName', - 'fileType', - 'version', - 'releaseDate', - 'fileSize', - 'canDownload' - ]); - }).catch((error) => { - //console.log('[CLI-ERROR]: ' + error.message); - console.log(error.message); - }); + client.main(category, version, type) } // list @@ -132,16 +119,7 @@ async function cmdPath(string) { } else { //console.log('Training Slash!! - call VERSIONS: ' + path[0]); let result = await client.getProductVersions(path[0]); - let table = new xtable({ - data: result.versions - }); - table.run(); - table.out([ - 'id', - 'name', - 'slugUrl', - 'isSelected' - ]); + console.log(result.versions) } } else if(path.length == 3) { if(path[2].length > 0) { @@ -156,9 +134,7 @@ async function cmdPath(string) { {id: 'CUSTOM_ISO'}, {id: 'ADDONS'} ]; - let table = new xtable({data}); - table.run(); - table.out(['id']); + console.log(data) } } else { console.log('INVALID PATH'); @@ -183,17 +159,8 @@ async function cmdGet(fileName) { // broken, fix async function cmdFile() { // load fileList and retrieve file details if(fs.existsSync(statedir + '/fileList.json')) { - let data = require(statedir + '/fileList.json'); - let table = new xtable({data}); - table.run(); - table.out([ - 'fileName', - 'fileType', - 'version', - 'releaseDate', - 'fileSize', - 'canDownload' - ]); + let data = JSON.parse(fs.readFileSync(statedir + '/fileList.json', 'utf8')); + console.log(data) } } diff --git a/lib/xcell.js b/lib/xcell.js deleted file mode 100755 index 3396ae2..0000000 --- a/lib/xcell.js +++ /dev/null @@ -1,48 +0,0 @@ -#!/usr/bin/env node -const self = xcell.prototype; - -// constructor -function xcell(opts) { - this.data = opts.data.map(a => ({...a})); // deep copy - this.view = []; - this.maps = []; -} -module.exports = xcell; - -// add map -self.addMap = function(field, rule) { - this.maps.push({ - 'field': field, - 'rule': rule - }); - return this; -}; - -// add filter -self.addFilter = function(f) { - return this.addMap(f.field, (val) => { - if(typeof(val) === 'string' && val.match(new RegExp(f.value, 'i'))) { - return val; - } else { - return null; - } - }); -}; - -// check type and translate record -self.modify = function(m, item) { - if(typeof(m.rule) === 'function') { - return (item[m.field] = m.rule(item[m.field])); - } else { - return (item[m.field] = m.rule); - } -}; - -// filter and transform current view -self.run = function(data = this.data) { - return this.view = data.filter((item) => { - return this.maps.every((m) => { - return this.modify(m, item); - }); - }); -}; diff --git a/lib/xtable.js b/lib/xtable.js deleted file mode 100755 index 2ee75bd..0000000 --- a/lib/xtable.js +++ /dev/null @@ -1,167 +0,0 @@ -#!/usr/bin/env node -var xcell = require('./xcell.js'); -var self = xtable.prototype; - -// colours -const chalk = require('chalk'); -const red = chalk.bold.red; -const orange = chalk.keyword('orange'); -const green = chalk.green; -const blue = chalk.blueBright; -const cyan = chalk.cyan; - -// rework as class - -// constructor -function xtable(opts) { // Object.assign? - this.cache = {}; - this.view = []; - this.header = opts.header; - this.cell = new xcell({ - data: opts.data - }); - this.data = this.cell.data; - this.column = opts.column; - this.filters = []; -} -module.exports = xtable; - -self.out = function(cols) { - if(this.cell.data.length > 0) { - if(!this.header) { // learn cols from first record - this.header = []; - Object.keys(this.cell.data[0]).forEach((item) => { - this.header.push(item); - }); - } - if(!cols) { - cols = this.header; - } - let col = {}; - cols.forEach((item) => { - col[item] = '<' + item + '>'; - }); - this.runColWidth(col); - - // scan widths - if(this.view.length == 0) { - this.run(); - } - this.view.forEach((item) => { - this.runColWidth(item); - }); - - // build header string - let headString = ''; - let dashString = ''; - let spacer = ' '; - let gap = spacer.repeat(2); - cols.forEach((item) => { - if(headString.length > 0) { - headString += gap; - dashString += gap; - } - headString += '<' + item + '>' + spacer.repeat(this.cache[item] - (item.length + 2)); - dashString += '-'.repeat(this.cache[item]); - }); - console.log(blue(headString)); - //console.log(dashString); - - // build data string - this.view.forEach((item) => { - let dataString = ''; - cols.forEach((col) => { - if(dataString.length > 0) { - dataString += gap; - } - if(item[col]) { - dataString += item[col] + spacer.repeat(this.cache[col] - self.getLength(item[col])); - } else { - dataString += spacer.repeat(this.cache[col]); - } - }); - console.log(dataString); - }); - - // display filter - //console.error(blue('[ ' + this.view.length + '/' + this.data.length + ' ] entries - filter [ ' + this.filterString() + ' ]')); - //console.error(blue('[ ' + green(this.view.length + '/' + this.data.length) + ' ] entries - filter [ ' + green(this.filterString()) + ' ]')); - } -} - -// determine maximum string length for column -self.runColWidth = function(item) { - for(let key in item) { // per record - if(value = item[key]) { - let length = self.getLength(value); - if(!this.cache[key] || this.cache[key] < length) { - this.cache[key] = length; - } - } - } -}; - -// determine maximum string length for column -self.getLength = function(value) { - switch(typeof(value)) { - case "number": - return value.toString().length; - break; - case "string": - return value.length; - break; - } -}; - -// stringify this.filters[]; -self.filterString = function() { - let string = ''; - let comma = ''; - this.filters.map((filter) => { - string += comma + filter.field + ':' + filter.value; - comma = ','; - }); - return string; -}; - -// parse and construct filter objects -self.buildFilters = function(string) { - let filters = []; - var rgxFilter = new RegExp('([^,:]+):([^,:]*)', 'g'); - while(m = rgxFilter.exec(string)) { - let val1 = m[1]; - let val2 = m[2]; - filters.push({ - field: val1, - value: val2 - }); - } - if(filters.length == 0) { - if(!string) string = ''; - filters.push({ - field: 'name', - value: string - }); - } - filters.forEach((filter) => { - this.addFilter(filter); - }); -}; - -// add map -self.addMap = function(field, mapper) { - this.cell.addMap(field, mapper); - return this; -}; - -// add filter -self.addFilter = function(filter) { - this.filters.push(filter); - this.cell.addFilter(filter); - return this; -}; - -// filter and transform current view -self.run = function(data = this.data) { - this.view = this.cell.run(data); -}; diff --git a/package.json b/package.json index 3713b36..97b4aea 100644 --- a/package.json +++ b/package.json @@ -10,6 +10,7 @@ "engines": { "node": ">=8" }, + "type": "module", "bin": { "vmw-cli": "./lib/vmw.cli.js" }, @@ -30,7 +31,7 @@ "got": "*", "md5-file": "*", "node-persist": "*", - "p-map": "=4.0.0", + "p-map": "*", "progress": "*", "tough-cookie": "*", "tough-cookie-file-store": "*"