forked from quasarframework/quasar
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
capacitor-config.js
309 lines (255 loc) · 8.32 KB
/
capacitor-config.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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
const fs = require('fs')
const path = require('path')
const appPaths = require('../app-paths')
const { log, warn } = require('../helpers/logger')
const ensureConsistency = require('./ensure-consistency')
const { capVersion } = require('./cap-cli')
function getAndroidMainActivity (capVersion, appId) {
if (capVersion === 1) {
return `
package ${appId};
import android.net.http.SslError;
import android.webkit.SslErrorHandler;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class EnableHttpsSelfSigned {
public static void enable(WebView webview) {
webview.setWebViewClient(new WebViewClient() {
@Override
public void onReceivedSslError(WebView view, final SslErrorHandler handler, SslError error) {
handler.proceed();
}
});
}
}`
}
// capVersion > 1
return `
package ${appId};
import android.net.http.SslError;
import android.webkit.SslErrorHandler;
import android.webkit.WebView;
import com.getcapacitor.Bridge;
import com.getcapacitor.BridgeWebViewClient;
public class EnableHttpsSelfSigned {
public static void enable(Bridge bridge) {
bridge.getWebView().setWebViewClient(new BridgeWebViewClient(bridge) {
@Override
public void onReceivedSslError(WebView view, final SslErrorHandler handler, SslError error) {
handler.proceed();
}
});
}
}`
}
class CapacitorConfig {
prepare (cfg) {
ensureConsistency()
this.pkg = require(appPaths.resolve.app('package.json'))
this.__updateCapPkg(cfg, this.pkg)
log(`Updated src-capacitor/package.json`)
this.tamperedFiles = []
const capJsonPath = this.__getCapJsonPath(cfg)
const capJson = require(capJsonPath)
this.tamperedFiles.push({
path: capJsonPath,
name: 'capacitor.config.json',
content: this.__updateCapJson(cfg, capJson),
originalContent: JSON.stringify(capJson, null, 2)
})
this.__save()
}
reset () {
this.tamperedFiles.forEach(file => {
file.content = file.originalContent
})
this.__save()
this.tamperedFiles = []
}
__save () {
this.tamperedFiles.forEach(file => {
fs.writeFileSync(file.path, file.content, 'utf8')
log(`Updated ${file.name}`)
})
}
__getCapJsonPath (cfg) {
let jsonPath
if (cfg.ctx.platformName === 'android') {
jsonPath = './android/app/src/main/assets/capacitor.config.json'
}
else if (cfg.ctx.platformName === 'ios') {
jsonPath = './ios/App/App/capacitor.config.json'
}
else {
jsonPath = './capacitor.config.json'
}
return path.join(appPaths.resolve.capacitor(jsonPath))
}
__updateCapJson (cfg, originalCapCfg) {
const capJson = { ...originalCapCfg }
capJson.appName = cfg.capacitor.appName || this.pkg.productName || 'Quasar App'
capJson.bundledWebRuntime = false
if (cfg.ctx.dev) {
capJson.server = capJson.server || {}
capJson.server.url = cfg.build.APP_URL
}
else {
capJson.webDir = 'www'
// ensure we don't run from a remote server
if (capJson.server && capJson.server.url) {
delete capJson.server.url
}
}
return JSON.stringify(capJson, null, 2)
}
__updateCapPkg (cfg, pkg) {
const capPkgPath = appPaths.resolve.capacitor('package.json')
const capPkg = require(capPkgPath)
Object.assign(capPkg, {
name: cfg.capacitor.appName || pkg.name,
version: cfg.capacitor.version || pkg.version,
description: cfg.capacitor.description || pkg.description,
author: pkg.author
})
fs.writeFileSync(capPkgPath, JSON.stringify(capPkg, null, 2), 'utf-8')
}
prepareSSL (add, target) {
if (target === 'ios') {
this.__handleSSLonIOS(add)
}
else {
this.__handleSSLonAndroid(add)
}
}
__getIosCapacitorBridgeFile () {
// we need to try multiple files because
// @capacitor/ios changed the location during its v2
const fileList = [
'node_modules/@capacitor/ios/ios/Capacitor/Capacitor/CAPBridgeViewController.swift',
'node_modules/@capacitor/ios/Capacitor/Capacitor/CAPBridgeViewController.swift'
]
for (let i = 0; i < fileList.length; i++) {
let file = appPaths.resolve.capacitor(fileList[i])
if (fs.existsSync(file)) {
return file
}
}
}
__handleSSLonIOS (add) {
const file = this.__getIosCapacitorBridgeFile()
const needle = 'public func getWebView() -> WKWebView {'
const content = `
// The following part was dynamically added by Quasar.
// This should NOT be part of the app when building for production,
// and it will be removed by Quasar automatically on "quasar build":
public func webView(_ webView: WKWebView, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
let cred = URLCredential(trust: challenge.protectionSpace.serverTrust!)
completionHandler(.useCredential, cred)
}
`
if (add) {
this.__injectIntoFile(file, needle, content)
}
else {
this.__removeFromFile(file, content)
}
}
__injectIntoFile (file, needle, content) {
const sslWarn = () => {
const shortFilename = path.basename(file)
warn()
warn()
warn()
warn()
warn(`${shortFilename} not found or content is unrecognized.`)
warn(`Your App will revoke the devserver's SSL certificate.`)
warn(`Please disable HTTPS from quasar.conf.js > devServer > https`)
warn()
warn()
warn()
warn()
}
if (!file) {
sslWarn()
return
}
const originalContent = fs.readFileSync(file, 'utf-8')
if (originalContent.indexOf(content) > -1) {
// it's already there
return
}
const index = originalContent.indexOf(needle)
if (index === -1) {
sslWarn()
return
}
const newContent = originalContent.substring(0, index) + content + originalContent.substring(index)
fs.writeFileSync(file, newContent, 'utf-8')
}
__removeFromFile (file, content) {
if (!file) {
return
}
const originalContent = fs.readFileSync(file, 'utf-8')
const index = originalContent.indexOf(content)
if (index > -1) {
const newContent = originalContent.replace(content, '')
fs.writeFileSync(file, newContent, 'utf-8')
}
}
__handleSSLonAndroid (add) {
const fglob = require('fast-glob')
const capacitorSrcPath = appPaths.resolve.capacitor('android/app/src/main/java')
let mainActivityPath = fglob.sync(`**/MainActivity.java`, { cwd: capacitorSrcPath, absolute: true })
if (mainActivityPath.length > 0) {
if (mainActivityPath.length > 1) {
warn(`Found multiple matches for MainActivity.java file, https might not work. Using file ${mainActivityPath[0]}.`)
}
mainActivityPath = mainActivityPath[0]
}
else if (mainActivityPath.length === 0) {
warn()
warn('IMPORTANT! Could not find MainActivity.java file and therefore cannot enable devServer: https support.')
warn()
return
}
const enableHttpsSelfSignedPath = path.join(path.dirname(mainActivityPath), 'EnableHttpsSelfSigned.java')
if (fs.existsSync(mainActivityPath)) {
let mainActivity = fs.readFileSync(mainActivityPath, 'utf8')
const sslString = `
if (BuildConfig.DEBUG) {
EnableHttpsSelfSigned.enable(${capVersion === 1 ? 'findViewById(R.id.webview)' : 'this.bridge'});
}
`
if (add) {
// Allow unsigned certificates in MainActivity
if (!/EnableHttpsSelfSigned\.enable/.test(mainActivity)) {
mainActivity = mainActivity.replace(
/this\.init\(.*}}\);/ms,
match => `${match}
${sslString}
`
)
}
// Add helper file
if (!fs.existsSync(enableHttpsSelfSignedPath)) {
const appId = mainActivity.match(/package ([\w\.]*);/)[1]
fs.writeFileSync(
enableHttpsSelfSignedPath,
getAndroidMainActivity(capVersion, appId)
)
}
}
else {
if (/EnableHttpsSelfSigned\.enable/.test(mainActivity)) {
mainActivity = mainActivity.replace(sslString, '')
}
if (fs.existsSync(enableHttpsSelfSignedPath)) {
fs.unlinkSync(enableHttpsSelfSignedPath)
}
}
fs.writeFileSync(mainActivityPath, mainActivity, 'utf-8')
}
}
}
module.exports = CapacitorConfig