CPM migration Nilppm

It is recommended to go to NILPPM to download the latest version. This CPM is no longer maintained.

What is a CPM

CPM is a lightweight and fully functional private Node package management source. It is based on the REX architecture of ClUSIC and features process load balancing. It mainly provides a set of easy installation mode, users just need to clone the project to the local, modify the file in the config folder to run. Its data source is based on mysql database and Redis cache (support redis cluster), which can greatly improve the download speed of NPM packages. It also has the ability to customize user system access, allowing enterprises to access their own user systems and determine the permissions of users to submit private packages based on their scopes.

The document

For details, please go here

preview

Set up

The prerequisites for CPM construction are:

  • Nodejs > = 8.0.0
  • MySQL > = 5.6.16
  • Redis unlimited

Please set up the above environment first.

download

We need to download our program from Github.

$ git clone https://github.com/cevio/cpm.git
Copy the code

Download the stable master branch. Other branches are for development only and are not recommended. Open database. SQL file to create mysql database.

Rely on

Install the necessary dependencies so that we can verify that the program can start.

$ npm i -g @clusic/cli pm2
$ cd cpm
$ npm i
Copy the code

Clusic/CLI is a development tool for the CLUSIC architecture.

Development and debugging

$ npm run dev
Copy the code

After installing the dependency, we can start the program. But you will find an error because we did not specify a user authentication system.

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)
Copy the code

Please don’t be nervous, it’s perfectly normal.

Modify the configuration

There are two changes to the configuration:

  • config/config.{env}.js {env}Represents your environment variable, which is generally developedconfig.development.js, the production environment isconfig.production.js. You are free to modify the parameter configuration. Pay attention toregistryHostProperties must be modified to your ownhttp://127.0.0.1:7002, otherwise download package will report error, if online, please directly modify to your domain name, such ashttp://npm.example.com.
  • config/plugin.{env}.js {env}Same as above. Generally used to configure plug-in data, here we need to modify according to the environmentmysqlandredisData configuration. Note that if Redis needs to support cluster mode, please configure redis belowoptionsProgram an array of equivalent data structures.

After making the above changes, we just need to support the users.

The user system

The user system is divided into two functions:

  • Login()Login authentication function
  • User()User query function

We need to run the command:

$ clusic add authorization --service
Copy the code

The app/service/ of the program will automatically create a file named authorization.js, which is our user system file.

To test startup, we can quickly create user system functions by directly copying the following code:

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: [The '@' + 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: [The '@' + account, '@html5'.'@node'].extra: {}}}};Copy the code

After saving, you can start to view it in the project root directory with the following command:

$ npm run dev
Copy the code

Go to http://127.0.0.1:7002 to see our page. Congratulations, then you can use CPM.

Command support

CPM supports the following command combinations:

$ npm login --registry=http://npm.test.cn
$ npm logout --registry=http://npm.test.cn
$ npm install (with no args, inpackage 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.cnCopy the code

For internal private packages, these commands are sufficient, if you need to extend them, you can extend them yourself, or send an Issue to me on Github, and I will consider adding an upgrade.

Simplify command

Writing –registry=http://npm.test.cn after each command is tedious, so we can generate our own command called CPM to simplify it. You can start creating your CPM commands with Yeoman:

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

Please modify http://npm.test.cn above for your own service address.

The principle is that when we replace NPM with CPM commands, we add a –registry=http://npm.test.cn at the end of the command to point to our Registry.

We can completely replace NPM with CPM. Such as

$ cpm login
$ cpm install vue
$ cpm publish
Copy the code

There are many other ways to simplify commands. You can download NPM install NRM -g to switch.

Go deep into user system

There is a string of test code above, let’s take a look. Both the Login and User functions return the following data structure:

  • account stringUser account, unique.
  • name stringThe user name
  • email stringUser mailbox
  • avatar stringThe avatars
  • scopes arrayArray of user private domains

As for “extra”, it is an extra parameter, which can be passed freely and applied to the Web interface. For scope, you can use your own logical code to provide the scopes that can be uploaded by different users.

The scopes results take effect after the user executes CPM login. If you change the code, the user needs to log in again to take effect.

online

To go online, the production generation environment needs to be configured completely. After going to the server, run the following command:

$ npm run start
Copy the code

update

Considering that most enterprises have their own GitLab, of course they will clone a copy to their own repository, so please run rm -rf. git after clone to clear the reference of the source repository. You can submit this program to your own warehouse. When you update, you just run it

npm run update
Copy the code

After executing this command, we will download the master branch code from Github via zip package mode and overwrite it locally, of course this is full coverage. Because your Git repository exists, you can compare which files have been modified, and you can revert or manage the contents of files that are not in app/, usually in configuration. Then modify submit online.

If you are online, git pull, of course, will pull the files you have updated locally and run the following command:

$ npm run restart
Copy the code

Thank you

Thank you for using CPM. If you have any questions, please submit an issue and I will help you answer them.