-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Expand file tree
/
Copy pathcordova-builder.js
More file actions
172 lines (142 loc) · 4.69 KB
/
Copy pathcordova-builder.js
File metadata and controls
172 lines (142 loc) · 4.69 KB
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
import fse from 'fs-extra'
import { join } from 'node:path'
import { AppBuilder } from '../../app-builder.js'
import { quasarCordovaConfig } from './cordova-config.js'
import { log, warn } from '../../utils/logger.js'
import { CordovaConfigFile } from './config-file.js'
import { spawn } from '../../utils/spawn.js'
import { openIDE } from '../../utils/open-ide.js'
import { onShutdown } from '../../utils/on-shutdown.js'
import { fixAndroidCleartext } from './android-cleartext.js'
import { SIGNALS } from '../../utils/signals.js'
const cordovaOutputFolders = {
ios: [
'platforms/ios/build/Release-iphoneos', // ios-cordova 7+
'platforms/ios/build/Debug-iphoneos', // ios-cordova 7+
'platforms/ios/build/Release-iphonesimulator', // ios-cordova 7+
'platforms/ios/build/Debug-iphonesimulator', // ios-cordova 7+
'platforms/ios/build/device',
'platforms/ios/build/emulator'
],
android: ['platforms/android/app/build/outputs']
}
function ensureArray(val) {
return !val || Array.isArray(val) ? val : [val]
}
export class QuasarModeBuilder extends AppBuilder {
#cordovaConfigFile = new CordovaConfigFile()
async build() {
this.cleanArtifacts()
await this.#buildFiles()
await this.#packageFiles()
}
async #buildFiles() {
const viteConfig = await quasarCordovaConfig.vite(this.quasarConf)
await this.buildWithVite('Cordova UI', viteConfig)
/**
* We inject the cordova.js external script after build
* so Vite won't warn about not being able to bundle script tag
* (it shouldn't bundle it anyway in this case)
*
* Vite's warning would be:
* <script src="cordova.js"> in "/index.html" can't be bundled without type="module" attribute
*/
if (this.quasarConf.ctx.prod) {
const indexHtmlFile = join(viteConfig.build.outDir, 'index.html')
let html = this.readFile(indexHtmlFile)
html = html.replace(
/(<head[^>]*)(>)/i,
(_, start, end) => `${start}${end}<script src="cordova.js"></script>`
)
this.writeFile(indexHtmlFile, html)
}
this.printSummary(viteConfig.build.outDir)
}
async #packageFiles() {
const target = this.ctx.targetName
const { appPaths } = this.ctx
if (target === 'android') {
fixAndroidCleartext(appPaths, 'remove')
}
const cordovaContext = {
debug: this.quasarConf.metaConf.debugging,
target
}
const outputTargetList =
ensureArray(
this.quasarConf.cordova.getCordovaBuildOutputFolder?.(cordovaContext)
) ||
cordovaOutputFolders[target] ||
[]
// Remove old build output
outputTargetList.forEach(outputFile => {
fse.removeSync(appPaths.resolve.cordova(outputFile))
})
onShutdown(() => {
this.#cleanup()
})
this.#cordovaConfigFile.prepare(this.quasarConf)
const args =
this.argv['skip-pkg'] || this.argv.ide
? ['prepare', target]
: this.quasarConf.cordova.getCordovaBuildParams?.(cordovaContext) || [
'build',
this.quasarConf.metaConf.debugging ? '--debug' : '--release',
'--device',
target
]
await this.#runCordovaCommand([...args, ...this.argv._], target)
if (this.argv['skip-pkg'] !== true) {
if (this.argv.ide) {
await openIDE({
mode: 'cordova',
bin: this.quasarConf.bin,
target,
appPaths
})
return SIGNALS.BUILD_EXTERNAL_TOOL_SPAWNED
}
const targetFolder = join(
this.quasarConf.build.distDir,
this.quasarConf.ctx.targetName
)
for (const folder of outputTargetList) {
const outputFolder = appPaths.resolve.cordova(folder)
if (fse.existsSync(outputFolder)) {
log(
`Copying Cordova distributables from ${outputFolder} to ${targetFolder}`
)
log()
fse.copySync(outputFolder, targetFolder)
return
}
}
warn(
`No output folder found for target "${target}".` +
' Files have not been copied to /dist. You will need' +
' to manually extract the Cordova distributables.'
)
log()
}
}
#cleanup() {
this.#cordovaConfigFile.reset()
}
#runCordovaCommand(args) {
const { promise, resolve } = Promise.withResolvers()
spawn('cordova', args, { cwd: this.ctx.appPaths.cordovaDir }, code => {
this.#cleanup()
if (code) {
console.log()
console.log(' ⚠️ Cordova CLI has failed to build!')
console.log(
' ⚠️ As an alternative, you can use the "--ide" param and build from the IDE.'
)
console.log()
process.exit(1)
}
resolve()
})
return promise
}
}