Skip to content

Commit

Permalink
fix: add --version flag (#620)
Browse files Browse the repository at this point in the history
  • Loading branch information
TarikGul committed Jul 28, 2021
1 parent a29ac17 commit 9d8bb98
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 18 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ Run the service from any directory on your machine:
substrate-api-sidecar
```

To check your version you may append the `--version` flag to `substrate-api-sidecar`.

### Local installation

Install the service locally:
Expand Down
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 { ConsoleTransportInstance } 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: 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);
});

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;
};

0 comments on commit 9d8bb98

Please sign in to comment.