Skip to content

πŸƒ Run commands based on user input, dynamically generating environment variables, arguments, flags

License

Notifications You must be signed in to change notification settings

a-nozeret/prompt-run

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

46 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸƒ prompt-run

NPM Travis Build Codecov Coverage JavaScript Style Guide Commitizen friendly

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

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

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

example-1-cli

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

example-2-script

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

example-3-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

yarn release
git push --follow-tags origin master

Github Action will be triggered, publishing the package to NPM

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.

About

πŸƒ Run commands based on user input, dynamically generating environment variables, arguments, flags

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published