Refactor code structure for improved readability and maintainability

This commit is contained in:
xiongxiao
2026-03-15 04:22:35 +08:00
committed by cnb
parent 228635a3cc
commit 342b68abe3
4 changed files with 5075 additions and 79 deletions

View File

@@ -52,7 +52,7 @@
"@kevisual/router": "^0.1.1", "@kevisual/router": "^0.1.1",
"@kevisual/types": "^0.0.12", "@kevisual/types": "^0.0.12",
"@kevisual/use-config": "^1.0.30", "@kevisual/use-config": "^1.0.30",
"@opencode-ai/plugin": "^1.2.24", "@opencode-ai/plugin": "^1.2.26",
"@types/bun": "^1.3.10", "@types/bun": "^1.3.10",
"@types/node": "^25.5.0", "@types/node": "^25.5.0",
"@types/send": "^1.2.1", "@types/send": "^1.2.1",
@@ -77,15 +77,15 @@
"access": "public" "access": "public"
}, },
"dependencies": { "dependencies": {
"@aws-sdk/client-s3": "^3.1008.0", "@aws-sdk/client-s3": "^3.1009.0",
"@kevisual/js-filter": "^0.0.6", "@kevisual/js-filter": "^0.0.6",
"@kevisual/oss": "^0.0.20", "@kevisual/oss": "^0.0.20",
"@kevisual/video-tools": "^0.0.13", "@kevisual/video-tools": "^0.0.13",
"@opencode-ai/sdk": "^1.2.24", "@opencode-ai/sdk": "^1.2.26",
"es-toolkit": "^1.45.1", "es-toolkit": "^1.45.1",
"eventemitter3": "^5.0.4", "eventemitter3": "^5.0.4",
"lowdb": "^7.0.1", "lowdb": "^7.0.1",
"lru-cache": "^11.2.6", "lru-cache": "^11.2.7",
"pm2": "^6.0.14", "pm2": "^6.0.14",
"unstorage": "^1.17.4", "unstorage": "^1.17.4",
"zod": "^4.3.6" "zod": "^4.3.6"

View File

@@ -1,6 +1,6 @@
{ {
"name": "@kevisual/cli", "name": "@kevisual/cli",
"version": "0.1.24", "version": "0.1.25",
"description": "envision 命令行工具", "description": "envision 命令行工具",
"type": "module", "type": "module",
"basename": "/root/cli", "basename": "/root/cli",
@@ -47,13 +47,13 @@
"@kevisual/context": "^0.0.8", "@kevisual/context": "^0.0.8",
"@kevisual/router": "^0.1.1", "@kevisual/router": "^0.1.1",
"@kevisual/use-config": "^1.0.30", "@kevisual/use-config": "^1.0.30",
"@opencode-ai/sdk": "^1.2.24", "@opencode-ai/sdk": "^1.2.26",
"@types/busboy": "^1.5.4", "@types/busboy": "^1.5.4",
"busboy": "^1.6.0", "busboy": "^1.6.0",
"eventemitter3": "^5.0.4", "eventemitter3": "^5.0.4",
"jose": "^6.2.1", "jose": "^6.2.1",
"lowdb": "^7.0.1", "lowdb": "^7.0.1",
"lru-cache": "^11.2.6", "lru-cache": "^11.2.7",
"micromatch": "^4.0.8", "micromatch": "^4.0.8",
"nanoid": "^5.1.6", "nanoid": "^5.1.6",
"pm2": "latest", "pm2": "latest",
@@ -62,7 +62,7 @@
}, },
"devDependencies": { "devDependencies": {
"@kevisual/api": "^0.0.64", "@kevisual/api": "^0.0.64",
"@kevisual/cnb": "^0.0.45", "@kevisual/cnb": "^0.0.46",
"@kevisual/dts": "^0.0.4", "@kevisual/dts": "^0.0.4",
"@kevisual/load": "^0.0.6", "@kevisual/load": "^0.0.6",
"@kevisual/logger": "^0.0.4", "@kevisual/logger": "^0.0.4",

4982
pnpm-lock.yaml generated Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -14,11 +14,79 @@ const parseIfJson = (str: string) => {
return {}; return {};
} }
}; };
const publishRegistry = (options: { execPath: string, registry: string, tag?: string, config: any, env: any }) => {
const packageJson = path.resolve(options.execPath, 'package.json');
let cmd = '';
const config = options.config || {};
const execPath = options.execPath;
const registry = options.registry;
const setEnv = options.env || {};
switch (registry) {
case 'npm':
cmd = 'npm publish -s --registry https://registry.npmjs.org';
break;
case 'cnb':
cmd = 'npm publish -s --registry https://npm.cnb.cool/kevisual/registry/-/packages/';
break;
default:
cmd = 'npm publish -s --registry https://registry.npmjs.org';
break;
}
if (fileIsExist(packageJson)) {
const keys = Object.keys(config).filter((key) => key.includes('NPM_TOKEN'));
const tokenEnv = keys.reduce((prev, key) => {
return {
...prev,
[key]: config[key],
};
}, {});
const pkg = fs.readFileSync(packageJson, 'utf-8');
const pkgJson = parseIfJson(pkg);
const version = pkgJson?.version as string;
if (version && options?.tag) {
let tag = String(version).split('-')[1] || '';
if (tag) {
if (tag.includes('.')) {
tag = tag.split('.')[0];
}
cmd = `${cmd} --tag ${tag}`;
}
}
console.log(chalk.green(cmd));
const child = spawn(cmd, {
shell: true,
cwd: execPath,
env: {
...process.env, // 保留当前环境变量
...tokenEnv,
...setEnv,
},
});
child.stdout.on('data', (data) => {
console.log(chalk.green(`${data}`));
});
child.stderr.on('data', (data) => {
// 过滤掉 'npm notice' 或者其他信息
if (data.toString().includes('npm notice')) {
console.log(chalk.yellow(`notice: ${data}`));
} else {
console.error(`stderr: ${data}`);
}
});
child.on('close', (code) => {
// console.log(`child process exited with code ${code}`);
});
} else {
console.error(chalk.red('package.json not found'));
}
}
const command = new Command('npm').description('npm command show publish and set .npmrc').action(async (options) => { }); const command = new Command('npm').description('npm command show publish and set .npmrc').action(async (options) => { });
const publish = new Command('publish') const publish = new Command('publish')
.argument('[registry]') .argument('[registry]')
.option('-p --proxy', 'proxy') .option('-p --proxy', 'proxy')
.option('-t, --tag', 'tag') .option('-t, --tag', 'tag')
.option('-u, --update', 'update new version')
.description('publish npm') .description('publish npm')
.action(async (registry, options) => { .action(async (registry, options) => {
if (!registry) { if (!registry) {
@@ -26,8 +94,8 @@ const publish = new Command('publish')
message: 'Select the registry to publish', message: 'Select the registry to publish',
choices: [ choices: [
{ {
name: 'me', name: 'all',
value: 'me', value: 'all',
}, },
{ {
name: 'npm', name: 'npm',
@@ -41,7 +109,6 @@ const publish = new Command('publish')
}); });
} }
const config = getConfig(); const config = getConfig();
let cmd = '';
const execPath = process.cwd(); const execPath = process.cwd();
let setEnv = {}; let setEnv = {};
const proxyEnv = { const proxyEnv = {
@@ -55,70 +122,15 @@ const publish = new Command('publish')
...proxyEnv, ...proxyEnv,
}; };
} }
if (registry) { if (options?.update) {
const packageJson = path.resolve(execPath, 'package.json'); patchFunc({ directory: execPath });
switch (registry) { }
case 'me':
cmd = 'npm publish -s --registry https://npm.xiongxiao.me';
break;
case 'npm':
cmd = 'npm publish -s --registry https://registry.npmjs.org';
break;
case 'cnb':
cmd = 'npm publish -s --registry https://npm.cnb.cool/kevisual/registry/-/packages/';
break;
default:
cmd = 'npm publish -s --registry https://npm.xiongxiao.me';
break;
}
if (fileIsExist(packageJson)) {
const keys = Object.keys(config).filter((key) => key.includes('NPM_TOKEN'));
const tokenEnv = keys.reduce((prev, key) => {
return {
...prev,
[key]: config[key],
};
}, {});
const pkg = fs.readFileSync(packageJson, 'utf-8');
const pkgJson = parseIfJson(pkg);
const version = pkgJson?.version as string;
if (version && options?.tag) {
let tag = String(version).split('-')[1] || '';
if (tag) {
if (tag.includes('.')) {
tag = tag.split('.')[0];
}
cmd = `${cmd} --tag ${tag}`;
}
}
console.log(chalk.green(cmd));
const child = spawn(cmd, { if (registry === 'all') {
shell: true, publishRegistry({ execPath, registry: 'npm', config, env: setEnv });
cwd: execPath, publishRegistry({ execPath, registry: 'cnb', config, env: setEnv });
env: { } else {
...process.env, // 保留当前环境变量 publishRegistry({ execPath, registry, tag: options?.tag, config, env: setEnv });
...tokenEnv,
...setEnv,
},
});
child.stdout.on('data', (data) => {
console.log(chalk.green(`${data}`));
});
child.stderr.on('data', (data) => {
// 过滤掉 'npm notice' 或者其他信息
if (data.toString().includes('npm notice')) {
console.log(chalk.yellow(`notice: ${data}`));
} else {
console.error(`stderr: ${data}`);
}
});
child.on('close', (code) => {
// console.log(`child process exited with code ${code}`);
});
} else {
console.error(chalk.red('package.json not found'));
}
} }
}); });
@@ -142,8 +154,7 @@ const npmrc = new Command('set')
const config = getConfig(); const config = getConfig();
const npmrcContent = const npmrcContent =
config?.npmrc || config?.npmrc ||
`//npm.xiongxiao.me/:_authToken=\${ME_NPM_TOKEN} `/npm.cnb.cool/kevisual/registry/-/packages/:_authToken=\${CNB_API_KEY}
//npm.cnb.cool/kevisual/registry/-/packages/:_authToken=\${CNB_API_KEY}
//registry.npmjs.org/:_authToken=\${NPM_TOKEN} //registry.npmjs.org/:_authToken=\${NPM_TOKEN}
`; `;
const execPath = process.cwd(); const execPath = process.cwd();
@@ -210,9 +221,8 @@ const install = new Command('install')
}); });
command.addCommand(install); command.addCommand(install);
// npm patch const patchFunc = (opts?: { directory?: string }) => {
const patch = new Command('patch').description('npm patch 发布补丁版本').action(async () => { const cwd = opts?.directory || process.cwd();
const cwd = process.cwd();
const packageJson = path.resolve(cwd, 'package.json'); const packageJson = path.resolve(cwd, 'package.json');
if (fileIsExist(packageJson)) { if (fileIsExist(packageJson)) {
const pkg = fs.readFileSync(packageJson, 'utf-8'); const pkg = fs.readFileSync(packageJson, 'utf-8');
@@ -229,6 +239,10 @@ const patch = new Command('patch').description('npm patch 发布补丁版本').a
} }
} }
} }
}
// npm patch
const patch = new Command('patch').description('npm patch 发布补丁版本').action(async () => {
patchFunc();
}); });
command.addCommand(patch); command.addCommand(patch);