Skip to content

Commit

Permalink
feat(util/node): makeLinkForce
Browse files Browse the repository at this point in the history
  • Loading branch information
AliMD authored and njfamirm committed May 8, 2023
1 parent 5700c45 commit f0c091e
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion core/util/src/node/fs.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {existsSync, readFileSync, writeFileSync, renameSync, mkdirSync} from 'node:fs';
import {rename, mkdir, writeFile, readFile} from 'node:fs/promises';
import {rename, mkdir, writeFile, readFile, rm, symlink} from 'node:fs/promises';
import {dirname} from 'node:path';

import {logger} from './_logger.js';
Expand Down Expand Up @@ -188,3 +188,29 @@ export const writeJsonFile = async <T extends StringifyableRecord = Stringifyabl

logger.timeEnd?.(`writeJsonFile(${timeKey})`);
};

/**
* Make a symbolic link
*
* **CAUTION: the destination path will be removed if exists**
*/
export const makeLinkForce = async (src: string, dest: string): Promise<void> => {
logger.logMethodArgs?.('makeLink', {src, dest});

try {
if (existsSync(dest)) {
await rm(dest, {recursive: false, force: true});
}
else {
const destDir = dirname(dest);
if (!existsSync(destDir)) {
await mkdir(dirname(dest), {recursive: true});
}
}

await symlink(src, dest);
}
catch (error) {
logger.error('makeLink', 'symlink_failed', error);
}
};

0 comments on commit f0c091e

Please sign in to comment.