From 17426a8be8407baa699d71322c80de1b7746b599 Mon Sep 17 00:00:00 2001 From: beetcb <63141491+beetcb@users.noreply.github.com> Date: Tue, 25 May 2021 08:43:10 +0800 Subject: [PATCH] fix(cli): error message --- .vscode/settings.json | 2 - cea/src/cli.ts | 129 ---------------------- core/package-lock.json | 2 +- core/src/conf.ts | 4 + {cea => internal}/package-lock.json | 1 + {cea => internal}/package.json | 4 +- internal/src/cli.ts | 159 ++++++++++++++++++++++++++++ {cea => internal}/src/constants.ts | 0 {cea => internal}/tsconfig.json | 0 lerna.json | 2 +- plugins/check-in/package-lock.json | 3 +- tsconfig.json | 3 +- 12 files changed, 172 insertions(+), 137 deletions(-) delete mode 100644 .vscode/settings.json delete mode 100644 cea/src/cli.ts rename {cea => internal}/package-lock.json (98%) rename {cea => internal}/package.json (90%) create mode 100644 internal/src/cli.ts rename {cea => internal}/src/constants.ts (100%) rename {cea => internal}/tsconfig.json (100%) diff --git a/.vscode/settings.json b/.vscode/settings.json deleted file mode 100644 index 7a73a41..0000000 --- a/.vscode/settings.json +++ /dev/null @@ -1,2 +0,0 @@ -{ -} \ No newline at end of file diff --git a/cea/src/cli.ts b/cea/src/cli.ts deleted file mode 100644 index b752640..0000000 --- a/cea/src/cli.ts +++ /dev/null @@ -1,129 +0,0 @@ -#!/usr/bin/env node -import { - sstore, - UsersConf, - UserConfOpts, - getSchoolInfos, - loadConfFromToml, -} from 'cea-core' -import { UserAction } from './constants' -import { prompt } from 'enquirer' -;(async () => { - const argv = process.argv[2] || '' - const argv2 = process.argv[3] - - switch (argv) { - case 'user': { - await promptToGetConf() - break - } - case 'rm': { - if (argv2 === 'all') { - sstore.clear() - } else { - sstore.del(argv2) - } - break - } - case 'sign': { - break - } - case 'load': { - loadConfFromToml() - break - } - - default: { - console.log(` - Usage: cea - All Commands: - user create|delete user - school config your school info - sign campusphere check in - load load config info from conf.toml - rm remove stored config feilds - `) - break - } - } - - sstore.close() -})() - -export async function promptToGetConf(): Promise { - const loadedUsers = sstore.get('users') as UsersConf - - const actionNaire = { - type: 'list', - name: 'actionType', - message: `用户编辑(已有用户:${loadedUsers.reduce((s, e) => { - const userInfo = e.alias - return s + ' ' + userInfo - }, '')})`, - choices: [ - { - name: UserAction.CREATE, - }, - { - name: UserAction.DELETE, - }, - { - name: UserAction.CANCEL, - }, - ], - } - const { actionType } = (await prompt([actionNaire])) as { - actionType: UserAction - } - - switch (actionType) { - case UserAction.CREATE: { - const form = { - type: 'form', - name: 'addUser', - message: 'Please provide the following information:', - choices: [ - { name: 'username', message: '请输入用户名', initial: '1913030099' }, - { name: 'password', message: '请输入密码', initial: '081312' }, - { - name: 'alias', - message: '请输入用户别名', - initial: 'foo', - }, - { - name: 'school', - message: - '学校的英文简称(推荐,仅部分学校支持使用简称)\n请参阅 https://github.com/beetcb/cea/blob/master/docs/abbrList.sh 自行判断\n或中文全称(备用选项,所有学校均支持):', - initial: 'whu', - }, - { - name: 'addr', - message: - '签到地址数组,留空使用学校地址签到,自定义签到地址:经度, 纬度, 中文地址', - initial: '', - }, - ], - } - const { addUser } = (await prompt([form])) as { addUser: UserConfOpts } - sstore.set('users', [...loadedUsers, addUser]) - break - } - case UserAction.DELETE: { - const deleteUserNaire = { - type: 'select', - name: 'deleteUser', - message: '请选择删除用户', - choices: loadedUsers.map((e) => e.alias), - } - const res = (await prompt([deleteUserNaire])) as { deleteUser: string } - sstore.set('users', [ - ...loadedUsers.filter((val) => val.alias !== res.deleteUser), - ]) - break - } - case UserAction.CANCEL: - return - } - const schools = await getSchoolInfos(sstore.get('users')) - sstore.set('schools', schools!) -} diff --git a/core/package-lock.json b/core/package-lock.json index e19121d..3960869 100644 --- a/core/package-lock.json +++ b/core/package-lock.json @@ -6,7 +6,7 @@ "packages": { "": { "name": "cea-core", - "version": "0.1.0", + "version": "0.1.1", "license": "MIT", "dependencies": { "@beetcb/sstore": "^0.1.0", diff --git a/core/src/conf.ts b/core/src/conf.ts index cad67a2..cb64d9e 100644 --- a/core/src/conf.ts +++ b/core/src/conf.ts @@ -12,6 +12,10 @@ export function loadConfFromToml(): UsersConf | null { const path = resolve('./conf.toml') if (fs.existsSync(path)) { const usersConf = parse(fs.readFileSync(path, 'utf8'))!.users as UsersConf + log.success({ + message: '成功加载用户', + suffix: `${usersConf.map((u) => `@${u.alias}`).join(' ')}`, + }) return usersConf } return null diff --git a/cea/package-lock.json b/internal/package-lock.json similarity index 98% rename from cea/package-lock.json rename to internal/package-lock.json index c149ce3..b3e86da 100644 --- a/cea/package-lock.json +++ b/internal/package-lock.json @@ -5,6 +5,7 @@ "requires": true, "packages": { "": { + "name": "cea", "version": "0.0.1", "license": "ISC", "dependencies": { diff --git a/cea/package.json b/internal/package.json similarity index 90% rename from cea/package.json rename to internal/package.json index b8f7733..45983c5 100644 --- a/cea/package.json +++ b/internal/package.json @@ -12,8 +12,8 @@ "author": "", "license": "ISC", "dependencies": { + "cea-check-in": "^0.0.1", "cea-core": "^0.1.1", "enquirer": "^2.3.6" - }, - "devDependencies": {} + } } diff --git a/internal/src/cli.ts b/internal/src/cli.ts new file mode 100644 index 0000000..4ae3b04 --- /dev/null +++ b/internal/src/cli.ts @@ -0,0 +1,159 @@ +#!/usr/bin/env node +import { + log, + sstore, + UsersConf, + UserConfOpts, + getSchoolInfos, + loadConfFromToml, +} from 'cea-core' +import { checkIn } from 'cea-check-in' +import { prompt } from 'enquirer' +import { UserAction } from './constants' +;(async () => { + const argv = process.argv[2] || '' + const argv2 = process.argv[3] + + switch (argv) { + case 'user': { + const users = await promptToGetConf() + await confSet(users) + break + } + case 'rm': { + if (argv2 === 'all') { + sstore.clear() + } else { + sstore.del(argv2) + } + break + } + case 'sign': { + await checkIn() + break + } + case 'load': { + const users = loadConfFromToml() + await confSet(users) + break + } + + default: { + console.log(` + Usage: cea + All Commands: + user create|delete user + school config your school info + sign campusphere check in + load load config info from conf.toml + rm remove stored config feilds + `) + break + } + } + + sstore.close() +})() + +async function confSet(users: UsersConf | null) { + if (users) { + const schoolInfos = await getSchoolInfos(users) + if (schoolInfos) { + sstore.set('schools', schoolInfos) + } + sstore.set('users', users) + } +} + +async function promptToGetConf(): Promise { + const loadedUsers = (sstore.get('users') as UsersConf) || [] + + const actionNaire = { + type: 'select', + name: 'actionType', + message: `用户编辑(已有用户:${loadedUsers.reduce((s, e) => { + const userInfo = e.alias + return s + ' ' + userInfo + }, '')})`, + choices: [ + { + name: UserAction.CREATE, + value: UserAction.CREATE, + }, + { + name: UserAction.DELETE, + value: UserAction.DELETE, + }, + { + name: UserAction.CANCEL, + value: UserAction.CANCEL, + }, + ], + } + const { actionType } = (await prompt([actionNaire]).catch((_) => _)) as { + actionType: UserAction + } + + switch (actionType) { + case UserAction.CREATE: { + const form = { + type: 'form', + name: 'addUser', + message: '请填写如下信息:', + choices: [ + { name: 'username', message: '用户名', initial: '1913030099' }, + { name: 'password', message: '密码', initial: '081312' }, + { + name: 'alias', + message: '用户别名', + initial: 'foo', + }, + { + name: 'school', + message: '学校简称', + initial: 'whu', + }, + ], + } + const { addUser } = (await prompt([form]).catch((_) => _)) as { + addUser: UserConfOpts + } + + const list = { + type: 'list', + name: 'addr', + message: '签到地址', + initial: [''], + } + const { addr } = (await prompt([list]).catch((_) => _)) as { + addr: Array + } + addUser.addr = addr + if (!loadedUsers.some((e) => e.alias === addUser.alias)) { + log.success({ message: '成功加载用户', suffix: `@${addUser.alias}` }) + return Array.from(new Set([...loadedUsers, addUser])) + } else { + log.error({ message: '用户已存在' }) + return null + } + } + case UserAction.DELETE: { + if (loadedUsers.length === 0) { + log.error({ message: '无用户可删除' }) + return null + } + const deleteUserNaire = { + type: 'select', + name: 'deleteUser', + message: '请选择删除用户', + choices: loadedUsers.map((e) => e.alias), + } + const res = (await prompt([deleteUserNaire]).catch((_) => _)) as { + deleteUser: string + } + return [...loadedUsers.filter((val) => val.alias !== res.deleteUser)] + } + default: + return null + } +} diff --git a/cea/src/constants.ts b/internal/src/constants.ts similarity index 100% rename from cea/src/constants.ts rename to internal/src/constants.ts diff --git a/cea/tsconfig.json b/internal/tsconfig.json similarity index 100% rename from cea/tsconfig.json rename to internal/tsconfig.json diff --git a/lerna.json b/lerna.json index ef3869a..3b0b361 100644 --- a/lerna.json +++ b/lerna.json @@ -1,4 +1,4 @@ { - "packages": ["core", "cea", "plugins/*"], + "packages": ["core", "internal", "plugins/*"], "version": "independent" } diff --git a/plugins/check-in/package-lock.json b/plugins/check-in/package-lock.json index a4bbde5..6251d10 100644 --- a/plugins/check-in/package-lock.json +++ b/plugins/check-in/package-lock.json @@ -6,7 +6,7 @@ "packages": { "": { "name": "cea-check-in", - "version": "0.0.0", + "version": "0.0.1", "license": "ISC", "dependencies": { "node-fetch": "^2.6.1", @@ -26,6 +26,7 @@ "version": "2.6.1", "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.1.tgz", "integrity": "sha512-V4aYg89jEoVRxRb2fJdAg8FHvI7cEyYdVAh94HH0UIK8oJxUfkjlDQN9RbMx+bEjP7+ggMiFRprSti032Oipxw==", + "license": "MIT", "engines": { "node": "4.x || >=6.0.0" } diff --git a/tsconfig.json b/tsconfig.json index df7e402..bfea584 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -6,6 +6,7 @@ }, { "path": "./core/" - } + }, + { "path": "./internal" } ] }