From ca743904a1e9731abccc5bc4cf40eba25831090d Mon Sep 17 00:00:00 2001 From: Yegor Bugayenko Date: Tue, 21 Jun 2022 06:35:51 +0300 Subject: [PATCH] #6 --verbose --- package.json | 1 + src/commands/assemble.js | 1 + src/commands/clean.js | 1 + src/commands/compile.js | 1 + src/commands/link.js | 1 + src/commands/register.js | 2 ++ src/commands/transpile.js | 1 + src/eoc.js | 16 ++++++--- src/mvnw.js | 10 +++--- src/tinted-console.js | 74 +++++++++++++++++++++++++++++++++++++++ 10 files changed, 99 insertions(+), 9 deletions(-) create mode 100644 src/tinted-console.js diff --git a/package.json b/package.json index 408cd81..7b48e11 100644 --- a/package.json +++ b/package.json @@ -13,6 +13,7 @@ "eoc": "./src/eoc.js" }, "dependencies": { + "colors": "1.4.0", "commander": "9.2.0", "fast-xml-parser": "4.0.0", "xmlhttprequest": "1.8.0" diff --git a/src/commands/assemble.js b/src/commands/assemble.js index b02c33f..e4a0bb4 100644 --- a/src/commands/assemble.js +++ b/src/commands/assemble.js @@ -33,6 +33,7 @@ const mvnwSync = require('../mvnw'); module.exports = function assemble(opts) { mvnwSync([ 'eo:assemble', + opts.verbose ? '' : '--quiet', `-Deo.targetDir=${path.resolve(opts.target)}`, `-Deo.outputDir=${path.resolve(opts.target, 'classes')}`, `-Deo.foreign=${path.resolve(opts.target, 'eo-foreign.csv')}`, diff --git a/src/commands/clean.js b/src/commands/clean.js index db84023..040f406 100644 --- a/src/commands/clean.js +++ b/src/commands/clean.js @@ -33,4 +33,5 @@ const path = require('path'); module.exports = function clean(opts) { const home = path.resolve(opts.target); fs.rmSync(home, {recursive: true, force: true}); + console.debug('The directory %s deleted', home); }; diff --git a/src/commands/compile.js b/src/commands/compile.js index dc00b8b..bb19e4b 100644 --- a/src/commands/compile.js +++ b/src/commands/compile.js @@ -33,6 +33,7 @@ const path = require('path'); module.exports = function compile(opts) { mvnwSync([ 'compiler:compile', + opts.verbose ? '' : '--quiet', `-Dmaven.compiler.source=1.8`, `-Dmaven.compiler.target=1.8`, `-Deo.targetDir=${path.resolve(opts.target)}`, diff --git a/src/commands/link.js b/src/commands/link.js index 59aaa77..33ce4b4 100644 --- a/src/commands/link.js +++ b/src/commands/link.js @@ -33,6 +33,7 @@ const path = require('path'); module.exports = function link(opts) { mvnwSync([ 'jar:jar', + opts.verbose ? '' : '--quiet', `-Deo.targetDir=${path.resolve(opts.target)}`, ]); }; diff --git a/src/commands/register.js b/src/commands/register.js index 5564233..9566b07 100644 --- a/src/commands/register.js +++ b/src/commands/register.js @@ -33,9 +33,11 @@ const path = require('path'); module.exports = function register(opts) { mvnwSync([ 'eo:register', + opts.verbose ? '' : '--quiet', `-Deo.targetDir=${path.resolve(opts.target)}`, `-Deo.sourcesDir=${path.resolve(opts.sources)}`, `-Deo.foreign=${path.resolve(opts.target, 'eo-foreign.csv')}`, `-Deo.foreignFormat=csv`, ]); + console.info(); }; diff --git a/src/commands/transpile.js b/src/commands/transpile.js index 29a64df..44f5d1d 100644 --- a/src/commands/transpile.js +++ b/src/commands/transpile.js @@ -33,6 +33,7 @@ const path = require('path'); module.exports = function transpile(opts) { mvnwSync([ 'eo:transpile', + opts.verbose ? '' : '--quiet', `-Deo.targetDir=${path.resolve(opts.target)}`, `-Deo.generatedDir=${path.resolve(opts.target, 'generated-sources')}`, `-Deo.foreign=${path.resolve(opts.target, 'eo-foreign.csv')}`, diff --git a/src/eoc.js b/src/eoc.js index 3b073c6..0d6d9a4 100755 --- a/src/eoc.js +++ b/src/eoc.js @@ -23,9 +23,8 @@ * SOFTWARE. */ -const {Command} = require('commander'); -const program = new Command(); - +const {program} = require('commander'); +const tinted = require('./tinted-console'); const audit = require('./commands/audit'); const clean = require('./commands/clean'); const assemble = require('./commands/assemble'); @@ -36,6 +35,11 @@ const link = require('./commands/link'); const dataize = require('./commands/dataize'); const test = require('./commands/test'); +if (process.argv.includes('--verbose')) { + tinted.enable('debug'); + console.debug('Debug output is turned ON'); +} + program .name('eoc') .description('EO command-line toolkit') @@ -44,7 +48,8 @@ program program .option('-s, --sources ', 'directory with .EO sources', '.') .option('-t, --target ', 'directory with all generated files', '.eoc') - .option('--alone', 'just run a single command without dependencies'); + .option('--alone', 'just run a single command without dependencies') + .option('--verbose', 'print debug messages and full output of child processes'); program.command('audit') .description('inspects all packages and reports their status') @@ -135,6 +140,7 @@ program.command('test') try { program.parse(process.argv); } catch (e) { - console.log('eoc failed: "' + e.message + '"'); + console.error(e.message); + console.debug(e.stack); process.exit(1); } diff --git a/src/mvnw.js b/src/mvnw.js index ccaa28e..d35b12e 100644 --- a/src/mvnw.js +++ b/src/mvnw.js @@ -46,9 +46,9 @@ function latest() { } const xml = new XMLParser().parse(xhr.responseText); version = xml.metadata.versioning.release; - console.log('The latest version of ' + repo + ' at ' + url + ' is ' + version); + console.debug('The latest version of %s at %s is %s', repo, url, version); } - console.log('Current version of ' + repo + ' is ' + version); + console.debug('Current version of %s is %s', repo, version); return version; } @@ -59,7 +59,9 @@ function latest() { module.exports = function mvnwSync(args) { const home = path.resolve(__dirname, '../mvnw'); const bin = path.resolve(home, 'mvnw'); - const params = args.concat([ + const params = args.filter(function(t) { + return t != ''; + }).concat([ '-Deo.version=' + latest(), '--errors', '--batch-mode', @@ -67,7 +69,7 @@ module.exports = function mvnwSync(args) { '--fail-fast', ]); const cmd = bin + ' ' + params.join(' '); - console.log('+ ' + cmd); + console.debug('+ %s', cmd); const result = spawnSync(bin, params, {cwd: home, stdio: 'inherit'}); if (result.status != 0) { throw new Error('The command "' + cmd + '" exited with #' + result.status + ' code'); diff --git a/src/tinted-console.js b/src/tinted-console.js new file mode 100644 index 0000000..75bc2b0 --- /dev/null +++ b/src/tinted-console.js @@ -0,0 +1,74 @@ +#! /usr/bin/env node +/* + * The MIT License (MIT) + * + * Copyright (c) 2022 Yegor Bugayenko + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +const safe = require('colors/safe'); +const util = require('node:util'); + +const levels = { + 'trace': false, + 'debug': false, + 'info': true, + 'warn': true, + 'error': true, +}; + +const colors = { + 'trace': 'gray', + 'debug': 'gray', + 'info': 'black', + 'warn': 'orange', + 'error': 'red', +}; + +for (const level in levels) { + if (levels.hasOwnProperty(level)) { + const before = console[level]; + const lvl = level; + console[level] = function(...args) { + if (!levels[lvl]) { + return; + } + before.call( + before, + safe[colors[lvl]](util.format(...args)) + ); + }; + } +} + +/** + * Enable this particular logging level. + * @param {String} level - The level to enable + */ +module.exports.enable = function enable(level) { + for (const key in levels) { + if (levels.hasOwnProperty(level)) { + levels[key] = true; + if (key === level) { + break; + } + } + } +};