-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
settings.coffee
141 lines (111 loc) · 3.46 KB
/
settings.coffee
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
_ = require("lodash")
Promise = require("bluebird")
path = require("path")
fs = require("fs-extra")
errors = require("../errors")
log = require("../log")
## TODO:
## think about adding another PSemaphore
## here since we can read + write the
## settings at the same time something else
## is potentially reading it
fs = Promise.promisifyAll(fs)
flattenCypress = (obj) ->
if cypress = obj.cypress
return cypress
renameVisitToPageLoad = (obj) ->
if v = obj.visitTimeout
obj = _.omit(obj, "visitTimeout")
obj.pageLoadTimeout = v
obj
renameCommandTimeout = (obj) ->
if c = obj.commandTimeout
obj = _.omit(obj, "commandTimeout")
obj.defaultCommandTimeout = c
obj
renameSupportFolder = (obj) ->
if sf = obj.supportFolder
obj = _.omit(obj, "supportFolder")
obj.supportFile = sf
obj
module.exports =
_pathToFile: (projectRoot, file) ->
path.join(projectRoot, file)
_err: (type, file, err) ->
e = errors.get(type, file, err)
e.code = err.code
e.errno = err.errno
throw e
_logReadErr: (file, err) ->
@_err("ERROR_READING_FILE", file, err)
_logWriteErr: (file, err) ->
@_err("ERROR_WRITING_FILE", file, err)
_write: (file, obj = {}) ->
fs.outputJsonAsync(file, obj, {spaces: 2})
.return(obj)
.catch (err) =>
@_logWriteErr(file, err)
_applyRewriteRules: (obj = {}) ->
_.reduce [flattenCypress, renameVisitToPageLoad, renameCommandTimeout, renameSupportFolder], (memo, fn) ->
if ret = fn(memo)
return ret
else
return memo
, _.cloneDeep(obj)
id: (projectRoot) ->
file = @_pathToFile(projectRoot, "cypress.json")
fs.readJsonAsync(file)
.get("projectId")
.catch ->
null
exists: (projectRoot) ->
file = @_pathToFile(projectRoot, "cypress.json")
## first check if cypress.json exists
fs.statAsync(file)
.then ->
## if it does also check that the projectRoot
## directory is writable
fs.accessAsync(projectRoot, fs.W_OK)
.catch {code: "ENOENT"}, (err) =>
## cypress.json does not exist, we missing project
log("cannot find file %s", file)
@_err("PROJECT_DOES_NOT_EXIST", projectRoot, err)
.catch (err) =>
throw err if errors.isCypressErr(err)
## else we cannot read due to folder permissions
@_logReadErr(file, err)
read: (projectRoot) ->
file = @_pathToFile(projectRoot, "cypress.json")
fs.readJsonAsync(file)
.catch {code: "ENOENT"}, =>
@_write(file, {})
.then (json = {}) =>
changed = @_applyRewriteRules(json)
## if our object is unchanged
## then just return it
if _.isEqual(json, changed)
return json
else
## else write the new reduced obj
@_write(file, changed)
.catch (err) =>
throw err if errors.isCypressErr(err)
@_logReadErr(file, err)
readEnv: (projectRoot) ->
file = @_pathToFile(projectRoot, "cypress.env.json")
fs.readJsonAsync(file)
.catch {code: "ENOENT"}, ->
return {}
.catch (err) =>
throw err if errors.isCypressErr(err)
@_logReadErr(file, err)
write: (projectRoot, obj = {}) ->
@read(projectRoot)
.then (settings) =>
_.extend settings, obj
file = @_pathToFile(projectRoot, "cypress.json")
@_write(file, settings)
remove: (projectRoot) ->
fs.unlinkSync @_pathToFile(projectRoot, "cypress.json")
pathToCypressJson: (projectRoot) ->
@_pathToFile(projectRoot, "cypress.json")