CPM - 輕量的NPM私有源程序搭建

CPM 遷移 Nilppm

建議前往NILPPM下載最新版本。此CPM再也不維護。javascript

什麼是CPM

CPM 是一套輕量且基礎功能完善的私有Node包管理源。它是基於 clusic 的 rex 架構開發,擁有進程負載均衡的特色。它主要提供一整套簡易安裝模式,用戶只須要clone此項目到本地,修改config文件夾下的文件便可運行。它的數據源基於mysql數據庫和redis緩存(支持redis集羣),可以有效提升NPM包的下載速度。它還擁有自定義用戶系統接入的功能,讓企業能夠自主接入本身的用戶體系,同時能夠根據用戶的scopes來肯定用戶提交私有包的權限。html

文檔

詳細請移步 這裏vue

預覽

預覽

搭建

CPM搭建的前提條件是:html5

  • Nodejs >= 8.0.0
  • MySQL >= 5.6.16
  • Redis 無限制

請先搭建好以上的環境。java

下載

咱們須要從Github下載咱們的程序。node

$ git clone https://github.com/cevio/cpm.git
複製代碼

請下載穩定的master分支,其餘分支僅開發使用,不建議下載。下載完畢後打開 database.sql 文件,建立mysql數據庫。mysql

依賴

安裝必要的依賴,以便咱們能夠驗證程序是否能夠啓動。git

$ npm i -g @clusic/cli pm2
$ cd cpm
$ npm i
複製代碼

@clusic/cli 是clusic架構的開發工具。github

開發調試

$ npm run dev
複製代碼

安裝完畢依賴,咱們便可以啓動程序。可是你會發現報錯,由於咱們沒有指定用戶驗證體系。web

Error: You should setup your own `UserService` first
    at module.exports (/Users/shenyunjie/code/mzftech/cpm/app.bootstrap.js:7:11)
    at WorkerService.createService (/Users/shenyunjie/code/mzftech/cpm/node_modules/@clusic/rex/lib/app.js:106:15)
    at process._tickCallback (internal/process/next_tick.js:68:7)
    at Function.Module.runMain (internal/modules/cjs/loader.js:744:11)
    at startup (internal/bootstrap/node.js:285:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:739:3)
複製代碼

請沒關係張,這是很是正常的。

修改配置

配置有兩個地方須要修改:

  • config/config.{env}.js {env}表示你的環境變量,通常開發是 config.development.js,生產環境是 config.production.js。你能夠自由修改參數配置。注意registryHost屬性必須修改成你本身的http://127.0.0.1:7002,不然下載包會報錯,若是上線後,請直接修改成你的域名,好比http://npm.example.com
  • config/plugin.{env}.js {env}同上。通常是用來配置插件的數據,這裏咱們須要根據環境不通來修改mysqlredis的數據配置。注意,redis若是須要支持集羣模式,請將redis的配置下面的options編程一個等價數據結構的數組便可。

完成上面修改後,咱們僅須要支持下用戶便可。

用戶體系

用戶體系分兩個函數須要實現:

  • Login() 登入驗證函數
  • User() 用戶查詢函數

咱們須要運行命令:

$ clusic add authorization --service
複製代碼

程序的app/service/下面會自動建立一個文件叫authorization.js,這個文件就是咱們的用戶體系文件。

爲了測試啓動,咱們能夠直接複製下面代碼來快速建立用戶體系函數:

const { ContextComponent } = require('@clusic/method');
module.exports = class AuthorizationService extends ContextComponent {
  constructor(ctx) {
    super(ctx);
  }

  async Login(account, password) {
    return {
      account: account,
      name: account,
      email: account + '@cpm.com',
      avatar: 'https://i.loli.net/2017/08/21/599a521472424.jpg',
      scopes: ['@' + account, '@html5', '@node'],
      extra: {}
    }
  }

  async User(account) {
    return {
      account: account,
      name: account,
      email: account + '@cpm.com',
      avatar: 'https://i.loli.net/2017/08/21/599a521472424.jpg',
      scopes: ['@' + account, '@html5', '@node'],
      extra: {}
    }
  }
};
複製代碼

保存後,你能夠在項目根目錄下經過如下命令啓動查看:

$ npm run dev
複製代碼

打開http://127.0.0.1:7002便可看到咱們的頁面。恭喜你,那麼你可使用CPM了。

命令支持

CPM支持如下的命令組合:

$ npm login --registry=http://npm.test.cn
$ npm logout --registry=http://npm.test.cn
$ npm install (with no args, in package dir) --registry=http://npm.test.cn
$ npm install [<@scope>/]<name> --registry=http://npm.test.cn
$ npm install [<@scope>/]<name>@<tag> --registry=http://npm.test.cn
$ npm install [<@scope>/]<name>@<version> --registry=http://npm.test.cn
$ npm install [<@scope>/]<name>@<version range> --registry=http://npm.test.cn
$ npm install <git-host>:<git-user>/<repo-name> --registry=http://npm.test.cn
$ npm install <git repo url> --registry=http://npm.test.cn
$ npm install <tarball file> --registry=http://npm.test.cn
$ npm install <tarball url> --registry=http://npm.test.cn
$ npm install <folder> --registry=http://npm.test.cn
$ npm update [-g] [<pkg>...] --registry=http://npm.test.cn
$ npm uninstall [<@scope>/]<pkg>[@<version>]... [-S|--save|-D|--save-dev|-O|--save-optional|--no-save] --registry=http://npm.test.cn
$ npm publish [<tarball>|<folder>] [--tag <tag>] [--otp otpcode] [--dry-run] --registry=http://npm.test.cn
$ npm unpublish [<@scope>/]<pkg>[@<version>] --registry=http://npm.test.cn
$ npm whoami [--registry <registry>] --registry=http://npm.test.cn
$ npm owner add <user> [<@scope>/]<pkg> --registry=http://npm.test.cn
$ npm owner rm <user> [<@scope>/]<pkg> --registry=http://npm.test.cn
$ npm owner ls [<@scope>/]<pkg> --registry=http://npm.test.cn
$ npm deprecate <pkg>[@<version>] <message> --registry=http://npm.test.cn
$ npm view [<@scope>/]<name>[@<version>] --registry=http://npm.test.cn
$ npm dist-tag add <pkg>@<version> [<tag>] --registry=http://npm.test.cn
$ npm dist-tag rm <pkg> <tag> --registry=http://npm.test.cn
$ npm dist-tag ls [<pkg>] --registry=http://npm.test.cn
$ npm access public [<package>] --registry=http://npm.test.cn
$ npm access restricted [<package>] --registry=http://npm.test.cn
複製代碼

對於內部私有包而言,這些命令已經足夠使用,若是須要擴展,能夠自行擴展,或者在 Github 上提Issue給我,我酌情考慮添加升級。

簡化命令

在每一個命令後面寫上 --registry=http://npm.test.cn 比較繁瑣,那麼咱們能夠本身生成一個命令叫cpm簡化它。你能夠經過 yeoman 開始建立大家的CPM命令:

const childProcess = require('child_process');
const argv = process.argv.slice(2);
argv.push('--registry=http://npm.test.cn');
childProcess.spawn('npm', argv, { stdio: 'inherit' });
複製代碼

請修改上面的http://npm.test.cn爲你本身的服務地址。

原理是在咱們經過cpm 命令代替 npm 命令的時候,在命令的最末尾加上一個 --registry=http://npm.test.cn 指向咱們的 Registry。

咱們徹底能夠用cpm代替掉npm了。好比

$ cpm login
$ cpm install vue
$ cpm publish
複製代碼

其餘簡化命令的方法有不少,你能夠下載npm install nrm -g等來切換。

深刻用戶體系

上面有一串測試用的代碼,咱們來剖析下。不管是Login仍是User函數都返回以下的數據結構:

  • account string 用戶帳號,惟一性的。
  • name string 用戶姓名
  • email string 用戶郵箱
  • avatar string 用戶頭像
  • scopes array 用戶私有域數組

至於 extra 是額外參數,能夠隨意傳,做用在web界面上。而scope,你能夠經過本身的邏輯代碼給不一樣用戶提供能夠上傳的scopes做用域。

scopes做用域做用結果都是在用戶執行cpm login後生效。若是你改動過代碼,須要用戶從新登陸生效。

上線

上線生成環境須要將production配置完整才能上線,上到服務器後運行命令:

$ npm run start
複製代碼

更新

考慮到通常企業擁有本身的gitlab,固然會clone一份到本身的倉庫,因此請在clone後執行rm -rf .git,清除源倉庫的引用。你能夠提交本程序到你本身的倉庫。更新的時候只須要運行

npm run update
複製代碼

執行完畢這個命令,咱們會從github上將master分支的代碼經過zip包模式下載,覆蓋到本地,固然這是全量覆蓋的。因爲您的git倉庫的存在,因此能夠對比出修改了哪些文件,你能夠revert或者本身處理非app/下的文件內容,通常都是配置。而後修改提交上線便可。

若是是在線上,固然是git pull拉去你已經本地更新好的文件,而後經過命令:

$ npm run restart
複製代碼

感謝

感謝你們使用CPM,若有疑問請提交issue,我會幫忙解答。

相關文章
相關標籤/搜索