1.4.5 • Published 4 years ago

prompt-run v1.4.5

Weekly downloads
876
License
MIT
Repository
github
Last release
4 years ago

Run commands based on user input, dynamically generating environment variables, arguments, flags

Getting started

yarn add -D prompt-run
prompt-run [options] [command]

API

Note: Check the Inquirer documentation for full details on how to create question objects.

CLI

  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

Module

const promptRun = require('prompt-run')

promptRun({
  command: 'yarn',
  options: {},
  questions: {
    env: [],
    args: [],
  },
}).then((childProcess) => {})

Command

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.

Options

NameTypeDefaultDescription
configstring"prompt-run.js"Path to questions config file
prefixstringundefinedPrompt existing scripts starting with the given prefix
dryRunbooleanfalseNo execution after generating the command
silentbooleanfalseDisable output of the generated command

Questions

Questions config file
module.exports = () => ({
  env: [
    // Question objects
  ],
  args: [
    // Question objects
  ],
})

The config is divided into:

  • env: Node Environment variables to prompt
  • args: Arguments/flags prompted. Anything coming after the base command.
Questions object

Check the Inquirer documentation for full details on how to create question objects.

Examples

1. CLI

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

2. Scripts

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

3. Prefix shortcut

Prompt existing scripts starting with a given prefix

package.json

{
  "scripts": {
    "start:dev": "...",
    "start:prod": "...",
    "start:docker": "..."
  }
}

Run

prompt-run -p start

Development

yarn
yarn link
...
yarn test --coverage
yarn lint
yarn commit
yarn unlink

Publish / release

Github Action auto-publishing for each commit pushed to master.

Todo list

  • 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

License

Licensed under the MIT License, Copyright © 2019-present Antoine Nozeret.

See LICENSE for more information.

1.4.5

4 years ago

1.4.4

4 years ago

1.4.0

4 years ago

1.3.0

4 years ago

1.2.1

4 years ago

1.1.0

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago