(cmd: string)
| 1 | import {NotEmptyArray} from './typings/common-types.js'; |
| 2 | |
| 3 | export function parseCommand(cmd: string) { |
| 4 | const extraSpacesStrippedCommand = cmd.replace(/\s{2,}/g, ' '); |
| 5 | const splitCommand = extraSpacesStrippedCommand.split(/\s+(?![^[]*]|[^<]*>)/); |
| 6 | const bregex = /\.*[\][<>]/g; |
| 7 | |
| 8 | const firstCommand = splitCommand.shift(); |
| 9 | if (!firstCommand) throw new Error(`No command found in: ${cmd}`); |
| 10 | |
| 11 | const parsedCommand: ParsedCommand = { |
| 12 | cmd: firstCommand.replace(bregex, ''), |
| 13 | demanded: [], |
| 14 | optional: [], |
| 15 | }; |
| 16 | splitCommand.forEach((cmd, i) => { |
| 17 | let variadic = false; |
| 18 | cmd = cmd.replace(/\s/g, ''); |
| 19 | if (/\.+[\]>]/.test(cmd) && i === splitCommand.length - 1) variadic = true; |
| 20 | if (/^\[/.test(cmd)) { |
| 21 | parsedCommand.optional.push({ |
| 22 | cmd: cmd.replace(bregex, '').split('|') as NotEmptyArray<string>, |
| 23 | variadic, |
| 24 | }); |
| 25 | } else { |
| 26 | parsedCommand.demanded.push({ |
| 27 | cmd: cmd.replace(bregex, '').split('|') as NotEmptyArray<string>, |
| 28 | variadic, |
| 29 | }); |
| 30 | } |
| 31 | }); |
| 32 | return parsedCommand; |
| 33 | } |
| 34 | |
| 35 | export interface ParsedCommand { |
| 36 | cmd: string; |
no outgoing calls
no test coverage detected
searching dependent graphs…