"feat: 同步功能增强与配置优化,支持多类型同步及日志分级"
This commit is contained in:
@@ -1,8 +1,11 @@
|
||||
import { program as app, Command } from '@/program.ts';
|
||||
import { SyncBase } from './modules/base.ts';
|
||||
import { baseURL } from '@/module/query.ts';
|
||||
import { baseURL, storage } from '@/module/query.ts';
|
||||
import { fetchLink } from '@/module/download/install.ts';
|
||||
import fs from 'node:fs';
|
||||
import { upload } from '@/module/download/upload.ts';
|
||||
import { logger } from '@/module/logger.ts';
|
||||
import { chalk } from '@/module/chalk.ts';
|
||||
|
||||
const command = new Command('sync')
|
||||
.option('-d --dir <dir>')
|
||||
@@ -10,21 +13,76 @@ const command = new Command('sync')
|
||||
.action(() => {
|
||||
console.log('同步项目');
|
||||
});
|
||||
const syncUpload = new Command('upload').description('上传项目').action(() => {
|
||||
console.log('上传项目');
|
||||
});
|
||||
const syncUpload = new Command('upload')
|
||||
.option('-d --dir <dir>', '配置目录')
|
||||
.option('-s --share <share>', '共享设置')
|
||||
.description('上传项目')
|
||||
.action(async (opts) => {
|
||||
console.log('上传项目');
|
||||
const sync = new SyncBase({ baseURL: baseURL });
|
||||
const syncList = await sync.getSyncList({ getFile: true });
|
||||
logger.debug(syncList);
|
||||
const nodonwArr: (typeof syncList)[number][] = [];
|
||||
const token = storage.getItem('token');
|
||||
const meta: Record<string, string> = {
|
||||
...sync.config.metadata,
|
||||
};
|
||||
if (opts.share) {
|
||||
meta.share = opts.share;
|
||||
}
|
||||
for (const item of syncList) {
|
||||
if (!item.auth || !item.exist) {
|
||||
nodonwArr.push(item);
|
||||
continue;
|
||||
}
|
||||
if (!sync.canDone(item.type, 'upload')) {
|
||||
nodonwArr.push(item);
|
||||
continue;
|
||||
}
|
||||
const res = await upload({
|
||||
token,
|
||||
file: fs.readFileSync(item.filepath),
|
||||
url: item.url,
|
||||
needHash: true,
|
||||
hash: item.hash,
|
||||
meta,
|
||||
});
|
||||
if (res.code === 200) {
|
||||
logger.info('上传成功', item.key, chalk.green(item.url));
|
||||
}
|
||||
logger.debug(res);
|
||||
}
|
||||
if (nodonwArr.length) {
|
||||
logger.warn('以下文件未上传\n', nodonwArr.map((item) => item.key).join(','));
|
||||
}
|
||||
});
|
||||
const syncDownload = new Command('download')
|
||||
.option('-d --dir <dir>', '配置目录')
|
||||
.description('下载项目')
|
||||
.action(async () => {
|
||||
console.log('下载项目');
|
||||
const sync = new SyncBase({ baseURL: baseURL });
|
||||
const syncList = await sync.getSyncList();
|
||||
console.log(syncList);
|
||||
logger.debug(syncList);
|
||||
const nodonwArr: (typeof syncList)[number][] = [];
|
||||
for (const item of syncList) {
|
||||
const { content } = await fetchLink(item.url, { setToken: item.auth, returnContent: true });
|
||||
await sync.getDir(item.filepath, true);
|
||||
fs.writeFileSync(item.filepath, content);
|
||||
if (!sync.canDone(item.type, 'download')) {
|
||||
nodonwArr.push(item);
|
||||
continue;
|
||||
}
|
||||
const hash = sync.getHash(item.filepath);
|
||||
const { content, status } = await fetchLink(item.url, { setToken: item.auth, returnContent: true, hash });
|
||||
if (status === 200) {
|
||||
await sync.getDir(item.filepath, true);
|
||||
fs.writeFileSync(item.filepath, content);
|
||||
logger.info('下载成功', item.key, chalk.green(item.url));
|
||||
} else if (status === 304) {
|
||||
logger.info('文件未修改', item.key, chalk.green(item.url));
|
||||
} else {
|
||||
logger.error('下载失败', item.key, chalk.red(item.url));
|
||||
}
|
||||
}
|
||||
if (nodonwArr.length) {
|
||||
logger.warn('以下文件未下载', nodonwArr.map((item) => item.key).join(','));
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user