diff --git a/README.md b/README.md index c9b8d23..edccfda 100644 --- a/README.md +++ b/README.md @@ -109,5 +109,21 @@ POSITIONAL ARGUMENTS: test: Runs mocha tests For global options help run: hardhat help -``` +```` +### Changed file as argument + +The path of the changed file can be inserted into positional arguments using the template parameter `{path}`. This speeds up iteration in testing, especially if using single test isolation (for example, by using `it.only("test")` in mocha.) + +Example: +```` +module.exports = { + watcher: { + test: { + tasks: [{ command: 'test', params: { testFiles: ['{path}'] } }], + files: ['./test/**/*'], + verbose: true + } + } +} +```` diff --git a/package.json b/package.json index c0f2475..17873e6 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "hardhat-watcher", - "version": "2.0.0", + "version": "2.1.1", "description": "Hardhat file watcher plugin", "repository": "https://github.com/N1ghtly/hardhat-watcher", "author": "Xander Deseyn", diff --git a/src/index.ts b/src/index.ts index 1f695f7..ef3d77b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -56,6 +56,24 @@ task("watch", "Start the file watcher") logVerbose("Starting file watcher", taskConfig.files); + const templateReplace = (value: any, pattern: string, replace: string) => { + if (Array.isArray(value)) { + return value.map(v => v.replace(pattern, replace)); + } else if (typeof(value) == 'string') { + return value.replace(pattern, replace); + } else { + return value + } + } + + const paramsTemplateReplace = (params: any, pattern: string, replace: string) => { + const newParams: any = {} + Object.keys(params).forEach(k => { + newParams[k] = templateReplace(params[k], pattern, replace) + }); + return newParams; + } + // Validate tasks taskConfig.tasks.forEach((task) => { if (!(task.command in tasks)) { @@ -73,16 +91,20 @@ task("watch", "Start the file watcher") usePolling: true, interval: 250, }) - .on("change", async () => { + .on("change", async path => { for (let i = 0; i < taskConfig.tasks.length; i++) { const task = taskConfig.tasks[i]; + + // Replace template pattern with the changed file + const newParams = paramsTemplateReplace(task.params, '{path}', path) + logVerbose( `Running task "${task.command}" with params ${JSON.stringify( - task.params + newParams )}` ); try { - await run(task.command, task.params); + await run(task.command, newParams); // This hack is required to allow running Mocha commands. Check out https://github.com/mochajs/mocha/issues/1938 for more details. Object.keys(require.cache).forEach(function (key) { if (key.startsWith(paths.tests)) {