Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add --version flag #620

Merged
merged 7 commits into from
Jul 28, 2021
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions src/logging/Log.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
import { createLogger, Logger } from 'winston';
import winston from 'winston/lib/winston/transports';

import { consoleTransport } from './transports';

// Note: there is a `fileTransport` that gets added in main.
const transports = [consoleTransport()];

/**
* Access a singleton winston.Logger that will be intialized on first use.
*/
export class Log {
private static _transports: winston.ConsoleTransportInstance[] | undefined;
private static _logger: Logger | undefined;
private static create(): Logger {
if (this._logger) {
return this._logger;
}

// Note: there is a `fileTransport` that gets added in main.
this._transports = [consoleTransport()];

this._logger = createLogger({
transports,
transports: this._transports,
exitOnError: false,
exceptionHandlers: transports,
exceptionHandlers: this._transports,
});

return this._logger;
Expand Down
7 changes: 3 additions & 4 deletions src/logging/transports/consoleTransport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,13 @@ import {
timeStamp,
} from '../transformers';

const {
config: { LOG },
} = SidecarConfig;

/**
* Console transport for winston logger.
*/
export function consoleTransport(): transports.ConsoleTransportInstance {
const {
config: { LOG },
} = SidecarConfig;
/**
* A simple printing format for how `TransformableInfo` shows up.
*/
Expand Down
28 changes: 19 additions & 9 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ import { getControllersForSpec } from './chains-config';
import { consoleOverride } from './logging/consoleOverride';
import { Log } from './logging/Log';
import * as middleware from './middleware';
import { parseArgs } from './parseArgs';
import { SidecarConfig } from './SidecarConfig';

const { logger } = Log;
const { config } = SidecarConfig;

async function main() {
const { config } = SidecarConfig;
const { logger } = Log;
// Overide console.{log, error, warn, etc}
consoleOverride(logger);

Expand Down Expand Up @@ -88,12 +88,6 @@ async function main() {
app.listen();
}

process.on('SIGINT', function () {
console.log('Caught interrupt signal, exiting...');
process.exit(0);
});
main().catch(console.log);

/**
* Prompt the user with some basic info about the node and the network they have
* connected Sidecar to.
Expand All @@ -103,6 +97,8 @@ main().catch(console.log);
* @param implName implementation name of the node Sidecar is connected to
*/
function startUpPrompt(wsUrl: string, chainName: string, implName: string) {
const { logger } = Log;
const { config } = SidecarConfig;
/**
* Best effort list of known public nodes that do not encourage high traffic
* sidecar installations connecting to them for non - testing / development purposes.
Expand Down Expand Up @@ -150,3 +146,17 @@ function startUpPrompt(wsUrl: string, chainName: string, implName: string) {
);
}
}

process.on('SIGINT', function () {
console.log('Caught interrupt signal, exiting...');
process.exit(0);
});
TarikGul marked this conversation as resolved.
Show resolved Hide resolved

const args = parseArgs();

if (args.version) {
console.log(`@substrate/api-sidecar v${packageJSON.version}`);
process.exit(0);
} else {
main().catch(console.log);
}
9 changes: 9 additions & 0 deletions src/parseArgs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { ArgumentParser, Namespace } from 'argparse';

export const parseArgs = (): Namespace => {
const parser = new ArgumentParser();

parser.add_argument('-v', '--version', { action: 'store_true' });

return parser.parse_args() as Namespace;
};