forked from bitrise-steplib/steps-expo-detach
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
300 lines (251 loc) · 7.66 KB
/
main.go
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
package main
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"strings"
"github.com/bitrise-io/go-utils/command"
"github.com/bitrise-io/go-utils/errorutil"
"github.com/bitrise-io/go-utils/fileutil"
"github.com/bitrise-io/go-utils/log"
"github.com/bitrise-io/go-utils/pathutil"
"github.com/bitrise-tools/go-steputils/stepconf"
"github.com/bitrise-tools/xcode-project/serialized"
)
// Config ...
type Config struct {
Workdir string `env:"project_path,dir"`
ExpoCLIVersion string `env:"expo_cli_verson,required"`
UserName string `env:"user_name"`
Password stepconf.Secret `env:"password"`
RunPublish string `env:"run_publish"`
OverrideReactNativeVersion string `env:"override_react_native_version"`
}
// EjectMethod if the project is using Expo SDK and you choose the "bare" --eject-method those imports will stop working.
type EjectMethod string
// const ...
const (
Bare EjectMethod = "bare"
ExpoKit EjectMethod = "expoKit"
)
// Expo CLI
type Expo struct {
Version string
Method EjectMethod
Workdir string
}
// installExpoCLI runs the install npm command to install the expo-cli
func (e Expo) installExpoCLI() error {
args := []string{"install", "-g"}
if e.Version != "latest" {
args = append(args, "expo-cli@"+e.Version)
} else {
args = append(args, "expo-cli")
}
cmd := command.New("npm", args...)
cmd.SetStdout(os.Stdout)
cmd.SetStderr(os.Stderr)
log.Donef("$ " + cmd.PrintableCommandArgs())
return cmd.Run()
}
// Login with your Expo account
func (e Expo) login(userName string, password stepconf.Secret) error {
args := []string{"login", "--non-interactive", "-u", userName, "-p", string(password), "force"}
cmd := command.New("expo", args...)
cmd.SetStdout(os.Stdout)
cmd.SetStderr(os.Stderr)
nonFilteredArgs := ("$ " + cmd.PrintableCommandArgs())
fileredArgs := strings.Replace(nonFilteredArgs, string(password), "[REDACTED]", -1)
log.Printf(fileredArgs)
return cmd.Run()
}
// Logout from your Expo account
func (e Expo) logout() error {
cmd := command.New("expo", "logout", "--non-interactive", "force")
cmd.SetStdout(os.Stdout)
cmd.SetStderr(os.Stderr)
log.Printf("$ " + cmd.PrintableCommandArgs())
return cmd.Run()
}
// Eject command creates Xcode and Android Studio projects for your app.
func (e Expo) eject() error {
args := []string{"eject", "--non-interactive", "--eject-method", string(e.Method), "force"}
cmd := command.New("expo", args...)
cmd.SetStdout(os.Stdout)
cmd.SetStderr(os.Stderr)
if e.Workdir != "" {
cmd.SetDir(e.Workdir)
}
log.Donef("$ " + cmd.PrintableCommandArgs())
return cmd.Run()
}
func (e Expo) publish() error {
args := []string{"publish", "--non-interactive", "force"}
cmd := command.New("expo", args...)
cmd.SetStdout(os.Stdout)
cmd.SetStderr(os.Stderr)
if e.Workdir != "" {
cmd.SetDir(e.Workdir)
}
log.Donef("$ " + cmd.PrintableCommandArgs())
return cmd.Run()
}
func parsePackageJSON(pth string) (serialized.Object, error) {
b, err := fileutil.ReadBytesFromFile(pth)
if err != nil {
return nil, fmt.Errorf("Failed to read package.json file: %s", err)
}
var packages serialized.Object
if err := json.Unmarshal(b, &packages); err != nil {
return nil, fmt.Errorf("Failed to parse package.json file: %s", err)
}
return packages, nil
}
func savePackageJSON(packages serialized.Object, pth string) error {
b, err := json.MarshalIndent(packages, "", " ")
if err != nil {
return fmt.Errorf("Failed to serialize modified package.json file: %s", err)
}
if err := fileutil.WriteBytesToFile(pth, b); err != nil {
return fmt.Errorf("Failed to write modified package.json file: %s", err)
}
return nil
}
func validateUserNameAndpassword(userName string, password stepconf.Secret) error {
if userName != "" && string(password) == "" {
return fmt.Errorf("user name is specified but password is not provided")
}
if userName == "" && string(password) != "" {
return fmt.Errorf("password is specified but is not provided user name")
}
return nil
}
func failf(format string, v ...interface{}) {
log.Errorf(format, v...)
os.Exit(1)
}
func main() {
var cfg Config
if err := stepconf.Parse(&cfg); err != nil {
failf("Issue with input: %s", err)
}
fmt.Println()
stepconf.Print(cfg)
if err := validateUserNameAndpassword(cfg.UserName, cfg.Password); err != nil {
failf("Input validation error: %s", err)
}
//
// Select the --eject-method
ejectMethod := Bare
fmt.Println()
log.Infof("Define --eject-method")
{
if cfg.UserName != "" {
ejectMethod = ExpoKit
log.Printf("Expo account credentials have provided => Set the --eject-method to %s", ejectMethod)
} else {
log.Printf("Expo account credentials have not provided => Set the --eject-method to %s", ejectMethod)
}
}
e := Expo{
Version: cfg.ExpoCLIVersion,
Method: ejectMethod,
Workdir: cfg.Workdir,
}
//
// Install expo-cli
fmt.Println()
log.Infof("Install Expo CLI version: %s", cfg.ExpoCLIVersion)
{
if err := e.installExpoCLI(); err != nil {
failf("Failed to install the selected (%s) version for Expo CLI, error: %s", cfg.ExpoCLIVersion, err)
}
}
//
// Logging in the user to the Expo account
fmt.Println()
log.Infof("Login to Expo")
{
switch ejectMethod {
case ExpoKit:
if err := e.login(cfg.UserName, cfg.Password); err != nil {
failf("Failed to log in to your provided Expo account, error: %s", err)
}
case Bare:
log.Printf("--eject-method has been set to bare => Skip...")
}
}
//
// Logging out the user from the Expo account at the end of the step (even if it fails)
defer func() {
fmt.Println()
log.Infof("Logging out from Expo")
{
if e.Method == ExpoKit {
if err := e.logout(); err != nil {
log.Warnf("Failed to log out from your Expo account, error: %s", err)
}
} else if e.Method == ExpoKit {
log.Printf("Logout input was set to false => Skip...")
} else {
log.Printf("You were not logged in => Skip...")
}
}
}()
//
// Eject project via the Expo CLI
fmt.Println()
log.Infof("Eject project")
{
if err := e.eject(); err != nil {
failf("Failed to eject project: %s", err)
}
}
fmt.Println()
log.Donef("Successfully ejected your project")
if cfg.RunPublish == "yes" {
fmt.Println()
log.Infof("Running expo publish")
if err := e.publish(); err != nil {
failf("Failed to publish project: %s", err)
}
}
if cfg.OverrideReactNativeVersion != "" {
//
// Force certain version of React Native in package.json file
fmt.Println()
log.Infof("Set react-native dependency version: %s", cfg.OverrideReactNativeVersion)
packageJSONPth := filepath.Join(cfg.Workdir, "package.json")
packages, err := parsePackageJSON(packageJSONPth)
if err != nil {
failf(err.Error())
}
deps, err := packages.Object("dependencies")
if err != nil {
failf("Failed to parse dependencies from package.json file: %s", err)
}
deps["react-native"] = cfg.OverrideReactNativeVersion
packages["dependencies"] = deps
if err := savePackageJSON(packages, packageJSONPth); err != nil {
failf(err.Error())
}
//
// Install new node dependencies
log.Printf("install new node dependencies")
nodeDepManager := "npm"
if exist, err := pathutil.IsPathExists(filepath.Join(cfg.Workdir, "yarn.lock")); err != nil {
log.Warnf("Failed to check if yarn.lock file exists in the workdir: %s", err)
} else if exist {
nodeDepManager = "yarn"
}
cmd := command.New(nodeDepManager, "install")
out, err := cmd.RunAndReturnTrimmedCombinedOutput()
if err != nil {
if errorutil.IsExitStatusError(err) {
failf("%s failed: %s", cmd.PrintableCommandArgs(), out)
}
failf("%s failed: %s", cmd.PrintableCommandArgs(), err)
}
}
}