add create file for sync

This commit is contained in:
熊潇 2025-05-16 00:16:03 +08:00
parent f26bc59e29
commit aab0662fbc
3 changed files with 46 additions and 28 deletions

View File

@ -88,29 +88,3 @@ const task3 = {
};
silkyCommand.addTask(task3);
// ===========================
const testTask = {
description: '测试',
command: 'npm i -g nodemon --registry=https://registry.npmmirror.com',
type: 'npm-install',
before: 'nodemon -v',
beforeCheck: '.',
key: 'test-task',
};
silkyCommand.addTask(testTask);
const runTask1 = async () => {
const tasksCommand = new TasksCommand();
tasksCommand.addTask(init2);
const res = tasksCommand.runTask(init2.description);
console.log(res);
};
// runTask1();
const runTestTask = async () => {
const tasksCommand = new TasksCommand();
tasksCommand.addTask(testTask);
const res = tasksCommand.runTask(testTask);
console.log(res);
return res;
};
// runTestTask();

View File

@ -47,6 +47,12 @@ export class SyncBase {
return {} as Config;
}
}
getRelativeFile(filename?: string) {
if (!filename) return false;
const dir = this.#dir;
const file = path.join(dir, filename);
return { relative: path.relative(dir, file), absolute: file };
}
async canDone(syncType: SyncConfigType, type?: SyncConfigType) {
if (syncType === 'sync') return true;
return syncType === type;

View File

@ -13,10 +13,12 @@ const command = new Command('sync')
.action(() => {
console.log('同步项目');
});
const syncUpload = new Command('upload')
.option('-d --dir <dir>', '配置目录')
.option('-s --share <share>', '共享设置')
.option('-c --config <config>', '配置文件的名字', 'kevisual.json')
.option('-f --file <file>', '操作的对应的文件名')
.description('上传项目')
.action(async (opts) => {
console.log('上传项目');
@ -31,6 +33,7 @@ const syncUpload = new Command('upload')
if (opts.share) {
meta.share = opts.share;
}
const filepath = sync.getRelativeFile(opts.file);
for (const item of syncList) {
if (!item.auth || !item.exist) {
nodonwArr.push(item);
@ -40,6 +43,9 @@ const syncUpload = new Command('upload')
nodonwArr.push(item);
continue;
}
if (filepath && item.filepath !== filepath.absolute) {
continue;
}
const res = await upload({
token,
file: fs.readFileSync(item.filepath),
@ -59,24 +65,29 @@ const syncUpload = new Command('upload')
}
logger.debug(res);
}
if (nodonwArr.length) {
if (nodonwArr.length && !filepath) {
logger.warn('以下文件未上传\n', nodonwArr.map((item) => item.key).join(','));
}
});
const syncDownload = new Command('download')
.option('-d --dir <dir>', '配置目录')
.option('-c --config <config>', '配置文件的名字', 'kevisual.json')
.option('-f --file <file>', '操作的对应的文件名')
.description('下载项目')
.action(async (opts) => {
const sync = new SyncBase({ dir: opts.dir, baseURL: baseURL, configFilename: opts.config });
const syncList = await sync.getSyncList();
logger.debug(syncList);
const nodonwArr: (typeof syncList)[number][] = [];
const filepath = sync.getRelativeFile(opts.file);
for (const item of syncList) {
if (!sync.canDone(item.type, 'download')) {
nodonwArr.push(item);
continue;
}
if (filepath && item.filepath !== filepath.absolute) {
continue;
}
const hash = sync.getHash(item.filepath);
const { content, status } = await fetchLink(item.url, { setToken: item.auth, returnContent: true, hash });
if (status === 200) {
@ -89,7 +100,7 @@ const syncDownload = new Command('download')
logger.error('下载失败', item.key, chalk.red(item.url));
}
}
if (nodonwArr.length) {
if (nodonwArr.length && !filepath) {
logger.warn('以下文件未下载', nodonwArr.map((item) => item.key).join(','));
}
});
@ -106,9 +117,36 @@ const syncList = new Command('list')
logger.info(chalk.blue(item.key), chalk.gray(item.type), chalk.green(item.url));
});
});
const syncCreateList = new Command('create')
.option('-d --dir <dir>', '配置目录')
.option('-c --config <config>', '配置文件的名字', 'kevisual.json')
.option('-o --output <output>', '输出文件')
.description('创建文件')
.action(async (opts) => {
const sync = new SyncBase({ dir: opts.dir, baseURL: baseURL, configFilename: opts.config });
const syncList = await sync.getSyncList();
logger.debug(syncList);
logger.info('同步列表\n');
let newSync = {};
syncList.forEach((item) => {
logger.info(chalk.blue(item.key), chalk.gray(item.type), chalk.green(item.url));
newSync[item.key] = item.url;
});
const newJson = { ...sync.config };
newJson.sync = newSync;
const filepath = sync.getRelativeFile(opts.output);
if (filepath) {
logger.debug('输出文件', filepath);
fs.writeFileSync(filepath.absolute, JSON.stringify(newJson, null, 2));
} else {
logger.info('输出内容\n');
logger.info(newJson);
}
});
command.addCommand(syncUpload);
command.addCommand(syncDownload);
command.addCommand(syncList);
command.addCommand(syncCreateList);
app.addCommand(command);