Skip to content

Commit 0224afb

Browse files
authored
Resolve the functionality to duplicate a page that has been already duplicated (#1776)
* Resolve the functionality to duplicate a page that has been already duplicated
1 parent e77668f commit 0224afb

File tree

1 file changed

+23
-2
lines changed

1 file changed

+23
-2
lines changed

apps/studio/electron/main/pages/duplicate.ts

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,26 @@ import { promises as fs } from 'fs';
22
import * as path from 'path';
33
import { detectRouterType, ROOT_PAGE_COPY_NAME, ROOT_PATH_IDENTIFIERS } from './helpers';
44

5+
async function getUniqueDir(basePath: string, dirName: string): Promise<string> {
6+
let uniquePath = dirName;
7+
let counter = 1;
8+
9+
const baseName = dirName.replace(/-copy(-\d+)?$/, '');
10+
11+
while (true) {
12+
try {
13+
await fs.access(path.join(basePath, uniquePath));
14+
uniquePath = `${baseName}-copy-${counter}`;
15+
counter++;
16+
} catch (err: any) {
17+
if (err.code === 'ENOENT') {
18+
return uniquePath;
19+
}
20+
throw err;
21+
}
22+
}
23+
}
24+
525
export async function duplicateNextJsPage(
626
projectRoot: string,
727
sourcePath: string,
@@ -17,7 +37,7 @@ export async function duplicateNextJsPage(
1737

1838
if (isRootPath) {
1939
const sourcePageFile = path.join(routerConfig.basePath, 'page.tsx');
20-
const targetDir = path.join(routerConfig.basePath, ROOT_PAGE_COPY_NAME);
40+
const targetDir = await getUniqueDir(routerConfig.basePath, ROOT_PAGE_COPY_NAME);
2141
const targetPageFile = path.join(targetDir, 'page.tsx');
2242

2343
// Check if target already exists
@@ -36,8 +56,9 @@ export async function duplicateNextJsPage(
3656
}
3757

3858
// Handle non-root pages
59+
3960
const normalizedSourcePath = sourcePath;
40-
const normalizedTargetPath = targetPath.endsWith('-copy') ? targetPath : `${targetPath}-copy`;
61+
const normalizedTargetPath = await getUniqueDir(routerConfig.basePath, targetPath);
4162

4263
const sourceFull = path.join(routerConfig.basePath, normalizedSourcePath);
4364
const targetFull = path.join(routerConfig.basePath, normalizedTargetPath);

0 commit comments

Comments
 (0)