(argv: string[])
| 54 | } |
| 55 | |
| 56 | function parseArgs(argv: string[]): Options { |
| 57 | if (argv.includes("--help") || argv.includes("-h")) { |
| 58 | printHelp(); |
| 59 | process.exit(0); |
| 60 | } |
| 61 | |
| 62 | let port = parsePort(process.env.STORYBOOK_PORT ?? String(DEFAULT_PORT)); |
| 63 | let timeoutMs = parsePositiveInteger( |
| 64 | process.env.STORYBOOK_TIMEOUT_MS ?? String(DEFAULT_TIMEOUT_MS), |
| 65 | "--timeout-ms" |
| 66 | ); |
| 67 | |
| 68 | const delimiterIndex = argv.indexOf("--"); |
| 69 | const optionArgs = delimiterIndex === -1 ? [] : argv.slice(0, delimiterIndex); |
| 70 | const command = delimiterIndex === -1 ? argv : argv.slice(delimiterIndex + 1); |
| 71 | if (delimiterIndex === -1 && argv[0]?.startsWith("-")) { |
| 72 | throw new Error("Missing `-- <command>` delimiter. Run with --help for usage."); |
| 73 | } |
| 74 | if (command.length === 0) { |
| 75 | throw new Error("Missing command after `--`. Run with --help for usage."); |
| 76 | } |
| 77 | |
| 78 | for (let index = 0; index < optionArgs.length; index += 1) { |
| 79 | const arg = optionArgs[index]; |
| 80 | if (arg === "--port" || arg === "-p") { |
| 81 | const value = optionArgs[index + 1]; |
| 82 | if (!value) throw new Error(`${arg} requires a value`); |
| 83 | port = parsePort(value); |
| 84 | index += 1; |
| 85 | } else if (arg === "--timeout-ms") { |
| 86 | const value = optionArgs[index + 1]; |
| 87 | if (!value) throw new Error(`${arg} requires a value`); |
| 88 | timeoutMs = parsePositiveInteger(value, arg); |
| 89 | index += 1; |
| 90 | } else { |
| 91 | throw new Error(`Unknown option: ${arg}`); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | return { port, timeoutMs, command }; |
| 96 | } |
| 97 | |
| 98 | function storybookUrl(port: number): string { |
| 99 | return `http://127.0.0.1:${port}`; |
no test coverage detected