| 507 | } |
| 508 | |
| 509 | class WebpackCLI { |
| 510 | #colors: Colors | undefined; |
| 511 | |
| 512 | // Created lazily because `#createColors` loads the (large) webpack package, |
| 513 | // which commands like `version`/`info` don't otherwise need. |
| 514 | get colors(): Colors { |
| 515 | return (this.#colors ??= this.#createColors()); |
| 516 | } |
| 517 | |
| 518 | set colors(value: Colors) { |
| 519 | this.#colors = value; |
| 520 | } |
| 521 | |
| 522 | logger: Logger; |
| 523 | |
| 524 | #isColorSupportChanged: boolean | undefined; |
| 525 | |
| 526 | // Flag tokens of the current invocation, used to register only the options |
| 527 | // actually present (instead of all ~850) when setting up a command. |
| 528 | #argvForParsing: readonly string[] | undefined; |
| 529 | |
| 530 | program: Command; |
| 531 | |
| 532 | constructor() { |
| 533 | this.logger = this.getLogger(); |
| 534 | |
| 535 | // Initialize program |
| 536 | this.program = program as Command; |
| 537 | this.program.name("webpack"); |
| 538 | this.program.configureOutput({ |
| 539 | writeErr: (str) => { |
| 540 | this.logger.error(str); |
| 541 | }, |
| 542 | outputError: (str, write) => { |
| 543 | write(`Error: ${this.capitalizeFirstLetter(str.replace(/^error:/, "").trim())}`); |
| 544 | }, |
| 545 | }); |
| 546 | } |
| 547 | |
| 548 | #createColors(useColor?: boolean): Colors { |
| 549 | let pkg: typeof webpack | undefined; |
| 550 | |
| 551 | try { |
| 552 | pkg = require(WEBPACK_PACKAGE); |
| 553 | } catch { |
| 554 | // Nothing |
| 555 | } |
| 556 | |
| 557 | // Some big repos can have a problem with update webpack everywhere, so let's create a simple proxy for colors |
| 558 | if (!pkg || !pkg.cli || typeof pkg.cli.createColors !== "function") { |
| 559 | // webpack provides the color helpers; when it isn't available, fall back to |
| 560 | // identity functions so output is plain (uncolored) text. Returning the |
| 561 | // string unchanged (rather than an array) is important because the value is |
| 562 | // passed to consumers — e.g. commander's `formatItem` — that expect a string. |
| 563 | return new Proxy({} as Colors, { |
| 564 | get(_target, property) { |
| 565 | return property === "isColorSupported" ? false : (value: string) => value; |
| 566 | }, |
nothing calls this directly
no test coverage detected