diff --git a/makefile b/makefile index 05cd635..6adc67e 100644 --- a/makefile +++ b/makefile @@ -2,4 +2,7 @@ install: deno run --unstable --allow-env --allow-run --allow-read --allow-write ./mapping.ts install link: - deno run --unstable --allow-env --allow-run --allow-read --allow-write ./mapping.ts link \ No newline at end of file + deno run --unstable --allow-env --allow-run --allow-read --allow-write ./mapping.ts link + +unlink: + deno run --unstable --allow-env --allow-run --allow-read --allow-write ./mapping.ts unlink \ No newline at end of file diff --git a/mapping.ts b/mapping.ts index c641472..bb95693 100755 --- a/mapping.ts +++ b/mapping.ts @@ -2,7 +2,7 @@ import { dot, DotOption } from "./scripts/dot.ts"; const common: DotOption = { link: { - gitconfig: ".gitconfig", + ".gitconfig": "gitconfig", }, }; @@ -24,23 +24,23 @@ const linux: DotOption = { ], link: { ...common.link, - "linux/alacritty": ".config/alacritty", - "linux/bin/ufetch": ".local/bin/ufetch", - "linux/bspwm": ".config/bspwm", - "linux/Code/User/settings.json": ".config/Code/User/settings.json", - "linux/dunst": ".config/dunst", - "linux/omz": ".config/omz", - "linux/picom": ".config/picom", - "linux/polybar": ".config/polybar", - "linux/rofi": ".config/rofi", - "linux/sxhkd": ".config/sxhkd", - "linux/fcitx5": ".config/fcitx5", - "linux/dconf": ".config/dconf", - "linux/bakamplayer.ini": ".config/bakamplayer.ini", - "linux/xinitrc": ".xinitrc", - "linux/pam_environment": ".pam_environment", - "linux/zshrc": ".zshrc", - "linux/Xmodmap": ".Xmodmap", + ".config/alacritty": "linux/alacritty", + ".local/bin/ufetch": "linux/bin/ufetch", + ".config/bspwm": "linux/bspwm", + ".config/Code/User/settings.json": "linux/Code/User/settings.json", + ".config/dunst": "linux/dunst", + ".config/omz": "linux/omz", + ".config/picom": "linux/picom", + ".config/polybar": "linux/polybar", + ".config/rofi": "linux/rofi", + ".config/sxhkd": "linux/sxhkd", + ".config/fcitx5": "linux/fcitx5", + ".config/dconf": "linux/dconf", + ".config/bakamplayer.ini": "linux/bakamplayer.ini", + ".xinitrc": "linux/xinitrc", + ".pam_environment": "linux/pam_environment", + ".zshrc": "linux/zshrc", + ".Xmodmap": "linux/Xmodmap", }, }; @@ -49,12 +49,12 @@ const linuxMacbook: DotOption = { hostname: "MBP", link: { ...linux.link, - "linux-macbook/dconf": ".config/dconf", - "linux-macbook/polybar": ".config/polybar", - "linux-macbook/rofi": ".config/rofi", - "linux-macbook/sxhkd": ".config/sxhkd", - "linux-macbook/pam_environment": ".pam_environment", - "linux-macbook/Xresources": ".Xresources", + ".config/dconf": "linux-macbook/dconf", + ".config/polybar": "linux-macbook/polybar", + ".config/rofi": "linux-macbook/rofi", + ".config/sxhkd": "linux-macbook/sxhkd", + ".pam_environment": "linux-macbook/pam_environment", + ".Xresources": "linux-macbook/Xresources", }, }; diff --git a/scripts/dot.ts b/scripts/dot.ts index d526c90..32719fb 100644 --- a/scripts/dot.ts +++ b/scripts/dot.ts @@ -30,20 +30,36 @@ export async function dot(args: string[], options: DotOption[]) { } } } else if (flags._[0] === "link" && target.link) { - for (const from in target.link) { + for (const value in target.link) { + const from = target.link[value]; + const to = `${Deno.env.get("HOME")}/${value}`; try { - const path = `${Deno.env.get("HOME")}/${target.link[from]}`; - if (existsSync(path)) { + if (existsSync(to)) { + if (Deno.readLinkSync(to) === Deno.realPathSync(from)) continue; let i = 0; - let mvPath = `${path}.bak`; - while (existsSync(mvPath)) mvPath = `${path}.${++i}.bak`; - console.log(`${path} does exist. move to ${mvPath}`); - moveSync(path, mvPath); + let mvPath = `${to}.bak`; + while (existsSync(mvPath)) mvPath = `${to}.${++i}.bak`; + console.log(`${to} does exist. move to ${mvPath}`); + moveSync(to, mvPath); } - await ensureSymlink(Deno.realPathSync(from), path); - console.log(`Link: ${path}`); + await ensureSymlink(Deno.realPathSync(from), to); + console.log(`Link: ${to}`); } catch (err) { - throw Error(`${err.message}\nerror link: ${JSON.stringify(from)}`); + throw Error(`${err.message}\nerror link: ${to}`); + } + } + } else if (flags._[0] === "unlink" && target.link) { + for (const value in target.link) { + const from = target.link[value]; + const to = `${Deno.env.get("HOME")}/${value}`; + try { + if (existsSync(to)) { + if (Deno.readLinkSync(to) !== Deno.realPathSync(from)) continue; + Deno.removeSync(to); + console.log(`Unlink: ${to}`); + } + } catch (err) { + throw Error(`${err.message}\nerror link: ${to}`); } } }