UNPKG

@augment-vir/node

Version:

A collection of augments, helpers types, functions, and classes only for Node.js (backend) JavaScript environments.

24 lines (23 loc) 918 B
import { existsSync } from 'node:fs'; import { lstat, readlink, stat, symlink } from 'node:fs/promises'; /** * Creates a symlink. * * @category Node : File * @category Package : @augment-vir/node * @package [`@augment-vir/node`](https://www.npmjs.com/package/@augment-vir/node) */ export async function createSymlink({ linkTo, symlinkPath, }) { if (existsSync(symlinkPath)) { if (!(await lstat(symlinkPath)).isSymbolicLink()) { throw new Error(`Tried to create symlink at '${symlinkPath}' but a non-symlink file already exists in that location.`); } else if ((await readlink(symlinkPath)) !== linkTo) { throw new Error(`Symlink already exists at '${symlinkPath}' but has a differently link path.`); } } else { const isDir = (await stat(linkTo)).isDirectory(); await symlink(linkTo, symlinkPath, isDir ? 'dir' : 'file'); } }