Skip to content

Commit

Permalink
fix: handle existing broken symlinks
Browse files Browse the repository at this point in the history
  • Loading branch information
privatenumber committed May 2, 2022
1 parent e685c7a commit b5b2446
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/utils/fs-exists.ts
@@ -1,8 +1,9 @@
import fs from 'fs';

// Checks if symlink file exists
export const fsExists = (
path: string,
) => fs.promises.access(path).then(
) => fs.promises.lstat(path).then(
() => true,
() => false,
);
2 changes: 1 addition & 1 deletion src/utils/symlink.ts
Expand Up @@ -8,7 +8,7 @@ export async function symlink(
type?: string,
) {
if (await fsExists(symlinkPath)) {
const symlinkRealpath = await fs.promises.realpath(symlinkPath);
const symlinkRealpath = await fs.promises.realpath(symlinkPath).catch(() => null);

if (targetPath === symlinkRealpath) {
return;
Expand Down
40 changes: 40 additions & 0 deletions tests/specs/cli.ts
Expand Up @@ -59,6 +59,46 @@ export default testSuite(({ describe }, nodePath: string) => {

await fixture.rm();
});

test('symlink exists', async () => {
const fixture = await createFixture('./tests/fixtures/');
const packageEntryPath = path.join(fixture.path, 'package-entry');

await fs.promises.mkdir(path.join(packageEntryPath, 'node_modules'));
await fs.promises.symlink(
'../../package-files',
path.join(packageEntryPath, 'node_modules/package-files'),
);

const linkProcess = await link(['../package-files'], {
cwd: packageEntryPath,
nodePath,
});

expect(linkProcess.exitCode).toBe(0);

await fixture.rm();
});

test('broken symlink exists', async () => {
const fixture = await createFixture('./tests/fixtures/');
const packageEntryPath = path.join(fixture.path, 'package-entry');

await fs.promises.mkdir(path.join(packageEntryPath, 'node_modules'));
await fs.promises.symlink(
'../broken-symlink/../../package-files',
path.join(packageEntryPath, 'node_modules/package-files'),
);

const linkProcess = await link(['../package-files'], {
cwd: path.join(fixture.path, 'package-entry'),
nodePath,
});

expect(linkProcess.exitCode).toBe(0);

await fixture.rm();
});
});

test('consecutive links', async () => {
Expand Down

0 comments on commit b5b2446

Please sign in to comment.