Skip to content

Commit

Permalink
feat: display sync log (#434)
Browse files Browse the repository at this point in the history
> closes #427 ,在 cli 展示实时同步状态


![image](https://github.com/cnpm/cnpm/assets/5574625/e255f728-6477-4b93-a90b-332970dfd732)

* 📒 修改 `syncInfo` 结构,分别进行查询任务状态和任务日志
* ♻️ 每次全量获取日志信息,去除无效的 offset 参数信息
* 💂 任务完成后再次判断日志信息,oss 日志可能会异步写入
  • Loading branch information
elrrrrrrr committed Dec 28, 2023
1 parent 674b43b commit 11cfecc
Showing 1 changed file with 40 additions and 30 deletions.
70 changes: 40 additions & 30 deletions bin/cnpm-sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

const path = require('node:path');
const fs = require('node:fs');
const { setTimeout } = require('node:timers/promises');
const querystring = require('node:querystring');
const Bagpipe = require('bagpipe');
const request = require('npm-request');
const argv = require('../lib/parse_argv')('sync');
const urllib = require('urllib');

const args = argv.args;
const registrys = [ argv.registry ];
Expand All @@ -20,6 +22,8 @@ let packageName;
let dependencies = [];
let isPrivate = false;

const LOG_FETCH_INTERVAL = 2000;

const packagePath = path.join(process.cwd(), 'package.json');
if (!names.length && fs.existsSync(packagePath)) {
try {
Expand Down Expand Up @@ -53,31 +57,41 @@ Options:
process.exit(1);
}

function showlog(registry, syncInfo, done) {
request({
method: 'GET',
path: syncInfo.logurl + '?offset=' + syncInfo.lastLines,
}, {
registry,
configFile: argv.userconfig,
}, function(err, info) {
if (err) {
return done(err);
}
if (!info || !info.log) {
return setTimeout(showlog.bind(null, registry, syncInfo, done), 2000);
}
const log = info.log.trim();
console.log(log);
syncInfo.lastLines += log.split('\n').length;
if (log.indexOf('[done] Sync ' + syncInfo.name) >= 0) {
done();
} else {
setTimeout(showlog.bind(null, registry, syncInfo, done), 2000);
}
async function showLog(syncInfo, showedLines = 0) {

const stateRes = await urllib.request(syncInfo.stateUrl, {
dataType: 'json',
});

const logRes = await urllib.request(syncInfo.logUrl, { followRedirect: true });
const currentLog = logRes.data.toString().trim();

const { state } = stateRes.data;
if (
state === 'processing' ||
state === 'waiting' ||
state === 'success'
) {
const currentLines = currentLog.split('\n').length;
if (currentLines > showedLines) {
console.log(currentLog.split('\n').slice(showedLines).join('\n'));
showedLines = currentLog.split('\n').length;
// flush success log
if (state === 'success') {
return;
}
}
await setTimeout(LOG_FETCH_INTERVAL);
await showLog(syncInfo, showedLines);
return;
}

// other log
const finalLog = logRes.data.toString().trim();
console.log(finalLog.split('\n').slice(showedLines).join('\n'));
}


function sync(registry, name, callback) {
let url = name + '/sync?';
url += querystring.stringify({
Expand Down Expand Up @@ -112,15 +126,11 @@ function sync(registry, name, callback) {
const syncInfo = {
name,
lastLines: 0,
logurl: name + '/sync/log/' + result.logId,
stateUrl: `${registry}/-/package/${name}/syncs/${result.logId}`,
logUrl: `${registry}/-/package/${name}/syncs/${result.logId}/log`,
};
console.log('logurl: %s/sync/%s#logid=%s', registrywebs[registry], name, result.logId);
showlog(registry, syncInfo, function(err) {
if (err) {
return callback(err);
}
callback(null, { ok: true });
});
console.log('logUrl: %s', syncInfo.logUrl);
showLog(syncInfo).then(() => callback(null, { ok: true })).catch(callback);
});
}

Expand Down

0 comments on commit 11cfecc

Please sign in to comment.