update
This commit is contained in:
57
demo-origin.ts
Normal file
57
demo-origin.ts
Normal file
@@ -0,0 +1,57 @@
|
||||
const main = () => {
|
||||
const random = Math.random().toString(36).slice(-6)
|
||||
return 'hello a ' + random
|
||||
}
|
||||
|
||||
// fs.writeFileSync('./a.txt', main() + '\n', { flag: 'a' })
|
||||
console.log('pwd', process.cwd())
|
||||
// const value = fs.readFileSync('./bun.config.ts', 'utf-8')
|
||||
// console.log('a.txt 内容:', value)
|
||||
const listen = async () => {
|
||||
console.log('子进程启动,等待消息...')
|
||||
|
||||
const getParams = async () => {
|
||||
return new Promise((resolve) => {
|
||||
process.on('message', (msg) => {
|
||||
console.log('子进程收到消息:', msg)
|
||||
resolve(msg)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
try {
|
||||
const params = await getParams()
|
||||
console.log('处理参数:', params)
|
||||
|
||||
// 执行主要逻辑
|
||||
const result = main()
|
||||
|
||||
// 发送结果回主进程
|
||||
const response = {
|
||||
foo: 'bar',
|
||||
params,
|
||||
success: true,
|
||||
data: { code: 200, data: result },
|
||||
timestamp: new Date().toISOString()
|
||||
}
|
||||
|
||||
process.send?.(response, (error) => {
|
||||
if (error) {
|
||||
console.error('发送消息失败:', error)
|
||||
} else {
|
||||
console.log('成功发送响应:', response)
|
||||
}
|
||||
process.exit(0)
|
||||
})
|
||||
} catch (error) {
|
||||
console.error('子进程执行出错:', error)
|
||||
process.send?.({
|
||||
success: false,
|
||||
error: error.message
|
||||
})
|
||||
process.exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
// 启动监听
|
||||
listen().catch(console.error)
|
||||
Reference in New Issue
Block a user