[
https://issues.apache.org/jira/browse/CB-8763?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=14531417#comment-14531417
]
Rob Paveza commented on CB-8763:
--------------------------------
I've spent some time trying to get this issue to reproduce and I can't.
However, what I have observed is that if you drop .js files into a named hooks
directory, the .js file executes incorrectly with the child_process.spawn
function.
If you dig up a little bit earlier in HooksRunner, you see that if there isn't
an explicit choice to use the module loader or not, we choose to do so if the
file ends in .js:
https://github.com/apache/cordova-lib/blob/d77ebc40a16ab27a8ed22c0bbf2ab8a84a5ac448/cordova-lib/src/hooks/HooksRunner.js#L123
That all having been said, here's what I did to investigate:
- Created a new Cordova project with Windows and Android
- Added hooks/before_compile_custom_action.js and set it to output something
via console.log, both synchronously (log then throw) and asynchronously
(returning a Q promise)
- Added that to config.xml
- That ran fine, both output my console message before "ERROR building one of
the platforms:..."
- Added hooks/before_compile_custom_action.bat and set it to output something
via echo and exit
- Replaced previous config.xml thing with the .bat file
- That ran fine, it printed my message before "Hook failed with exit code 1"
The specific error I see is if I add hooks/before_build/test.js which is
identical to the previous .js file (and take the <hook /> element out of
config.xml), I get the following output instead:
C:\Users\me\test>cordova build windows
Running command: C:\Users\me\test\hooks\before_build\test.js C:\Users\me\test
Error: spawn UNKNOWN
This seems instead to point that, when .js files are discovered via just the
directory structure, they don't get loaded via the module loader. So keying
off of that question, I looked for where that might get populated, and found
this comment:
https://github.com/apache/cordova-lib/blob/d77ebc40a16ab27a8ed22c0bbf2ab8a84a5ac448/cordova-lib/src/hooks/scriptsFinder.js#L98
So there ya go. If you want to use module-syntax, you need to load the script
via a <hook> element.
> Unable to use console logging with hooks when using exports
> -----------------------------------------------------------
>
> Key: CB-8763
> URL: https://issues.apache.org/jira/browse/CB-8763
> Project: Apache Cordova
> Issue Type: Bug
> Components: CLI
> Reporter: Raymond Camden
> Assignee: Rob Paveza
> Priority: Minor
>
> I was testing Holly's after_platform_add hook and decided to rewrite it
> inside a module.exports format. This is what the hook's ReadMe.md guide
> recommends. When I did that, I noticed that my console.log messages stopped
> working. I posted this on the dev group and Shazron noted:
> https://github.com/apache/cordova-lib/blob/d77ebc40a16ab27a8ed22c0bbf2ab8a84a5ac448/cordova-lib/src/hooks/HooksRunner.js#L188
> "It's using child_process.spawn (if you dig down) and is probably
> ignoring all stdio when running the script.
> actually it has stdio:inherit, and does capture the output and pushes
> it back up the chain, so somewhere up the chain no one does anything
> with it"
> Apparently there is no workaround for this currently.
--
This message was sent by Atlassian JIRA
(v6.3.4#6332)
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]