Skip to content

Commit

Permalink
Add runOnLaunch flag
Browse files Browse the repository at this point in the history
  • Loading branch information
xanderdeseyn committed Aug 19, 2022
1 parent af9d87a commit 937a7ee
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 35 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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".
}
}
};
Expand Down Expand Up @@ -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: [
Expand Down
75 changes: 42 additions & 33 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ extendConfig((config: HardhatConfig, userConfig: Readonly<HardhatUserConfig>) =>
verbose: task.verbose ?? false,
start: task.start ?? '',
clearOnStart: task.clearOnStart ?? false,
runOnLaunch: task.runOnLaunch ?? false,
}
})

Expand Down Expand Up @@ -79,48 +80,56 @@ 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,
ignoreInitial: true,
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)
Expand Down
2 changes: 2 additions & 0 deletions src/type-extensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ declare module 'hardhat/types/config' {
verbose?: boolean
start?: string
clearOnStart?: boolean
runOnLaunch?: boolean
}

// User facing config
Expand All @@ -33,6 +34,7 @@ declare module 'hardhat/types/config' {
verbose: boolean
start?: string
clearOnStart?: boolean
runOnLaunch?: boolean
}
}

Expand Down
1 change: 1 addition & 0 deletions test/fixture-projects/hardhat-project/hardhat.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const config: HardhatUserConfig = {
files: ['./contracts'],
ignoredFiles: ['**/.editortest'],
verbose: true,
runOnLaunch: true,
},
},
}
Expand Down
2 changes: 1 addition & 1 deletion test/project.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down

0 comments on commit 937a7ee

Please sign in to comment.