Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

command-workflow

Package Overview
Dependencies
Maintainers
1
Versions
20
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

command-workflow

command-workflow Configurable command workflow that executes specific tasks based on commands. Simple, flexible, and easily expandable. <a href="https://github.com/murongg/command-workflow/blob/ma

  • 0.3.12
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
0
decreased by-100%
Maintainers
1
Weekly downloads
 
Created
Source

command-workflow

Configurable command workflow that executes specific tasks based on commands. Simple, flexible, and easily expandable.

English|中文文档

NPM version

Installation

npm install command-workflow -D

Usage

Basic Usage


```bash
# create config file
touch cwf.config.js
// cwf.config.js
import { defineConfig } from 'command-workflow'

const filename = new Date().getTime()

export default defineConfig({
  steps: [
    'ls',
    {
      command: 'touch #{filename}',
      tags: {
        filename: () => {
          console.log('filename:', filename)
          return filename
        }
      }
    }, {
      command: 'vim #{filename}',
      tags: {
        filename: () => {
          return filename
        }
      }
    }],
})
# Run command 
npx cwf
# Run log
$ cwf
11:03:10 AM [CWF] Run command: ls
cwf.config.js           node_modules            package-lock.json       package.json
filename: 1690340590431
11:03:10 AM [CWF] Run command: touch 1690340590431
11:03:10 AM [CWF] Run command: vim 1690340590431
✨  Done in 1.99s.

CWF allows you to use the #{tag} format in the command field to pass custom tags. You can use the tag names as keys in the tags field and pass methods or strings as values to CWF. CWF will parse these tags, apply them to the command, and execute the corresponding operations.
Of course, CWF also provides some built-in tags for your convenience. View

Advanced Usage

# create config file
touch cwf.config.js
// cwf.config.js
import { defineConfig } from 'command-workflow'

const filename = new Date().getTime()

export default defineConfig({
  firstCommand: {
    steps: [{
      command: 'ls',
    }, {
      command: 'touch #{filename}',
      tags: {
        filename: () => {
          console.log('filename:', filename)
          return filename
        }
      }
    }, {
      command: 'vim #{filename}',
      tags: {
        filename: () => {
          return filename
        }
      }
    }],
  }
})
# Run command 
npx cwf firstCommand
# Run log
$ cwf
11:03:10 AM [CWF] Run command: ls
cwf.config.js           node_modules            package-lock.json       package.json
filename: 1690340590431
11:03:10 AM [CWF] Run command: touch 1690340590431
11:03:10 AM [CWF] Run command: vim 1690340590431
✨  Done in 1.99s.

You can customize CWF sub-commands in the configuration file and implement multiple command workflows by appending custom sub-commands after the CWF command. As shown above, by defining a sub-command named firstCommand in the configuration file, you can execute the specified workflow by running the cwf firstCommand command. This way, you can easily configure and execute multiple command workflows according to your needs.

Specify Steps

You can define unikey in steps, and then specify the steps to be executed through the command line.
Use cwf -s 'xxx' to specify the steps to be executed. The value is the unikey you set, use , split.

// cwf.config.js
import { defineConfig } from 'command-workflow'

let gitUser = ''

export default defineConfig({
  steps: [{
    command: 'ls',
    unikey: '1'
  }, {
    unikey: '2',
    command: 'touch #{git_user_name}',
    before: (command, tags) => {
      console.log('before command: ', command)
      console.log('before tags: ', tags)
      gitUser = tags.git_user_name
    },
    after: (command, exec) => {
      console.log('after real command: ', command)
      console.log('after exec: ', exec)
    }
  }, {
    unikey: '3',
    command: 'echo #{user}',
    tags: {
      user: () => gitUser
    }
  }],
})
# Run command 
npx cwf -s '2,3,1'
# Run log
$ cwf -s '2,3,1'
before command:  touch murong
before tags:  { git_user_name: 'murong' }
4:22:41 PM [CWF] Run command: touch murong
after real command:  touch murong
after exec:  null
4:22:41 PM [CWF] Run command: echo murong
murong
4:22:41 PM [CWF] Run command: ls
cwf.config.js           node_modules            package-lock.json       
package.json            murong
✨  Done in 2.21s.

Use hooks

  • before: Before executing the command, a callback function can be used to modify the command and tag collection. This callback function takes the command and tag collection as parameters and allows for modifications to the command during execution. Once the callback function has completed, the program will execute the modified command returned by the callback function.
  • after: After the command is executed, you can retrieve the executed command and the execution result through a callback function. The callback function takes the command and execution result as parameters, and it does not have a return value. This allows you to conveniently view the final executed command and its corresponding execution result.
  • error: When an error occurs during command execution, you can use a callback function to handle the error. The callback function takes the error as a parameter and does not have a return value. This allows you to conveniently handle errors that occur during command execution.
// cwf.config.js
export default defineConfig({
  steps: [{
    command: 'ls',
  }, {
    command: 'touch #{git_user_name}',
    before: (command, tags) => {
      console.log('before command: ', command)
      console.log('before tags: ', tags)
      return `${command}-murong`
    },
    after: (command, exec) => {
      console.log('after real command: ', command)
      console.log('after exec: ', exec)
    },
    // eslint-disable-next-line n/handle-callback-err
    error: (err) => {
      console.log('error:', error)
    }
  }],
})

Built-in tag

TagDescriptionExample
#{timestamp}Timestamptouch #{timestamp}
#{current_git_branch}Current git branchgit checkout -b #{current_git_branch}
#{current_git_commit}Current git commit hashgit commit -m #{current_git_commit}
#{current_git_commit_message}Current git commit messagegit commit -m #{current_git_commit_message}
#{current_git_tag}Current git taggit tag #{current_git_tag}
#{current_git_repo}Current git repogit clone #{current_git_repo}
#{current_git_repo_url}Current git repo urlgit clone #{current_git_repo_url}
#{current_git_repo_name}Current git repo nameecho #{current_git_repo_name}
#{current_git_repo_owner}Current git repo ownerecho #{current_git_repo_owner}
#{git_user_name}Local git user nameecho #{git_user_name}
#{git_user_email}Local git user emailecho #{git_user_email}

Config

UserConfig

NameDescriptionTypeDefaultRequired
stepsworkflow stepStep[]
logLevellog levelerror warn info silentinfo
logTimelog time options
isSkipErrorWhether to skip the error logbooleanfalse
isThrowErrorBreakDo not continue execution if an error occursbooleanfalse

Step

NameDescriptionTypeDefaultRequired
commandcommand to executestring
tagstags map[x: string]: (() => string)string
disabledWhether to disabled command to runboolean ((command: string, tags: Record<string, any>) => boolean)false
errorerror callback, no return value(error: Error) => void
beforeCallback before the command is executed, the return value is the command you expect to be executed eventually, the return value is not required(command: string, tags: Record<string, any>) => string
afterCallback after the command is executed, no return value(command: string, buffer: Buffer) => void

CLI Options

OptionDescriptionExample
-c, --config <file>Path to config filecwf -c cwf.custom.config.js
-r, --root <root>Path to config root.cwf -r /Users/murong/documents
-t, --tags <tags>Global tags for commandcwf --tags 'tag1=1|tag2=2|tag3=3'
-s, --specify-steps <steps>Specify steps to run, the value is the unikey you set.cwf -s '1,3,2'

FAQs

Package last updated on 31 Jul 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc