Skip to content

Commit

Permalink
Enhanced logging information (#23)
Browse files Browse the repository at this point in the history
* implemented interface for centralized logging behaviour
* Added success and error logging for all commands
* Added logging for download progress
  • Loading branch information
AlexanderSkrock authored Oct 2, 2023
1 parent 1ce10ef commit 377590d
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 10 deletions.
17 changes: 11 additions & 6 deletions bin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
const yargs = require('yargs/yargs');
const { hideBin } = require('yargs/helpers');

const { getAllVersions, download, install, launch } = require('../lib');
const { getConfig } = require('../lib');
const { getAllVersions, download, getConfig, install, launch } = require('../lib');
const { error, info } = require('../lib/logging');

// eslint-disable-next-line
yargs(hideBin(process.argv))
Expand All @@ -12,7 +12,9 @@ yargs(hideBin(process.argv))
'versions',
'List available versions of the Camunda Modeler',
() => {},
() => getAllVersions().then((versions) => process.stdout.write(`Versions:\n${versions.join('\n')}\n`)),
() => getAllVersions()
.then((versions) => info(versions.join('\n')))
.catch(err => error(err)),
)
.command(
'download',
Expand All @@ -26,7 +28,8 @@ yargs(hideBin(process.argv))
version: args.version,
noCache: !args.cache,
cachePath: args['cache-path'],
}).then((res) => process.stdout.write(`Download was finished successfully: ${res}`)),
}).then((res) => info(`Download was finished successfully: ${res}`))
.catch(err => error(err)),
)
.command(
'install',
Expand All @@ -46,14 +49,16 @@ yargs(hideBin(process.argv))
installationPath: args.path,
overwriteExistingInstallation: args.overwrite,
linkedPlugins: [args['link-plugin']],
}).then((res) => process.stdout.write(`Installation was finished successfully: ${res}\n`)),
}).then((res) => info(`Installation was finished successfully: ${res}`))
.catch(err => error(err)),
)
.command(
'launch',
'Launch Camunda Modeler',
(yargs) => yargs.option('path', { default: getConfig().getInstallationPath() }),
args => launch({
installationPath: args.path,
}).catch(err => `An error occurred: ${err}`),
}).then(() => info('Camunda Modeler launched successfully'))
.catch(err => error(err)),
)
.argv;
11 changes: 7 additions & 4 deletions lib/download.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const { basename } = require('node:path');

const { getReleaseByVersion, getLatestRelease } = require('./api');
const Cache = require('./cache');
const { ProgressTransformStream } = require('./logging');

function getDownloadLink (platform, version) {
const release = version === null || version === undefined
Expand All @@ -20,9 +21,10 @@ function getDownloadLink (platform, version) {
function downloadToCache (cache, cacheKey, link) {
return fetch(link).then(response => {
if (!response.ok) {
return new Error('Response status was ' + response.status);
throw new Error('Response status was ' + response.status);
}
return cache.put(cacheKey, response.body);
const length = response.headers.get('Content-Length');
return cache.put(cacheKey, response.body.pipeThrough(new ProgressTransformStream(length)));
});
}

Expand All @@ -34,8 +36,9 @@ function downloadCamundaModeler (config) {
return Promise.all([
cache.isCached(fileName),
config.shouldSkipCache() ? Promise.reject(new Error('cache may not be used')) : Promise.resolve(),
]).catch(() => downloadToCache(cache, fileName, link))
.then(() => cache.get(fileName));
]).catch(() => {
return downloadToCache(cache, fileName, link);
}).then(() => cache.get(fileName));
});
}

Expand Down
33 changes: 33 additions & 0 deletions lib/logging/ProgressTransformStream.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
const { info } = require('./logger');

const PERCENTAGE_STEP_SIZE = 5;

class ProgressTransformOptions {
constructor (streamLength) {
this.lastLogged = 0;
this.progress = 0;
this.length = streamLength;
}

transform (chunk, controller) {
this.progress += chunk.length;
controller.enqueue(chunk);
this.logProgress();
}

logProgress () {
const percentage = Math.trunc(this.progress * 100 / this.length);
if (percentage >= this.lastLogged + PERCENTAGE_STEP_SIZE) {
info(`Progress: ${percentage}%`);
this.lastLogged = percentage;
}
}
}

class ProgressTransformStream extends TransformStream {
constructor (streamLength) {
super(new ProgressTransformOptions(streamLength));
}
}

module.exports = ProgressTransformStream;
16 changes: 16 additions & 0 deletions lib/logging/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const logger = require('./logger');
const ProgressTransformStream = require('./ProgressTransformStream');

function info (msg) {
logger.info(msg);
}

function error (error) {
logger.error(error);
}

module.exports = {
info,
error,
ProgressTransformStream,
};
6 changes: 6 additions & 0 deletions lib/logging/logger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const LOGGER = {
info: msg => console.info(msg),
error: err => console.error(err),
};

module.exports = LOGGER;

0 comments on commit 377590d

Please sign in to comment.