diff --git a/README.md b/README.md index f0f5870..48d0247 100644 --- a/README.md +++ b/README.md @@ -18,13 +18,18 @@ 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`) @@ -32,7 +37,7 @@ You can match as many glob patterns as you like, just put the command you want t 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`) @@ -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`) @@ -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`. diff --git a/src/index.ts b/src/index.ts index 45724c2..9dbb9bb 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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"; @@ -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; } /** @@ -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) { @@ -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( @@ -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.