Node.js - 启动一个进程(firebase 模拟器)并读取它的输出

Node.js - start a process (firebase emulators) and read its output

我想在 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.

因此,我需要:

如何读取输出?

我试过以下,它打印的都是换行符。

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

这正是我们制作 firebase emulators:exec 命令的原因。

假设您使用 "npm run test" 到 运行 您的 Jest 脚本,您将使用:

firebase emulators:exec "npm run test"

这将:

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