前言

当你拥有一大堆 React 的时候你会发现:当你多个项目使用了同样的框架如 Ant Design Pro,你会发现你的电脑里面存了好几份一样的 node_modules 副本,而且每一个都动辄好几个 Gb,移动、删除都很不方便。那为什么不构建一个统一的本地 node_modules 仓库呢!

原理

Windwos 符号连接

# An example.
cd E:\Users\Dell\IdeaProjects\Hnuahe
E:
mklink /j .\schedule.wx.hnuahe.edu.cn\node_modules\ .\_libraries\node_modules\

实现

基于 Python 3.x 实现,实现了图形化的源文件夹和目标文件夹选择,简化了原先手动启动 CMD 且需要进入管理员模式并手动定位文件夹的繁琐过程。

如果你想将 C:\\_libraries\\node_modules 连接到 C:\\passport.liusuyun.com\node_modules,你只需要选择 C:\\_libraries\\node_modules 以及 C:\\passport.liusuyun.com。!!!请注意,目标文件夹下不要存在 node_modules 文件夹,所以你也选择不到这个文件夹。~!!!

# link.pyw

import ctypes
import sys
import tkinter
from tkinter import filedialog
from tkinter import messagebox


def preflight():
    if not ctypes.windll.shell32.IsUserAnAdmin():
        ctypes.windll.shell32.ShellExecuteW(None, 'runas', sys.executable, __file__, None, 1)
        exit(0)


def select_source():
    # 删除 Source 选择组件
    source_selector.grid_forget()
    # 添加 Source 展示组件
    source_content.set(tkinter.filedialog.askdirectory())
    source.grid(row=0, column=1, padx=0, pady=0, sticky=tkinter.W)
    # 添加 Destination 选择组件
    destination_label.grid(row=1, column=0, padx=0, pady=0, sticky=tkinter.W)
    destination_selector.grid(row=1, column=1, padx=0, pady=0, sticky=tkinter.W)


def select_destination():
    # 删除 Destination 选择组件
    destination_selector.grid_forget()
    # 构建具体文件夹
    target = source_content.get().split(sep='/')
    # 添加 Destination 展示组件
    destination_content.set('{}/{}'.format(tkinter.filedialog.askdirectory(), target[-1]))
    destination.grid(row=1, column=1, padx=0, pady=0, sticky=tkinter.W)
    # 添加创建按钮
    tkinter.Button(frame, text='连接', command=create) \
        .grid(row=2, column=0, padx=0, pady=0, sticky=tkinter.W)


root = tkinter.Tk()
frame = tkinter.Frame(root)
source_content = tkinter.StringVar()
source = tkinter.Label(frame, textvariable=source_content)
source_label = tkinter.Label(frame, text='源文件夹: ')
source_selector = tkinter.Button(frame, text="请选择源文件夹", command=select_source)
destination_content = tkinter.StringVar()
destination = tkinter.Label(frame, textvariable=destination_content)
destination_label = tkinter.Label(frame, text='目标文件夹: ')
destination_selector = tkinter.Button(frame, text="请选择目标文件夹", command=select_destination)


def show():
    # 添加控件
    frame.pack(padx=24, pady=24, ipadx=0)
    source_label.grid(row=0, column=0, padx=0, pady=0, sticky=tkinter.W)
    source_selector.grid(row=0, column=1, padx=0, pady=0, sticky=tkinter.W)
    # 居中显示窗口
    root.update_idletasks()
    x = (root.winfo_screenwidth() - root.winfo_reqwidth()) / 2
    y = (root.winfo_screenheight() - root.winfo_reqheight()) / 2
    root.geometry("+%d+%d" % (x, y))
    root.title = 'Ying · 符号连接制造器'
    # 进入消息循环
    root.mainloop()


def create():
    kernel = ctypes.windll.LoadLibrary("kernel32.dll")
    kernel.CreateSymbolicLinkW(destination_content.get(), source_content.get(), 1)
    tkinter.messagebox.showinfo(
        '符号连接创建成功',
        '{} -> {}'.format(destination_content.get(), source_content.get())
    )


if __name__ == '__main__':
    preflight()
    show()
最后修改:2022 年 03 月 17 日
如果觉得我的文章对你有用,请随意赞赏