Skip to content

Commit 8fdfc84

Browse files
emresiimseekmuhammedsimsekpartner
andauthored
fix: symbolic link handling for services and actions (#22)
* Fix symbolic link handling * Specify function return types * Used arrow function for method fields to be ensure correct preservation of the 'this' --------- Co-authored-by: muhammedsimsekpartner <[email protected]>
1 parent 38c6c5e commit 8fdfc84

File tree

1 file changed

+53
-21
lines changed

1 file changed

+53
-21
lines changed

src/lib/readMsgFiles.ts

Lines changed: 53 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { readdir, readFile, realpath, writeFile } from 'fs/promises';
1+
import { Dirent } from 'fs';
2+
import { readdir, readFile, realpath, stat, writeFile } from 'fs/promises';
23
import { basename, join } from 'path';
34

45
/* eslint-disable functional/prefer-readonly-type,functional/no-let,functional/no-loop-statement,functional/immutable-data */
@@ -112,32 +113,63 @@ export const generateMsgsFromActionsFiles = async (
112113
}
113114
};
114115

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+
115147
export const getMsgFiles = async (
116148
dir: string,
117149
tmpDir: string
118150
): Promise<string[]> => {
119151
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);
121158
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+
}
141173
}
142174
}
143175
return output;

0 commit comments

Comments
 (0)