Refactor environment factory and promisify newman
This commit is contained in:
@@ -23,8 +23,11 @@
|
|||||||
},
|
},
|
||||||
"homepage": "https://github.com/0xProject/0x.js/packages/abi-gen/README.md",
|
"homepage": "https://github.com/0xProject/0x.js/packages/abi-gen/README.md",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"0x.js": "^0.32.4",
|
||||||
"@0xproject/assert": "^0.0.20",
|
"@0xproject/assert": "^0.0.20",
|
||||||
|
"@0xproject/connect": "^0.6.1",
|
||||||
"@0xproject/json-schemas": "^0.7.12",
|
"@0xproject/json-schemas": "^0.7.12",
|
||||||
|
"@0xproject/utils": "^0.3.4",
|
||||||
"chalk": "^2.3.0",
|
"chalk": "^2.3.0",
|
||||||
"lodash": "^4.17.4",
|
"lodash": "^4.17.4",
|
||||||
"newman": "^3.9.3",
|
"newman": "^3.9.3",
|
||||||
|
|||||||
@@ -2,23 +2,18 @@
|
|||||||
|
|
||||||
import { assert } from '@0xproject/assert';
|
import { assert } from '@0xproject/assert';
|
||||||
import { Schema, schemas } from '@0xproject/json-schemas';
|
import { Schema, schemas } from '@0xproject/json-schemas';
|
||||||
|
import { promisify } from '@0xproject/utils';
|
||||||
import chalk from 'chalk';
|
import chalk from 'chalk';
|
||||||
import * as _ from 'lodash';
|
import * as _ from 'lodash';
|
||||||
import * as newman from 'newman';
|
import * as newman from 'newman';
|
||||||
import * as yargs from 'yargs';
|
import * as yargs from 'yargs';
|
||||||
|
|
||||||
import * as sraReportCollectionJSON from '../postman_configs/collections/sra_report.postman_collection.json';
|
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 { postmanEnvironmentFactory } from './postman_environment_factory';
|
||||||
import { utils } from './utils';
|
import { utils } from './utils';
|
||||||
|
|
||||||
interface GlobalsValue {
|
const newmanRunAsync = promisify<void>(newman.run);
|
||||||
key: string;
|
|
||||||
value: string;
|
|
||||||
enabled: boolean;
|
|
||||||
type: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
const DEFAULT_NETWORK_ID = 1;
|
const DEFAULT_NETWORK_ID = 1;
|
||||||
const SUPPORTED_NETWORK_IDS = [1, 42];
|
const SUPPORTED_NETWORK_IDS = [1, 42];
|
||||||
@@ -58,72 +53,16 @@ if (!_.includes(SUPPORTED_NETWORK_IDS, args.networkId)) {
|
|||||||
utils.log(`${chalk.bold(`Supported network ids:`)} ${SUPPORTED_NETWORK_IDS}`);
|
utils.log(`${chalk.bold(`Supported network ids:`)} ${SUPPORTED_NETWORK_IDS}`);
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
}
|
}
|
||||||
// run newman
|
|
||||||
newman.run(
|
const mainAsync = async () => {
|
||||||
{
|
const newmanRunOptions = {
|
||||||
collection: sraReportCollectionJSON,
|
collection: sraReportCollectionJSON,
|
||||||
reporters: 'cli',
|
reporters: 'cli',
|
||||||
globals: createGlobals(args.url, _.values(schemas)),
|
globals: postmanEnvironmentFactory.createGlobalEnvironment(args.url),
|
||||||
environment: getEnvironment(args.networkId),
|
environment: postmanEnvironmentFactory.createNetworkEnvironment(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 = _.compact(
|
await newmanRunAsync(newmanRunOptions);
|
||||||
_.map(schemaList, (schema: Schema) => {
|
};
|
||||||
if (_.isUndefined(schema.id)) {
|
|
||||||
return undefined;
|
mainAsync()
|
||||||
} else {
|
.catch(err => utils.log);
|
||||||
return {
|
|
||||||
key: convertSchemaIdToKey(schema.id),
|
|
||||||
value: JSON.stringify(schema),
|
|
||||||
enabled: true,
|
|
||||||
type: 'text',
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}),
|
|
||||||
);
|
|
||||||
const schemaKeys = _.map(schemaGlobalsValues, (globalsValue: GlobalsValue) => {
|
|
||||||
return globalsValue.key;
|
|
||||||
});
|
|
||||||
const schemaKeysGlobalsValue = {
|
|
||||||
key: 'schemaKeys',
|
|
||||||
value: JSON.stringify(schemaKeys),
|
|
||||||
enabled: true,
|
|
||||||
type: 'text',
|
|
||||||
};
|
|
||||||
const globalsValues = _.concat(schemaGlobalsValues, urlGlobalsValue, schemaKeysGlobalsValue);
|
|
||||||
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 {};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
71
packages/sra-report/src/postman_environment_factory.ts
Normal file
71
packages/sra-report/src/postman_environment_factory.ts
Normal file
@@ -0,0 +1,71 @@
|
|||||||
|
import { Schema, schemas as schemasByName } from '@0xproject/json-schemas';
|
||||||
|
import * as _ from 'lodash';
|
||||||
|
|
||||||
|
import * as kovanTokensEnvironmentJSON from '../postman_configs/environments/kovan_tokens.postman_environment.json';
|
||||||
|
import * as mainnetTokensEnvironmentJSON from '../postman_configs/environments/mainnet_tokens.postman_environment.json';
|
||||||
|
|
||||||
|
interface EnvironmentValue {
|
||||||
|
key: string;
|
||||||
|
value: string;
|
||||||
|
enabled: boolean;
|
||||||
|
type: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export const postmanEnvironmentFactory = {
|
||||||
|
createGlobalEnvironment(url: string) {
|
||||||
|
const urlEnvironmentValue = {
|
||||||
|
key: 'url',
|
||||||
|
value: url,
|
||||||
|
enabled: true,
|
||||||
|
type: 'text',
|
||||||
|
};
|
||||||
|
const schemas: Schema[] = _.values(schemasByName);
|
||||||
|
const schemaEnvironmentValues = _.compact(
|
||||||
|
_.map(schemas, (schema: Schema) => {
|
||||||
|
if (_.isUndefined(schema.id)) {
|
||||||
|
return undefined;
|
||||||
|
} else {
|
||||||
|
return {
|
||||||
|
key: convertSchemaIdToKey(schema.id),
|
||||||
|
value: JSON.stringify(schema),
|
||||||
|
enabled: true,
|
||||||
|
type: 'text',
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
const schemaKeys = _.map(schemaEnvironmentValues, (environmentValue: EnvironmentValue) => {
|
||||||
|
return environmentValue.key;
|
||||||
|
});
|
||||||
|
const schemaKeysEnvironmentValue = {
|
||||||
|
key: 'schemaKeys',
|
||||||
|
value: JSON.stringify(schemaKeys),
|
||||||
|
enabled: true,
|
||||||
|
type: 'text',
|
||||||
|
};
|
||||||
|
const environmentValues = _.concat(schemaEnvironmentValues, urlEnvironmentValue, schemaKeysEnvironmentValue);
|
||||||
|
const environment = {
|
||||||
|
values: environmentValues,
|
||||||
|
};
|
||||||
|
return environment;
|
||||||
|
},
|
||||||
|
createNetworkEnvironment(networkId: number) {
|
||||||
|
switch (networkId) {
|
||||||
|
case 1:
|
||||||
|
return mainnetTokensEnvironmentJSON;
|
||||||
|
case 42:
|
||||||
|
return kovanTokensEnvironmentJSON;
|
||||||
|
default:
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
function convertSchemaIdToKey(schemaId: string) {
|
||||||
|
let result = schemaId;
|
||||||
|
if (_.startsWith(result, '/')) {
|
||||||
|
result = result.substr(1);
|
||||||
|
}
|
||||||
|
result = `${result}Schema`;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user