-
Notifications
You must be signed in to change notification settings - Fork 113
/
upload.ts
204 lines (195 loc) · 6.19 KB
/
upload.ts
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
import {
AppVersionDescriptor,
BundleStoreEngine,
log,
PackagePath,
kax,
createTmpDir,
NativePlatform,
} from 'ern-core'
import { getActiveCauldron } from 'ern-cauldron-api'
import { generateComposite } from 'ern-composite-gen'
import { bundleMiniAppsFromComposite } from 'ern-container-gen'
import {
epilog,
tryCatchWrap,
logErrorAndExitIfNotSatisfied,
askUserToChooseANapDescriptorFromCauldron,
} from '../../lib'
import { Argv } from 'yargs'
import path from 'path'
import untildify from 'untildify'
export const command = 'upload'
export const desc = 'Upload a bundle to a store'
export const builder = (argv: Argv) => {
return argv
.option('baseComposite', {
describe: 'Base Composite',
type: 'string',
})
.coerce('baseComposite', d => PackagePath.fromString(d))
.option('descriptor', {
alias: 'd',
describe: 'Full native application descriptor',
type: 'string',
})
.coerce('descriptor', d => AppVersionDescriptor.fromString(d))
.option('extraJsDependencies', {
alias: 'e',
describe: 'Additional JS dependency(ies)',
type: 'array',
})
.coerce('extraJsDependencies', d => d.map(PackagePath.fromString))
.option('fromGitBranches', {
describe: 'Favor MiniApp(s) branches',
type: 'boolean',
})
.option('fromPackager', {
describe: 'Upload bundle served by current local packager',
type: 'boolean',
})
.option('jsApiImpls', {
describe: 'One or more JS API implementation(s)',
type: 'array',
})
.coerce('jsApiImpls', d => d.map(PackagePath.fromString))
.option('miniapps', {
alias: 'm',
describe: 'One or more MiniApp(s)',
type: 'array',
})
.option('platform', {
choices: ['android', 'ios', undefined],
describe:
'If set will only upload the bundle for this platform (otherwise will upload for both platforms)',
type: 'string',
})
.option('prod', {
describe:
'Set to upload a production bundle rather than a development one',
type: 'boolean',
})
.coerce('miniapps', d => d.map(PackagePath.fromString))
.epilog(epilog(exports))
}
export const commandHandler = async ({
baseComposite,
descriptor,
extraJsDependencies,
fromGitBranches,
fromPackager,
jsApiImpls,
miniapps,
platform,
prod,
}: {
baseComposite?: PackagePath
descriptor?: AppVersionDescriptor
extraJsDependencies?: PackagePath[]
fromGitBranches?: boolean
fromPackager?: boolean
jsApiImpls?: PackagePath[]
miniapps?: PackagePath[]
platform?: NativePlatform
prod?: boolean
} = {}) => {
await logErrorAndExitIfNotSatisfied({
bundleStoreAccessKeyIsSet: {
extraErrorMessage: `You can use:
'ern bundlestore use <accessKey>' to use an existing store OR
'ern bundlestore create <storeName>' to create a new store`,
},
bundleStoreUrlSetInCauldron: {
extraErrorMessage: `You should add bundleStore config in your Cauldron`,
},
})
const platforms: NativePlatform[] = platform ? [platform] : ['android', 'ios']
const cauldron = await getActiveCauldron()
const bundleStoreUrl = (await cauldron.getBundleStoreConfig()).url
const engine = new BundleStoreEngine(bundleStoreUrl)
if (fromPackager) {
for (const curPlatform of platforms) {
const bundleId = await kax
.task(`Uploading ${curPlatform} bundle`)
.run(engine.uploadFromPackager({ platform: curPlatform, dev: !prod }))
log.info(`Successfully uploaded ${curPlatform} bundle [id: ${bundleId}]`)
}
} else {
// Full native application descriptor was not provided.
// Ask the user to select a completeNapDescriptor from a list
// containing all the native applications versions in the cauldron
// Not needed if miniapps are directly provided
if (!descriptor && !miniapps) {
descriptor = await askUserToChooseANapDescriptorFromCauldron()
}
let pathToYarnLock
let resolutions
if (descriptor) {
await logErrorAndExitIfNotSatisfied({
napDescriptorExistInCauldron: {
descriptor,
extraErrorMessage:
'You cannot create a composite for a non-existing native application version.',
},
})
miniapps = await cauldron.getContainerMiniApps(descriptor, {
favorGitBranches: !!fromGitBranches,
})
jsApiImpls = await cauldron.getContainerJsApiImpls(descriptor)
const containerGenConfig = await cauldron.getContainerGeneratorConfig(
descriptor
)
if (!containerGenConfig || !containerGenConfig.bypassYarnLock) {
pathToYarnLock = await cauldron.getPathToYarnLock(
descriptor,
'container'
)
} else {
log.debug(
'Bypassing yarn.lock usage as bypassYarnLock flag is set in config'
)
}
const compositeGenConfig = await cauldron.getCompositeGeneratorConfig(
descriptor
)
baseComposite =
baseComposite ||
(compositeGenConfig && compositeGenConfig.baseComposite)
resolutions = compositeGenConfig && compositeGenConfig.resolutions
}
const compositeDir = createTmpDir()
await kax.task('Generating Composite').run(
generateComposite({
baseComposite,
extraJsDependencies,
jsApiImplDependencies: jsApiImpls,
miniApps: miniapps!,
outDir: compositeDir,
pathToYarnLock,
resolutions,
})
)
for (const curPlatform of platforms) {
const outDir = createTmpDir()
const bundlePath = path.join(outDir, 'index.bundle')
const sourceMapPath = path.join(outDir, 'index.map')
await kax.task(`Bundling MiniApps for ${curPlatform}`).run(
bundleMiniAppsFromComposite({
bundleOutput: bundlePath,
compositeDir,
dev: !prod,
outDir,
platform: curPlatform,
sourceMapOutput: path.join(outDir, 'index.map'),
})
)
const bundleId = await kax
.task(`Uploading ${curPlatform} bundle`)
.run(
engine.upload({ bundlePath, platform: curPlatform, sourceMapPath })
)
log.info(`Successfully uploaded ${curPlatform} bundle [id: ${bundleId}]`)
}
}
}
export const handler = tryCatchWrap(commandHandler)