Merge pull request #5776 from electron/fix-node-output

Correctly redirect output to console on Windows
This commit is contained in:
Cheng Zhao 2016-05-31 03:19:58 +00:00
commit 6c705fdd70
3 changed files with 22 additions and 14 deletions

View file

@ -15,6 +15,7 @@
#include "atom/app/atom_main_delegate.h" #include "atom/app/atom_main_delegate.h"
#include "atom/common/crash_reporter/win/crash_service_main.h" #include "atom/common/crash_reporter/win/crash_service_main.h"
#include "base/environment.h" #include "base/environment.h"
#include "base/process/launch.h"
#include "base/win/windows_version.h" #include "base/win/windows_version.h"
#include "content/public/app/sandbox_helper_win.h" #include "content/public/app/sandbox_helper_win.h"
#include "sandbox/win/src/sandbox_types.h" #include "sandbox/win/src/sandbox_types.h"
@ -46,10 +47,6 @@ bool IsEnvSet(const char* name) {
#endif #endif
} }
bool IsRunAsNode() {
return IsEnvSet(kRunAsNode);
}
} // namespace } // namespace
#if defined(OS_WIN) #if defined(OS_WIN)
@ -57,14 +54,11 @@ int APIENTRY wWinMain(HINSTANCE instance, HINSTANCE, wchar_t* cmd, int) {
int argc = 0; int argc = 0;
wchar_t** wargv = ::CommandLineToArgvW(::GetCommandLineW(), &argc); wchar_t** wargv = ::CommandLineToArgvW(::GetCommandLineW(), &argc);
// Make output work in console if we are not in cygiwn. bool run_as_node = IsEnvSet(kRunAsNode);
if (!IsEnvSet("TERM") && !IsEnvSet("ELECTRON_NO_ATTACH_CONSOLE")) {
AttachConsole(ATTACH_PARENT_PROCESS);
FILE* dontcare; // Make sure the output is printed to console.
freopen_s(&dontcare, "CON", "w", stdout); if (run_as_node || !IsEnvSet("ELECTRON_NO_ATTACH_CONSOLE"))
freopen_s(&dontcare, "CON", "w", stderr); base::RouteStdioToConsole(false);
}
// Convert argv to to UTF8 // Convert argv to to UTF8
char** argv = new char*[argc]; char** argv = new char*[argc];
@ -100,7 +94,7 @@ int APIENTRY wWinMain(HINSTANCE instance, HINSTANCE, wchar_t* cmd, int) {
} }
} }
if (IsRunAsNode()) { if (run_as_node) {
// Now that argv conversion is done, we can finally start. // Now that argv conversion is done, we can finally start.
base::AtExitManager atexit_manager; base::AtExitManager atexit_manager;
base::i18n::InitializeICU(); base::i18n::InitializeICU();
@ -123,7 +117,7 @@ int APIENTRY wWinMain(HINSTANCE instance, HINSTANCE, wchar_t* cmd, int) {
#elif defined(OS_LINUX) // defined(OS_WIN) #elif defined(OS_LINUX) // defined(OS_WIN)
int main(int argc, const char* argv[]) { int main(int argc, const char* argv[]) {
if (IsRunAsNode()) { if (IsEnvSet(kRunAsNode)) {
base::i18n::InitializeICU(); base::i18n::InitializeICU();
base::AtExitManager atexit_manager; base::AtExitManager atexit_manager;
return atom::NodeMain(argc, const_cast<char**>(argv)); return atom::NodeMain(argc, const_cast<char**>(argv));
@ -140,7 +134,7 @@ int main(int argc, const char* argv[]) {
#else // defined(OS_LINUX) #else // defined(OS_LINUX)
int main(int argc, const char* argv[]) { int main(int argc, const char* argv[]) {
if (IsRunAsNode()) { if (IsEnvSet(kRunAsNode)) {
return AtomInitializeICUandStartNode(argc, const_cast<char**>(argv)); return AtomInitializeICUandStartNode(argc, const_cast<char**>(argv));
} }

View file

@ -0,0 +1 @@
process.stdout.write('pipes stdio')

View file

@ -76,6 +76,19 @@ describe('node feature', function () {
}) })
child.send('message') child.send('message')
}) })
it('pipes stdio', function (done) {
let child = child_process.fork(path.join(fixtures, 'module', 'process-stdout.js'), {silent: true})
let data = ''
child.stdout.on('data', (chunk) => {
data += String(chunk)
})
child.on('exit', (code) => {
assert.equal(code, 0)
assert.equal(data, 'pipes stdio')
done()
})
})
}) })
}) })