-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathcodepush.ts
186 lines (158 loc) · 6.06 KB
/
codepush.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
import {exec} from 'child_process'
import {Cli, Command, Option} from 'clipanion'
import {FIPS_ENV_VAR, FIPS_IGNORE_ERROR_ENV_VAR} from '../../constants'
import {toBoolean} from '../../helpers/env'
import {enableFips} from '../../helpers/fips'
import {CodepushHistoryCommandError, CodepushHistoryParseError, NoCodepushReleaseError} from './errors'
import {RNPlatform, RN_SUPPORTED_PLATFORMS} from './interfaces'
import {UploadCommand} from './upload'
export class CodepushCommand extends Command {
public static paths = [['react-native', 'codepush']]
public static usage = Command.Usage({
category: 'RUM',
description: 'Upload your React Native Codepush bundle and sourcemaps to Datadog.',
details: `
This command will upload React Native Codepush sourcemaps and their corresponding JavaScript bundle to Datadog in order to un-minify front-end stack traces received by Datadog.\n
See README for details.
`,
examples: [
[
'Upload ios staging sourcemaps for Company/AppNameiOS',
'datadog-ci react-native codepush --platform ios --service com.company.app --bundle ./build/main.jsbundle --sourcemap ./build/main.jsbundle.map --app Company/AppNameiOS --deployment Staging',
],
[
'Upload android production sourcemaps for Company/AppNameAndroid',
'datadog-ci react-native codepush --platform android --service com.company.app --bundle ./build/index.android.bundle --sourcemap ./build/index.android.bundle.map --app Company/AppNameAndroid --deployment Production',
],
],
})
private appCenterAppName = Option.String('--app')
private appCenterDeployment = Option.String('--deployment')
/**
* There should not be multiple uploads with the same version in the case
* of codepush, so we can go with a default of "1".
*/
private buildVersion = Option.String('--build-version', '1')
private bundle = Option.String('--bundle')
private configPath = Option.String('--config')
private disableGit = Option.Boolean('--disable-git')
private dryRun = Option.Boolean('--dry-run', false)
private maxConcurrency = Option.String('--max-concurrency', '20')
private platform?: RNPlatform = Option.String('--platform')
private projectPath = Option.String('--project-path')
private removeSourcesContent = Option.Boolean('--remove-sources-content')
private repositoryURL = Option.String('--repository-url')
private service = Option.String('--service')
private sourcemap = Option.String('--sourcemap')
private releaseVersion?: string
private fips = Option.Boolean('--fips', false)
private fipsIgnoreError = Option.Boolean('--fips-ignore-error', false)
private config = {
fips: toBoolean(process.env[FIPS_ENV_VAR]) ?? false,
fipsIgnoreError: toBoolean(process.env[FIPS_IGNORE_ERROR_ENV_VAR]) ?? false,
}
public async execute() {
enableFips(this.fips || this.config.fips, this.fipsIgnoreError || this.config.fipsIgnoreError)
if (!this.service) {
this.context.stderr.write('Missing service\n')
return 1
}
if (!this.platform) {
this.context.stderr.write('Missing platform\n')
return 1
}
if (!RN_SUPPORTED_PLATFORMS.includes(this.platform)) {
this.context.stderr.write(
`Platform ${this.platform} is not supported.\nSupported platforms are ios and android.\n`
)
return 1
}
if (!this.sourcemap) {
this.context.stderr.write('Missing sourcemap file path\n')
return 1
}
if (!this.appCenterAppName) {
this.context.stderr.write('Missing AppCenter app name\n')
return 1
}
if (!this.appCenterDeployment) {
this.context.stderr.write('Missing AppCenter deployment\n')
return 1
}
this.releaseVersion = await this.getReleaseVersionFromCodepushHistory(
this.appCenterAppName,
this.appCenterDeployment
)
// Run upload script in the background
const cli = new Cli()
cli.register(UploadCommand)
const uploadCommand = [
'react-native',
'upload',
'--platform',
this.platform,
'--release-version',
this.releaseVersion,
'--build-version',
this.buildVersion,
'--service',
this.service,
'--sourcemap',
this.sourcemap,
]
if (this.bundle) {
uploadCommand.push('--bundle', this.bundle)
}
if (this.configPath) {
uploadCommand.push('--config')
uploadCommand.push(this.configPath)
}
if (this.maxConcurrency) {
uploadCommand.push('--max-concurrency')
uploadCommand.push(this.maxConcurrency.toString())
}
if (this.projectPath) {
uploadCommand.push('--project-path')
uploadCommand.push(this.projectPath)
}
if (this.repositoryURL) {
uploadCommand.push('--repository-url')
uploadCommand.push(this.repositoryURL)
}
if (this.disableGit) {
uploadCommand.push('--disable-git')
}
if (this.removeSourcesContent) {
uploadCommand.push('--remove-sources-content')
}
if (this.dryRun) {
uploadCommand.push('--dry-run')
}
return cli.run(uploadCommand, this.context)
}
private getReleaseVersionFromCodepushHistory = async (
appCenterAppName: string,
appCenterDeployment: string
): Promise<string> => {
const command = `appcenter codepush deployment history ${appCenterDeployment} --app ${appCenterAppName} --output json`
return new Promise((resolve, reject) => {
exec(command, (error, stdout, stderr) => {
if (error) {
reject(new CodepushHistoryCommandError(stderr, command))
return
}
try {
const history = JSON.parse(stdout)
if (history.length === 0) {
reject(new NoCodepushReleaseError(appCenterAppName, appCenterDeployment))
}
const lastDeployment = history[history.length - 1]
const [lastCodePushLabel, _, lastVersion] = lastDeployment
resolve(`${lastVersion}-codepush.${lastCodePushLabel}`)
} catch (parseError) {
reject(new CodepushHistoryParseError(`Error parsing codepush history: \n${parseError}\n${stdout}`))
}
})
})
}
}