-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
77 lines (65 loc) · 1.94 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
'use strict'
const url = require('url')
const axios = require('axios')
const chalk = require('chalk')
const Migrate = require('./lib/migrate')
const config = require('./config')
const ONLINE_DOMAIN = 'https://easy-mock.com'
function main () {
let title = chalk.green(`
==============
你正在使用 migrate2local 迁移你的线上(easy-mock.com)项目到你的本地环境。
请先配置当前目录下的 config.js 以完成准备工作。
注意:该操作只是调用相应环境的接口,并非真正意义上的数据迁移(不过也能达到目的)。
==============`)
console.log(title)
console.log()
login()
}
function login () {
let onlineRequest
let localRequest
createRequestByToken()
.post('/u/login', {
name: config.onlineUserName,
password: config.onlineUserPassword
})
.then(res => {
const body = res.data.data
if (body && body.token) {
onlineRequest = createRequestByToken(body.token)
return createRequestByToken(null, config.domain).post('/u/login', {
name: config.localUserName,
password: config.localUserPassword
})
}
})
.then(res => {
const body = res.data.data
if (body && body.token) {
localRequest = createRequestByToken(body.token, config.domain)
const migrate = new Migrate(onlineRequest, localRequest, config.domain)
migrate.setMigrateType()
}
})
}
function createRequestByToken (token, domain) {
const instance = axios.create({
baseURL: url.resolve(domain || ONLINE_DOMAIN, '/api'),
headers: {
Authorization: `Bearer ${token}`
}
})
instance.interceptors.response.use(response => {
if (!response.data.success) {
console.log(chalk.red(`\n${response.data.message}(${response.config.url})`))
process.exit(1)
}
return response
}, error => {
console.log(chalk.red(error.message))
process.exit(1)
})
return instance
}
main()