From 4dbee366c50d83a1f9c900c450bec1fff7218461 Mon Sep 17 00:00:00 2001 From: abearxiong Date: Sun, 12 Oct 2025 19:55:33 +0800 Subject: [PATCH] feat: add clone file --- package.json | 2 +- pnpm-workspace.yaml | 4 +-- src/command/gist/index.ts | 74 +++++++++++++++++++++++++++++++++++++++ src/index.ts | 1 + src/module/query.ts | 13 +++++++ 5 files changed, 90 insertions(+), 4 deletions(-) create mode 100644 src/command/gist/index.ts diff --git a/package.json b/package.json index 6779fe9..5bc672a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@kevisual/cli", - "version": "0.0.58", + "version": "0.0.59", "description": "envision command tools", "main": "dist/app.mjs", "type": "module", diff --git a/pnpm-workspace.yaml b/pnpm-workspace.yaml index c8e25c5..89d5c72 100644 --- a/pnpm-workspace.yaml +++ b/pnpm-workspace.yaml @@ -1,4 +1,2 @@ packages: - - 'submodules/*' - - 'assistant' - - '!submodules/assistant-center' \ No newline at end of file + - 'assistant' \ No newline at end of file diff --git a/src/command/gist/index.ts b/src/command/gist/index.ts new file mode 100644 index 0000000..6b8b250 --- /dev/null +++ b/src/command/gist/index.ts @@ -0,0 +1,74 @@ +import { program as app, Command } from '@/program.ts'; +import path from 'node:path'; +import fs from 'node:fs'; +import { spawn } from 'child_process'; + +import { chalk } from '@/module/chalk.ts'; +import { getHeader, query } from '../../module/query.ts'; + +// pnpm dev gist "https://kevisual.xiongxiao.me/root/resources/vite-3d-template/1.0.0/kevisual.json" +const command = new Command('gist') + .description('同步片段代码') + .option('-d, --dir ', '配置目录') + .arguments('') + .action((link, opts) => { + if (!link) { + console.log(chalk.red('请提供链接')); + return; + } + const dir = path.resolve(opts.dir || process.cwd()); + // dir不存在就创建 + if (!fs.existsSync(dir)) { + fs.mkdirSync(dir, { recursive: true }); + } + const cmd = `ev gist download -l ${link} -s ` + console.log(chalk.green('开始执行'), cmd); + spawn(cmd, { + shell: true, stdio: 'inherit', + cwd: dir + }); + }); + +// pnpm dev gist download -l "https://kevisual.xiongxiao.me/root/resources/vite-3d-template/1.0.0/kevisual.json" -d clone -s +const download = new Command('download') + .option('-d --dir ', '配置目录') + .option('-c --config ', '配置文件的名字', 'kevisual.json') + .option('-s --sync', '下载配置成功后,是否需要同步文件') + .option('-l --link ', '下载配置链接') + .description('克隆代码片段') + .action(async (opts) => { + console.log('克隆代码片段', opts); + const dir = path.resolve(opts.dir || process.cwd()); + const link = opts.link || ''; + const configFilename = opts.config || 'kevisual.json'; + const configPath = path.join(dir, configFilename); + if (!link) { + console.log(chalk.red('请提供链接')); + return; + } + + const res = await fetch(link, { + headers: await getHeader(), + }).then(res => { + return res.json(); + }).catch((err) => { + console.log(chalk.red('配置文件下载失败')); + throw '配置文件下载失败'; + }); + fs.mkdirSync(dir, { recursive: true }); + fs.writeFileSync(configPath, JSON.stringify(res, null, 2)); + console.log(chalk.green('配置文件下载成功: ' + configPath)); + if (opts.sync) { + const cmd = `ev sync download --config "${configFilename}"`; + console.log(chalk.green('开始同步文件'), cmd); + spawn(cmd, { + cwd: dir, + shell: true, + stdio: 'inherit', + }); + } + }); + +command.addCommand(download); + +app.addCommand(command); \ No newline at end of file diff --git a/src/index.ts b/src/index.ts index 1dc1723..b26a2eb 100644 --- a/src/index.ts +++ b/src/index.ts @@ -14,6 +14,7 @@ import './command/sync/sync.ts'; import './command/app/index.ts'; +import './command/gist/index.ts'; // program.parse(process.argv); export const runParser = async (argv: string[]) => { diff --git a/src/module/query.ts b/src/module/query.ts index 7f86600..52b1baa 100644 --- a/src/module/query.ts +++ b/src/module/query.ts @@ -22,6 +22,19 @@ export const getBaseURL = () => { export const query = new Query({ url: `${getBaseURL()}/api/router`, }); +export const getHeader = async () => { + const headers: Record = { + 'Content-Type': 'application/json', + }; + let token = process.env.KEVISUAL_TOKEN; + if (!token) { + token = await storage.getItem('token'); + } + if (token) { + headers['Authorization'] = 'Bearer ' + token; + } + return headers; +} query.beforeRequest = async (config) => { if (config.headers) {