Skip to content

Commit

Permalink
feat: add more replacements (#114)
Browse files Browse the repository at this point in the history
  • Loading branch information
forivall authored Oct 21, 2020
1 parent 97aa645 commit a5af5d7
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 10 deletions.
13 changes: 9 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,26 @@ onchange 'app/**/*.js' 'test/**/*.js' -- npm test
onchange -i -k '**/*.js' -- node server.js

# On every change `echo` file change.
onchange '**/*.js' -- echo '{{event}} to {{changed}}'
onchange '**/*.js' -- echo '{{event}} to {{file}}'
```

NOTE: Windows users may need to use double quotes rather than single quotes. If used in an npm script, remember to escape the double quotes.

You can match as many glob patterns as you like, just put the command you want to run after the `--` and it will run any time a file matching any of the globs is added changed or deleted.

Other available replacement variables are `fileExt`
(`path.extname(file)`), `fileBase` (`path.basename(file)`),
`fileBaseNoExt` (basename, without extension), and `fileDir`
(`path.dirname(file)`).

## Options

### Add (`-a`, `--add`)

To execute the command for all initially added paths:

```sh
onchange -a 'config.json' -- microservice-proxy -c {{changed}} -p 9000
onchange -a 'config.json' -- microservice-proxy -c {{file}} -p 9000
```

### Initial (`-i`, `--initial`)
Expand Down Expand Up @@ -80,7 +85,7 @@ onchange -k '**/*.js' -- npm test
Set the maximum concurrent processes to run (default is `1`):

```sh
onchange -j2 '**/*.js' -- cp -v -r '{{changed}}' 'test/{{changed}}'
onchange -j2 '**/*.js' -- cp -v -r '{{file}}' 'test/{{file}}'
```

### Delay (`-d`, `--delay`)
Expand Down Expand Up @@ -112,7 +117,7 @@ onchange -p 100 '**/*.js' -- npm test
Shell command to execute every change:

```sh
onchange -o '> .changelog' 'src/**/*.js' -- echo '{{event}} to {{changed}}'
onchange -o '> .changelog' 'src/**/*.js' -- echo '{{event}} to {{file}}'
```

**P.S.** When a command is used with `--outpipe`, the `stdout` from the command will be piped into `outpipe`.
Expand Down
27 changes: 21 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import treeKill from "tree-kill";
import { resolve } from "path";
import { resolve, dirname, extname, basename } from "path";
import { ChildProcess } from "child_process";
import { spawn } from "cross-spawn";
import chokidar from "chokidar";
Expand All @@ -15,7 +15,13 @@ const ECHO_CMD = `${quote(process.execPath)} ${quote(ECHO_JS_PATH)}`;
*/
interface State {
event: string;
/** @deprecated use file instead */
changed: string;
file: string;
fileExt: string;
fileBase: string;
fileBaseNoExt: string;
fileDir: string;
}

/**
Expand Down Expand Up @@ -207,8 +213,17 @@ export function onchange(options: Options): () => void {
/**
* Enqueue the next change event to run.
*/
function enqueue(event: string, changed: string) {
const state: State = { event, changed };
function enqueue(event: string, file: string) {
const fileExt = extname(file);
const state: State = {
event,
changed: file,
file,
fileExt,
fileBase: basename(file),
fileBaseNoExt: basename(file, fileExt),
fileDir: dirname(file),
};

// Kill all existing tasks on `enqueue`.
if (kill) {
Expand All @@ -217,7 +232,7 @@ export function onchange(options: Options): () => void {
}

// Log the event and the file affected.
log(`${changed}: ${event}`);
log(`${file}: ${event}`);

// Add item to job queue.
queue.push(
Expand All @@ -236,10 +251,10 @@ export function onchange(options: Options): () => void {
if (initial) enqueue("", "");

// For any change, creation or deletion, try to run.
watcher.on("all", (event, changed) => {
watcher.on("all", (event, file) => {
if (filter.length && filter.indexOf(event) === -1) return;

return enqueue(event, changed);
return enqueue(event, file);
});

// On ready, prepare triggers.
Expand Down

0 comments on commit a5af5d7

Please sign in to comment.