-
Notifications
You must be signed in to change notification settings - Fork 239
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1c5f294
commit 8120d79
Showing
2 changed files
with
51 additions
and
39 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 |
---|---|---|
@@ -1,55 +1,61 @@ | ||
const SYNC_URL = 'https://registry-direct.npmmirror.com/echarts-nightly/sync'; | ||
|
||
let timer; | ||
|
||
async function getSyncLog(id) { | ||
const data = await (await fetch(SYNC_URL + '/log/' + id)).json(); | ||
if (!data.ok) { | ||
return; | ||
} | ||
|
||
const log = data.log || ''; | ||
console.log('Sync log:', log); | ||
if (log.includes('Fail: [')) { | ||
const failInfo = log.match(/Fail: \[ (.*?) \]/); | ||
if (failInfo && failInfo[1]) { | ||
throw new Error('Sync failed!'); | ||
} | ||
} | ||
if (data.syncDone) { | ||
clearInterval(timer); | ||
console.log('Sync successfully!'); | ||
try { | ||
const fullLog = await (await fetch(data.logUrl)).text(); | ||
console.log('Sync full log:\n', fullLog); | ||
} catch {} | ||
} | ||
} | ||
|
||
async function sync(retryCount = 0) { | ||
console.log(`Start to sync nightly mirror...`); | ||
/** | ||
* @param {string} pkgName | ||
* @param {number} [retryCount=3] | ||
*/ | ||
async function sync(pkgName, retryCount = 3) { | ||
const tag = `[${pkgName}]`; | ||
const action = 'sync mirror'; | ||
const syncUrl = `https://registry-direct.npmmirror.com/${pkgName}/sync`; | ||
console.log(tag, `Start to ${action}...`); | ||
try { | ||
const data = await ( | ||
await fetch(SYNC_URL, { | ||
await fetch(syncUrl, { | ||
method: 'PUT', | ||
query: { | ||
sync_upstream: true | ||
} | ||
}) | ||
).json(); | ||
console.log('Sync request data', data); | ||
console.log(tag, 'Sync request data', data); | ||
if (!data.ok) { | ||
throw new Error('Sync request error!'); | ||
throw new Error(tag + ' Sync request error!'); | ||
} | ||
timer = setInterval(() => getSyncLog(data.logId), 2e3); | ||
const timer = setInterval(async () => { | ||
const logData = await ( | ||
await fetch(syncUrl + '/log/' + data.logId) | ||
).json(); | ||
if (!logData.ok) { | ||
return; | ||
} | ||
const log = logData.log || ''; | ||
console.log(tag, 'Sync log:', log); | ||
if (log.includes('Fail: [')) { | ||
const failInfo = log.match(/Fail: \[ (.*?) \]/); | ||
if (failInfo && failInfo[1]) { | ||
throw new Error(tag + ' Sync failed!'); | ||
} | ||
} | ||
if (logData.syncDone) { | ||
clearInterval(timer); | ||
console.log(tag, 'Sync successfully!'); | ||
try { | ||
const fullLog = await (await fetch(logData.logUrl)).text(); | ||
console.log(tag, 'Sync full log:\n', fullLog); | ||
} catch {} | ||
} | ||
}, 2e3); | ||
} catch (e) { | ||
console.error('failed to sync nightly mirror', e); | ||
console.error(tag, `failed to ${action}`, e); | ||
if (!retryCount) { | ||
throw e; | ||
} | ||
console.log('Retry to sync nightly mirror...', retryCount); | ||
console.log(tag, `Retry to ${action}...`, retryCount); | ||
sync(--retryCount); | ||
} | ||
} | ||
|
||
sync(3); | ||
const pkgName = process.argv[2]; | ||
if (!pkgName) { | ||
throw new Error('No package to sync'); | ||
} | ||
sync(pkgName); |
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