From edb0d560949e886df8423cab20a9f0284f5b74e0 Mon Sep 17 00:00:00 2001 From: xion Date: Thu, 10 Oct 2024 02:10:16 +0800 Subject: [PATCH] fix bugs and change version --- package.json | 2 +- src/app.ts | 2 +- src/command/deploy.ts | 27 +++++++++++++---------- src/command/login.ts | 49 ++++++++++++++++++++++++++++++++++++++++- src/route/user/login.ts | 19 ++++++++++++++++ 5 files changed, 84 insertions(+), 15 deletions(-) diff --git a/package.json b/package.json index 10a4ea4..e3788e6 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@kevisual/envision-cli", - "version": "0.0.1", + "version": "0.0.3", "description": "envision command tools", "main": "dist/index.js", "type": "module", diff --git a/src/app.ts b/src/app.ts index 3910c42..e03faa4 100644 --- a/src/app.ts +++ b/src/app.ts @@ -2,7 +2,7 @@ import { program , Command} from 'commander'; import fs from 'fs'; // 将多个子命令加入主程序中 -program.name('app').description('A CLI tool with envison').version('0.0.1'); +program.name('app').description('A CLI tool with envison').version('0.0.3'); const ls =new Command('ls') .description('List files in the current directory') diff --git a/src/command/deploy.ts b/src/command/deploy.ts index ba575b4..28b37d9 100644 --- a/src/command/deploy.ts +++ b/src/command/deploy.ts @@ -12,9 +12,10 @@ const command = new Command('deploy') .argument('', 'Path to the file to be uploaded') // 定义文件路径参数 .option('-v, --version ', 'verbose') .option('-k, --key ', 'key') + .option('-y, --yes ', 'yes') .action(async (filePath, options) => { try { - let { version, key } = options; + let { version, key, yes } = options; if (!version || !key) { const answers = await inquirer.prompt([ { @@ -36,20 +37,22 @@ const command = new Command('deploy') const pwd = process.cwd(); const directory = path.join(pwd, filePath); const gPath = path.join(directory, '**/*'); - const files = await glob(gPath, { cwd: pwd, ignore: ['node_modules/**/*'] }); + const files = await glob(gPath, { cwd: pwd, ignore: ['node_modules/**/*'], nodir: true }); const _relativeFiles = files.map((file) => file.replace(directory + '/', '')); console.log('upload Files', _relativeFiles); console.log('upload Files Key', key, version); - // 确认是否上传 - const confirm = await inquirer.prompt([ - { - type: 'confirm', - name: 'confirm', - message: 'Do you want to upload these files?', - }, - ]); - if (!confirm.confirm) { - return; + if (!yes) { + // 确认是否上传 + const confirm = await inquirer.prompt([ + { + type: 'confirm', + name: 'confirm', + message: 'Do you want to upload these files?', + }, + ]); + if (!confirm.confirm) { + return; + } } const res = await uploadFiles(_relativeFiles, directory, { key, version }); diff --git a/src/command/login.ts b/src/command/login.ts index 9bdf810..77b2152 100644 --- a/src/command/login.ts +++ b/src/command/login.ts @@ -1,6 +1,6 @@ import { app, program, Command } from '@/app.ts'; import { getConfig, writeConfig } from '@/module/get-config.ts'; -import { queryLogin, queryMe } from '@/route/index.ts'; +import { queryLogin, queryMe, switchOrg, switchMe } from '@/route/index.ts'; import inquirer from 'inquirer'; // 导入 login 命令 @@ -57,3 +57,50 @@ const loginCommand = new Command('login') }); app.addCommand(loginCommand); + +const showMe = async () => { + const me = await queryMe(); + if (me.code === 200) { + console.log('Me', me.data); + } else { + console.log('Me failed', me.message); + } +}; + +const switchOrgCommand = new Command('switchOrg').argument('', 'Switch to another organization').action(async (options) => { + // console.log('options', options); + const config = getConfig(); + if (!config.token) { + console.log('Please login first'); + return; + } + const res = await switchOrg(options); + if (res.code === 200) { + const token = res.data.token; + writeConfig({ ...config, token }); + console.log('Switch Org Success'); + showMe(); + } else { + console.log('Switch Org Failed', res.message || ''); + } +}); + +app.addCommand(switchOrgCommand); + +const switchMeCommand = new Command('switchMe').action(async () => { + const config = getConfig(); + if (!config.token) { + console.log('Please login first'); + return; + } + const res = await switchMe(); + if (res.code === 200) { + const token = res.data.token; + writeConfig({ ...config, token }); + console.log('Switch Me Success'); + showMe(); + } else { + console.log('Switch Me Failed', res.message || ''); + } +}); +app.addCommand(switchMeCommand); diff --git a/src/route/user/login.ts b/src/route/user/login.ts index 985bbe6..8943c93 100644 --- a/src/route/user/login.ts +++ b/src/route/user/login.ts @@ -17,3 +17,22 @@ export const queryMe = async () => { key: 'me', }); }; + +export const switchOrg = async (username) => { + return await query.post({ + path: 'user', + key: 'switchOrg', + data: { + username, + }, + }); +}; +export const switchMe = async () => { + return await query.post({ + path: 'user', + key: 'switchOrg', + data: { + type: 'user', + }, + }); +};