Skip to content
This repository has been archived by the owner on Jun 3, 2021. It is now read-only.

Commit

Permalink
fix: await sstore, then close
Browse files Browse the repository at this point in the history
  • Loading branch information
beetcb committed Feb 22, 2021
1 parent fc185f0 commit faeeb0a
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 29 deletions.
1 change: 0 additions & 1 deletion campusphere/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ exports.signApp = class signApp extends campusphereApp {
}

async signWithForm() {
console.log(this)
if (!this.curTask) {
this.result = { 失败原因: 'Cookie无效,取消签到' }
return
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@beetcb/cea",
"version": "2.0.6",
"version": "2.0.7",
"description": "campusphere-elegant-auth: login to swms/campusphere at ease",
"main": "dist/index.js",
"bin": "dist/cli.js",
Expand All @@ -27,7 +27,7 @@
},
"license": "ISC",
"dependencies": {
"@beetcb/sstore": "^0.0.5",
"@beetcb/sstore": "^0.0.7",
"@iarna/toml": "^2.2.5",
"chalk": "^4.1.0",
"cheerio": "^1.0.0-rc.5",
Expand Down
2 changes: 1 addition & 1 deletion serverless/cea/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@
"author": "",
"license": "ISC",
"dependencies": {
"@beetcb/cea": "^2.0.5"
"@beetcb/cea": "^2.0.6"
}
}
38 changes: 17 additions & 21 deletions src/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ function loadConfFromToml(path) {
conf.init = async function () {
const env = process.env
const toml = loadConfFromToml('./conf.toml')
const userUlti = new User(conf)
const schoolUlti = new School(conf)
const userUlti = new User()
const schoolUlti = new School()

if (env.users && env.school) {
log.warning('尝试从环境变量加载配置')
Expand All @@ -32,9 +32,8 @@ conf.init = async function () {
}

class User {
constructor(conf) {
constructor() {
this.initConf()
this.conf = conf
this.selectType = null
}

Expand All @@ -43,14 +42,14 @@ class User {
}

storeUsers(loadedUsers) {
const storedUsers = this.conf.get('users') || []
const storedUsers = conf.get('users')
const alias = storedUsers.map(e => e.alias)
if (loadedUsers) {
loadedUsers = loadedUsers.filter(e => !alias.includes(e.alias))
} else {
loadedUsers = []
}
this.conf.set('users', [...loadedUsers, ...storedUsers])
conf.set('users', [...loadedUsers, ...storedUsers])
}

loadUserFromToml(toml) {
Expand All @@ -75,8 +74,8 @@ class User {
type: 'list',
name: 'type',
message: `用户编辑: ${
this.conf.get('school') ? ' 学校信息已成功配置' : ' 学校信息未配置'
}\n 已有用户:${this.conf.get('users').reduce((s, e) => {
conf.get('school') ? ' 学校信息已成功配置' : ' 学校信息未配置'
}\n 已有用户:${conf.get('users').reduce((s, e) => {
const userInfo = e.alias
return s + ' ' + userInfo
}, '')}`,
Expand Down Expand Up @@ -127,14 +126,14 @@ class User {

const res = await prompt(questions)

if (!this.conf.get('users').some(e => e.alias === res.alias)) {
if (!conf.get('users').some(e => e.alias === res.alias)) {
const addUser = {
username: res.username,
password: res.password,
alias: res.alias || null,
cookie: res.cookie,
}
this.conf.set('users', [addUser, ...this.conf.get('users')])
conf.set('users', [addUser, ...conf.get('users')])
log.success('🎉 成功添加用户', addUser)
} else {
log.error('🙃 用户已存在')
Expand All @@ -148,7 +147,7 @@ class User {
name: 'selection',
message: '请选择删除对象:',
choices: [
...this.conf.get('users').map((e, idx) => ({
...get('users').map((e, idx) => ({
value: idx,
name: `${e.alias || e.user.name}`,
})),
Expand All @@ -164,18 +163,15 @@ class User {
const neoUsers = conf
.get('users')
.filter((el, index) => index !== res.selection)
this.conf.set('users', neoUsers)
conf.set('users', neoUsers)

log.success('🎉 成功删除用户')
}
}

class School {
constructor(conf) {
this.conf = conf
}
async init() {
if (!this.conf.get('school')) {
if (!conf.get('school')) {
const questions = [
{
type: 'input',
Expand Down Expand Up @@ -204,19 +200,19 @@ class School {
const school = await this.schoolApi(res.ids, isSignAtHome)

if (!isSignAtHome) school.addr = await this.schoolAddr(school.name)
this.conf.set('school', school)
conf.set('school', school)
log.success(`您的学校 ${school.name} 已完成设定`)
} else {
log.warning('学校信息已配置')
}
}

async loadSchoolFromToml(toml) {
if (!this.conf.get('school')) {
if (!conf.get('school')) {
const isSignAtHome = toml.users[0].addr
const school = await this.schoolApi(toml.school, isSignAtHome)
if (!isSignAtHome) school.addr = await this.schoolAddr(school.name)
this.conf.set('school', school)
conf.set('school', school)
log.success(`您的学校 ${school.name} 已完成设定`)
}
}
Expand All @@ -226,11 +222,11 @@ class School {
* @param {string} name school
*/
async loadSchoolFromEnv({ school: name, users }) {
if (!this.conf.get('school')) {
if (!conf.get('school')) {
const isSignAtHome = users.includes('home')
const school = await this.schoolApi(name, isSignAtHome)
if (!isSignAtHome) school.addr = await this.schoolAddr(school.name)
this.conf.set('school', school)
conf.set('school', school)
log.success(`您的学校 ${school.name} 已完成设定`)
} else {
log.warning('学校信息已配置')
Expand Down
9 changes: 6 additions & 3 deletions src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ const { User, School, conf } = require('./api')
const userUlti = new User(conf)
await userUlti.load()
const type = userUlti.selectType
if (type === 1) userUlti.createUser()
if (type === 2) userUlti.deleteUser()
if (type === 1) await userUlti.createUser()
if (type === 2) await userUlti.deleteUser()
break
}
case '-s':
case '--school': {
new School(conf).init()
await new School(conf).init()
break
}
case 'rm':
Expand All @@ -31,9 +31,12 @@ const { User, School, conf } = require('./api')
break
}
case 'load': {
// If load and -u mixed in, use load first
conf.clear()
await conf.init()
}
}

conf.close()
})()

Expand Down
1 change: 0 additions & 1 deletion src/cookie.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ async function handleLogin(i, storeCookiePath) {
const isNeedLogIn = await sign.signInfo(cookie)
if (isNeedLogIn) {
cookie = await login(conf.get('school'), i)
console.log(cookie)
if (cookie) {
conf.set(storeCookiePath, cookie)
await sign.signInfo(cookie)
Expand Down

0 comments on commit faeeb0a

Please sign in to comment.