Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 46 additions & 1 deletion packages/cli/src/create/__tests__/prompts.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import path from 'node:path';

import { afterEach, describe, expect, it } from 'vitest';

import { isTargetDirAvailable, suggestAvailableTargetDir } from '../prompts.js';
import { emptyDir, isTargetDirAvailable, suggestAvailableTargetDir } from '../prompts.js';

const tempDirs: string[] = [];

Expand Down Expand Up @@ -44,3 +44,48 @@ describe('target directory helpers', () => {
expect(fs.existsSync(path.join(cwd, suggested))).toBe(false);
});
});

describe('emptyDir', () => {
it('removes existing files but keeps .git', () => {
const targetDir = makeTempDir();
fs.mkdirSync(path.join(targetDir, '.git'));
fs.mkdirSync(path.join(targetDir, 'src'));
fs.writeFileSync(path.join(targetDir, 'package.json'), '{}');

emptyDir(targetDir);

expect(fs.readdirSync(targetDir)).toEqual(['.git']);
});

it('removes a symlinked target without touching the linked directory', () => {
const cwd = makeTempDir();
const linkedDir = path.join(cwd, 'linked-directory');
const targetDir = path.join(cwd, 'new-project');
fs.mkdirSync(linkedDir);
fs.writeFileSync(path.join(linkedDir, 'keep.txt'), 'keep\n');
fs.symlinkSync(linkedDir, targetDir, 'dir');

emptyDir(targetDir);

expect(fs.existsSync(targetDir)).toBe(false);
expect(fs.readFileSync(path.join(linkedDir, 'keep.txt'), 'utf8')).toBe('keep\n');
});

it('removes a symlinked target given with a trailing separator', () => {
const cwd = makeTempDir();
const linkedDir = path.join(cwd, 'linked-directory');
const targetDir = path.join(cwd, 'new-project');
fs.mkdirSync(linkedDir);
fs.writeFileSync(path.join(linkedDir, 'keep.txt'), 'keep\n');
fs.symlinkSync(linkedDir, targetDir, 'dir');

emptyDir(`${targetDir}${path.sep}`);

expect(fs.existsSync(targetDir)).toBe(false);
expect(fs.readFileSync(path.join(linkedDir, 'keep.txt'), 'utf8')).toBe('keep\n');
});

it('ignores a missing target', () => {
expect(() => emptyDir(path.join(makeTempDir(), 'missing'))).not.toThrow();
});
});
22 changes: 18 additions & 4 deletions packages/cli/src/create/prompts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,15 +97,21 @@ export async function checkProjectDirExists(projectDirFullPath: string, interact
}

// Handle directory if it exists and is not empty
// `path.resolve` drops a trailing separator, which would make `lstat` follow the link.
const isSymlink = fs.lstatSync(path.resolve(projectDirFullPath)).isSymbolicLink();
const overwrite = await prompts.select({
message: `Target directory "${projectDirFullPath}" is not empty. Please choose how to proceed:`,
message: isSymlink
? `Target directory "${projectDirFullPath}" is a symbolic link to "${fs.realpathSync(projectDirFullPath)}", which is not empty. Please choose how to proceed:`
: `Target directory "${projectDirFullPath}" is not empty. Please choose how to proceed:`,
options: [
{
label: 'Cancel operation',
value: 'no',
},
{
label: 'Remove existing files and continue',
label: isSymlink
? 'Remove the symbolic link and continue'
: 'Remove existing files and continue',
value: 'yes',
},
],
Expand All @@ -129,8 +135,16 @@ function isEmpty(path: string) {
return files.length === 0 || (files.length === 1 && files[0] === '.git');
}

function emptyDir(dir: string) {
if (!fs.existsSync(dir)) {
export function emptyDir(dir: string) {
// `path.resolve` drops a trailing separator, which would make `lstat` follow the link.
const target = path.resolve(dir);
const stat = fs.lstatSync(target, { throwIfNoEntry: false });
if (!stat) {
return;
}
// Remove a symlinked target itself, never the contents of the directory it points to.
if (stat.isSymbolicLink()) {
fs.unlinkSync(target);
return;
}
for (const file of fs.readdirSync(dir)) {
Expand Down
Loading