[node.js] 一个 alias 帮助你优先使用当前 nod

2016-10-29  本文已影响562人  jacobbubu

最近由于Node.JS v7 增加了对 async/await 的支持,Coffee-Script 社区也在增加类似的语法,所以我经常要写一些测试程序,需要在既有的 Coffee-Script 版本和开发过程中的 版本 进行切换。所以我增加了一个 Shell Alias 来简化使用过程。

alias coffee='function _coffee(){
    if [ -e "./node_modules/.bin/coffee" ]; then
        "./node_modules/.bin/coffee" --nodejs --harmony --nodejs --expose-wasm $@;
    else
        `npm config get prefix`/bin/coffee --nodejs --harmony --nodejs --expose-wasm $@;
    fi;
};_coffee'

这段程序的逻辑很简单,如果用户在当前目录下输入 coffee yourCode.coffee,那么优先到当前目录的 .node_modules/.bin 下去找 coffee 程序,否则采用全局的 coffee 版本。

以此类推,如果你有针对 webpackgulpmocha 等等有这样的需求,照方处理:

alias webpack='function webpack(){
    if [ -e "./node_modules/.bin/coffee" ]; then
        "./node_modules/.bin/webpack" $@;
    else
        `npm config get prefix`/bin/webpack $@;
    fi;
};webpack'

优先使用本地化的版本是避免 "Dependency Hell" 的主要方法之一。

上一篇下一篇

猜你喜欢

热点阅读