diff --git a/atom/app/atom_main.cc b/atom/app/atom_main.cc index 03055e04daf5..1e69ba4ba839 100644 --- a/atom/app/atom_main.cc +++ b/atom/app/atom_main.cc @@ -15,6 +15,7 @@ #include "atom/app/atom_main_delegate.h" #include "atom/common/crash_reporter/win/crash_service_main.h" #include "base/environment.h" +#include "base/process/launch.h" #include "base/win/windows_version.h" #include "content/public/app/sandbox_helper_win.h" #include "sandbox/win/src/sandbox_types.h" @@ -46,10 +47,6 @@ bool IsEnvSet(const char* name) { #endif } -bool IsRunAsNode() { - return IsEnvSet(kRunAsNode); -} - } // namespace #if defined(OS_WIN) @@ -57,14 +54,11 @@ int APIENTRY wWinMain(HINSTANCE instance, HINSTANCE, wchar_t* cmd, int) { int argc = 0; wchar_t** wargv = ::CommandLineToArgvW(::GetCommandLineW(), &argc); - // Make output work in console if we are not in cygiwn. - if (!IsEnvSet("TERM") && !IsEnvSet("ELECTRON_NO_ATTACH_CONSOLE")) { - AttachConsole(ATTACH_PARENT_PROCESS); + bool run_as_node = IsEnvSet(kRunAsNode); - FILE* dontcare; - freopen_s(&dontcare, "CON", "w", stdout); - freopen_s(&dontcare, "CON", "w", stderr); - } + // Make sure the output is printed to console. + if (run_as_node || !IsEnvSet("ELECTRON_NO_ATTACH_CONSOLE")) + base::RouteStdioToConsole(false); // Convert argv to to UTF8 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. base::AtExitManager atexit_manager; base::i18n::InitializeICU(); @@ -123,7 +117,7 @@ int APIENTRY wWinMain(HINSTANCE instance, HINSTANCE, wchar_t* cmd, int) { #elif defined(OS_LINUX) // defined(OS_WIN) int main(int argc, const char* argv[]) { - if (IsRunAsNode()) { + if (IsEnvSet(kRunAsNode)) { base::i18n::InitializeICU(); base::AtExitManager atexit_manager; return atom::NodeMain(argc, const_cast(argv)); @@ -140,7 +134,7 @@ int main(int argc, const char* argv[]) { #else // defined(OS_LINUX) int main(int argc, const char* argv[]) { - if (IsRunAsNode()) { + if (IsEnvSet(kRunAsNode)) { return AtomInitializeICUandStartNode(argc, const_cast(argv)); } diff --git a/spec/fixtures/module/process-stdout.js b/spec/fixtures/module/process-stdout.js new file mode 100644 index 000000000000..953750a247f9 --- /dev/null +++ b/spec/fixtures/module/process-stdout.js @@ -0,0 +1 @@ +process.stdout.write('pipes stdio') diff --git a/spec/node-spec.js b/spec/node-spec.js index 71fdbac98664..93a247ef4ff6 100644 --- a/spec/node-spec.js +++ b/spec/node-spec.js @@ -76,6 +76,19 @@ describe('node feature', function () { }) 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() + }) + }) }) })