Node.js - 启动进程(firebase 模拟器)并读取其输出

Joh*_*ith 3 child-process node.js

我想在 Jest 测试之前启动 firebase 模拟器。

执行此操作,但以编程方式执行:

E:\my-projct>firebase emulators:start --only firestore
i  emulators: Starting emulators: firestore
i  firestore: Serving ALL traffic (including WebChannel) on http://localhost:8080
!  firestore: Support for WebChannel on a separate port (8081) is DEPRECATED and will go away soon. Please use port above instead.
i  firestore: Emulator logging to firestore-debug.log
+  firestore: Emulator started at http://localhost:8080
i  firestore: For testing set FIRESTORE_EMULATOR_HOST=localhost:8080
+  All emulators started, it is now safe to connect.
Run Code Online (Sandbox Code Playgroud)

因此,我需要:

  • 执行命令

  • 等待“所有模拟器已启动”字符串出现。

我如何读取输出?

我尝试过以下操作,它打印的只是换行符。

const cp = require('child_process');
const child = cp.exec('firebase emulators:start --only firestore');
child.stdout.addListener('data', data => console.log(data.toString()));
Run Code Online (Sandbox Code Playgroud)

Sam*_*ern 9

这正是我们发出命令的原因firebase emulators:exec

假设您使用“npm run test”来运行 Jest 脚本,您将使用:

firebase emulators:exec "npm run test"
Run Code Online (Sandbox Code Playgroud)

这会:

  1. 启动模拟器并等待它们准备好
  2. 运行您的脚本(npm run test在本例中)
  3. 等待脚本退出
  4. 干净地关闭模拟器