/
index.js
202 lines (194 loc) · 6.8 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
const execa = require('execa')
const fs = require('fs-extra')
const { done, error, info, warn } = require('@vue/cli-shared-utils')
module.exports = (api, options) => {
const pluginOptions = options ? options.pluginOptions || {} : {}
api.registerCommand(
'capacitor:build',
{
description: 'build app for Android/iOS with Capacitor',
usage: 'vue-cli-service capacitor:build --(android|ios)'
},
async args => {
const path = require('path')
const outputDir = args.dest || pluginOptions.outputDir || 'dist'
args.dest = path.join(outputDir, 'bundled')
// Uses platforms set in args or installed platforms
const platforms = []
if (args.android || args.ios) {
if (args.android) {
platforms.push('android')
}
if (args.ios) {
platforms.push('ios')
}
} else {
if (await fs.pathExists(api.resolve('./android'))) {
platforms.push('android')
}
if (await fs.pathExists(api.resolve('./ios'))) {
platforms.push('ios')
}
}
// Build app with Vue CLI
if (!args.skipBundle) await api.service.run('build', args)
// Set web dir to build output
setCapacitorConfig('main config', false, api.resolve(args.dest))
for (const platform of platforms) {
// Copy app
await execa('cap', ['copy', platform], {
stdio: 'inherit'
})
if (args.ide || platform === 'ios') {
info(
`\nLaunching ${
platform === 'android' ? 'Android Studio' : 'XCode'
}. Build your app here to finish.\n`
)
// Launch native studio to finish build
await execa('cap', ['open', platform], {
stdio: 'inherit'
})
} else {
const basePath = api.resolve('android/app/build/outputs/apk/release')
// Remove old build output
await fs.remove(basePath)
info('Building native app')
await execa(
`./gradlew${process.platform === 'win32' ? '.bat' : ''}`,
['assembleRelease'],
{ stdio: 'inherit', cwd: api.resolve('android') }
)
// Copy built apk to dist folder
const unsignedPath = path.join(basePath, 'app-release-unsigned.apk')
const signedPath = path.join(basePath, 'app-release.apk')
if (await fs.exists(signedPath)) {
await fs.copy(
signedPath,
path.join(api.resolve(outputDir), 'app-release.apk')
)
} else if (await fs.exists(unsignedPath)) {
await fs.copy(
unsignedPath,
path.join(api.resolve(outputDir), 'app-release.apk')
)
} else {
error(
'Could not find outputted apk. Please resolve any errors with Capacitor. To run the build in Android Studio, pass the "--ide" argument to this command.'
)
}
}
done('Build Complete!')
}
}
)
api.registerCommand(
'capacitor:serve',
{
description: 'serve app in Android/iOS with Capacitor',
usage: 'vue-cli-service capacitor:serve --(android|ios)'
},
async args => {
const getLanUrl = require('./util/getLanUrl')
let platform
if (args.android && args.ios) {
throw new Error('Please specify a single platform to develop with')
} else {
if (args.android) {
platform = 'android'
} else if (args.ios) {
platform = 'ios'
} else {
platform = pluginOptions.defaultPlatform || 'android'
}
}
// Make sure cleartext is enabled
if (platform === 'android') {
const androidManifest = await fs.readFile(
api.resolve('android/app/src/main/AndroidManifest.xml'),
'utf8'
)
if (!androidManifest.match('android:usesCleartextTraffic="true"')) {
warn(
'Cleartext must be enabled to connect to the dev server on Android!'
)
warn(
'Add `android:usesCleartextTraffic="true"` to src-capacitor/android/app/src/main/AndroidManifest.xml in the `application` tag'
)
// TODO: actual url with instructions
// warn('See [need url] for more instructions')
process.exit(1)
}
}
// Make sure there is an index.html, otherwise Capacitor will crash
await fs.ensureFile(api.resolve('./dist/index.html'))
// Copy app data
await execa('cap', ['copy', platform])
// Start dev server
const { url } = await api.service.run('serve')
// resolve server options
const projectDevServerOptions = Object.assign(
api.resolveWebpackConfig().devServer || {},
options.devServer
)
const protocol =
args.https || projectDevServerOptions.https ? 'https' : 'http'
const host =
args.host ||
process.env.HOST ||
projectDevServerOptions.host ||
'0.0.0.0'
let port = url.match(/:(\d{2,4})\/?/)[1]
let networkUrl = getLanUrl(protocol, host, port, options.baseUrl)
if (!networkUrl && platform === 'android') {
// AVDs can connect to localhost of host computer
warn(
'WARNING! Network server is not available. App will only work on an AVD, not on a physical device'
)
} else if (!networkUrl) {
throw new Error(
'Unable to host app on network. This is required to run a dev server on iOS.'
)
} else {
networkUrl = networkUrl.replace(/\/$/, '')
}
setCapacitorConfig(platform, networkUrl || `http://10.0.2.2${port}`)
info(
`\nLaunching ${
platform === 'android' ? 'Android Studio' : 'XCode'
}. Run your app here, and it will automatically connect to the dev server.\n`
)
// Launch native studio
await execa('cap', ['open', platform], {
stdio: 'inherit'
})
}
)
async function setCapacitorConfig (platform, serverUrl, webDir) {
let configPath
// Read existing config
if (platform === 'android') {
configPath = './android/app/src/main/assets/capacitor.config.json'
} else if (platform === 'ios') {
configPath = './ios/App/App/capacitor.config.json'
} else {
configPath = './capacitor.config.json'
}
const capacitorConfig = JSON.parse(
await fs.readFile(api.resolve(configPath), 'utf8')
)
if (serverUrl) {
capacitorConfig.server = capacitorConfig.server || {}
capacitorConfig.server.url = serverUrl
}
if (webDir) {
capacitorConfig.webDir = webDir
}
// Write updated config
await fs.writeFile(api.resolve(configPath), JSON.stringify(capacitorConfig))
}
}
module.exports.defaultModes = {
'capacitor:build': 'production',
'capacitor:serve': 'development'
}