Awesome Open Source
Awesome Open Source
Sponsorship

run-when Build Status

Run tasks based on git diff files

Usage

Having this directory tree with the following files changed:

ā”œā”€ā”€ app
ā”œā”€ā”€ā”€ā”€ā”€ā”€ components
ā”œ      ā”œā”€ā”€ index.js
ā”œ      ā”œā”€ā”€ app.jsx
ā””ā”€ā”€ __tests__
ā”œ      ā”œā”€ā”€ app.spec.jsx
ā””ā”€ā”€ package.json

run-when will check glob rules against it and run tasks if any changes have been made.

Javascript

import runWhen from 'run-when';

runWhen([
  {
    glob: ['app/components/index.js', 'app/__tests__/**'],
    task(paths) {
      console.log('This will be called!');
    }
  },
  {
    glob: ['!package.json'],
    task(paths) {
      return Promise.resolve('You can return a promise from your task');
    }
  },
  {
    // Optionally pass changed files
    changedFiles: () => Promise.resolve(['app/index.js', 'app/components/header.jsx']),
    glob: ['app/components/**'],
    task(paths) {
      console.log(paths === ['app/components/header.jsx']);
    }
  }
]);

CLI

$ run-when '["app/components/**", "app/utils/**"]' 'echo running tests... && yarn test'
  • First argument is a stringified JSON containing glob patterns.
  • Second argument is the task to run.

How it works

By default run-when will use git to know which files have been changed. You can change that passing an array of files to changedFiles.

Globbing pattern

The library uses multimatch for the globbing matching (sindresorhus šŸ˜»). Just a quick overview:

  • * matches any number of characters, but not /
  • ? matches a single character, but not /
  • ** matches any number of characters, including /, as long as it's the only thing in a path part
  • {} allows for a comma-separated list of "or" expressions
  • ! at the beginning of a pattern will negate the match

Various patterns and expected matches.

Api

type Files = Array<string>;
interface Rule {
  glob: Array<string>,
  task: (results: Files) => void,
  changedFiles?: () => Promise<Files>
}
type runWhen = (rules: Array<Rule>) => Promise;

Installation

$ yarn add run-when -D

Get A Weekly Email With Trending Projects For These Topics
No Spam. Unsubscribe easily at any time.
javascript (66,106)Ā 
cli (1,582)Ā 
task (90)Ā 
task-runner (46)Ā 
glob (26)Ā 

Find Open Source By Browsing 7,000 Topics Across 59 Categories