This repository has been archived by the owner on Jun 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 746
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add sync not exist tools, close #424
- Loading branch information
1 parent
b735c9c
commit dc72396
Showing
1 changed file
with
80 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/*! | ||
* cnpmjs.org - tools/sync_not_exist.js | ||
* | ||
* Copyright(c) cnpmjs.org and other contributors. | ||
* MIT Licensed | ||
* | ||
* Authors: | ||
* dead_horse <[email protected]> (http://deadhorse.me) | ||
*/ | ||
|
||
'use strict'; | ||
|
||
/** | ||
* 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() { | ||
var syncTime = Date.now(); | ||
|
||
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); | ||
|
||
Total.setLastExistSyncTime(syncTime, utility.noop); | ||
return { | ||
successes: worker.successes, | ||
fails: worker.fails | ||
}; | ||
} | ||
|
||
module.exports = sync; | ||
|
||
if (!module.parent) { | ||
console.log('[tools/sync_not_exist.js] start sync not exist modules'); | ||
co(sync)(); | ||
} |