@lazycatcloud/lzc-cli 1.2.62 → 1.2.63
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/changelog.md +7 -0
- package/lib/appstore/login.js +124 -123
- package/package.json +1 -1
package/changelog.md
CHANGED
package/lib/appstore/login.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import fetch from "node-fetch"
|
|
2
2
|
import path from "path"
|
|
3
|
+
import url from "url"
|
|
3
4
|
import logger from "loglevel"
|
|
4
5
|
import env from "../config/env.js"
|
|
5
6
|
import inquirer from "inquirer"
|
|
@@ -7,156 +8,156 @@ import inquirer from "inquirer"
|
|
|
7
8
|
const accountServerUrl = "https://account.lazycat.cloud"
|
|
8
9
|
|
|
9
10
|
function join(...params) {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
11
|
+
let x = ""
|
|
12
|
+
try {
|
|
13
|
+
// fix: url path join failed for windows
|
|
14
|
+
const u = new url.URL(params[0])
|
|
15
|
+
params.splice(0, 1)
|
|
16
|
+
u.pathname = path.posix.join(u.pathname, ...params)
|
|
17
|
+
x = u.toString()
|
|
18
|
+
} catch (error) {
|
|
19
|
+
console.warn(error)
|
|
20
|
+
x = path.join(...params)
|
|
21
|
+
}
|
|
22
|
+
return x
|
|
22
23
|
}
|
|
23
24
|
|
|
24
25
|
async function login(username, password) {
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
26
|
+
return fetch(join(accountServerUrl, "/api/login/signin"), {
|
|
27
|
+
method: "POST",
|
|
28
|
+
body: new URLSearchParams({
|
|
29
|
+
username: username,
|
|
30
|
+
password: password
|
|
31
|
+
})
|
|
32
|
+
})
|
|
33
|
+
.then(async (res) => {
|
|
34
|
+
let bodyText = await res.text()
|
|
35
|
+
let body = JSON.parse(bodyText)
|
|
36
|
+
if (body.success) {
|
|
37
|
+
return Promise.resolve(body.data)
|
|
38
|
+
}
|
|
39
|
+
return Promise.reject(body.message)
|
|
40
|
+
})
|
|
41
|
+
.then((data) => {
|
|
42
|
+
env.set({ token: data.token }, true)
|
|
43
|
+
logger.info("登录成功!")
|
|
44
|
+
})
|
|
44
45
|
}
|
|
45
46
|
|
|
46
47
|
async function isLogin() {
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
48
|
+
try {
|
|
49
|
+
let token = env.get("token")
|
|
50
|
+
if (!token) {
|
|
51
|
+
return false
|
|
52
|
+
}
|
|
53
|
+
return fetch(join(accountServerUrl, "/api/user/current"), {
|
|
54
|
+
method: "GET",
|
|
55
|
+
headers: {
|
|
56
|
+
"X-User-Token": token
|
|
57
|
+
}
|
|
58
|
+
}).then(async (res) => {
|
|
59
|
+
let bodyText = await res.text()
|
|
60
|
+
let body = JSON.parse(bodyText)
|
|
61
|
+
if (body.success) {
|
|
62
|
+
return token
|
|
63
|
+
}
|
|
64
|
+
logger.debug(body.message)
|
|
65
|
+
return false
|
|
66
|
+
})
|
|
67
|
+
} catch (e) {
|
|
68
|
+
logger.trace(e)
|
|
69
|
+
return false
|
|
70
|
+
}
|
|
70
71
|
}
|
|
71
72
|
|
|
72
73
|
async function askUserInfo() {
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
74
|
+
const noEmpty = (value) => value != ""
|
|
75
|
+
return await inquirer.prompt([
|
|
76
|
+
{
|
|
77
|
+
type: "input",
|
|
78
|
+
name: "username",
|
|
79
|
+
message: "请输入登录用户名",
|
|
80
|
+
validate: noEmpty
|
|
81
|
+
},
|
|
82
|
+
{
|
|
83
|
+
type: "password",
|
|
84
|
+
mask: "*",
|
|
85
|
+
name: "password",
|
|
86
|
+
message: "请输入登录密码",
|
|
87
|
+
validate: noEmpty
|
|
88
|
+
}
|
|
89
|
+
])
|
|
89
90
|
}
|
|
90
91
|
|
|
91
92
|
async function interactiveLogin() {
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
93
|
+
try {
|
|
94
|
+
let info = await askUserInfo()
|
|
95
|
+
await login(info.username, info.password)
|
|
96
|
+
} catch (e) {
|
|
97
|
+
logger.debug("login error: ", e)
|
|
98
|
+
logger.error("帐号或者密码错误,请重新输入!")
|
|
99
|
+
return interactiveLogin()
|
|
100
|
+
}
|
|
100
101
|
}
|
|
101
102
|
|
|
102
103
|
function tipsFirstLogin() {
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
104
|
+
let token = env.get("token")
|
|
105
|
+
if (!token) {
|
|
106
|
+
// 还没登录过的用户提示用户前往 https://developer.lazycat.cloud/manage 注册开发者账号
|
|
107
|
+
logger.info("请登录懒猫微服社区账号,账号注册以及开发者权限申请地址: https://developer.lazycat.cloud/manage")
|
|
108
|
+
}
|
|
108
109
|
}
|
|
109
110
|
|
|
110
111
|
export async function reLogin() {
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
112
|
+
let token = await isLogin()
|
|
113
|
+
if (token) {
|
|
114
|
+
const questions = [
|
|
115
|
+
{
|
|
116
|
+
name: "relogin",
|
|
117
|
+
type: "input",
|
|
118
|
+
default: "n",
|
|
119
|
+
message: "检测到已经登录,是否重新登录(y/n):"
|
|
120
|
+
}
|
|
121
|
+
]
|
|
122
|
+
const answers = await inquirer.prompt(questions)
|
|
123
|
+
if (answers.relogin.toLowerCase() === "n") {
|
|
124
|
+
logger.info(`token: ${token}`)
|
|
125
|
+
return
|
|
126
|
+
}
|
|
127
|
+
} else {
|
|
128
|
+
tipsFirstLogin()
|
|
129
|
+
}
|
|
130
|
+
await interactiveLogin()
|
|
130
131
|
}
|
|
131
132
|
|
|
132
133
|
export async function autoLogin() {
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
134
|
+
let token = await isLogin()
|
|
135
|
+
if (token) {
|
|
136
|
+
logger.debug("appstore 已经登录")
|
|
137
|
+
return
|
|
138
|
+
} else {
|
|
139
|
+
tipsFirstLogin()
|
|
140
|
+
}
|
|
140
141
|
|
|
141
|
-
|
|
142
|
-
|
|
142
|
+
logger.debug("token错误,尝试自动登录")
|
|
143
|
+
await interactiveLogin()
|
|
143
144
|
}
|
|
144
145
|
|
|
145
146
|
export async function request(url, options = {}) {
|
|
146
|
-
|
|
147
|
+
await autoLogin()
|
|
147
148
|
|
|
148
|
-
|
|
149
|
+
const token = env.get("token")
|
|
149
150
|
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
151
|
+
let headers = {
|
|
152
|
+
"X-User-Token": token,
|
|
153
|
+
cookie: `userToken=${token}`
|
|
154
|
+
}
|
|
155
|
+
if (options.headers) {
|
|
156
|
+
headers = Object.assign({}, options.headers, headers)
|
|
157
|
+
}
|
|
157
158
|
|
|
158
|
-
|
|
159
|
-
|
|
159
|
+
options = Object.assign({}, options, { headers })
|
|
160
|
+
logger.trace(`fetch ${url}`, options)
|
|
160
161
|
|
|
161
|
-
|
|
162
|
+
return fetch(url, options)
|
|
162
163
|
}
|