Skip to content

Commit

Permalink
#6 --verbose
Browse files Browse the repository at this point in the history
  • Loading branch information
yegor256 committed Jun 21, 2022
1 parent 2ae22cb commit ca74390
Show file tree
Hide file tree
Showing 10 changed files with 99 additions and 9 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
1 change: 1 addition & 0 deletions src/commands/assemble.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')}`,
Expand Down
1 change: 1 addition & 0 deletions src/commands/clean.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
1 change: 1 addition & 0 deletions src/commands/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)}`,
Expand Down
1 change: 1 addition & 0 deletions src/commands/link.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)}`,
]);
};
2 changes: 2 additions & 0 deletions src/commands/register.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
};
1 change: 1 addition & 0 deletions src/commands/transpile.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')}`,
Expand Down
16 changes: 11 additions & 5 deletions src/eoc.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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')
Expand All @@ -44,7 +48,8 @@ program
program
.option('-s, --sources <path>', 'directory with .EO sources', '.')
.option('-t, --target <path>', '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')
Expand Down Expand Up @@ -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);
}
10 changes: 6 additions & 4 deletions src/mvnw.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -59,15 +59,17 @@ 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',
'--update-snapshots',
'--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');
Expand Down
74 changes: 74 additions & 0 deletions src/tinted-console.js
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
};

0 comments on commit ca74390

Please sign in to comment.