1 | import fs from 'node:fs';
|
2 | import url from 'node:url';
|
3 | import path from 'node:path';
|
4 | import yargs from 'yargs';
|
5 | import { hideBin } from 'yargs/helpers';
|
6 | import { commands } from './commands/index.js';
|
7 | import { handler, cmdArgs } from './commands/run.js';
|
8 | import { CLI_EPILOGUE, pkg } from './constants.js';
|
9 | const __dirname = path.dirname(url.fileURLToPath(import.meta.url));
|
10 | const DEFAULT_CONFIG_FILENAME = 'wdio.conf.js';
|
11 | const DESCRIPTION = [
|
12 | 'The `wdio` command allows you run and manage your WebdriverIO test suite.',
|
13 | 'If no command is provided it calls the `run` command by default, so:',
|
14 | '',
|
15 | '$ wdio wdio.conf.js',
|
16 | '',
|
17 | 'is the same as:',
|
18 | '$ wdio run wdio.conf.js',
|
19 | '',
|
20 | 'For more information, visit: https://webdriver.io/docs/clioptions'
|
21 | ];
|
22 | export default async function run() {
|
23 | const commandDir = path.join(__dirname, 'commands');
|
24 | const argv = yargs(hideBin(process.argv))
|
25 | .command(commands)
|
26 | .example('wdio run wdio.conf.js --suite foobar', 'Run suite on testsuite "foobar"')
|
27 | .example('wdio run wdio.conf.js --spec ./tests/e2e/a.js --spec ./tests/e2e/b.js', 'Run suite on specific specs')
|
28 | .example('wdio run wdio.conf.js --spec ./tests/e2e/a.feature:5', 'Run scenario by line number')
|
29 | .example('wdio run wdio.conf.js --spec ./tests/e2e/a.feature:5:10', 'Run scenarios by line number')
|
30 | .example('wdio run wdio.conf.js --spec ./tests/e2e/a.feature:5:10 --spec ./test/e2e/b.feature', 'Run scenarios by line number in single feature and another complete feature')
|
31 | .example('wdio install reporter spec', 'Install @wdio/spec-reporter')
|
32 | .example('wdio repl chrome -u <SAUCE_USERNAME> -k <SAUCE_ACCESS_KEY>', 'Run repl in Sauce Labs cloud')
|
33 | .updateStrings({ 'Commands:': `${DESCRIPTION.join('\n')}\n\nCommands:` })
|
34 | .version(pkg.version)
|
35 | .epilogue(CLI_EPILOGUE);
|
36 | |
37 |
|
38 |
|
39 |
|
40 |
|
41 | if (!process.argv.find((arg) => arg === '--help')) {
|
42 | argv.options(cmdArgs);
|
43 | }
|
44 | |
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 | const params = await argv.parse();
|
53 | const supportedCommands = fs
|
54 | .readdirSync(commandDir)
|
55 | .map((file) => file.slice(0, -3));
|
56 | if (!params._ || params._.find((param) => supportedCommands.includes(param))) {
|
57 | return;
|
58 | }
|
59 | const args = {
|
60 | ...params,
|
61 | configPath: path.resolve(process.cwd(), params._[0] && params._[0].toString() || DEFAULT_CONFIG_FILENAME)
|
62 | };
|
63 | try {
|
64 | const cp = await handler(args);
|
65 | return cp;
|
66 | }
|
67 | catch (err) {
|
68 | const output = await new Promise((resolve) => (yargs(hideBin(process.argv)).parse('--help', (err, argv, output) => resolve(output))));
|
69 | console.error(`${output}\n\n${err.stack}`);
|
70 |
|
71 | if (!process.env.VITEST_WORKER_ID) {
|
72 | process.exit(1);
|
73 | }
|
74 | }
|
75 | }
|