Giter VIP home page Giter VIP logo

cli-utils's Introduction

cli-utils Build status

Description

A private collection of utilities for developing cli tools.

Usage

You should generally not use it. You would use tools built or developed with it, for example:

I may use it for building or developing other tools (see above). Below is a brief overview of what's in the box.

Programmatic usage

This package exposes the following utilities (see the respective source files for API docs):

  • commandUtils:

    • expandCmd(cmd: string, runtimeArgs: string[], config: IRunConfig): Promise<string>:
      Expand a command string, by substituting argument identifiers with the specified arguments. It also supports default/fallback arguments (specified either as static values or as commands to execute and use the output).

    • preprocessArgs(rawArgs: string[]): {args: string[], config: IRunConfig}:
      Preprocess a list of input arguments into a list of arguments that can be used for substituting into commands. Also, derive a configuration object to modify the behavior of commandUtils.run().

    • run(cmd: string, runtimeArgs?: string[], config?: IRunConfig): Promise<string>:
      Run a command. Could be a complex command with |, && and ||. It also supports argument substitution with commandUtils.expandCmd().

    • spawnAsPromised(cmd: string, config?: IRunConfig): Promise<string>:
      Spawn a complex command (or series of piped commands) and return a promise that resolves or rejects based on the command's outcome. It provides some extras on top of child_process.spawn().

  • processUtils:

    • doOnExit(proc: Process, action: Function): Function:
      Run the specified action, when exit or SIGINT are fired on the specified process.

    • suppressTerminateBatchJobConfirmation(proc: Process): Function:
      Suppress the "Terminate batch job (Y/N)?" confirmation on Windows for the specified process. Calling it with a non-Windows process is a no-op.

      NOTE: This is still an experimental feature and not guaranteed to work as expected. It is known to not work with certain types of commands (e.g. vim).

  • testingUtils:

    • testCmd(cmd: string, config?: IRunConfig): Promise<string>:
      Run the specified command using commandUtils.spawnAsPromised(), capture the output and return it (after normalizing newlines and trimming it).

    • testScriptFactory(scriptPath: string, config?: IRunConfig): Function:
      Create a function that can be used for testing a Node.js script with testingUtils.testCmd(). Different arguments can be passed per call of the returned function.

    • withJasmineTimeout(newTimeout: number, testSuite: Function): void:
      Run a test suite (i.e. describe() block) with a different DEFAULT_TIMEOUT_INTERVAL. The previous timeout interval is restored after all tests of the suite have completed.

Command-line usage

This package exposes the following commands (see the respective source files for API docs):

  • gkcu-expand-cmd "<cmd>" <arg1> <arg2> --gkcu-<arg3> ...:
    Expand a command string by substituting argument identifiers with the specified arguments. It also supports default/fallback arguments (specified either as static values or as commands to execute and use the output).

    Examples:

    gkcu-expand-cmd "echo \$1 \${2:bar} \$1" foo
    #--> echo foo bar foo
    
    gkcu-expand-cmd "echo \${1:Hello}, \${0:::whoami}!" Hey
    #--> echo Hey, gkalpak!
    
  • gkcu-run "<cmd>" <arg1> <arg2> --gkcu-<arg3> ...:
    Run a command with support for argument substitution. Could be a complex command with |, && and || (but not guaranteed to work if too complex :P).

    Examples:

    gkcu-run "echo \$1 \${2:bar} \$1" foo
    #--> foo bar foo
    
    gkcu-run "echo \${1:Hello}, \${0:::whoami}!" Hey
    #--> Hey, gkalpak!
    

Testing

The following test-types/modes are available:

  • Code-linting: npm run lint Lint TypeScript files using TSLint.

  • Unit tests: npm run test-unit Run all the unit tests once. These tests are quick and suitable to be run on every change.

  • E2E tests: npm run test-e2e Run all the end-to-end tests once. These test may hit actual API endpoints or perform expensive I/O operations and are considerably slower than unit tests.

  • All tests: npm test / npm run test Run all of the above tests (code-linting, unit tests, e2e tests). This command is automatically run before every release (via npm run release).

  • "Dev" mode: npm run dev Watch all files and rerun the unit tests whenever something changes. For performance reasons, code-linting and e2e tests are omitted.

TODO

Things I want to (but won't necessarily) do:

  • Add more unit tests for commandUtils.spawnAsPromised().

cli-utils's People

Contributors

gkalpak avatar

Watchers

 avatar

Forkers

kinker5

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.