blog icon indicating copy to clipboard operation
blog copied to clipboard

通过sinopia搭建私有的包管理器

Open huangshuwei opened this issue 6 years ago • 0 comments

前言

出于安全原因,不敢私自将公司代码放到github 或者 npm 等开源平台。又因为急切需要管理开发的公共组件,所以决定自己搭建一套私有的包管理器。下面是通过sinopia搭建私有包管理器的。

推荐使用 verdaccio搭建私有的包管理器,参考:通过verdaccio搭建私有的包管理器

准备工作

需要已经配置好nodejs及npm环境

服务端部署

全局安装 sinopia $ npm install -g sinopia

启动 $ sinopia

将会看到 sinopia-install config file - sinopia 配置文件 http address - sinopia 的包管理地址(默认localhost:4873/)

验证是否安装成功,打开localhost:4873/,如果可以正常访问代表启动成功。

config.yaml默认配置

#
# This is the default config file. It allows all users to do anything,
# so don't use it on production systems.
#
# Look here for more config file examples:
# https://github.com/rlidwka/sinopia/tree/master/conf
#

# path to a directory with all packages
storage: ./storage // npm包存放的路径

auth:
  htpasswd:
    file: ./htpasswd // 保存用户的账号密码等信息
    # Maximum amount of users allowed to register, defaults to "+inf".
    # You can set this to -1 to disable registration.
    #max_users: 1000

# a list of other known repositories we can talk to
uplinks:
  npmjs:
    url: https://registry.npmjs.org/ //拉取公共包的地址源,默认为npm的官网,可以使用淘宝的npm镜像地址

packages: // 配置权限管理
  '@*/*':
    # scoped packages
    access: $all
    publish: $authenticated

  '*':
    # allow all users (including non-authenticated users) to read and
    # publish all packages
    #
    # you can specify usernames/groupnames (depending on your auth plugin)
    # and three keywords: "$all", "$anonymous", "$authenticated"
    access: $all

    # allow all known users to publish packages
    # (anyone can register by default, remember?)
    publish: $authenticated

    # if package is not available locally, proxy requests to 'npmjs' registry
    proxy: npmjs

# log settings
logs:
  - {type: stdout, format: pretty, level: http}
  #- {type: file, path: sinopia.log, level: info}

外放访问配置 默认访问地址是 http://localhost:4873/,我们在搭建完后需要对外暴露一个可访问的地址(通常是局域网)。那么我们可以在 config.yaml文件中配置,需要在文件中加入

# listen
listen: xxx.xxx.xxx.xxx:4873

账号配置 账号配置对应的是config.yaml文件中的auth部分,默认可以通过客户端?npm adduser添加账号。

$ npm adduser --registry http://localhost:4873/

会提示输入用户名和密码,密码信息将会保存到 htpasswd文件中。

客户端配置

安装 nrm 进行 包管理器的切换(切换 npm 和 sinopia) $ npm install -g nrm

此时我们执行 $ nrm ls查看所有的仓库配置,是看不到sinopia的:

 npm ---- https://registry.npmjs.org/
  cnpm --- http://r.cnpmjs.org/
  taobao - https://registry.npm.taobao.org/
  nj ----- https://registry.nodejitsu.com/
  rednpm - http://registry.mirror.cqupt.edu.cn/
  npmMirror  https://skimdb.npmjs.com/registry/
  edunpm - http://registry.enpmjs.org/

添加sinopia仓库地址

nrm add sinopia http://xxx.xxx.xxx.xxx:4873

如果觉得单词sinopia不好记,可以改成其他的好记的名字,下面是以sinopia为例。

添加成功后会提示 add registry sinopia success

切换到私有仓库 $ nrm use sinopia 会提示

verb config Skipping project config: C:\Users\shuwei/.npmrc. (matches userconfig)
Registry has been set to: http:// xxx.xxx.xxx.xxx:4873/

这时我们再去执行$ nrm ls可以看到 sinopia

  npm ---- https://registry.npmjs.org/
  cnpm --- http://r.cnpmjs.org/
  taobao - https://registry.npm.taobao.org/
  nj ----- https://registry.nodejitsu.com/
  rednpm - http://registry.mirror.cqupt.edu.cn/
  npmMirror  https://skimdb.npmjs.com/registry/
  edunpm - http://registry.enpmjs.org/
* sinopia  http://xxx.xxx.xxx.xxx:4873/

发布私有包(和 npm 发布包模式一样) 首先假设我们已经准备好了一个要上传的私有包叫 test-private-mod。 我们cd到包所在的目录,执行npm publish,此时会报错,这是我们需要配置账号信息。 可以执行命令

$ npm adduser

需要注意的是,确保当前包管理服务是sinopia (通过nrm ls查看)。如果当前的包管理服务是npm,那么你通过命令 $ npm adduser将会添加到 npm包管理服务中,此时发布的也是发到了https://registry.npmjs.org/

再次执行发布命令:

$ npm publish

发布成功后我们可以看到storage文件夹下的确有了这个包。 并且可以通过访问http:// xxx.xxx.xxx.xxx:4873/看到已经发布的包了。 但是当发布了多个版本后,并没有列出所有的包的版本,所以建议每个包的readme一定要把每次发布的包给记录进去。

安装私有包 如果当前的包管理服务是npm那么你安装不到私有的安装包的,需要切换到sinopia服务($ nrm use sinopia)。但是如果当前是sinopia包服务,既可以安装到私有的包也能安装到公共包。

If some package doesn't exist in the storage, server will try to fetch it from npmjs.org. If npmjs.org is down, it serves packages from cache pretending that no other packages exist. Sinopia will download only what's needed (= requested by clients), and this information will be cached, so if client will ask the same thing second time, it can be served without asking npmjs.org for it.

当然安装包的方式也是和npm一样。我们安装一把之前发布的私有包:

$ npm install test-private-mod --save-dev

结果:

npm notice
npm notice package: test-private-mod
npm notice === Tarball Contents ===
npm notice 282B  package.json
npm notice 566B  index.js
npm notice 50B   readme.md
npm notice 155B  .idea/inspectionProfiles/Project_Default.xml
npm notice 174B  .idea/misc.xml
npm notice 280B  .idea/modules.xml
npm notice 458B  .idea/private-module.iml
npm notice 8.5kB .idea/workspace.xml
npm notice 732B  test-mod/url/parseQueryString.js
npm notice 648B  test-mod/url/stringfyQueryString.js
npm notice === Tarball Details ===
npm notice name:          test-private-mod
npm notice version:       1.0.0
npm notice package size:  3.8 kB
npm notice unpacked size: 11.8 kB
npm notice shasum:        fe1d72fb344483f6c04093d11bc25d221ab97f57
npm notice integrity:     sha512-4KMWOQbgOIfaP[...]dazor3nQdKxxQ==
npm notice total files:   10
npm notice
+ test-private-mod

并且已安装的包也会体现在 package.json文件中,这里有一点要注意的是,如果发布的包没有规范命名是很难区分公共包和私有包的,所以在发布包的时候包名可以加上前缀‘local-’,或者其他可以区分的前缀。

不足之处(具体看sinopia

  • 不支持 npm tag
  • 不支持 star、unstar

sinopia现状

上次发布还是2015年,所以这个项目已经停止维护了,如果后面发现不能满足需求会选择其他的方案。

总结

刚配置好,就发现一个更好的替代方案:verdaccio,由于sinopia已经很久没更新了,而且文档不全,也不太敢用。verdaccio有详细的文档。当然我还是会记录自己的采坑过程,详见通过verdaccio搭建私有的包管理器

huangshuwei avatar Jul 13 '18 08:07 huangshuwei