中年危机程序猿的日常

使用 pm2 + sinopia 搭建本地 npm 私有仓库

2018-02-26  本文已影响115人  脱非入欧

背景

相关准备

搭建步骤

安装 nodejs

下载 binary ,用 tar 命令解压缩,然后添加环境变量。

tar -Jxvf node-v8.9.3-linux-x64.tar.xz 

// 修改.bash_profile 
vi .bash_profile 
// 添加内容 
export NODE_HOME=/opt/ibank/node-v8.9.3-linux-x64 
export PATH=$PATH:$NODE_HOME/bin 
export NODE_PATH=$NODE_HOME/lib/node_modules 

// 更新 
source .bash_profile 

ISSUE: 网上说要添加软链接 ln -s /[your node path] /usr/local/bin ,实际上并不需要。

然后,node -v

升级gcc(备选)

下载gcc,解压,查看依赖。这里下的4.9.4,毕竟之前大版本是4,免得跨大版本扯到蛋。

tar -zxvf gcc-4.9.4.tar.gz 
vi ./gcc-4.9.4/contrib/download_prerequisites 

下载依赖库 mpfr,gmp,isl,cloog,mpc并解压,按照download_prerequisites脚本跑一遍命令添加软链接,如:

MPFR=mpfr-2.4.2 
GMP=gmp-4.3.2 
MPC=mpc-0.8.1 
#下载 
wget [ftp://gcc.gnu.org/pub/gcc/infrastructure/$MPFR.tar.bz2](ftp://gcc.gnu.org/pub/gcc/infrastructure/$MPFR.tar.bz2) || exit 1 
#解压 
tar xjf $MPFR.tar.bz2 || exit 1 
#软链接 
ln -sf $MPFR mpfr || exit 1 

在gcc源码目录外建立自己的gcc-build目录,然后运行configure,配置根据自己需要选择。如我的gcc-buildgcc-4.9.4是同级目录。

../gcc-4.9.4/configure --prefix=/usr --infodir=/usr/share/info --mandir=/usr/share/man --libdir=/usr/lib64 --libexecdir=/usr/lib64 --enable-languages=c,c++,objc,fortran,obj-c++,java --enable-checking=release --with-gxx-include-dir=/usr/include/c++/4.3 --enable-ssp --disable-libssp --with-bugurl=http://bugs.opensuse.org/ --with-pkgversion='SUSE Linux' --disable-libgcj --disable-libmudflap --with-slibdir=/lib64 --with-system-zlib --enable-__cxa_atexit --enable-libstdcxx-allocator=new --disable-libstdcxx-pch --enable-version-specific-runtime-libs --program-suffix=-4.3 --enable-linux-futex --without-system-libunwind --with-cpu=generic --build=x86_64-suse-linuxgeneric --build=x86_64-suse-linux 

然后make && make install

ISSUE: 安装过程中缺少zlib,自行下载、解压、./configuremake && make install即可。

备份之前的 libstdc++.so.6

mv libstdc++.so.6 libstdc++.so.6.bak 

更新软链接。

ln -s /usr/lib64/gcc-4.9.4/libstdc++.so.6.20  /usr/lib64/libstdc++.so.6 

此时node -v输出版本号。

搭建私有仓库

内网需要连接代理,设置注册中心。这里连到内网代理 nexus 。

npm set proxy [http://$](https://outlook.live.com/mail/){your nodejs proxy address} 
npm set registry [http://$](https://outlook.live.com/mail/){your nodejs registry} 

安装 pm2、sinopia 和 sinopia-pm2-starter 。

npm install -g pm2 
npm install -g sinopia 
npm install -g sinopia-pm2-starter 

使用方式:

sinopia-pm2-starter --help 
sinopia-pm2-starter config:host '0.0.0.0' 
sinopia-pm2-starter config:post ${port} 
sinopia-pm2-starter start 

修改现在内网客户端的 npm 配置。

npm set proxy null 
npm set registry [http://$](https://outlook.live.com/mail/){address}:${port}/ 

ISSUE: '@storybook/cli' is not in the npm registry,需要修改 sinopia 配置,增加proxy。

vi .config/sinopia/config.yaml 
packages: 
  '@*/*': 
    # scoped packages 
    access: $all 
    publish: $authenticated 
    proxy: npmjs #<====add this 
sinopia-pm2-starter restart 

验证

修改 autopackage.batcall "C:\nodejs\npm" config set registry ${sinopiaAddress},提交构建。

分析日志,nexus 本身自带缓存,在下载 npm lib 方面不存在提升空间。主要问题在于每次构建会删除之前构建的目录,包括 node_modules,导致 npm install 时下载时间较短,安装时间较长。

没关系,最终目的是要管理职能组内部的公共模块。

发布

首先,在 sinopia 添加用户。

:~> npm adduser --registry ${sinopiaAddress} 
Username: ${username} 
Password: ${password} 

然后找一个模块试一试,这里用我自己弄的中国行政区划代码这个公共模块来实验。步骤非常简单:

至于该如何抽取公共模块,应该遵循什么规则,得另外起一个章节了。需要注意的点很多,比如:

上一篇 下一篇

猜你喜欢

热点阅读