|
1 | | -import { readdir, readFile, realpath, writeFile } from 'fs/promises'; |
| 1 | +import { Dirent } from 'fs'; |
| 2 | +import { readdir, readFile, realpath, stat, writeFile } from 'fs/promises'; |
2 | 3 | import { basename, join } from 'path'; |
3 | 4 |
|
4 | 5 | /* eslint-disable functional/prefer-readonly-type,functional/no-let,functional/no-loop-statement,functional/immutable-data */ |
@@ -112,32 +113,63 @@ export const generateMsgsFromActionsFiles = async ( |
112 | 113 | } |
113 | 114 | }; |
114 | 115 |
|
| 116 | +async function processEntries( |
| 117 | + entries: Dirent[], |
| 118 | + dir: string |
| 119 | +): Promise<Dirent[]> { |
| 120 | + const processedEntries = await Promise.all( |
| 121 | + entries.map(async (entry): Promise<Dirent> => { |
| 122 | + if (entry.isSymbolicLink()) { |
| 123 | + const fullPath = join(dir, entry.name); |
| 124 | + |
| 125 | + const resolvedPath = await realpath(fullPath); |
| 126 | + const stats = await stat(resolvedPath); |
| 127 | + |
| 128 | + return { |
| 129 | + name: entry.name, |
| 130 | + isDirectory: () => stats.isDirectory(), |
| 131 | + isFile: () => stats.isFile(), |
| 132 | + isBlockDevice: () => entry.isBlockDevice(), |
| 133 | + isCharacterDevice: () => entry.isCharacterDevice(), |
| 134 | + isSymbolicLink: () => entry.isSymbolicLink(), |
| 135 | + isFIFO: () => entry.isFIFO(), |
| 136 | + isSocket: () => entry.isSocket(), |
| 137 | + }; |
| 138 | + } else { |
| 139 | + return entry; |
| 140 | + } |
| 141 | + }) |
| 142 | + ); |
| 143 | + |
| 144 | + return processedEntries; |
| 145 | +} |
| 146 | + |
115 | 147 | export const getMsgFiles = async ( |
116 | 148 | dir: string, |
117 | 149 | tmpDir: string |
118 | 150 | ): Promise<string[]> => { |
119 | 151 | let output: string[] = []; |
120 | | - for (const entry of await readdir(dir, { withFileTypes: true })) { |
| 152 | + |
| 153 | + const entries = await readdir(dir, { withFileTypes: true }); |
| 154 | + const processedEntries = await processEntries(entries, dir); |
| 155 | + |
| 156 | + for (const entry of processedEntries) { |
| 157 | + const fullPath = join(dir, entry.name); |
121 | 158 | if (entry.isDirectory()) { |
122 | | - output = output.concat(await getMsgFiles(join(dir, entry.name), tmpDir)); |
123 | | - } else if (entry.isFile() && entry.name.endsWith('.msg')) { |
124 | | - output.push(join(dir, entry.name)); |
125 | | - } else if (entry.isFile() && entry.name.endsWith('.srv')) { |
126 | | - const srvFiles = await generateMsgsFromSrvFiles( |
127 | | - dir + '/' + entry.name, |
128 | | - tmpDir |
129 | | - ); |
130 | | - output.push(...srvFiles); |
131 | | - } else if (entry.isFile() && entry.name.endsWith('.action')) { |
132 | | - const actionFiles = await generateMsgsFromActionsFiles( |
133 | | - dir + '/' + entry.name, |
134 | | - tmpDir |
135 | | - ); |
136 | | - output.push(...actionFiles); |
137 | | - } else if (entry.isSymbolicLink()) { |
138 | | - const fullPath = join(dir, entry.name); |
139 | | - const resolvedPath = await realpath(fullPath); |
140 | | - output.push(resolvedPath); |
| 159 | + output = output.concat(await getMsgFiles(fullPath, tmpDir)); |
| 160 | + } else if (entry.isFile()) { |
| 161 | + if (entry.name.endsWith('.msg')) { |
| 162 | + output.push(fullPath); |
| 163 | + } else if (entry.name.endsWith('.srv')) { |
| 164 | + const srvFiles = await generateMsgsFromSrvFiles(fullPath, tmpDir); |
| 165 | + output.push(...srvFiles); |
| 166 | + } else if (entry.name.endsWith('.action')) { |
| 167 | + const actionFiles = await generateMsgsFromActionsFiles( |
| 168 | + fullPath, |
| 169 | + tmpDir |
| 170 | + ); |
| 171 | + output.push(...actionFiles); |
| 172 | + } |
141 | 173 | } |
142 | 174 | } |
143 | 175 | return output; |
|
0 commit comments