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.
feat: support sync private package from custom registry
- Loading branch information
wangjiaxin2
committed
Feb 8, 2022
1 parent
ad622d5
commit e4dae73
Showing
9 changed files
with
366 additions
and
13 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
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
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
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,23 @@ | ||
'use strict'; | ||
var config = require('../../config'); | ||
var npmService = require('../../services/npm'); | ||
|
||
module.exports = function* showScopeSync () { | ||
var scope = this.params.scope; | ||
var scopeConfig = (config.syncScopeConfig || []).find(function (item) { | ||
return item.scope === scope | ||
}) | ||
|
||
if (!scopeConfig) { | ||
return this.redirect('/'); | ||
} | ||
|
||
var packages = yield* npmService.getScopePackagesShort(scope, scopeConfig.sourceCnpmWeb) | ||
|
||
yield this.render('scope_sync', { | ||
packages: packages, | ||
scope: scopeConfig.scope, | ||
sourceCnpmRegistry: scopeConfig.sourceCnpmRegistry, | ||
title: 'Sync Scope Packages', | ||
}); | ||
}; |
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
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
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
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,89 @@ | ||
'use strict'; | ||
|
||
var thunkify = require('thunkify-wrap'); | ||
const co = require('co'); | ||
const ms = require('humanize-ms'); | ||
var config = require('../config'); | ||
var npmService = require('../services/npm'); | ||
var SyncModuleWorker = require('../controllers/sync_module_worker'); | ||
var logger = require('../common/logger'); | ||
|
||
|
||
let syncing = false; | ||
const syncFn = co.wrap(function*() { | ||
if (syncing) { return; } | ||
syncing = true; | ||
logger.syncInfo('Start syncing scope modules...'); | ||
let data; | ||
let error; | ||
try { | ||
data = yield sync(); | ||
} catch (err) { | ||
error = err; | ||
error.message += ' (sync package error)'; | ||
logger.syncError(error); | ||
} | ||
|
||
if (data) { | ||
logger.syncInfo(data); | ||
} | ||
if (!config.debug) { | ||
sendMailToAdmin(error, data, new Date()); | ||
} | ||
syncing = false; | ||
}); | ||
|
||
syncFn().catch(onerror); | ||
setInterval(() => syncFn().catch(onerror), ms(config.syncScopeInterval)); | ||
|
||
function onerror(err) { | ||
logger.error('====================== scope sync error ========================'); | ||
logger.error(err); | ||
} | ||
|
||
function* getOtherCnpmDefineScopePackages(scopes) { | ||
var arr = [] | ||
for (var i = 0; i < scopes.length; i++) { | ||
var packageList = yield* npmService.getScopePackagesShort(scopes[i].scope, scopes[i].sourceCnpmWeb) | ||
arr = arr.concat(packageList) | ||
} | ||
return arr | ||
} | ||
|
||
function* sync() { | ||
var scopeConfig = config.syncScopeConfig | ||
if (!scopeConfig || scopeConfig.length === 0) { | ||
process.exit(0); | ||
} | ||
var packages = yield* getOtherCnpmDefineScopePackages(scopeConfig); | ||
|
||
if (!packages.length) { | ||
return; | ||
} | ||
logger.syncInfo('Total %d scope packages to sync: %j', packages.length, packages); | ||
|
||
var worker = new SyncModuleWorker({ | ||
username: 'admin', | ||
name: packages, | ||
noDep: true, | ||
syncUpstreamFirst: false, | ||
publish: true, | ||
concurrency: config.syncConcurrency, | ||
syncPrivatePackage: scopeConfig.reduce((arr, cur) => { | ||
arr[cur.scope] = cur.sourceCnpmRegistry | ||
return arr | ||
}, {}) | ||
}); | ||
worker.start(); | ||
var end = thunkify.event(worker); | ||
yield end(); | ||
|
||
logger.syncInfo('scope packages sync done, successes %d, fails %d, updates %d', | ||
worker.successes.length, worker.fails.length, worker.updates.length); | ||
|
||
return { | ||
successes: worker.successes, | ||
fails: worker.fails, | ||
updates: worker.updates, | ||
}; | ||
}; |
Oops, something went wrong.