Skip to content

Commit

Permalink
Merge pull request #3 from NotRealAI/main
Browse files Browse the repository at this point in the history
Pass changed file as template parameter to watchers
  • Loading branch information
xanderdeseyn authored Jan 30, 2021
2 parents 337231e + c4935b0 commit fab1c05
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 5 deletions.
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}
````

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "hardhat-watcher",
"version": "2.0.0",
"version": "2.1.0",
"description": "Hardhat file watcher plugin",
"repository": "https://github.com/N1ghtly/hardhat-watcher",
"author": "Xander Deseyn",
Expand Down
28 changes: 25 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -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)) {
Expand Down

0 comments on commit fab1c05

Please sign in to comment.