|
| 1 | +import { spawn } from 'child_process'; |
| 2 | +import * as fs from 'fs'; |
| 3 | +import * as path from 'path'; |
| 4 | +import { |
| 5 | + allApplications, |
| 6 | + childProcessResponse, |
| 7 | + cps, |
| 8 | + currentFile, |
| 9 | + protocol |
| 10 | +} from '../constants'; |
| 11 | +import { isIAllApps } from './utils'; |
| 12 | + |
| 13 | +export const findJsonFilesRecursively = async ( |
| 14 | + appsDir: string |
| 15 | +): Promise<void> => { |
| 16 | + const files = fs.readdirSync(appsDir, { withFileTypes: true }); |
| 17 | + for (const file of files) { |
| 18 | + if (file.isDirectory()) { |
| 19 | + await findJsonFilesRecursively(path.join(appsDir, file.name)); |
| 20 | + } else if (file.name === 'metacall.json') { |
| 21 | + const filePath = path.join(appsDir, file.name); |
| 22 | + const desiredPath = path.join(__dirname, '../worker/index.js'); |
| 23 | + const id = path.basename(appsDir); |
| 24 | + |
| 25 | + currentFile.id = id; |
| 26 | + (currentFile.type = 'application/x-zip-compressed'), |
| 27 | + (currentFile.path = appsDir); |
| 28 | + |
| 29 | + const proc = spawn('metacall', [desiredPath, filePath], { |
| 30 | + stdio: ['pipe', 'pipe', 'pipe', 'ipc'] |
| 31 | + }); |
| 32 | + |
| 33 | + proc.send({ |
| 34 | + type: protocol.l, |
| 35 | + currentFile |
| 36 | + }); |
| 37 | + |
| 38 | + proc.stdout?.on('data', (data: Buffer) => { |
| 39 | + console.log(data.toString().green); |
| 40 | + }); |
| 41 | + proc.stderr?.on('data', (data: Buffer) => { |
| 42 | + console.log(data.toString().red); |
| 43 | + }); |
| 44 | + |
| 45 | + proc.on('message', (data: childProcessResponse) => { |
| 46 | + if (data.type === protocol.g) { |
| 47 | + if (isIAllApps(data.data)) { |
| 48 | + const appName = Object.keys(data.data)[0]; |
| 49 | + cps[appName] = proc; |
| 50 | + allApplications[appName] = data.data[appName]; |
| 51 | + } |
| 52 | + } |
| 53 | + }); |
| 54 | + } |
| 55 | + } |
| 56 | +}; |
0 commit comments