Created
May 23, 2024 18:18
-
-
Save JosXa/f542e07b84796b9ec9d3cee0bc9b9ea1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Name: Create Hardlink | |
import "@johnlindquist/kit" | |
import { basename, dirname } from "path" | |
import { rm } from "fs/promises" | |
export const sleep = async (timeMs: number) => await new Promise((resolve) => setTimeout(resolve, timeMs)) | |
const fromPath = await path({ hint: `Select a source file or folder to create a Hardlink to` }) | |
const sourceIsDir = await isDir(fromPath) | |
let toPath: string | |
while (true) { | |
toPath = await path({ | |
hint: `Where to put the link?`, | |
footer: "test", | |
startPath: dirname(fromPath), | |
onlyDirs: sourceIsDir, | |
}) | |
if (sourceIsDir && (await isFile(toPath))) { | |
await div("You selected a file as the target, not a directory!") | |
continue | |
} | |
if (!sourceIsDir && (await pathExists(toPath))) { | |
if (await isDir(toPath)) { | |
await div("You selected a directory as the target, not a file!") | |
continue | |
} | |
console.log("Deleting existing file at target path") | |
await rm(toPath) | |
if (await pathExists(toPath)) { | |
await div("Failed to delete existing file at target path") | |
exit(1) | |
} | |
} | |
break // ok | |
} | |
// -Name parameter must be relative | |
const workdir = dirname(toPath) | |
const filename = basename(toPath) | |
const command = `New-Item -ItemType ${sourceIsDir ? "Junction" : "HardLink"} -Name "${filename}" -Value "${fromPath}"` | |
try { | |
const res = await exec(command, { shell: "pwsh.exe", cwd: workdir }) | |
if (res.exitCode === 0) { | |
try { | |
await exec(`explorer.exe .`, { shell: "pwsh.exe", cwd: toPath }) | |
} catch (err) { | |
console.error(err) | |
// ok | |
} | |
} else { | |
console.log(res) | |
await showLogWindow() | |
await div("See error logs") | |
} | |
console.log("Done!") | |
} catch (err) { | |
console.error(err) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment