log progress without new line

This commit is contained in:
Issue哥 2020-06-30 11:43:39 +08:00
parent 27aed0b80d
commit 46a32b7dc7
2 changed files with 41 additions and 26 deletions

2
copy
View File

@ -42,7 +42,7 @@ if (validate_fid(source)) {
copy({ source, target, name, min_size, update, not_teamdrive, service_account }).then(folder => {
if (!folder) return
const link = 'https://drive.google.com/drive/folders/' + folder.id
console.log('任务完成,新文件夹链接:\n', link)
console.log('\n任务完成,新文件夹链接:\n', link)
})
} else {
console.warn('目录ID缺失或格式错误')

View File

@ -153,11 +153,10 @@ async function walk_and_save ({ fid, not_teamdrive, update, service_account }) {
const limit = pLimit(PARALLEL_LIMIT)
const loop = setInterval(() => {
console.log('================')
console.log('已获取的对象数量', result.length)
console.log('正在进行的网络请求', limit.activeCount)
console.log('排队等候的目录数量', limit.pendingCount)
}, LOG_DELAY)
const now = dayjs().format('HH:mm:ss')
const message = `${now} | 已获取对象 ${result.length} | 排队等候的网络请求 ${limit.pendingCount}`
print_progress(message)
}, 1000)
async function recur (parent) {
let files, should_save
@ -181,8 +180,12 @@ async function walk_and_save ({ fid, not_teamdrive, update, service_account }) {
result.push(...files)
return Promise.all(folders.map(v => recur(v.id)))
}
try {
await recur(fid)
console.log('信息获取完毕')
} catch (e) {
console.error(e)
}
console.log('\n信息获取完毕')
not_finished.length ? console.log('未读取完毕的目录ID', JSON.stringify(not_finished)) : console.log('所有目录读取完毕')
clearInterval(loop)
const smy = summary(result)
@ -483,23 +486,27 @@ async function real_copy ({ source, target, name, min_size, update, not_teamdriv
}
async function copy_files ({ files, mapping, root, task_id }) {
console.log('开始复制文件,总数:', files.length)
console.log('\n开始复制文件,总数:', files.length)
const limit = pLimit(PARALLEL_LIMIT)
let count = 0
const loop = setInterval(() => {
console.log('================')
console.log('已复制的文件数量', count)
console.log('正在进行的网络请求', limit.activeCount)
console.log('排队等候的文件数量', limit.pendingCount)
}, LOG_DELAY)
const now = dayjs().format('HH:mm:ss')
const message = `${now} | 已复制文件数 ${count} | 排队等候的网络请求 ${limit.pendingCount}`
print_progress(message)
}, 1000)
await Promise.all(files.map(async file => {
try {
const { id, parent } = file
const target = mapping[parent] || root
const new_file = await limit(() => copy_file(id, target))
if (new_file) {
db.prepare('update task set status=?, copied = copied || ? where id=?').run('copying', id + '\n', task_id)
db.prepare('update task set status=?, copied = copied || ? where id=?')
.run('copying', id + '\n', task_id)
}
count++
} catch (e) {
console.error(e)
}
}))
clearInterval(loop)
}
@ -552,11 +559,10 @@ async function create_folders ({ source, old_mapping, folders, root, task_id, se
let same_levels = folders.filter(v => v.parent === folders[0].parent)
const loop = setInterval(() => {
console.log('================')
console.log('已创建的文件夹数量', count)
console.log('正在进行的网络请求', limit.activeCount)
console.log('排队等候的网络请求', limit.pendingCount)
}, LOG_DELAY)
const now = dayjs().format('HH:mm:ss')
const message = `${now} | 已创建目录数 ${count} | 排队等候的网络请求 ${limit.pendingCount}`
print_progress(message)
}, 1000)
while (same_levels.length) {
await Promise.all(same_levels.map(async v => {
@ -688,7 +694,16 @@ async function dedupe ({ fid, update, service_account }) {
function handle_error (err) {
const data = err && err.response && err.response.data
data ? console.error(JSON.stringify(data)) : console.error(err.message)
if (data) {
console.error(JSON.stringify(data))
} else {
if (!err.message.includes('timeout')) console.error(err.message)
}
}
function print_progress (msg) {
process.stdout.cursorTo(0)
process.stdout.write(msg)
}
module.exports = { ls_folder, count, validate_fid, copy, dedupe, copy_file, gen_count_body, real_copy }