Skip to content

Commit 0e1535c

Browse files
committed
add more validations
1 parent e5559cd commit 0e1535c

File tree

8 files changed

+46
-29
lines changed

8 files changed

+46
-29
lines changed

src/commands/run.ts

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import Poller from '../utils/poller';
55
import Config from '../utils/config';
66
import Tunnel from '../utils/tunnel';
77
import ora from 'ora';
8+
import chalk from 'chalk';
89

910
interface Arguments {
1011
[x: string]: unknown;
@@ -16,24 +17,28 @@ export default class RunProject {
1617
private uploader: Uploader | undefined = undefined;
1718
private poller: Poller | undefined = undefined;
1819
private tunnel: Tunnel | undefined = undefined;
20+
private configFilePath: string | undefined = undefined;
1921

2022
constructor(argv: Arguments) {
21-
try {
22-
this.start();
23-
} catch (e) {
24-
console.log('error', e)
25-
log.error(e)
23+
if (typeof(argv.cf) === 'string') {
24+
this.configFilePath = argv.cf;
2625
}
2726
}
2827

2928
public async start(): Promise<void> {
30-
const config = await Config.getConfig();
29+
let config;
30+
try {
31+
config = await Config.getConfig(this.configFilePath || `testingbot.json`);
32+
} catch (e) {
33+
console.error(chalk.white.bgRed.bold(`Configuration file problem: ${e.message} for Config File: ${this.configFilePath || `testingbot.json`}`));
34+
return;
35+
}
36+
3137
const configValidationErrors = Config.validate(config);
3238

3339
if (configValidationErrors.length > 0) {
34-
throw new Error(
35-
`Configuration errors: ${configValidationErrors.join('\n')}`,
36-
);
40+
console.error(chalk.white.bgRed.bold(`Configuration errors: ${configValidationErrors.join('\n')}`));
41+
return;
3742
}
3843

3944
this.archiver = new Archiver(config);
@@ -44,7 +49,8 @@ export default class RunProject {
4449
let zipFile: string;
4550

4651
if (!this.archiver || !this.uploader) {
47-
throw new Error(`Invalid state, please try again`);
52+
console.error(chalk.white.bgRed.bold(`Invalid state, please try again`));
53+
return;
4854
}
4955

5056
if (config.run_settings.start_tunnel) {
@@ -53,24 +59,23 @@ export default class RunProject {
5359
tunnelSpinner.succeed('TestingBot Tunnel Ready');
5460
}
5561

62+
const uploadSpinner = ora('Starting Project on TestingBot').start();
5663
try {
5764
zipFile = await this.archiver.start();
5865
} catch (err) {
59-
log.error(err);
66+
console.error(err);
6067
return;
6168
}
6269

6370
try {
64-
const uploadSpinner = ora('Starting Project on TestingBot').start();
65-
6671
const response = await this.uploader.start(zipFile);
6772
uploadSpinner.succeed('Cypress is now running on TestingBot')
6873

69-
const poller = await this.poller.check(response.id)
74+
const poller = await this.poller.check(response.id, uploadSpinner)
7075
log.info(poller)
7176

7277
} catch (err) {
73-
log.error(err);
78+
console.error(chalk.white.bgRed.bold(err));
7479
if (config.run_settings.start_tunnel) {
7580
await this.tunnel.stop();
7681
}

src/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ yargs
2323
.help('help')
2424
.wrap(null).argv;
2525

26-
return new InitProject(initArgv);
26+
return new InitProject(initArgv);
2727
})
2828
.command('run', 'more info', function (yargs) {
2929
const initArgv = yargs
@@ -39,7 +39,8 @@ yargs
3939
})
4040
.help('help')
4141
.wrap(null).argv;
42-
return new RunProject(initArgv);
42+
const runProject = new RunProject(initArgv);
43+
runProject.start();
4344
})
4445
.alias('h', 'help')
4546
.help('help')

src/templates/config.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,16 @@ export default (): string => {
1414
run_settings: {
1515
cypress_project_dir:
1616
'/path/to/directory-that-contains-<cypress.json>-file',
17-
project_name: 'project-name',
1817
build_name: 'build-name',
1918
parallel_count: 'How many tests you want to run in parallel',
2019
npm_dependencies: {},
2120
package_config_options: {},
2221
start_tunnel: true,
2322
local_ports: []
2423
},
24+
tunnel_settings: {
25+
verbose: false
26+
}
2527
};
2628

2729
return JSON.stringify(config, null, 4);

src/utils/archiver.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ import os from 'os';
55
import log from './../log';
66
import { IConfig } from './config';
77

8-
const fsPromises = fs.promises;
9-
108
export default class Archiver {
119
private config: IConfig;
1210

src/utils/config.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ export interface ICapability {
1212
platform: string;
1313
version: string | number;
1414
localHttpPorts?: number[];
15+
build?: string;
1516
}
1617

1718
interface IRunSettings {
1819
cypress_project_dir: string
19-
project_name: string
2020
build_name: string
2121
parallel_count: number
2222
npm_dependencies: any
@@ -29,11 +29,12 @@ export interface IConfig {
2929
auth: IAuth;
3030
browsers: ICapability[];
3131
run_settings: IRunSettings;
32+
tunnel_settings: any;
3233
}
3334

3435
export default {
35-
async getConfig(): Promise<IConfig> {
36-
const configString = await fsPromises.readFile(`testingbot.json`);
36+
async getConfig(configFilePath: string): Promise<IConfig> {
37+
const configString = await fsPromises.readFile(configFilePath);
3738
const configObject = JSON.parse(configString.toString());
3839

3940
return configObject as IConfig;

src/utils/poller.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import request from 'request';
22
import log from './../log';
33
import { IConfig } from './config';
4+
import ora from 'ora';
45

56
interface IPollResponse {
67
status: string
@@ -18,14 +19,15 @@ export default class Poller {
1819
this.config = config;
1920
}
2021

21-
public async check(id: number): Promise<IPollResponse> {
22+
public async check(id: number, spinner: ora.Ora): Promise<IPollResponse> {
2223
return new Promise((resolve, reject) => {
2324
this.intervalId = setInterval(async () => {
2425
const response = await this.getStatus(id);
25-
log.info('checking', response);
2626

2727
if (response.status === 'DONE') {
28-
if (response.errors.length === 0) {
28+
spinner.succeed('Cypress Project has finished running on TestingBot');
29+
log.info(response);
30+
if (Object.keys(response.errors).length === 0) {
2931
if (this.intervalId) {
3032
clearInterval(this.intervalId);
3133
this.intervalId = undefined;

src/utils/tunnel.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@ export default class Tunnel {
1212

1313
public async start(): Promise<void> {
1414
return new Promise((resolve, reject) => {
15-
testingbotTunnel({
15+
const tunnelOpts = Object.assign({
1616
apiKey: this.config.auth.key,
1717
apiSecret: this.config.auth.secret,
18-
}, (err: any, tunnel: any) => {
18+
}, this.config.tunnel_settings);
19+
20+
testingbotTunnel(tunnelOpts, (err: any, tunnel: any) => {
1921
if (err) {
2022
reject(err);
2123
return;

src/utils/uploader.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,13 @@ export default class Uploader {
1818
const capabilities = this.config.browsers;
1919
if (this.config.run_settings.local_ports.length > 0) {
2020
capabilities.map((capability: ICapability) => {
21-
capability.localHttpPorts = this.config.run_settings.local_ports
21+
capability.localHttpPorts = this.config.run_settings.local_ports;
22+
})
23+
}
24+
25+
if (this.config.run_settings.build_name && this.config.run_settings.build_name.length > 0 && this.config.run_settings.build_name !== 'build-name') {
26+
capabilities.map((capability: ICapability) => {
27+
capability.build = this.config.run_settings.build_name;
2228
})
2329
}
2430
const requestOptions = {

0 commit comments

Comments
 (0)