-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweex-run-util.js
161 lines (134 loc) · 4.87 KB
/
weex-run-util.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
'use babel'
'use strict'
const SimulatorManager = require('./simulatorManagerUtil')
const PreviewClient = require('./previewClient')
const PreviewServer = require('./previewServer')
const QRCodeViewUtil = require('./qrcodeViewUtil')
const notifier = require('./notifier')
const path = require('path')
const touch = require('touch')
const configurationManager = require('./configurationManager')
const fs = require('fs')
const chokidar = require('chokidar')
const findPort = require('find-free-port')
const commandTransformed = 'thera-live-server:transformed'
const findChangedCommand = 'thera-project-service:file-changed'
module.exports = class WeexRunUtil {
constructor () {
this.simulatorManagerUtil = new SimulatorManager()
this.previewClient = new PreviewClient()
this.previewServer = new PreviewServer()
this._activate()
}
deactivate () {
this.previewServer.deactivate()
}
_activate () {
let _this = this
this.simulatorManagerUtil.devicesList().then((sims) => {
atom.commands.dispatch(atom.views.getView(atom.workspace), 'tool-bar:flash-devicelist', sims)
_this.simulatorManagerUtil.load()
})
// init events callback
atom.commands.add(atom.commands.rootNode, commandTransformed, (notify) => _this._transformed(notify))
}
_startPreviewFile () {
return configurationManager.getWatchFilePath()
}
_saveWatchingFile () {
return new Promise((resolve, reject) => {
// save the editor
if (this.watchFilePath) {
touch(this.watchFilePath)
}
resolve()
})
}
qrcode () {
this.qrcodeViewUtil = this.qrcodeViewUtil || new QRCodeViewUtil()
if (!this.previewServer.getDumplingProcess()) {
// generate unused port
findPort(7000, (err, port) => {
atom.config.set('weex-run.dumplingPort', '' + port)
this.previewServer.boot()
.then(() => this.previewClient.requestQRCode())
.then((qrcodeContent) => this.qrcodeViewUtil.displayQRCode(qrcodeContent))
.catch((err) => console.error(err))
.then(() => this.previewClient.wsConnect())
.then(() => this._watchConfigFile())
// start to preview file
.then(() => this._startPreviewFile())
.then(() => this.previewClient.start(this.watchFilePath))
.then(() => this._saveWatchingFile())
.catch(err => console.error(err.message))
})
} else {
this.previewClient.requestQRCode()
.then((qrcodeContent) => this.qrcodeViewUtil.displayQRCode(qrcodeContent))
.catch((err) => console.error(err))
.then(() => this.previewClient.wsConnect())
// start to preview file
.then(() => this._startPreviewFile())
.then(() => this.previewClient.start(this.watchFilePath))
.then(() => this._saveWatchingFile())
.catch(err => console.error(err.message))
}
return this.qrcodeViewUtil.qrImageWithGuide
}
_watchConfigFile () {
if (!this.watchingConfig) {
const _this = this
if (atom.project.getDirectories()[0]) {
this._onProjectDirChange(atom.project.getDirectories()[0].path)
}
atom.project.onDidChangePaths((projectPaths) => {
if (projectPaths[0] && this.watchProjectPath !== projectPaths[0]) {
_this._onProjectDirChange(projectPaths[0])
}
})
}
}
_onProjectDirChange (projectPath) {
this.watchProjectPath = projectPath
const _this = this
chokidar.watch(path.join(projectPath), {ignored: path.join(projectPath, 'node_modules')}).on('all', (event, filePath) => {
// consumes launch config message
if (filePath.endsWith(path.join('.thera', 'launch.json'))) {
// init project config
if (event === 'add') {
_this.notifySwitch = 'on'
_this._previewConfigChange()
} else if (event === 'unlink') {
_this.notifySwitch = 'off'
} else if (event === 'change'){
_this._previewConfigChange()
}
}
if (_this.notifySwitch === 'on') {
// dispatch all file change events
atom.commands.dispatch(atom.views.getView(atom.workspace), findChangedCommand, {event: event, filePath: filePath})
}
})
}
_previewConfigChange () {
this._startPreviewFile()
.then(() => this.previewClient.start(this.watchFilePath))
}
updateQrCode () {
this.previewClient.requestQRCode()
.then((qrcodeContent) => this.qrcodeViewUtil.displayQRCode(qrcodeContent))
}
_transformed (notify) {
configurationManager.getConfig()
.then(({notifyOnSync}) => {
if (notifyOnSync) {
let {message, data} = notify.detail
if (message === 'transformSuccessNotify') {
notifier.addSuccess('sync success')
} else if (message === 'transformFailedNotify') {
notifier.addError(`sync faled, ${JSON.stringify(data.error)}`)
}
}
})
}
}