-
-
Notifications
You must be signed in to change notification settings - Fork 593
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: add utils package to webpack-cli
- Loading branch information
Showing
7 changed files
with
59 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
export function packageExists(packageName: string): boolean { | ||
try { | ||
require(packageName); | ||
return true; | ||
} catch (err) { | ||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { prompt } from 'enquirer'; | ||
import { green } from 'colorette'; | ||
import { runCommand } from './run-command'; | ||
|
||
/** | ||
* | ||
* @param packageName | ||
* @param preMessage Message to show before the question | ||
*/ | ||
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type | ||
export async function promptInstallation(packageName: string, preMessage?: Function) { | ||
const packageManager = exports.getPackageManager(); | ||
const options = [packageManager === 'yarn' ? 'add' : 'install', '-D', packageName]; | ||
|
||
const commandToBeRun = `${packageManager} ${options.join(' ')}`; | ||
if (preMessage) { | ||
preMessage(); | ||
} | ||
const question = `Would you like to install ${packageName}? (That will run ${green(commandToBeRun)})`; | ||
const { installConfirm } = await prompt([ | ||
{ | ||
type: 'confirm', | ||
name: 'installConfirm', | ||
message: question, | ||
initial: 'Y', | ||
}, | ||
]); | ||
if (installConfirm) { | ||
await runCommand(commandToBeRun); | ||
return exports.packageExists(packageName); | ||
} | ||
// eslint-disable-next-line require-atomic-updates | ||
process.exitCode = 2; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import execa from 'execa'; | ||
|
||
export async function runCommand(command, args = []): Promise<void> { | ||
try { | ||
await execa(command, args, { | ||
stdio: 'inherit', | ||
shell: true, | ||
}); | ||
} catch (e) { | ||
throw new Error(e); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters