Skip to content

Instantly share code, notes, and snippets.

@shrinktofit
Last active August 22, 2021 11:59
Show Gist options
  • Save shrinktofit/c0d8dbc368eb2cf9e8936a41568f6b8c to your computer and use it in GitHub Desktop.
Save shrinktofit/c0d8dbc368eb2cf9e8936a41568f6b8c to your computer and use it in GitHub Desktop.
Copy preserve and redirect symbol link
const { link, copy, ensureDir, stat, readdir, realpath, symlink } = require('fs-extra');
const { relative, join } = require('path');
(async () => {
await myCopy(
String.raw`X:\Temp\editor-ci-build-test\editor-3d\app`,
// String.raw`X:\Temp\6`,
String.raw`X:\Temp\editor-ci-build-test\editor-3d\.publish\CocosCreator\resources\app`,
);
})();
async function myCopy(source, dest) {
const root = source;
const dstRoot = dest;
const symlinkItems = [];
await copyDir(source, dest);
await Promise.all(symlinkItems.map(async ([source, target]) => {
await symlink(target, source, 'junction');
}));
async function copyFile(source, dest) {
await copy(source, dest);
}
async function copyDir(source, dest) {
await ensureDir(dest);
const dirEntries = await readdir(source, { withFileTypes: true });
await Promise.all(dirEntries.map(async (dirEntry) => {
const sourceFileName = join(source, dirEntry.name);
const destFileName = join(dest, dirEntry.name);
if (dirEntry.isFile()) {
await copyFile(sourceFileName, destFileName);
} else if (dirEntry.isDirectory()) {
await copyDir(sourceFileName, destFileName);
} else if (dirEntry.isSymbolicLink()) {
await copySymbolink(sourceFileName, destFileName);
}
}));
}
async function copySymbolink(source, dest) {
const real = await realpath(source);
const relativeReal = relative(root, real);
if (relativeReal.startsWith('..')) {
return;
}
const realDest = join(dstRoot, relativeReal);
symlinkItems.push([dest, realDest]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment