Skip to content

Commit 666da23

Browse files
committed
fix: support multiple extensions for files with same name
Prevent overwriting when files share the same basename but have different extensions. Extensions are now grouped into arrays while copying and creating folder structures. closes #11
1 parent 0b15ec2 commit 666da23

4 files changed

Lines changed: 44 additions & 25 deletions

File tree

package-lock.json

Lines changed: 5 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/services/formatters/gitignestFormatter.ts

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,24 @@ export class GitignestFormatter extends BaseFormatter {
1515
return entries
1616
.map(([name, value], idx) => {
1717
const isLast = idx === entries.length - 1;
18-
const connector = isRoot
19-
? TREE_SYMBOLS.LAST
20-
: isLast
21-
? TREE_SYMBOLS.LAST
22-
: TREE_SYMBOLS.BRANCH;
23-
const isDir = typeof value === 'object' && value !== null;
24-
const displayName = !isDir
25-
? (value as string) === 'file' || (value as string).trim() === ''
26-
? name
27-
: `${name}.${value as string}`
28-
: `${name}/`;
29-
const line = `${isRoot ? '' : prefix}${connector}${displayName}`;
18+
const connector = isRoot ? TREE_SYMBOLS.LAST : TREE_SYMBOLS.BRANCH;
19+
const isDir = typeof value === 'object' && value !== null && !Array.isArray(value);
20+
let displayName: string = '';
21+
let names = [];
22+
if (isDir) {
23+
displayName = `${name}/`;
24+
} else {
25+
for (const val of Array.isArray(value) ? value : [value]) {
26+
names.push(val === 'file' || val.trim() === '' ? name : `${name}.${val}`);
27+
}
28+
}
29+
const createLine = (displayName: string, isLast: boolean = false) =>
30+
`${isRoot ? '' : prefix}${isLast ? TREE_SYMBOLS.LAST : connector}${displayName}`;
3031

3132
if (!isDir) {
32-
return line;
33+
return names
34+
.map((n, i) => createLine(n, isLast && i === names.length - 1))
35+
.join('\n');
3336
}
3437

3538
const childPrefix = isRoot
@@ -39,7 +42,7 @@ export class GitignestFormatter extends BaseFormatter {
3942
? TREE_SYMBOLS.INDENT
4043
: `${TREE_SYMBOLS.VERTICAL}${TREE_SYMBOLS.INDENT.slice(1)}`);
4144
const nested = this.formatStructure(value as FolderStructure, childPrefix);
42-
return `${line}\n${nested}`;
45+
return `${createLine(displayName)}\n${nested}`;
4346
})
4447
.join('\n');
4548
}

src/services/structure.ts

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,13 @@ export class StructureService {
5454
} else {
5555
const ext = this.fileTypeFor(entry.name);
5656
const base = this.baseNameFor(entry.name);
57-
const key = structure[base] === undefined ? base : entry.name; // avoid collision
58-
structure[key] = ext;
57+
const existing = structure[base] as string[] | string | undefined;
58+
structure[base] =
59+
existing === undefined
60+
? ext
61+
: Array.isArray(existing)
62+
? [...existing, ext]
63+
: [existing, ext];
5964
}
6065
}
6166

@@ -144,10 +149,13 @@ export class StructureService {
144149
structure: FolderStructure,
145150
): Promise<void> {
146151
for (const [key, value] of Object.entries(structure)) {
147-
if (typeof value === 'string') {
148-
const fileName = value === 'file' || value.trim() === '' ? key : `${key}.${value}`;
149-
const fullPath = vscode.Uri.joinPath(baseUri, fileName);
150-
await FileSystemService.writeFileIfAbsent(fullPath, '');
152+
if (Array.isArray(value) || typeof value === 'string') {
153+
const uniqueTypes = Array.from(new Set(Array.isArray(value) ? value : [value]));
154+
for (const val of uniqueTypes) {
155+
const fileName = val === 'file' || val.trim() === '' ? key : `${key}.${val}`;
156+
const fullPath = vscode.Uri.joinPath(baseUri, fileName);
157+
await FileSystemService.writeFileIfAbsent(fullPath, '');
158+
}
151159
} else {
152160
const dirPath = vscode.Uri.joinPath(baseUri, key);
153161
await FileSystemService.mkdirIfAbsent(dirPath);
@@ -241,8 +249,13 @@ export class StructureService {
241249
} else {
242250
const type = this.fileTypeFor(node.name);
243251
const base = this.baseNameFor(node.name);
244-
const key = (ctx as any)[base] === undefined ? base : node.name;
245-
ctx[key] = type;
252+
const existing = ctx[base] as string[] | string | undefined;
253+
ctx[base] =
254+
existing === undefined
255+
? type
256+
: Array.isArray(existing)
257+
? [...existing, type]
258+
: [existing, type];
246259
}
247260
});
248261

src/types/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
export interface FolderStructure {
22
// key: name
33
// value: nested FolderStructure for directories, or string file type (e.g., "ts", "css") for files
4-
[key: string]: FolderStructure | string;
4+
[key: string]: FolderStructure | string | string[];
55
}
66

77
export type OutputFormat = 'Plain Text Format' | 'JSON Format';

0 commit comments

Comments
 (0)