Run commands based on user input, dynamically generating environment variables, arguments, flags
yarn add -D prompt-run
prompt-run [options] [command]
Note: Check the Inquirer documentation for full details on how to create question objects.
Usage
$ prompt-run [options] [command]
Options
--config, -c <filename> Path to questions config file
--prefix, -p <prefix> Prompt scripts with the provided prefix
--dry-run No execution after generating the command
--silent, -s Disable output of generated command
const promptRun = require('prompt-run')
promptRun({
command: 'yarn',
options: {},
questions: {
env: [],
args: [],
},
}).then((childProcess) => {})
The base used to run along the generated command.
Default: yarn
(or npm run
if your project doesn't contain a yarn.lock
file)
It can be anything which is not needed to prompt.
Name | Type | Default | Description |
---|---|---|---|
config | string | "prompt-run.js" | Path to questions config file |
prefix | string | undefined | Prompt existing scripts starting with the given prefix |
dryRun | boolean | false | No execution after generating the command |
silent | boolean | false | Disable output of the generated command |
module.exports = () => ({
env: [
// Question objects
],
args: [
// Question objects
],
})
The config is divided into:
env
: Node Environment variables to promptargs
: Arguments/flags prompted. Anything coming after the base command.
Check the Inquirer documentation for full details on how to create question objects.
With a defined config of questions
questions.js
module.exports = () => ({
env: [
{
type: 'list',
name: 'NODE_ENV',
choices: ['production', 'development'],
},
{
name: 'SECRET_KEY',
},
],
args: [
{
type: 'confirm',
name: '--watch',
default: false,
},
{
name: '--log-level',
type: 'list',
choices: ['error', 'warning'],
},
],
})
Run
prompt-run -c questions.js yarn start
Example output / command executed
$ NODE_ENV=development SECRET_KEY=1234 yarn start --watch --log-level warning
As a node module in scripts
dependency-info.js
const promptRun = require('prompt-run')
const packageJson = require('./package.json')
const dependencies = Object.keys(packageJson.dependencies)
const fields = ['description', 'readme', 'version', 'dependencies']
promptRun({
command: 'yarn info',
questions: {
args: [
{
type: 'list',
name: 'dependency',
message: 'Select a dependency',
choices: dependencies,
},
{
type: 'list',
name: 'field',
message: 'Select a field to print',
choices: fields,
},
],
},
}).then((childProcess) => {
childProcess.on('close', () => {
console.log('\nFinished with the child process!')
})
})
Run
node dependency-info.js
Prompt existing scripts starting with a given prefix
package.json
{
"scripts": {
"start:dev": "...",
"start:prod": "...",
"start:docker": "..."
}
}
Run
prompt-run -p start
yarn
yarn link
...
yarn test --coverage
yarn lint
yarn commit
yarn unlink
yarn release
git push --follow-tags origin master
Github Action will be triggered, publishing the package to NPM
- Bugs:
- Inquirer errors do not appear directly in CLI mode
- Configs should consist in promptRun Object argument
- feature:
$ prompt-run -env NODE_ENV yarn start
shortcut for question
Licensed under the MIT License, Copyright Β© 2019-present Antoine Nozeret.
See LICENSE for more information.