feat: add clone file

This commit is contained in:
2025-10-12 19:55:33 +08:00
parent 02d5cc9fb0
commit 4dbee366c5
5 changed files with 90 additions and 4 deletions

View File

@@ -1,6 +1,6 @@
{ {
"name": "@kevisual/cli", "name": "@kevisual/cli",
"version": "0.0.58", "version": "0.0.59",
"description": "envision command tools", "description": "envision command tools",
"main": "dist/app.mjs", "main": "dist/app.mjs",
"type": "module", "type": "module",

View File

@@ -1,4 +1,2 @@
packages: packages:
- 'submodules/*' - 'assistant'
- 'assistant'
- '!submodules/assistant-center'

74
src/command/gist/index.ts Normal file
View File

@@ -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 <dir>', '配置目录')
.arguments('<link>')
.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 <dir>', '配置目录')
.option('-c --config <config>', '配置文件的名字', 'kevisual.json')
.option('-s --sync', '下载配置成功后,是否需要同步文件')
.option('-l --link <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);

View File

@@ -14,6 +14,7 @@ import './command/sync/sync.ts';
import './command/app/index.ts'; import './command/app/index.ts';
import './command/gist/index.ts';
// program.parse(process.argv); // program.parse(process.argv);
export const runParser = async (argv: string[]) => { export const runParser = async (argv: string[]) => {

View File

@@ -22,6 +22,19 @@ export const getBaseURL = () => {
export const query = new Query({ export const query = new Query({
url: `${getBaseURL()}/api/router`, url: `${getBaseURL()}/api/router`,
}); });
export const getHeader = async () => {
const headers: Record<string, string> = {
'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) => { query.beforeRequest = async (config) => {
if (config.headers) { if (config.headers) {