diff --git a/README.md b/README.md index e9be9fc..5ed131f 100644 --- a/README.md +++ b/README.md @@ -51,6 +51,7 @@ module.exports = { verbose?: boolean; // Turn on for extra logging clearOnStart?: boolean; // Turn on to clear the logs (of older task runs) each time before running the task start?: string; // Run any desirable command each time before the task runs + runOnLaunch?: boolean; // Turn on to run tasks immediately on launch. Be aware, tasks will be run with path parameter "none". } } }; @@ -83,7 +84,7 @@ module.exports = { ignoredFiles: ['**/.vscode'], verbose: true, clearOnStart: true, - start: 'echo Running my compilation task now..' + start: 'echo Running my compilation task now..', }, ci: { tasks: [ diff --git a/src/index.ts b/src/index.ts index dd077b5..316b4c5 100644 --- a/src/index.ts +++ b/src/index.ts @@ -30,6 +30,7 @@ extendConfig((config: HardhatConfig, userConfig: Readonly) => verbose: task.verbose ?? false, start: task.start ?? '', clearOnStart: task.clearOnStart ?? false, + runOnLaunch: task.runOnLaunch ?? false, } }) @@ -79,6 +80,42 @@ task('watch', 'Start the file watcher') } }) + const runTasks = async (path: string) => { + // Clear on on changed files received + if (taskConfig.clearOnStart) { + console.clear() + } + if (taskConfig.start) { + try { + execSync(taskConfig.start, { stdio: 'inherit' }) + } catch (error) { + console.log("Failed to execute 'start' script:", taskConfig.start) + console.error(error) + } + } + + 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(newParams)}`) + try { + 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)) { + delete require.cache[key] + } + }) + } catch (err) { + console.log(`Task "${task.command}" failed.`) + console.log(err) + } + } + } + chokidar .watch(taskConfig.files, { ignored: taskConfig.ignoredFiles, @@ -86,41 +123,13 @@ task('watch', 'Start the file watcher') usePolling: true, interval: 250, }) - .on('change', async path => { - // Clear on on changed files received - if (taskConfig.clearOnStart) { - console.clear() - } - if (taskConfig.start) { - try { - execSync(taskConfig.start, { stdio: 'inherit' }) - } catch (error) { - console.log("Failed to execute 'start' script:", taskConfig.start) - console.error(error) - } - } - - 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(newParams)}`) - try { - 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)) { - delete require.cache[key] - } - }) - } catch (err) { - console.log(`Task "${task.command}" failed.`) - console.log(err) - } + .on('ready', () => { + if (taskConfig.runOnLaunch) { + console.log('Run on launch is enabled, immediately running tasks.') + runTasks('none') } }) + .on('change', runTasks) .on('error', (error: Error) => { console.log(`Watcher error: ${error}`) process.exit(1) diff --git a/src/type-extensions.ts b/src/type-extensions.ts index 86ec32c..f18c34a 100644 --- a/src/type-extensions.ts +++ b/src/type-extensions.ts @@ -18,6 +18,7 @@ declare module 'hardhat/types/config' { verbose?: boolean start?: string clearOnStart?: boolean + runOnLaunch?: boolean } // User facing config @@ -33,6 +34,7 @@ declare module 'hardhat/types/config' { verbose: boolean start?: string clearOnStart?: boolean + runOnLaunch?: boolean } } diff --git a/test/fixture-projects/hardhat-project/hardhat.config.ts b/test/fixture-projects/hardhat-project/hardhat.config.ts index 3a70257..4c04008 100644 --- a/test/fixture-projects/hardhat-project/hardhat.config.ts +++ b/test/fixture-projects/hardhat-project/hardhat.config.ts @@ -11,6 +11,7 @@ const config: HardhatUserConfig = { files: ['./contracts'], ignoredFiles: ['**/.editortest'], verbose: true, + runOnLaunch: true, }, }, } diff --git a/test/project.test.ts b/test/project.test.ts index 5fffdf1..2106ff3 100644 --- a/test/project.test.ts +++ b/test/project.test.ts @@ -17,7 +17,7 @@ describe('Watcher tests', function () { simulateFileChange('Contract.sol') simulateFileChange('.editortest', 'contracts/.editortest') this.hre.run('watch', { watcherTask: 'compilation' }) - await sleep(1000) + await sleep(5000) console.log('=========== Simulating Contract.sol change =============') simulateFileChange('Contract2.sol')