Skip to content

Commit

Permalink
feat(cli): make a cli bundle in dist/
Browse files Browse the repository at this point in the history
  • Loading branch information
Benjamin Reed committed Aug 7, 2017
1 parent 0880cf7 commit 02298d7
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 62 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -34,5 +34,6 @@ profile.cov
grafana
.notouch

dist/cli.*
dist/opennms.*
dist/docs
7 changes: 5 additions & 2 deletions package.json
Expand Up @@ -3,6 +3,9 @@
"version": "0.1.0-alpha.4",
"description": "Client API for the OpenNMS network monitoring platform",
"main": "dist/opennms.js",
"bin": {
"opennms": "dist/cli.node.js"
},
"author": "Benjamin Reed",
"license": "MIT",
"bugs": {
Expand Down Expand Up @@ -33,6 +36,7 @@
"babel-preset-es2015": "^6.24.1",
"babel-preset-latest": "^6.24.1",
"chai": "3.5.0",
"child_process": "^1.0.2",
"conventional-changelog-lint": "^1.1.9",
"husky": "^0.13.3",
"jest": "^20.0.3",
Expand Down Expand Up @@ -70,8 +74,7 @@
},
"dependencies": {
"axios": "^0.16.1",
"child_process": "^1.0.2",
"cliff": "^0.1.10",
"cli-table": "^0.3.1",
"commander": "^2.9.0",
"fs": "^0.0.1-security",
"ip-address": "^5.8.8",
Expand Down
49 changes: 41 additions & 8 deletions src/CLI.ts
Expand Up @@ -15,7 +15,7 @@ const CLI = () => {
const catCLI = new Category('cli', catRoot);

// tslint:disable
const cliff = require('cliff');
const Table = require('cli-table');
const colors = require('colors');
const fs = require('fs');
const path = require('path');
Expand All @@ -25,6 +25,32 @@ const CLI = () => {
const homedir = process.env[(process.platform === 'win32') ? 'USERPROFILE' : 'HOME'];
const defaultConfigFile = path.join(homedir, '.opennms-cli.config.json');

const tableFormat = {
head: [],
/* tslint:disable:object-literal-sort-keys */
chars: {
'middle': ' ',
'top': '',
'top-mid': '',
'top-left': '',
'top-right': '',
'left': '',
'left-mid': '',
'mid': '',
'mid-mid': '',
'right': '',
'right-mid': '',
'bottom': '',
'bottom-mid': '',
'bottom-left': '',
'bottom-right': '',
},
style: {
'padding-left': 0,
'padding-right': 0,
},
};

const readConfig = () => {
const configfile = program.config || defaultConfigFile;
let config;
Expand Down Expand Up @@ -122,14 +148,14 @@ const CLI = () => {
console.log('');

const caps = res.capabilities();
const rows = [];
const t = new Table(tableFormat);
for (const cap in caps) {
if (cap === 'type') {
continue;
}
rows.push([startCase(cap) + ':', caps[cap]]);
t.push([startCase(cap) + ':', caps[cap]]);
}
console.log(cliff.stringifyRows(rows));
console.log(t.toString());
console.log('');

return res;
Expand Down Expand Up @@ -160,9 +186,9 @@ const CLI = () => {

let logMessage = '';
if (alarm.logMessage) {
logMessage = alarm.logMessage.trim();
logMessage = alarm.logMessage.replace('[\r\n].*$').trim();
if (logMessage.length > logMessageLength) {
logMessage = logMessage.slice(0, logMessageLength) + '…';
logMessage = logMessage.slice(0, logMessageLength).trim() + '…';
}
}

Expand Down Expand Up @@ -199,8 +225,15 @@ const CLI = () => {
}

return dao.find(filter).then((alarms) => {
const headers = ['id', 'severity', 'node', 'count', 'time', 'log'];
console.log(cliff.stringifyObjectRows(formatAlarms(alarms), headers, ['red']));
const format = Object.assign({}, tableFormat);
format.head = [ 'ID', 'Severity', 'Node', 'Count', 'Time', 'Log' ];
const t = new Table(format);
const formatted = formatAlarms(alarms);
for (const alarm of formatted) {
t.push([alarm.id, alarm.severity, alarm.node, alarm.count, alarm.time, alarm.log]);
}
console.log(t.toString());
console.log('');
});
}).catch((err) => {
return handleError('Alarm list failed', err);
Expand Down
22 changes: 16 additions & 6 deletions webpack.config.js
Expand Up @@ -13,7 +13,7 @@ var justDocs = argv.env === 'docs';
var libraryName = 'opennms';

var variants = {
web: [ true, false ],
target: [ 'web', 'node' ],
};

if (isProduction) {
Expand All @@ -22,7 +22,7 @@ if (isProduction) {

if (justDocs) {
variants = {
web: [true],
target: ['node'],
docs: [true],
};
}
Expand Down Expand Up @@ -87,16 +87,26 @@ function createConfig(options) {
var myconf = clonedeep(config);
myconf.output.filename = '[name]';
var defs = {
'IS_WEB': options.web,
'IS_WEB': options.target === 'web',
'IS_PRODUCTION': options.production,
};

if (options.web) {
if (options.target === 'web') {
myconf.target = 'web';
} else {
myconf.target = 'node';
myconf.node = { process: false };
}

if (options.target === 'node') {
myconf.output.filename += '.node';
myconf.entry.cli = __dirname + '/src/CLI.ts';
myconf.plugins.push(new webpack.BannerPlugin({
banner: '#!/usr/bin/env node',
raw: true,
entryOnly: true,
include: /cli/i,
}));
}

if (options.production) {
Expand All @@ -119,7 +129,7 @@ function createConfig(options) {
myconf.plugins.push(new webpack.ProvidePlugin({X2JS: 'x2js'}));

// build docs either on a dedicated doc build, or during production node.js build
var buildDocs = justDocs || (options.production && !options.web);
var buildDocs = !!(justDocs || (options.production && options.target === 'node'));
if (buildDocs) {
// generate documentation
var tsconfig = require('./tsconfig.json');
Expand All @@ -133,7 +143,7 @@ function createConfig(options) {

myconf.output.filename += '.js';

console.log('Building variant: web=' + (!!options.web) + ', production=' + (!!options.production) + ', docs=' + buildDocs);
console.log('Building variant: target=' + options.target + ', production=' + (!!options.production) + ', docs=' + buildDocs);

return myconf;
}
Expand Down
58 changes: 12 additions & 46 deletions yarn.lock
Expand Up @@ -277,10 +277,6 @@ async-each@^1.0.0:
version "1.0.1"
resolved "https://registry.yarnpkg.com/async-each/-/async-each-1.0.1.tgz#19d386a1d9edc6e7c1c85d388aedbcc56d33602d"

async@0.2.x:
version "0.2.10"
resolved "https://registry.yarnpkg.com/async/-/async-0.2.10.tgz#b6bbe0b0674b9d719708ca38de8c237cb526c3d1"

async@^1.4.0, async@^1.5.2:
version "1.5.2"
resolved "https://registry.yarnpkg.com/async/-/async-1.5.2.tgz#ec6a61ae56480c0c3cb241c95618e20892f9672a"
Expand Down Expand Up @@ -1178,13 +1174,11 @@ cipher-base@^1.0.0, cipher-base@^1.0.1, cipher-base@^1.0.3:
inherits "^2.0.1"
safe-buffer "^5.0.1"

cliff@^0.1.10:
version "0.1.10"
resolved "https://registry.yarnpkg.com/cliff/-/cliff-0.1.10.tgz#53be33ea9f59bec85609ee300ac4207603e52013"
cli-table@^0.3.1:
version "0.3.1"
resolved "https://registry.yarnpkg.com/cli-table/-/cli-table-0.3.1.tgz#f53b05266a8b1a0b934b3d0821e6e2dc5914ae23"
dependencies:
colors "~1.0.3"
eyes "~0.1.8"
winston "0.8.x"
colors "1.0.3"

cliui@^2.1.0:
version "2.1.0"
Expand Down Expand Up @@ -1248,18 +1242,14 @@ color-name@^1.1.1:
version "1.1.3"
resolved "https://registry.yarnpkg.com/color-name/-/color-name-1.1.3.tgz#a7d0558bd89c42f795dd42328f740831ca53bc25"

colors@0.6.x:
version "0.6.2"
resolved "https://registry.yarnpkg.com/colors/-/colors-0.6.2.tgz#2423fe6678ac0c5dae8852e5d0e5be08c997abcc"
colors@1.0.3:
version "1.0.3"
resolved "https://registry.yarnpkg.com/colors/-/colors-1.0.3.tgz#0433f44d809680fdeb60ed260f1b0c262e82a40b"

colors@^1.1.2:
version "1.1.2"
resolved "https://registry.yarnpkg.com/colors/-/colors-1.1.2.tgz#168a4701756b6a7f51a12ce0c97bfa28c084ed63"

colors@~1.0.3:
version "1.0.3"
resolved "https://registry.yarnpkg.com/colors/-/colors-1.0.3.tgz#0433f44d809680fdeb60ed260f1b0c262e82a40b"

combined-stream@^1.0.5, combined-stream@~1.0.5:
version "1.0.5"
resolved "https://registry.yarnpkg.com/combined-stream/-/combined-stream-1.0.5.tgz#938370a57b4a51dea2c77c15d5c5fdf895164009"
Expand Down Expand Up @@ -1480,10 +1470,6 @@ currently-unhandled@^0.4.1:
dependencies:
array-find-index "^1.0.1"

cycle@1.0.x:
version "1.0.3"
resolved "https://registry.yarnpkg.com/cycle/-/cycle-1.0.3.tgz#21e80b2be8580f98b468f379430662b046c34ad2"

dargs@^4.0.1:
version "4.1.0"
resolved "https://registry.yarnpkg.com/dargs/-/dargs-4.1.0.tgz#03a9dbb4b5c2f139bf14ae53f0b8a2a6a86f4e17"
Expand Down Expand Up @@ -1732,10 +1718,6 @@ extsprintf@1.0.2:
version "1.0.2"
resolved "https://registry.yarnpkg.com/extsprintf/-/extsprintf-1.0.2.tgz#e1080e0658e300b06294990cc70e1502235fd550"

eyes@0.1.x, eyes@~0.1.8:
version "0.1.8"
resolved "https://registry.yarnpkg.com/eyes/-/eyes-0.1.8.tgz#62cf120234c683785d902348a800ef3e0cc20bc0"

fancy-log@^1.1.0:
version "1.3.0"
resolved "https://registry.yarnpkg.com/fancy-log/-/fancy-log-1.3.0.tgz#45be17d02bb9917d60ccffd4995c999e6c8c9948"
Expand Down Expand Up @@ -2394,7 +2376,7 @@ isobject@^2.0.0:
dependencies:
isarray "1.0.0"

isstream@0.1.x, isstream@~0.1.2:
isstream@~0.1.2:
version "0.1.2"
resolved "https://registry.yarnpkg.com/isstream/-/isstream-0.1.2.tgz#47e63f7af55afa6f92e1500e690eb8b8529c099a"

Expand Down Expand Up @@ -3316,6 +3298,10 @@ object.omit@^2.0.0:
for-own "^0.1.4"
is-extendable "^0.1.1"

octal-number-loader@^0.1.4:
version "0.1.4"
resolved "https://registry.yarnpkg.com/octal-number-loader/-/octal-number-loader-0.1.4.tgz#74ba032bf4524db23d096cc2063cb8710807317b"

once@^1.3.0, once@^1.3.3, once@^1.4.0:
version "1.4.0"
resolved "https://registry.yarnpkg.com/once/-/once-1.4.0.tgz#583b1aa775961d4b113ac17d9c50baef9dd76bd1"
Expand Down Expand Up @@ -3526,10 +3512,6 @@ pkg-dir@^2.0.0:
dependencies:
find-up "^2.1.0"

pkginfo@0.3.x:
version "0.3.1"
resolved "https://registry.yarnpkg.com/pkginfo/-/pkginfo-0.3.1.tgz#5b29f6a81f70717142e09e765bbeab97b4f81e21"

pluralize@^1.2.1:
version "1.2.1"
resolved "https://registry.yarnpkg.com/pluralize/-/pluralize-1.2.1.tgz#d1a21483fd22bb41e58a12fa3421823140897c45"
Expand Down Expand Up @@ -4024,10 +4006,6 @@ stack-generator@^1.0.7:
dependencies:
stackframe "^1.0.2"

stack-trace@0.0.x:
version "0.0.10"
resolved "https://registry.yarnpkg.com/stack-trace/-/stack-trace-0.0.10.tgz#547c70b347e8d32b4e108ea1a2a159e5fdde19c0"

stackframe@^0.3.1, stackframe@~0.3:
version "0.3.1"
resolved "https://registry.yarnpkg.com/stackframe/-/stackframe-0.3.1.tgz#33aa84f1177a5548c8935533cbfeb3420975f5a4"
Expand Down Expand Up @@ -4693,18 +4671,6 @@ window-size@0.1.0:
version "0.1.0"
resolved "https://registry.yarnpkg.com/window-size/-/window-size-0.1.0.tgz#5438cd2ea93b202efa3a19fe8887aee7c94f9c9d"

winston@0.8.x:
version "0.8.3"
resolved "https://registry.yarnpkg.com/winston/-/winston-0.8.3.tgz#64b6abf4cd01adcaefd5009393b1d8e8bec19db0"
dependencies:
async "0.2.x"
colors "0.6.x"
cycle "1.0.x"
eyes "0.1.x"
isstream "0.1.x"
pkginfo "0.3.x"
stack-trace "0.0.x"

wordwrap@0.0.2:
version "0.0.2"
resolved "https://registry.yarnpkg.com/wordwrap/-/wordwrap-0.0.2.tgz#b79669bb42ecb409f83d583cad52ca17eaa1643f"
Expand Down

0 comments on commit 02298d7

Please sign in to comment.