cnpmjs.org
Version:
Private npm registry and web for Enterprise, base on MySQL and Simple Store Service
84 lines (72 loc) • 1.91 kB
JavaScript
/*!
* cnpmjs.org - tools/sync_not_exist.js
*
* Copyright(c) cnpmjs.org and other contributors.
* MIT Licensed
*
* Authors:
* dead_horse <dead_horse@qq.com> (http://deadhorse.me)
*/
;
/**
* Module dependencies.
*/
var debug = require('debug')('cnpmjs.org:tools:sync_not_exist');
var SyncModuleWorker = require('../proxy/sync_module_worker');
var thunkify = require('thunkify-wrap');
var Module = require('../proxy/module');
var config = require('../config');
var Npm = require('../proxy/npm');
var utility = require('utility');
var co = require('co');
function subtraction(arrOne, arrTwo) {
arrOne = arrOne || [];
arrTwo = arrTwo || [];
var map = {};
var results = [];
arrTwo.forEach(function (name) {
map[name] = true;
});
arrOne.forEach(function (name) {
!map[name] && results.push(name);
});
return results;
}
function *sync(conf) {
if (conf) {
debug('load custom config');
config.loadConfig(conf);
}
var allExists = yield Module.listShort();
var existPackages = allExists.map(function (p) {
return p.name;
});
var allPackages = yield Npm.getShort();
var packages = subtraction(allPackages, existPackages);
if (!packages.length) {
debug('no packages need be sync');
return;
}
debug('Total %d packages to sync', packages.length);
var worker = new SyncModuleWorker({
username: 'admin',
name: packages,
concurrency: config.syncConcurrency
});
worker.start();
var end = thunkify.event(worker);
yield end();
debug('All packages sync done, successes %d, fails %d',
worker.successes.length, worker.fails.length);
return {
successes: worker.successes,
fails: worker.fails
};
}
module.exports = function (config, callback) {
co(sync(config))(callback);
};
if (!module.parent) {
console.log('[tools/sync_not_exist.js] start sync not exist modules');
module.exports();
}