避免
iCloud
同步node_modules
(Avoidnode_modules
to sync withiCloud
)。
npm i -g nosync-icloud
# or
yarn global add nosync-icloud
很多前端小伙伴都在使用 Mac
作为自己的主力开发机型,而苹果自家 iCloud
同步的便利性相信也不需要我多解释,特别是当你有多台苹果设备时,那种无缝的体验,一旦用了就回不去。可作为一名前端开发,今天,无论你使用的是 Vue
还是 React
亦或任何其他的前端技术栈,几乎是不可能避开 npm
的,但是如果你想把自己的代码也备份到 iCloud
,为它上一份双保险(git仓库
一份)。那么你会发现,当 iCloud
自动同步 node_modules
时,那是一种多么痛的领悟 —— 无尽的文件、嵌套的层级、庞大的体积等等,而 node_modules
也并没有同步的必要,你只需要一个 package.json
和 lock file
就可以随时随地,无缝还原。在这方面,iCloud
的糟糕体验使得你不得不打消用它来备份代码的念头。
如何避免 iCloud
自动同步 node_modules
?方法还是有的,你只需要创建一个 node_modules.nosync
文件夹,然后为它制作一个名为 node_modules
的替身(快捷方式)即可。iCloud
不会同步以 .nosync
结尾的文件或者文件夹,而这样做也不会影响到你的开发,你依然可以使用 npm install
。
mkdir node_modules.nosync && ln -s node_modules.nosync node_modules
但这并不是最佳实践,所以当你用谷歌搜索 iCloud
,node_modules
这些关键字的时候,你会发现大量用户抱怨这个,我也是其中之一,并给苹果提交的反馈,但是苹果似乎并没有积极解决这个问题的态度,网上提供的方案也大体和我上面说的一样。
难道就要这样将就?其实上面的那行命令完全可以做成一个可执行文件,这样每次需要时,只需要执行一个简短的命令就 OK 了。再联想到我们平时用 npm
全局安装的一些 CLI
工具,比如vue init
、create-react-app
、nodemon
等等,我觉得:身为一个前端,我应该做点什么了。于是 —— nosync-icloud 就诞生了👏👏👏🎉🎉🎉。
npm i -g nosync-icloud
# or
yarn global add nosync-icloud
安装成功后会创建 nosync
和 ns
(简写,作用相同)的全局命令。
打开 iCloud 云盘
,进入任何一个你的项目中,在终端中执行 ns
即可。ns
命令会根据你当前项目结构,自动处理 node_modules
,如果你之前没有安装过 node_modules
,它会提供三种可选安装方式 —— npm
、yarn
、cnpm
,当然你也可以选择稍后安装。安装完成后,它会提示你是否将 node_modules*
的忽略规则添加 .gitignore
。
nosync-icloud
不仅可以 禁止 iCloud
自动同步 node_modules
,你还可以通过 ns -f foo
指定任何你不希望同步的文件夹。
指令 | 简写 | 作用 |
---|---|---|
--version | -v | 查看当前版本号 |
--help | -h | 输出帮助信息 |
--folder | -f | 指定不同步到 iCloud 的文件夹,默认 node_modules,如: ns -f foo |
--git | -g | 跳过提示,直接添加 .gitignore,可选:ns -g false,跳过提示,不添加 .gitignore |
希望 ns
命令能成为使用 Mac
开发的前端小伙伴们回不去的习惯,也许以后你进入项目的第一件事不是执行npm install
或 yarn
,而是执行 ns
,为自己的前端项目上一份双保险。如果它实实在在给你带来了便利,也感谢你的 Star
,你也不妨把它分享给身边的其他人。如果你有任何问题或建议,欢迎提交 Issue
和 PR
。