UNPKG

3.51 kBJavaScriptView Raw
1import fs from 'node:fs';
2import url from 'node:url';
3import path from 'node:path';
4import yargs from 'yargs';
5import { hideBin } from 'yargs/helpers';
6import { commands } from './commands/index.js';
7import { handler, cmdArgs } from './commands/run.js';
8import { CLI_EPILOGUE, pkg } from './constants.js';
9const __dirname = path.dirname(url.fileURLToPath(import.meta.url));
10const DEFAULT_CONFIG_FILENAME = 'wdio.conf.js';
11const 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];
22export 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 * parse CLI arguments according to what run expects, without this adding
38 * `--spec ./test.js` results in propagating the spec parameter as a
39 * string while in reality is should be parsed into a array of strings
40 */
41 if (!process.argv.find((arg) => arg === '--help')) {
42 argv.options(cmdArgs);
43 }
44 /**
45 * The only way we reach this point is if the user runs the binary without a command (i.e. wdio wdio.conf.js)
46 * We can safely assume that if this is the case, the user is trying to execute the `run` command as it
47 * was previous to https://github.com/webdriverio/webdriverio/pull/4402
48 *
49 * Since the `run` command verifies if the configuration file exists before executing
50 * we don't have to check that again here.
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 /* istanbul ignore if */
71 if (!process.env.VITEST_WORKER_ID) {
72 process.exit(1);
73 }
74 }
75}