forked from m0dch3n/vue-cli-plugin-cordova
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
321 lines (282 loc) · 9.71 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
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
310
311
312
313
314
315
316
317
318
319
320
321
const spawn = require('cross-spawn')
const { info, error } = require('@vue/cli-shared-utils')
const fs = require('fs')
const portfinder = require('portfinder')
const address = require('address')
const defaults = require('./defaults')
const defaultServe = require('./default-serve')
const defaultModes = {
'cordova-serve-android': 'development',
'cordova-build-android': 'production',
'cordova-serve-ios': 'development',
'cordova-build-ios': 'production',
'cordova-serve-browser': 'development',
'cordova-build-browser': 'production',
'cordova-serve-osx': 'development',
'cordova-build-osx': 'production',
'cordova-build-only-www-ios': 'production',
'cordova-build-only-www-android': 'production',
'cordova-build-only-www-browser': 'production',
'cordova-build-only-www-osx': 'production',
'cordova-prepare': 'production'
}
module.exports = (api, options) => {
const cordovaPath = options.pluginOptions.cordovaPath || defaults.cordovaPath
const srcCordovaPath = api.resolve(cordovaPath)
const getPlatformPath = platform => {
return api.resolve(`${cordovaPath}/platforms/${platform}`)
}
const getPlatformPathWWW = platform => {
return api.resolve(`${cordovaPath}/platforms/${platform}/platform_www`)
}
const getCordovaPathConfig = platform => {
let cordovaConfigPathToUpdate
if (platform === 'android') {
cordovaConfigPathToUpdate = 'app/src/main/res/xml/config.xml'
} else if (platform === 'ios' || platform === 'osx') {
const cordovaConfigPath = api.resolve(`${cordovaPath}/config.xml`)
const cordovaConfig = fs.readFileSync(cordovaConfigPath, 'utf-8')
const regexAppName = /\s+<name(.*)>(.*)<\/name>/
const appNameMatch = cordovaConfig.match(regexAppName)
if (appNameMatch.length >= 3) {
const appName = appNameMatch[2]
cordovaConfigPathToUpdate = `${appName}/config.xml`
} else {
error('Unable to detect AppName!')
}
} else {
cordovaConfigPathToUpdate = 'config.xml'
}
return api.resolve(`${cordovaPath}/platforms/${platform}/${cordovaConfigPathToUpdate}`)
}
const cordovaRun = platform => {
// cordova run platform
info(`executing "cordova run ${platform}" in folder ${srcCordovaPath}`)
return spawn.sync('cordova', [
'run',
platform
], {
cwd: srcCordovaPath,
env: process.env,
stdio: 'inherit', // pipe to console
encoding: 'utf-8'
})
}
const cordovaPrepare = () => {
// cordova run platform
info(`executing "cordova prepare in folder ${srcCordovaPath}`)
return spawn.sync('cordova', [
'prepare'
], {
cwd: srcCordovaPath,
env: process.env,
stdio: 'inherit', // pipe to console
encoding: 'utf-8'
})
}
const cordovaBuild = (platform, release = true) => {
// cordova run platform
const cordovaMode = release ? '--release' : '--debug'
info(`executing "cordova build ${platform} ${cordovaMode}" in folder ${srcCordovaPath}`)
return spawn.sync('cordova', [
'build',
platform,
cordovaMode
], {
cwd: srcCordovaPath,
env: process.env,
stdio: 'inherit', // pipe to console
encoding: 'utf-8'
})
}
const cordovaClean = () => {
// cordova clean
info(`executing "cordova clean" in folder ${srcCordovaPath}`)
return spawn.sync('cordova', [
'clean'
], {
cwd: srcCordovaPath,
env: process.env,
stdio: 'inherit', // pipe to console
encoding: 'utf-8'
})
}
const cordovaJSMiddleware = platform => {
return (req, res, next) => {
if (req.url !== '/') {
const filePath = getPlatformPathWWW(platform) + req.url
try {
if (fs.existsSync(filePath)) {
const fileContent = fs.readFileSync(filePath, 'utf-8')
res.send(fileContent)
return
}
} catch (err) {
}
}
next()
}
}
const runServe = async (platform, args) => {
const availablePlatforms = []
const platforms = defaults.platforms
platforms.forEach(platform => {
const platformPath = getPlatformPath(platform)
if (fs.existsSync(platformPath)) {
availablePlatforms.push(platform)
}
})
if (availablePlatforms.includes(platform)) {
// add cordova.js, define process.env.CORDOVA_PLATFORM
chainWebPack(platform)
// Add js middleware
configureDevServer(platform)
const projectDevServerOptions = options.devServer || {}
// resolve server options
const open = false // browser does not need to be opened
const https = options.devServer.https === null || options.devServer.https === undefined ? true : options.devServer.https // check devServer.options for user defined https setting
const protocol = https ? 'https' : 'http'
const host = args.host || process.env.HOST || projectDevServerOptions.host || defaultServe.host
let port = args.port || process.env.PORT || projectDevServerOptions.port || defaultServe.port
portfinder.basePort = port
port = await portfinder.getPortPromise()
const publicArg = args.public || projectDevServerOptions.public
const defaultPublicURL = `${protocol}://${address.ip()}:${port}`
const rawPublicUrl = publicArg || defaultPublicURL
const publicUrl = rawPublicUrl
? /^[a-zA-Z]+:\/\//.test(rawPublicUrl)
? rawPublicUrl
: `${protocol}://${rawPublicUrl}`
: null
const serveArgs = {
open,
host,
port,
https,
public: publicArg
}
// npm run serve
const server = await api.service.run('serve', serveArgs)
// set content url to devServer
process.env.CORDOVA_WEBVIEW_SRC = publicUrl
process.env.CORDOVA_PREPARE_CONFIG = getCordovaPathConfig(platform)
cordovaClean()
cordovaRun(platform)
return server
} else {
if (availablePlatforms.length === 0) {
error(`No platforms installed in '${srcCordovaPath}', please execute "cordova platform add ${platform}" in ${srcCordovaPath}`)
} else {
error(`Missing platform '${platform}', please execute "cordova platform add ${platform}" in ${srcCordovaPath}`)
}
}
}
const runBuild = async (platform, args) => {
// build WWW
await runWWWBuild(platform, args)
// cordova clean
await cordovaClean()
// cordova build --release (if you want a build debug build, use cordovaBuild(platform, false)
await cordovaBuild(platform)
}
const addGitIgnoreToWWW = () => {
const wwwIgnorePath = api.resolve(`${cordovaPath}/www/.gitignore`)
fs.writeFileSync(wwwIgnorePath, defaults.gitIgnoreContent)
}
const runPrepare = async (args) => {
// build WWW
await runWWWBuild(null, args)
// add www/.gitignore again (because build will delete it)
addGitIgnoreToWWW()
// cordova prepare
await cordovaPrepare()
}
const runWWWBuild = async (platform, args) => {
// add cordova.js, define process.env.CORDOVA_PLATFORM
chainWebPack(platform)
// set build output folder
args.dest = cordovaPath + '/www'
// build
await api.service.run('build', args)
// add www/.gitignore again (because build will delete it)
addGitIgnoreToWWW()
}
const configureDevServer = platform => {
api.configureDevServer(app => {
// /cordova.js should resolve to platform cordova.js
app.use(cordovaJSMiddleware(platform))
})
}
const chainWebPack = platform => {
api.chainWebpack(webpackConfig => {
// add cordova.js to index.html
webpackConfig.plugin('cordova')
.use(require('html-webpack-include-assets-plugin'), [{
assets: 'cordova.js',
append: false,
publicPath: false
}])
// process.env.CORDOVA_PLATFORM = platform
if (platform !== null) {
webpackConfig.plugin('define')
.tap(args => {
const { 'process.env': env, ...rest } = args[0]
return [{
'process.env': Object.assign(
{},
env,
{
CORDOVA_PLATFORM: '\'' + platform + '\''
}
),
...rest
}]
})
}
})
}
api.registerCommand('cordova-serve-android', async args => {
return await runServe('android', args)
})
api.registerCommand('cordova-build-android', async args => {
return await runBuild('android', args)
})
api.registerCommand('cordova-serve-ios', async args => {
return await runServe('ios', args)
})
api.registerCommand('cordova-build-ios', async args => {
return await runBuild('ios', args)
})
api.registerCommand('cordova-serve-osx', async args => {
return await runServe('osx', args)
})
api.registerCommand('cordova-build-osx', async args => {
return await runBuild('osx', args)
})
api.registerCommand('cordova-build-only-www-ios', async args => {
return await runWWWBuild('ios', args)
})
api.registerCommand('cordova-build-only-www-android', async args => {
return await runWWWBuild('android', args)
})
api.registerCommand('cordova-build-only-www-browser', async args => {
return await runWWWBuild('browser', args)
})
api.registerCommand('cordova-build-only-www-osx', async args => {
return await runWWWBuild('osx', args)
})
api.registerCommand('cordova-prepare', async args => {
return await runPrepare(args)
})
api.registerCommand('cordova-serve-browser', async args => {
args.open = true
const platform = 'browser'
chainWebPack(platform)
configureDevServer(platform)
return await api.service.run('serve', args)
})
api.registerCommand('cordova-build-browser', async args => {
return await runBuild('browser', args)
})
}
module.exports.defaultModes = defaultModes