import { program as app, Command } from '@/program.ts';
import { SyncBase } from './modules/base.ts';
import { baseURL, query, storage } from '@/module/query.ts';
import { fetchLink, fetchAiList } from '@/module/download/install.ts';
import fs from 'node:fs';
import { upload } from '@/module/download/upload.ts';
import { logger, printClickableLink } from '@/module/logger.ts';
import { chalk } from '@/module/chalk.ts';
import path from 'node:path';
import { fileIsExist } from '@/uitls/file.ts';
const command = new Command('sync')
.option('-d --dir
')
.description('同步项目')
.action(() => {
console.log('同步项目');
});
const syncUpload = new Command('upload')
.option('-d --dir ', '配置目录')
.option('-c --config ', '配置文件的名字', 'kevisual.json')
.option('-f --file ', '操作的对应的文件名')
.description('上传项目, 上传需要和registry的地址同步。')
.action(async (opts) => {
console.log('上传项目');
const sync = new SyncBase({ dir: opts.dir, baseURL: baseURL, configFilename: opts.config });
const syncList = await sync.getSyncList({ getFile: true });
logger.debug(syncList);
const nodonwArr: (typeof syncList)[number][] = [];
const token = storage.getItem('token');
const meta: Record = {
...sync.config.metadata,
};
const filepath = sync.getRelativePath(opts.file);
const newInfos = [];
for (const item of syncList) {
if (!item.auth || !item.exist) {
nodonwArr.push(item);
continue;
}
if (!sync.canDone(item.type, 'upload')) {
nodonwArr.push(item);
continue;
}
if (filepath && item.filepath !== filepath.absolute) {
continue;
}
const res = await upload({
token,
file: fs.readFileSync(item.filepath),
url: item.url,
needHash: true,
hash: item.hash,
meta: item.metadata ?? meta,
});
if (res.code === 200) {
if (res.data?.isNew) {
newInfos.push(['上传成功', item.key, chalk.green(item.url), chalk.green('文件上传')]);
} else if (res.data?.isNewMeta) {
newInfos.push(['上传成功', item.key, chalk.green(item.url), chalk.green('元数据更新')]);
} else {
// 文件未更新
logger.debug('上传成功', item.key, chalk.green(item.url), chalk.blue('文件未更新'));
}
}
logger.debug(res);
}
if (newInfos.length) {
logger.info('上传成功的文件\n');
newInfos.forEach((item) => {
logger.info(...item);
});
}
if (nodonwArr.length && !filepath) {
logger.warn('以下文件未上传\n', nodonwArr.map((item) => item.key).join(','));
}
});
const syncDownload = new Command('download')
.option('-d --dir ', '配置目录')
.option('-c --config ', '配置文件的名字', 'kevisual.json')
.option('-f --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.getRelativePath(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) {
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 && !filepath) {
logger.warn('以下文件未下载', nodonwArr.map((item) => item.key).join(','));
}
});
const syncList = new Command('list')
.option('-d --dir ', '配置目录')
.option('-c --config ', '配置文件的名字', 'kevisual.json')
.option('-a --all', '显示所有的文件')
.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');
syncList.forEach((item) => {
if (opts.all) {
logger.info(item);
} else {
logger.info(chalk.green(printClickableLink({ url: item.url, text: item.key, print: false })), chalk.gray(item.type));
}
});
});
const syncCreateList = new Command('create')
.option('-d --dir ', '配置目录')
.option('-c --config ', '配置文件的名字', 'kevisual.json')
.option('-o --output