(name: string, rootPath: string)
| 25 | } |
| 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, |
| 67 | findFile, |
| 68 | updateFile: async (file, withinModule, updater) => { |
| 69 | const path = await findFile(file, withinModule); |
| 70 | const contents = await readFile(path); |
| 71 | const updated = await updater(contents); |
| 72 | await writeFile(path, updated); |
| 73 | }, |
| 74 | createFile: async (filePath, withinModule, source) => { |
| 75 | const fullPath = path.resolve(path.join(rootPath, withinModule ? `modules/${withinModule}` : '.', filePath)); |
| 76 | await mkdir(path.dirname(fullPath), { recursive: true }); |
| 77 | await writeFile(fullPath, source); |
| 78 | return fullPath; |
| 79 | }, |
| 80 | }; |
| 81 | } |
| 82 | |
| 83 | type LineMatcher = (line: string) => boolean; |
| 84 |
no test coverage detected