(
packageName: string,
options: { preMessage?: () => void } = {},
)
| 722 | } |
| 723 | |
| 724 | async installPackage( |
| 725 | packageName: string, |
| 726 | options: { preMessage?: () => void } = {}, |
| 727 | ): Promise<string> { |
| 728 | const packageManager = await this.getDefaultPackageManager(); |
| 729 | |
| 730 | if (!packageManager) { |
| 731 | this.logger.error("Can't find package manager"); |
| 732 | |
| 733 | process.exit(2); |
| 734 | } |
| 735 | |
| 736 | if (options.preMessage) { |
| 737 | options.preMessage(); |
| 738 | } |
| 739 | |
| 740 | const { createInterface } = await import("node:readline"); |
| 741 | |
| 742 | const prompt = ({ |
| 743 | message, |
| 744 | defaultResponse, |
| 745 | stream, |
| 746 | }: { |
| 747 | message: string; |
| 748 | defaultResponse: string; |
| 749 | stream: NodeJS.WritableStream; |
| 750 | }) => { |
| 751 | const rl = createInterface({ |
| 752 | input: process.stdin, |
| 753 | output: stream, |
| 754 | }); |
| 755 | |
| 756 | return new Promise((resolve) => { |
| 757 | rl.question(`${message} `, (answer: string) => { |
| 758 | // Close the stream |
| 759 | rl.close(); |
| 760 | |
| 761 | const response = (answer || defaultResponse).toLowerCase(); |
| 762 | |
| 763 | // Resolve with the input response |
| 764 | if (response === "y" || response === "yes") { |
| 765 | resolve(true); |
| 766 | } else { |
| 767 | resolve(false); |
| 768 | } |
| 769 | }); |
| 770 | }); |
| 771 | }; |
| 772 | |
| 773 | // yarn uses 'add' command, rest npm and pnpm both use 'install' |
| 774 | const commandArguments = [packageManager === "yarn" ? "add" : "install", "-D", packageName]; |
| 775 | const commandToBeRun = `${packageManager} ${commandArguments.join(" ")}`; |
| 776 | |
| 777 | let needInstall; |
| 778 | |
| 779 | try { |
| 780 | needInstall = await prompt({ |
| 781 | message: `[webpack-cli] Would you like to install '${this.colors.green( |
no test coverage detected