Skip to content

Instantly share code, notes, and snippets.

@lpenaud
Last active March 4, 2025 19:38
Show Gist options
  • Select an option

  • Save lpenaud/7a726faa643abe8dfd3749295438e622 to your computer and use it in GitHub Desktop.

Select an option

Save lpenaud/7a726faa643abe8dfd3749295438e622 to your computer and use it in GitHub Desktop.
Create physical link recursively
#!/usr/bin/env -S deno run --allow-read --allow-write
import {
walk,
type WalkEntry,
type WalkOptions,
} from "https://deno.land/[email protected]/fs/walk.ts";
import * as path from "https://deno.land/[email protected]/path/mod.ts";
async function* multiWalk(roots: string[], options?: WalkOptions) {
for (const root of roots) {
const dirname = path.dirname(root);
for await (const entry of walk(root, options)) {
yield {
entry,
relative: path.relative(dirname, entry.path),
};
}
}
}
async function link(src: WalkEntry, dest: string) {
if (src.isFile) {
try {
await Deno.link(src.path, dest);
} catch (error) {
console.warn(error);
}
console.log(src.path, "=>", dest);
return;
}
console.log("Make directory", dest);
return Deno.mkdir(dest, {
recursive: true,
});
}
function usage(): number {
console.error(
"Usage:",
import.meta.filename ?? import.meta.url,
"SRC",
"[...SRC]",
"OUTDIR",
);
return 1;
}
async function main(args: string[]): Promise<number> {
const outdir = args.pop();
if (outdir === undefined) {
console.error("OUTDIR is required.");
return usage();
}
if (args.length === 0) {
console.error("At least one INDIR is required.");
return usage();
}
for await (const { entry, relative } of multiWalk(args)) {
await link(entry, path.resolve(outdir, relative));
}
return 0;
}
if (import.meta.main) {
main(Deno.args.slice())
.then(Deno.exit)
.catch((e) => {
console.error(e);
Deno.exit(2);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment