节点:在文件夹上运行无铬自动化

我试图在我的testing文件夹中的所有文件上运行chromeless
这是我的index.js到目前为止

 const {spawn} = require('child_process'); let fs = require('fs'); const proc = spawn('chrome', [ '--remote-debugging-port=9222', '--disable-gpu', '--headless' ]); console.log(`Spawned child pid: ${proc.pid}`); async function run() { let path = __dirname; const files = fs.readdirSync(path); for (let filename of files) { if (/.js$/.test(filename) && filename !== 'index.js') { console.log(`executing: ${path}\\${filename}`); const run = require(`${path}\\${filename}`); await run(); } } process.kill(proc.pid); } run().catch(console.error.bind(console)); 

我不能让节点等待内部脚本执行,所以无头进程立即死亡

什么是正确的方法来做到这一点? 我是否需要使用另一种无铬工具,或者只需要改进我的入门代码?

编辑(解决)
我做错了,我改变了readdir readdirSync,然后从“子”脚本返回一个承诺,并等待它
这是子脚本代码

 const { Chromeless } = require('chromeless') async function run() { const chromeless = new Chromeless() const screenshot = await chromeless .goto('https://www.google.com') .type('chromeless', 'input[name="q"]') .press(13) .wait('#resultStats') .screenshot() console.log(screenshot) // prints local file path or S3 url // this returns a promise return chromeless.end() } // run().catch(console.error.bind(console)); // default export the run function module.exports = run;