diff --git a/package.json b/package.json index 0219b53..e57aabf 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@kevisual/envision-cli", - "version": "0.0.46", + "version": "0.0.48", "description": "envision command tools", "main": "dist/app.mjs", "type": "module", diff --git a/src/command/sync/modules/base.ts b/src/command/sync/modules/base.ts index 7232c87..f5a5917 100644 --- a/src/command/sync/modules/base.ts +++ b/src/command/sync/modules/base.ts @@ -4,6 +4,7 @@ import { Config, SyncList, SyncConfigType, SyncConfig } from './type.ts'; import { fileIsExist } from '@/uitls/file.ts'; import { getHash } from '@/uitls/hash.ts'; import glob from 'fast-glob'; +import { logger } from '@/module/logger.ts'; export type SyncOptions = { dir?: string; @@ -50,6 +51,12 @@ export class SyncBase { if (syncType === 'sync') return true; return syncType === type; } + /** + * + * @param opts + * @param opts.getFile 是否检测文件是否存在 + * @returns + */ async getSyncList(opts?: { getFile?: boolean }): Promise { const config = this.config!; let sync = config?.sync || {}; @@ -110,7 +117,7 @@ export class SyncBase { let obj: Record = {}; const keys: string[] = []; for (let item of syncDirectory) { - const { registry, ignore = [], files, replace = {} } = item; + const { registry, ignore = [], files = [], replace = {} } = item; const cwd = this.#dir; const glob_files = await glob(files, { ignore, @@ -119,9 +126,14 @@ export class SyncBase { dot: true, absolute: true, }); + const registyURL = registry || config.registry; + if (!registyURL) { + logger.error('请配置 registry', item); + continue; + } for (let file of glob_files) { const key = path.relative(cwd, file); - const _registryURL = new URL(registry); + const _registryURL = new URL(registyURL); const replaceKeys = Object.keys(replace); let newKey = key; for (let replaceKey of replaceKeys) { @@ -137,6 +149,11 @@ export class SyncBase { } return { sync: obj, keys }; } + /** + * 获取文件列表,检测文件是否存在 + * @param syncList + * @returns + */ async getSyncListFile(syncList: SyncList[]) { let syncListFile: SyncList[] = []; for (let item of syncList) { diff --git a/src/command/sync/sync.ts b/src/command/sync/sync.ts index 434d664..d2956c5 100644 --- a/src/command/sync/sync.ts +++ b/src/command/sync/sync.ts @@ -16,10 +16,11 @@ const command = new Command('sync') const syncUpload = new Command('upload') .option('-d --dir ', '配置目录') .option('-s --share ', '共享设置') + .option('-c --config ', '配置文件的名字', 'kevisual.json') .description('上传项目') .action(async (opts) => { console.log('上传项目'); - const sync = new SyncBase({ baseURL: baseURL }); + 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][] = []; @@ -48,7 +49,13 @@ const syncUpload = new Command('upload') meta, }); if (res.code === 200) { - logger.info('上传成功', item.key, chalk.green(item.url)); + if (res.data?.isNew) { + logger.info('上传成功', item.key, chalk.green(item.url), chalk.green('文件上传')); + } else if (res.data?.isNewMeta) { + logger.info('上传成功', item.key, chalk.green(item.url), chalk.green('元数据更新')); + } else { + logger.info('上传成功', item.key, chalk.green(item.url), chalk.blue('文件未更新')); + } } logger.debug(res); } @@ -58,9 +65,10 @@ const syncUpload = new Command('upload') }); const syncDownload = new Command('download') .option('-d --dir ', '配置目录') + .option('-c --config ', '配置文件的名字', 'kevisual.json') .description('下载项目') - .action(async () => { - const sync = new SyncBase({ baseURL: baseURL }); + .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][] = []; @@ -85,7 +93,22 @@ const syncDownload = new Command('download') logger.warn('以下文件未下载', nodonwArr.map((item) => item.key).join(',')); } }); +const syncList = new Command('list') + .option('-d --dir ', '配置目录') + .option('-c --config ', '配置文件的名字', 'kevisual.json') + .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) => { + logger.info(chalk.blue(item.key), chalk.gray(item.type), chalk.green(item.url)); + }); + }); command.addCommand(syncUpload); command.addCommand(syncDownload); +command.addCommand(syncList); + app.addCommand(command);