aboutsummaryrefslogtreecommitdiffstats
path: root/packages/sra-report/src/index.ts
blob: 9a203b654632a0bf3bd86d8cc4fe1c8a71cc6f4e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#!/usr/bin/env node
import { assert } from '@0xproject/assert';
import { Schema, schemas } from '@0xproject/json-schemas';
import { logUtils, promisify } from '@0xproject/utils';
import chalk from 'chalk';
import * as _ from 'lodash';
import * as newman from 'newman';
import * as yargs from 'yargs';

import * as sraReportCollectionJSON from '../../postman_collections/sra_report.postman_collection.json';

import { postmanEnvironmentFactory } from './postman_environment_factory';
import { utils } from './utils';

const DEFAULT_NETWORK_ID = 1;
const SUPPORTED_NETWORK_IDS = [1, 3, 4, 42];

// extract command line arguments
const args = yargs
    .option('endpoint-url', {
        alias: ['e'],
        describe: 'API endpoint url to test for standard relayer API compliance',
        type: 'string',
        demandOption: true,
    })
    .option('output', {
        alias: ['o', 'out'],
        describe: 'The relative path to write the report generated by the collection run, prints to console by default',
        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,
    })
    .option('environment', {
        alias: ['env'],
        describe: 'The relative path to a postman environment file for the collection run',
        type: 'string',
        normalize: true,
        demandOption: false,
    })
    .option('export-collection', {
        alias: ['ec'],
        describe: 'The relative path to write the postman collection file used by the collection run',
        type: 'string',
        normalize: true,
        demandOption: false,
    })
    .option('export-environment', {
        alias: ['ee'],
        describe: 'The relative path to write the postman environment file used by the collection run',
        type: 'string',
        normalize: true,
        demandOption: false,
    })
    .example(
        "$0 --endpoint-url 'http://api.example.com' --out 'path/to/report.json' --network-id 42 --environment 'path/to/custom/environment.json' --export-collection 'path/to/collection.json' --export-environment 'path/to/environment.json'",
        'Full usage example',
    ).argv;
// perform extra validation on command line arguments
try {
    assert.isWebUri('args', args.endpointUrl);
} catch (err) {
    logUtils.log(`${chalk.red(`Invalid url format:`)} ${args.endpointUrl}`);
    process.exit(1);
}
if (!_.includes(SUPPORTED_NETWORK_IDS, args.networkId)) {
    logUtils.log(`${chalk.red(`Unsupported network id:`)} ${args.networkId}`);
    logUtils.log(`${chalk.bold(`Supported network ids:`)} ${SUPPORTED_NETWORK_IDS}`);
    process.exit(1);
}
const mainAsync = async () => {
    const newmanReporterOptions = !_.isUndefined(args.output)
        ? {
              reporters: 'json',
              reporter: {
                  json: {
                      export: args.output,
                  },
              },
          }
        : {
              reporters: 'cli',
          };
    const environment = !_.isUndefined(args.environment)
        ? args.environment
        : await postmanEnvironmentFactory.createPostmanEnvironmentAsync(args.endpointUrl, args.networkId);
    const newmanRunOptions = {
        collection: sraReportCollectionJSON,
        environment,
        exportCollection: args.exportCollection,
        exportEnvironment: args.exportEnvironment,
        ...newmanReporterOptions,
    };
    await utils.newmanRunAsync(newmanRunOptions);
};
mainAsync().catch(logUtils.log);