| 26 | |
| 27 | export function createProject(name: string, rootPath: string): ProjectConfig { |
| 28 | const findFile: ProjectConfig['findFile'] = (fileName, withinModule) => { |
| 29 | return new Promise((resolve, reject) => { |
| 30 | const buffers: Buffer[] = []; |
| 31 | |
| 32 | const errorBuffers: Buffer[] = []; |
| 33 | const directory = path.join(...['.', 'modules'].concat(withinModule ?? [])); |
| 34 | |
| 35 | const child = spawn('find', [directory]); |
| 36 | child.stdout.on('data', (chunk: Buffer) => { |
| 37 | buffers.push(chunk); |
| 38 | }); |
| 39 | |
| 40 | child.stderr.on('data', (chunk: Buffer) => { |
| 41 | errorBuffers.push(chunk); |
| 42 | }); |
| 43 | |
| 44 | child.on('close', code => { |
| 45 | if (!!code && code > 0) { |
| 46 | reject(new Error('exited with error code: ' + code.toString())); |
| 47 | } |
| 48 | |
| 49 | const output = Buffer.concat(buffers).toString('utf8'); |
| 50 | |
| 51 | const files = output.trim().split('\n'); |
| 52 | |
| 53 | const found = files.find(file => file.endsWith(`/${fileName}`)); |
| 54 | |
| 55 | if (!found) { |
| 56 | reject(new Error('File not found: ' + fileName)); |
| 57 | return; |
| 58 | } |
| 59 | |
| 60 | resolve(path.join(rootPath, found)); |
| 61 | }); |
| 62 | }); |
| 63 | }; |
| 64 | return { |
| 65 | name, |
| 66 | rootPath, |