zoukankan      html  css  js  c++  java
  • npm 安装私有 git 包

    npm 安装私有 git 包

    npm 对于前端开发来说是一种必备的工具,对于开源项目来说,完全没有任何问题,安装包的依赖直接依赖于 Npm 即可。
    对于公司内网的一些项目就不是太方便了,因为我们通常会有这样的项目结构:

    项目结构

    对于 npm 公用包来说是比较方便的,直接引用即可。而内网的代码应该怎么引入呢?大概有以下几种方式:

    1. npm 公有包
    2. npm 私有包
    3. 搭建 npm 私有服务器
    4. git 仓库

    公有包肯定是满足不了需求的,因为所有人都能下载包,也就意味着代码是被泄漏了,首先被排除。npm 私有包是收费的,
    而搭建 npm 服务器也是需要一定成本的,所以最好的方案自然是采用 npm 安装 git 仓库的方式了。

    下看 npm 对于安装 git 仓库的命令:

    npm install <git remote url>
    

    实际上就是直接 install 一个 URL 而已。对于一些公有仓库, npm 还是做了一些集成的,比如 github等(示例全部出自 npm 官方文档):

    npm install github:mygithubuser/myproject
    npm install bitbucket:mybitbucketuser/myproject
    npm install gitlab:myusr/myproj#semver:^5.0
    

    如果我们直接安装 github 上,使用网址的方式可以表示为:

    npm install git+https://isaacs@github.com/npm/npm.git
    

    看下 npm 安装 git 仓库的协议:

    <protocol>://[<user>[:<password>]@]<hostname>[:<port>][:][/]<path>[#<commit-ish> | #semver:<semver>]
    

    <protocol> is one of git, git+ssh, git+http, git+https, or git+file.

    If #<commit-ish> is provided, it will be used to clone exactly that commit. If the commit-ish has the format #semver:<semver>, <semver> can be any valid semver range or exact version, and npm will look for any tags or refs matching that range in the remote repository, much as it would for a registry dependency. If neither #<commit-ish>or #semver:<semver> is specified, then master is used.

    即 protocol 支持 git, git+ssh, git+http, git+https, git+file,私有仓库需要用户名和密码时需要填写用户名和密码,semver 表示需要使用的版本号, 不过貌似不生效。(npm 中有个包 semver 是专门用于比较包的版本号大小)

    直接写 #branch 表示需要安装的分支号。

    所以在开发过程中我们可以这么写包:

    npm i git+https://username:password@git.example.com/path/reposity#master
    

    或者使用打的 tag

    npm i git+https://username:password@git.example.com/path/reposity#1.0.0
    

    可能存在的问题是:
    由于新版的 npm install 在安装时会使用 package-lock.json, 有时候同一分支不会从 github 上拉取最新的,
    可能需要手动再安装一下(拿自己的仓库试了下,果然不会更新),所以安装时尽量以 tag 为标签进行安装,这样确保代码是正确的

    此外,由于私有仓库都是需要密码的,这个时候需要提供一个公共账号和密码,某种程度上不利于管理吧

  • 相关阅读:
    C++ template —— 类型区分(十一)
    C++ template —— 表达式模板(十)
    C++ template —— template metaprogram(九)
    C++ template —— 模板与继承(八)
    [转]2015有得有悟,2016笨鸟起飞
    C++ template —— trait与policy类(七)
    protobuf与json相互转换的方法
    如何通过卡面标识区分SD卡的速度等级
    MyEclipse设置字体和背景的方法
    JAVA中日期转换和日期计算的方法
  • 原文地址:https://www.cnblogs.com/dreamless/p/8616670.html
Copyright © 2011-2022 走看看