npm私有库之cnpmjs.org + mysql

依赖安装

修改node_modules/cnpmjs.org/config/index.js 文件

把下面的copy一下,替换到index.js钟

'use strict';

var mkdirp = require('mkdirp');
var copy = require('copy-to');
var path = require('path');
var fs = require('fs');
var os = require('os');

var version = require('../package.json').version;

var root = path.dirname(__dirname);
var dataDir = path.join(process.env.HOME || root, '.cnpmjs.org');

var config = {
  version: version,
  dataDir: dataDir,

  /**
   * Cluster mode
   */
  enableCluster: false,
  numCPUs: os.cpus().length,

  /*
   * server configure
   */

  registryPort: 7001,  //仓库访问端口(执行发布安装)
  webPort: 7002,   //展示查询站点访问端口
  bindingHost: '', //监听绑定的 Host,默认127.0.0.1,外网访问注释掉此项即可

  // debug mode
  // if in debug mode, some middleware like limit wont load
  // logger module will print to stdout
  debug: process.env.NODE_ENV === 'development',
  // page mode, enable on development env
  pagemock: process.env.NODE_ENV === 'development',
  // session secret
  sessionSecret: 'cnpmjs.org test session secret',
  // max request json body size
  jsonLimit: '10mb',
  // log dir name
  logdir: path.join(dataDir, 'logs'),
  // update file template dir
  uploadDir: path.join(dataDir, 'downloads'),
  // web page viewCache
  viewCache: false,

  // config for koa-limit middleware
  // for limit download rates
  limit: {
    enable: false,
    token: 'koa-limit:download',
    limit: 1000,
    interval: 1000 * 60 * 60 * 24,
    whiteList: [],
    blackList: [],
    message: 'request frequency limited, any question, please contact [email protected]',
  },

  enableCompress: false, // enable gzip response or not

  // default system admins 默认管理员账号
  admins: {
    // name: email
    //fengmk2: '[email protected]',
    admin: '[email protected]',
    //dead_horse: '[email protected]',
  },

  // email notification for errors
  // check https://github.com/andris9/Nodemailer for more informations
  mail: {
    enable: false,
    appname: 'cnpmjs.org',
    from: 'cnpmjs.org mail sender <[email protected]>',
    service: 'gmail',
    auth: {
      user: '[email protected]',
      pass: 'your password'
    }
  },

  logoURL: 'https://os.alipayobjects.com/rmsportal/oygxuIUkkrRccUz.jpg', // cnpm logo image url
  adBanner: '',
  customReadmeFile: '', // you can use your custom readme file instead the cnpm one
  customFooter: '', // you can add copyright and site total script html here
  npmClientName: 'cnpm', // use `${name} install package`
  packagePageContributorSearch: true, // package page contributor link to search, default is true

  // max handle number of package.json `dependencies` property
  maxDependencies: 200,
  // backup filepath prefix
  backupFilePrefix: '/cnpm/backup/',

  /**
   * database config //数据库相关设置 默认sqlite, 使用mysql
   */

  database: {
    db: 'cnpmjs',   //数据库名称
    username: 'root',  //账号
    password: '123456',  // 密码

    // the sql dialect of the database
    // - currently supported: 'mysql', 'sqlite', 'postgres', 'mariadb'
    dialect: 'mysql',  //,默认sqlite,这里我们改成mysql

    // custom host; default: 127.0.0.1
    host: '127.0.0.1',

    // custom port; default: 3306
    port: 3306,

    // use pooling in order to reduce db connection overload and to increase speed
    // currently only for mysql and postgresql (since v1.5.0)
    pool: {
      maxConnections: 10,
      minConnections: 0,
      maxIdleTime: 30000
    },

    // the storage engine for 'sqlite'
    // default store into ~/.cnpmjs.org/data.sqlite
    //storage: path.join(dataDir, 'data.sqlite'),

    //logging: !!process.env.SQL_DEBUG,
  },
  
  // package tarball store in local filesystem by default
  //  node包  下载位置, 本地存储
  nfs: require('fs-cnpm')({
    dir: path.join(dataDir, 'nfs')
  }),
  // if set true, will 302 redirect to `nfs.url(dist.key)`
  downloadRedirectToNFS: false,

  // registry url name
  //模块注册列表访问域名,默认r.cnpmjs.org,安装模块时会到这个域名下查找,这个默认设置略坑,建议没有外网域名的先清空回头再配
  registryHost: '',

  /** 
   * registry mode config  私有模块发布相关配置
   */

  //是否开启私有模式,默认为 false;
  //私有模式下只有管理员能发布模块,其他账号只有同步权限
  //非私有模式,注册用户都可以发布模块
  enablePrivate: false,

  // registry scopes, if don't set, means do not support scopes
  //若为非私有模式发布则此项必填,非管理员发布模块式命名必须以scopes字段开头,模块命名示例“@cnpm/packagename”
  scopes: [ '@cnpm', '@cnpmtest', '@cnpm-test' ],

  // some registry already have some private packages in global scope
  // but we want to treat them as scoped private packages,
  // so you can use this white list.
   // 私有模块非scopes白名单,各种非以scope方式发布的老模块的白名单管理,数组形式维护
  privatePackages: [],

  /**
   * sync configs 同步源仓库相关设置
   */

  //npm官方registry地址,不会直接从这个地址同步模块,但有时会从这里获取模块信息,除非必要请勿更改
  // the official npm registry
  // cnpm wont directly sync from this one
  // but sometimes will request it for some package infomations
  // please don't change it if not necessary
  officialNpmRegistry: 'https://registry.npmjs.com',
  officialNpmReplicate: 'https://replicate.npmjs.com',

  // sync source, upstream registry
  // If you want to directly sync from official npm's registry
  // please drop them an email first
  //同步模块上游registry地址
  sourceNpmRegistry: 'https://registry.npm.taobao.org',

  //上游registry是否是cnpm,默认true,若要使用npm官方地址作为同步上游,请设置为false
  sourceNpmRegistryIsCNpm: true,

  //若安装时模块不存在,是否向源registry进行同步,默认true
  syncByInstall: true,

    // 同步模式选项
    // none: 不进行同步,只管理用户上传的私有模块,公共模块直接从上游获取
    // exist: 只同步已经存在于数据库的模块
    // all: 定时同步所有源registry的模块
  syncModel: 'exist', // 'none', 'all', 'exist'

  syncConcurrency: 1,
  // 同步时间间隔,默认10分钟
  syncInterval: '10m',

  // sync polular modules, default to false
  // because cnpm can't auto sync tag change for now
  // so we want to sync popular modules to ensure their tags
  syncPopular: false,
  syncPopularInterval: '1h',
  // top 100
  topPopular: 100,

  // 是否同步模块中devDependencies,默认false
  syncDevDependencies: false,

  // changes streaming sync
  syncChangesStream: false,
  handleSyncRegistry: 'http://127.0.0.1:7001',

  // badge subject on http://shields.io/
  badgePrefixURL: 'https://img.shields.io/badge',
  badgeSubject: 'cnpm',

  // custom user service, @see https://github.com/cnpm/cnpmjs.org/wiki/Use-Your-Own-User-Authorization
  // when you not intend to ingegrate with your company's user system, then use null, it would
  // use the default cnpm user system
  userService: null,
  
  // 一定要加上这个为true, 默认为false
  enableAbbreviatedMetadata: true,
  
  
  // always-auth https://docs.npmjs.com/misc/config#always-auth
  // Force npm to always require authentication when accessing the registry, even for GET requests.
  alwaysAuth: false,

  // if you're behind firewall, need to request through http proxy, please set this
  // e.g.: `httpProxy: 'http://proxy.mycompany.com:8080'`
  httpProxy: null,

  // snyk.io root url
  snykUrl: 'https://snyk.io',
};

if (process.env.NODE_ENV !== 'test') {
  var customConfig;
  if (process.env.NODE_ENV === 'development') {
    customConfig = path.join(root, 'config', 'config.js');
  } else {
    // 1. try to load `$dataDir/config.json` first, not exists then goto 2.
    // 2. load config/config.js, everything in config.js will cover the same key in index.js
    customConfig = path.join(dataDir, 'config.json');
    if (!fs.existsSync(customConfig)) {
      customConfig = path.join(root, 'config', 'config.js');
    }
  }
  if (fs.existsSync(customConfig)) {
    copy(require(customConfig)).override(config);
  }
}

mkdirp.sync(config.logdir);
mkdirp.sync(config.uploadDir);

module.exports = config;

config.loadConfig = function (customConfig) {
  if (!customConfig) {
    return;
  }
  copy(customConfig).override(config);
};


  • 安装mysql

命令行 或者 使用Navicat for MySQL

登录  
mysql -uroot -p 


create database cnpmjs;
show databases;  
use cnpmjs;

导数据
source c://xxx/xxx/docs/db/sql

show tables;

  • nrm
npm i -g nrm 

nrm add knpm http://127.0.0.1:7001

nrm use knpm

  • pm2
npm i -g pm2
pm2 ls
pm2 start file
pm2 stop id
pm2 delete id

pm2 start dispatch.js

cnpmjs.org start

选择某个包sync

总结

  • 相对于 verdaccio, 会下载相关的包, 及其版本, 可以自动更新包, 但是相对而言本地文件也会相应增加

猜你喜欢

转载自blog.csdn.net/luchuanqi67/article/details/86483098