I am not able to write a simple plugin that uses a callback and see the results of the callback. Here is plugin code:
const request = require('request');
module.exports = (config) => request('http://google.com', {}, (err, res, body) => {
console.log("Hi, rocky!")
})
Running this produces no output.
Go through the plugin instruction at https://github.com/trufflesuite/truffle/releases and use the above code.
Hi, rocky
no output.
Tried same code and it doesn't work. It would be useful to allow truffle handle plugins with asynchronous code correctly. I don't see any reason why it can't be implemented. I've looked into source code and found that callback function done is called right after plugin command has been called. Why just not pass done as second parameter to a plugin's exposed function?
https://github.com/trufflesuite/truffle/blob/c1da40e23a7f30fa0673130c428c72725c06c05f/packages/truffle-core/lib/commands/run.js#L31
So this is a code responsible for calling plugin command
if (config.plugins) {
let pluginConfigs = Plugin.load(config, done);
Run.run(pluginConfigs, customCommand, config);
done();
} else {
If we could rewrite it as
if (config.plugins) {
let pluginConfigs = Plugin.load(config, done);
Run.run(pluginConfigs, customCommand, config, done);
} else {
From
run(pluginConfigs, customCommand, config) {
let runCommand = this.initializeCommand(pluginConfigs, customCommand);
runCommand(config);
}
to
run(pluginConfigs, customCommand, config, done) {
let runCommand = this.initializeCommand(pluginConfigs, customCommand);
runCommand(config, done);
}
We could then write plugin as
const request = require('request');
module.exports = (config, done) => request('http://google.com', {}, (err, res, body) => {
console.log("Hi, rocky!");
done();
})
@daniyarchambylov thanks for investigating this!
I spoke with @rocky and concluded that we should support plugins that accept a callback as well as plugins that return a promise. I don't suppose you would be interested in opening a PR containing this change?
Thank you!
@gnidan
Working on it!
@gnidan submited PR https://github.com/trufflesuite/truffle/pull/1512
After PR #1519 has been merged and truffle version updated to v5.0.0-next.20 running 3rd party plugins fail due to missing dependency. Required package app-module-path hasn't been installed.
npm i [email protected]./node_modules/.bin/truffle run analyzeCommand should execute successfully without errors.
$ ./node_modules/.bin/truffle run analyze --help
Error: Cannot find module 'app-module-path'
at Function.Module._resolveFilename (module.js:547:15)
at Function.Module._load (module.js:474:25)
at Module.require (module.js:596:17)
at require (internal/module.js:11:18)
at checkPluginModules (/home/daniyar/tmp/truffle-next-20/node_modules/truffle/build/webpack:/packages/truffle-core/lib/plugin.js:18:1)
at /home/daniyar/tmp/truffle-next-20/node_modules/truffle/build/webpack:/packages/truffle-core/~/lodash/_createFlow.js:71:1
at Object.load (/home/daniyar/tmp/truffle-next-20/node_modules/truffle/build/webpack:/packages/truffle-core/lib/plugin.js:70:1)
at Object.run (/home/daniyar/tmp/truffle-next-20/node_modules/truffle/build/webpack:/packages/truffle-core/lib/commands/run.js:29:1)
at Command.run (/home/daniyar/tmp/truffle-next-20/node_modules/truffle/build/webpack:/packages/truffle-core/lib/command.js:113:1)
at Object.<anonymous> (/home/daniyar/tmp/truffle-next-20/node_modules/truffle/build/webpack:/packages/truffle-core/cli.js:47:1)
at __webpack_require__ (/home/daniyar/tmp/truffle-next-20/node_modules/truffle/build/webpack:/webpack/bootstrap 89bbbec125c6289b9791:19:1)
at /home/daniyar/tmp/truffle-next-20/node_modules/truffle/build/webpack:/webpack/bootstrap 89bbbec125c6289b9791:65:1
at Object.<anonymous> (/home/daniyar/tmp/truffle-next-20/node_modules/truffle/build/cli.bundled.js:71:10)
at Module._compile (module.js:652:30)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
at Function.Module.runMain (module.js:693:10)
at startup (bootstrap_node.js:188:16)
at bootstrap_node.js:609:3
Truffle v5.0.0-next.20 (core: 5.0.0-beta.2)
Solidity v0.5.0 (solc-js)
Node v8.10.0
truffle version): v5.0.0-next.20 (core: 5.0.0-beta.2)node --version): v8.10.0npm --version): v.3.5.2@daniyarchambylov the app-module-path problem should be resolved now on 5.0.0-next.22
All fixed now. Thanks for all the great work!
@gnidan I confirm it's working with 5.0.0-next.24 for sure!
Most helpful comment
@daniyarchambylov thanks for investigating this!
I spoke with @rocky and concluded that we should support plugins that accept a callback as well as plugins that return a promise. I don't suppose you would be interested in opening a PR containing this change?
Thank you!