* - Delete every *.sql file in the subfolders of viewsDir * - Delete any empty subfolders * - Delete viewsDir if it is empty
(viewsDir: string, viewFilesToKeep: string[] = [])
| 97 | * - Delete viewsDir if it is empty |
| 98 | */ |
| 99 | async function cleanLeftoversIO(viewsDir: string, viewFilesToKeep: string[] = []): Promise<void> { |
| 100 | const pipeline = pipe( |
| 101 | // remove any SQL files in the views directory beyond the ones just created, concurrently, collapsing the possible errors |
| 102 | fsFunctional.getFilesInDir(viewsDir, '**/*/*.sql'), |
| 103 | T.chain((filesInViewsDir) => { |
| 104 | const viewFilesToRemove = filesInViewsDir.filter((file) => !viewFilesToKeep.includes(file)) |
| 105 | return TE.traverseArray(fsFunctional.removeFile)(viewFilesToRemove) |
| 106 | }), |
| 107 | |
| 108 | // remove any empty directories in the views directory, recursively |
| 109 | TE.chainW(() => fsFunctional.removeEmptyDirs(viewsDir)), |
| 110 | ) |
| 111 | |
| 112 | const either = await pipeline() |
| 113 | |
| 114 | if (E.isRight(either)) { |
| 115 | return |
| 116 | } |
| 117 | |
| 118 | // failure: check which error to throw |
| 119 | const error = match(either.left) |
| 120 | .with({ type: 'fs-remove-empty-dirs' }, (e) => { |
| 121 | throw new Error(`Error removing empty directories in: ${e.meta.dir}.\n${e.error}.`) |
| 122 | }) |
| 123 | .with({ type: 'fs-remove-file' }, (e) => { |
| 124 | throw new Error(`Error removing the file: ${e.meta.filePath}.\n${e.error}.`) |
| 125 | }) |
| 126 | .exhaustive() |
| 127 | // We execute the cleanup and ignore the possible errors |
| 128 | await pipeline() |
| 129 | |
| 130 | throw error |
| 131 | } |
no test coverage detected