parse.d.ts 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. declare namespace parse {
  2. /** A shell control operator. */
  3. export interface ControlOperator {
  4. op: '||' | '&&' | ';;' | '|&' | '<(' | '<<<' | '>>' | '>&' | '<&' | '&' | ';' | '(' | ')' | '|' | '<' | '>';
  5. }
  6. /** A glob pattern parsed from the shell command. */
  7. export interface GlobPattern {
  8. op: 'glob';
  9. pattern: string;
  10. }
  11. /** A shell comment. */
  12. export interface Comment {
  13. comment: string;
  14. }
  15. /** A parsed token returned by {@link parse}. */
  16. export type ParseEntry = string | ControlOperator | GlobPattern | Comment;
  17. /** Options for the {@link parse} function. */
  18. export interface ParseOptions {
  19. /** Custom escape character. Defaults to `\\`. */
  20. escape?: string;
  21. /** Field-splits an unquoted variable expansion, the way a shell does using `IFS`; quoted expansions are never split. `true` uses the default IFS (space, tab, newline); a string uses its characters as the IFS. An empty string, `false`, or omitting the option disables splitting. Defaults to false. */
  22. splitUnquoted?: boolean | string;
  23. }
  24. export type Env =
  25. | Record<string, string | undefined>
  26. | ((key: string) => string | object | undefined);
  27. }
  28. /**
  29. * Parses a shell command string into an array of tokens.
  30. *
  31. * @param s - The shell command string to parse.
  32. * @param env - Optional environment variables for expansion, either as an object of string values or a lookup function. When the lookup function returns an object, that object is inserted into the result verbatim.
  33. * @param opts - Optional parsing options.
  34. * @returns An array of parsed tokens, including any objects returned by an `env` lookup function.
  35. */
  36. declare function parse<T extends string | object = never>(
  37. s: string,
  38. env?: parse.Env,
  39. opts?: parse.ParseOptions,
  40. ): (parse.ParseEntry | T)[];
  41. export = parse;