-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
project-model.js
283 lines (225 loc) · 6.47 KB
/
project-model.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
import _ from 'lodash'
import { action, computed, observable, toJS } from 'mobx'
import Browser from '../lib/browser-model'
import Warning from './warning-model'
const cacheProps = [
'id',
'name',
'public',
'orgName',
'orgId',
'defaultOrg',
'lastBuildStatus',
'lastBuildCreatedAt',
]
const validProps = cacheProps.concat([
'state',
'isChosen',
'isLoading',
'isNew',
'configFile',
'browsers',
'onBoardingModalOpen',
'browserState',
'resolvedConfig',
'parentTestsFolderDisplay',
'integrationExampleName',
'scaffoldedFiles',
'resolvedNodePath',
'resolvedNodeVersion',
])
export default class Project {
// state constants
static VALID = 'VALID'
static INVALID = 'INVALID'
static UNAUTHORIZED = 'UNAUTHORIZED'
// persisted with api
@observable id
@observable name
@observable public
@observable lastBuildStatus
@observable lastBuildCreatedAt
@observable orgName
@observable orgId
@observable defaultOrg
// comes from ipc, but not persisted
@observable state = Project.VALID
// local state
@observable isChosen = false
@observable isLoading = false
@observable isNew = false
@observable browsers = []
@observable onBoardingModalOpen = false
@observable browserState = 'closed'
@observable resolvedConfig
@observable error
/** @type {{[key: string] : {warning:Error & {dismissed: boolean}}}} */
@observable _warnings = {}
@observable apiError
@observable parentTestsFolderDisplay
@observable integrationExampleName
@observable scaffoldedFiles = []
@observable resolvedNodePath
@observable resolvedNodeVersion
// should never change after first set
@observable path
// not observable
dismissedWarnings = {}
constructor (props) {
this.path = props.path
this.update(props)
}
@computed get displayName () {
if (this.name) return this.name
// need normalize windows paths with \ before split
const normalizedPath = this.path.replace(/\\/g, '/')
const lastDir = _.last(normalizedPath.split('/'))
return _.truncate(lastDir, { length: 60 })
}
@computed get displayPath () {
const maxPathLength = 45
if (this.path.length <= maxPathLength) return this.path
const truncatedPath = this.path.slice((this.path.length - 1) - maxPathLength, this.path.length)
return '...'.concat(truncatedPath)
}
@computed get isUnauthorized () {
return this.state === Project.UNAUTHORIZED
}
@computed get isValid () {
return this.state === Project.VALID
}
@computed get isInvalid () {
return this.state === Project.INVALID
}
@computed get isSetupForRecording () {
return this.id && this.isValid
}
@computed get otherBrowsers () {
return _.filter(this.browsers, { isChosen: false })
}
@computed get chosenBrowser () {
return _.find(this.browsers, { isChosen: true })
}
@computed get defaultBrowser () {
return this.browsers[0]
}
@computed get warnings () {
return _.reject(this._warnings, { isDismissed: true })
}
@action update (props) {
if (!props) return
_.each(validProps, (prop) => {
this._updateProp(props, prop)
})
}
_updateProp (props, prop) {
if (props[prop] != null) this[prop] = props[prop]
}
@action setLoading (isLoading) {
this.isLoading = isLoading
}
@action openModal () {
this.onBoardingModalOpen = true
}
@action closeModal () {
this.onBoardingModalOpen = false
}
@action browserOpening () {
this.browserState = 'opening'
}
@action browserOpened () {
this.browserState = 'opened'
}
@action browserClosed () {
this.browserState = 'closed'
}
@action setBrowsers (browsers = []) {
if (browsers.length) {
this.browsers = _.map(browsers, (browser) => {
return new Browser(browser)
})
// use a custom browser if one is supplied. or, if they already have
// a browser chosen that's been saved in localStorage, then select that
// otherwise just do the default.
const customBrowser = _.find(this.browsers, { custom: true })
if (customBrowser) {
return this.setChosenBrowser(customBrowser, { save: false })
}
const ls = localStorage.getItem('chosenBrowser')
if (ls) {
return this.setChosenBrowserFromLocalStorage(ls)
}
return this.setChosenBrowser(this.defaultBrowser)
}
}
@action setChosenBrowser (browser, { save } = {}) {
_.each(this.browsers, (browser) => {
browser.isChosen = false
})
if (save !== false) {
localStorage.setItem('chosenBrowser', JSON.stringify(_.pick(browser, 'name', 'channel')))
}
browser.isChosen = true
}
@action setOnBoardingConfig (config) {
this.isNew = config.isNewProject
this.integrationFolder = config.integrationFolder
this.parentTestsFolderDisplay = config.parentTestsFolderDisplay
this.fileServerFolder = config.fileServerFolder
this.integrationExampleName = config.integrationExampleName
this.integrationExamplePath = config.integrationExamplePath
this.scaffoldedFiles = config.scaffoldedFiles
}
@action setResolvedConfig (resolved) {
this.resolvedConfig = resolved
}
@action setError (err = {}) {
// for some reason, the original `stack` is unavailable on `err` once it is set on the model
// `stack2` remains usable though, for some reason
err.stack2 = err.stack
this.error = err
}
@action clearError () {
this.error = null
}
@action addWarning (warning) {
const type = warning.type
if (type && this._warnings[type] && this._warnings[type].isDismissed) {
return
}
this._warnings[type] = new Warning(warning)
}
@action dismissWarning (warning) {
if (!warning) {
// calling with no warning clears all warnings
return _.each(this._warnings, ((warning) => {
return this.dismissWarning(warning)
}))
}
warning.setDismissed(true)
}
@action setApiError = (err = {}) => {
this.apiError = err
}
@action setChosenBrowserFromLocalStorage (ls) {
let filter = {}
try {
_.merge(filter, JSON.parse(ls))
} catch (err) {
// localStorage pre-dates JSON filter, assume "name"
filter.name = ls
}
const browser = _.find(this.browsers, filter) || this.defaultBrowser
this.setChosenBrowser(browser)
}
clientDetails () {
return _.pick(this, 'id', 'path')
}
getConfigValue (key) {
if (!this.resolvedConfig) return
return toJS(this.resolvedConfig[key]).value
}
serialize () {
return _.pick(this, cacheProps)
}
}