aboutsummaryrefslogtreecommitdiffstats
path: root/packages/sra-report/src
diff options
context:
space:
mode:
authorBrandon Millman <brandon.millman@gmail.com>2018-02-22 16:30:35 +0800
committerBrandon Millman <brandon.millman@gmail.com>2018-02-23 05:12:44 +0800
commit42ecc087cbd3558258107fbce645441955416497 (patch)
treebb5f6fe7cad0cdfa2799219282c69b99fab7b0ee /packages/sra-report/src
parent0e95fd0b9b9fc340854ba39c63059edb3b1a1be8 (diff)
downloaddexon-sol-tools-42ecc087cbd3558258107fbce645441955416497.tar.gz
dexon-sol-tools-42ecc087cbd3558258107fbce645441955416497.tar.zst
dexon-sol-tools-42ecc087cbd3558258107fbce645441955416497.zip
Implement initial sra-report command line tool
Diffstat (limited to 'packages/sra-report/src')
-rw-r--r--packages/sra-report/src/globals.d.ts6
-rw-r--r--packages/sra-report/src/index.ts108
-rw-r--r--packages/sra-report/src/types.ts3
-rw-r--r--packages/sra-report/src/utils.ts5
4 files changed, 122 insertions, 0 deletions
diff --git a/packages/sra-report/src/globals.d.ts b/packages/sra-report/src/globals.d.ts
new file mode 100644
index 000000000..eb09d3fcf
--- /dev/null
+++ b/packages/sra-report/src/globals.d.ts
@@ -0,0 +1,6 @@
+declare module 'newman';
+
+declare module '*.json' {
+ const value: any;
+ export default value;
+}
diff --git a/packages/sra-report/src/index.ts b/packages/sra-report/src/index.ts
new file mode 100644
index 000000000..fe100bd42
--- /dev/null
+++ b/packages/sra-report/src/index.ts
@@ -0,0 +1,108 @@
+#!/usr/bin/env node
+
+import { assert } from '@0xproject/assert';
+import { schemas } from '@0xproject/json-schemas';
+import chalk from 'chalk';
+import * as _ from 'lodash';
+import * as newman from 'newman';
+import * as yargs from 'yargs';
+
+import * as sraReportCollectionJSON from '../postman_configs/collections/sra_report.postman_collection.json';
+import * as kovanTokensEnvironmentJSON from '../postman_configs/environments/kovan_tokens.postman_environment.json';
+import * as mainnetTokensEnvironmentJSON from '../postman_configs/environments/mainnet_tokens.postman_environment.json';
+
+import { Schema } from './types';
+import { utils } from './utils';
+
+const DEFAULT_NETWORK_ID = 1;
+const SUPPORTED_NETWORK_IDS = [1, 42];
+
+// extract command line arguments
+const args = yargs
+ .option('url', {
+ alias: ['u'],
+ describe: 'API endpoint to test for standard relayer API compliance',
+ type: 'string',
+ demandOption: true,
+ })
+ .option('output', {
+ alias: ['o', 'out'],
+ describe: 'Folder where to write the reports',
+ type: 'string',
+ normalize: true,
+ demandOption: false,
+ })
+ .option('network-id', {
+ alias: ['n'],
+ describe: 'ID of the network that the API is serving orders from',
+ type: 'number',
+ default: DEFAULT_NETWORK_ID,
+ })
+ .example("$0 --url 'http://api.example.com' --out 'src/contracts/generated/' --network-id 42", 'Full usage example')
+ .argv;
+// perform extra validation on command line arguments
+try {
+ assert.isHttpUrl('args', args.url);
+} catch (err) {
+ utils.log(`${chalk.red(`Invalid url format:`)} ${args.url}`);
+ process.exit(1);
+}
+if (!_.includes(SUPPORTED_NETWORK_IDS, args.networkId)) {
+ utils.log(`${chalk.red(`Unsupported network id:`)} ${args.networkId}`);
+ utils.log(`${chalk.bold(`Supported network ids:`)} ${SUPPORTED_NETWORK_IDS}`);
+ process.exit(1);
+}
+// run newman
+newman.run(
+ {
+ collection: sraReportCollectionJSON,
+ reporters: 'cli',
+ globals: createGlobals(args.url, _.values(schemas)),
+ environment: getEnvironment(args.networkId),
+ },
+ (err: Error) => {
+ if (err) {
+ throw err;
+ }
+ utils.log('collection run complete!');
+ },
+);
+function createGlobals(url: string, schemaList: Schema[]) {
+ const urlGlobalsValue = {
+ key: 'url',
+ value: args.url,
+ enabled: true,
+ type: 'text',
+ };
+ const schemaGlobalsValues = _.map(schemaList, (schema: Schema) => {
+ return {
+ key: convertSchemaIdToKey(schema.id),
+ value: JSON.stringify(schema),
+ enabled: true,
+ type: 'text',
+ };
+ });
+ const globalsValues = _.concat(schemaGlobalsValues, urlGlobalsValue);
+ const globals = {
+ values: globalsValues,
+ };
+ return globals;
+}
+function convertSchemaIdToKey(schemaId: string) {
+ let result = schemaId;
+ if (_.startsWith(result, '/')) {
+ result = result.substr(1);
+ }
+ result = `${result}Schema`;
+ return result;
+}
+function getEnvironment(networkId: number) {
+ switch (networkId) {
+ case 1:
+ return mainnetTokensEnvironmentJSON;
+ case 42:
+ return kovanTokensEnvironmentJSON;
+ default:
+ return {};
+ }
+}
diff --git a/packages/sra-report/src/types.ts b/packages/sra-report/src/types.ts
new file mode 100644
index 000000000..f08ef4042
--- /dev/null
+++ b/packages/sra-report/src/types.ts
@@ -0,0 +1,3 @@
+export interface Schema {
+ id: string;
+}
diff --git a/packages/sra-report/src/utils.ts b/packages/sra-report/src/utils.ts
new file mode 100644
index 000000000..5423cabd9
--- /dev/null
+++ b/packages/sra-report/src/utils.ts
@@ -0,0 +1,5 @@
+export const utils = {
+ log(...args: any[]): void {
+ console.log(...args); // tslint:disable-line:no-console
+ },
+};