github.com/hazelops/ize@v1.1.12-0.20230915191306-97d7c0e48f11/internal/manager/serverless/native.go (about) 1 package serverless 2 3 import ( 4 "fmt" 5 "io" 6 "os" 7 "os/exec" 8 "path/filepath" 9 "strings" 10 11 "github.com/hazelops/ize/pkg/term" 12 "github.com/sirupsen/logrus" 13 ) 14 15 func (sls *Manager) runNpmInstall(w io.Writer) error { 16 nvmDir := os.Getenv("NVM_DIR") 17 if len(nvmDir) == 0 { 18 nvmDir = "$HOME/.nvm" 19 } 20 21 command := fmt.Sprintf("source %s/nvm.sh && nvm use %s && npm install --save-dev", nvmDir, sls.App.NodeVersion) 22 23 if sls.App.UseYarn { 24 command = npmToYarn(command) 25 } 26 27 logrus.SetOutput(w) 28 logrus.Debugf("command: %s", command) 29 30 cmd := exec.Command("bash", "-c", command) 31 cmd.Stdout = w 32 cmd.Stderr = w 33 cmd.Dir = filepath.Join(sls.App.Path) 34 err := cmd.Run() 35 if err != nil { 36 return err 37 } 38 39 return nil 40 } 41 42 func (sls *Manager) nvm(w io.Writer, command string) error { 43 nvmDir := os.Getenv("NVM_DIR") 44 if len(nvmDir) == 0 { 45 nvmDir = "$HOME/.nvm" 46 } 47 err := sls.readNvmrc() 48 if err != nil { 49 return err 50 } 51 52 cmd := exec.Command("bash", "-c", 53 fmt.Sprintf("source %s/nvm.sh && nvm install %s && %s", nvmDir, sls.App.NodeVersion, command), 54 ) 55 56 return term.New( 57 term.WithDir(sls.App.Path), 58 term.WithStdout(w), 59 term.WithStderr(w), 60 ).InteractiveRun(cmd) 61 } 62 63 func (sls *Manager) readNvmrc() error { 64 _, err := os.Stat(filepath.Join(sls.App.Path, ".nvmrc")) 65 if os.IsNotExist(err) { 66 } else { 67 file, err := os.ReadFile(filepath.Join(sls.App.Path, ".nvmrc")) 68 if err != nil { 69 return fmt.Errorf("can't read .nvmrc: %w", err) 70 } 71 sls.App.NodeVersion = strings.TrimSpace(string(file)) 72 } 73 return nil 74 } 75 76 func (sls *Manager) runNvm(w io.Writer) error { 77 nvmDir := os.Getenv("NVM_DIR") 78 if len(nvmDir) == 0 { 79 nvmDir = "$HOME/.nvm" 80 } 81 82 err := sls.readNvmrc() 83 if err != nil { 84 return err 85 } 86 87 command := fmt.Sprintf("source %s/nvm.sh && nvm install %s", nvmDir, sls.App.NodeVersion) 88 89 logrus.SetOutput(w) 90 logrus.Debugf("command: %s", command) 91 92 cmd := exec.Command("bash", "-c", command) 93 94 return term.New( 95 term.WithDir(sls.App.Path), 96 term.WithStdout(w), 97 term.WithStderr(w), 98 ).InteractiveRun(cmd) 99 } 100 101 func (sls *Manager) runDeploy(w io.Writer) error { 102 103 nvmDir := os.Getenv("NVM_DIR") 104 if len(nvmDir) == 0 { 105 nvmDir = "$HOME/.nvm" 106 } 107 var command string 108 109 // SLS v3 has breaking changes in syntax 110 if sls.App.ServerlessVersion == "3" { 111 command = fmt.Sprintf( 112 `source %s/nvm.sh && 113 nvm use %s && 114 npx serverless deploy \ 115 --config=%s \ 116 --param="service=%s" \ 117 --region=%s \ 118 --aws-profile=%s \ 119 --stage=%s \ 120 --verbose`, 121 nvmDir, sls.App.NodeVersion, sls.App.File, 122 sls.App.Name, sls.App.AwsRegion, 123 sls.App.AwsProfile, sls.Project.Env) 124 } else { 125 command = fmt.Sprintf( 126 `source %s/nvm.sh && 127 nvm use %s && 128 npx serverless deploy \ 129 --config %s \ 130 --service %s \ 131 --verbose \ 132 --region %s \ 133 --aws-profile %s \ 134 --stage %s`, 135 nvmDir, sls.App.NodeVersion, sls.App.File, 136 sls.App.Name, sls.App.AwsRegion, 137 sls.App.AwsProfile, sls.Project.Env) 138 } 139 140 if sls.App.UseYarn { 141 command = npmToYarn(command) 142 } 143 144 if sls.App.Force { 145 command += ` \ 146 --force` 147 } 148 149 logrus.SetOutput(w) 150 logrus.Debugf("command: %s", command) 151 152 cmd := exec.Command("bash", "-c", command) 153 154 return term.New( 155 term.WithDir(sls.App.Path), 156 term.WithStdout(w), 157 term.WithStderr(w), 158 ).InteractiveRun(cmd) 159 } 160 161 func (sls *Manager) runRemove(w io.Writer) error { 162 163 nvmDir := os.Getenv("NVM_DIR") 164 if len(nvmDir) == 0 { 165 nvmDir = "$HOME/.nvm" 166 } 167 168 var command string 169 170 // SLS v3 has breaking changes in syntax 171 if sls.App.ServerlessVersion == "3" { 172 command = fmt.Sprintf( 173 `source %s/nvm.sh && \ 174 nvm use %s && \ 175 npx serverless remove \ 176 --config=%s \ 177 --param="service=%s" \ 178 --region=%s \ 179 --aws-profile=%s \ 180 --stage=%s \ 181 --verbose`, 182 nvmDir, sls.App.NodeVersion, sls.App.File, 183 sls.App.Name, sls.App.AwsRegion, 184 sls.App.AwsProfile, sls.Project.Env) 185 } else { 186 command = fmt.Sprintf( 187 `source %s/nvm.sh && \ 188 nvm use %s && \ 189 npx serverless remove \ 190 --config %s \ 191 --service %s \ 192 --verbose \ 193 --region %s \ 194 --aws-profile %s \ 195 --stage %s`, 196 nvmDir, sls.App.NodeVersion, sls.App.File, 197 sls.App.Name, sls.App.AwsRegion, 198 sls.App.AwsProfile, sls.Project.Env) 199 } 200 201 if sls.App.UseYarn { 202 command = npmToYarn(command) 203 } 204 205 logrus.SetOutput(w) 206 logrus.Debugf("command: %s", command) 207 208 cmd := exec.Command("bash", "-c", command) 209 210 return term.New( 211 term.WithDir(sls.App.Path), 212 term.WithStdout(w), 213 term.WithStderr(w), 214 ).InteractiveRun(cmd) 215 } 216 217 func (sls *Manager) runCreateDomain(w io.Writer) error { 218 nvmDir := os.Getenv("NVM_DIR") 219 if len(nvmDir) == 0 { 220 nvmDir = "$HOME/.nvm" 221 } 222 223 command := fmt.Sprintf( 224 `source %s/nvm.sh && \ 225 nvm use %s && \ 226 npx serverless create_domain \ 227 --verbose \ 228 --region %s \ 229 --aws-profile %s \ 230 --stage %s`, 231 nvmDir, sls.App.NodeVersion, sls.App.AwsRegion, 232 sls.App.AwsProfile, sls.Project.Env) 233 234 if sls.App.UseYarn { 235 command = npmToYarn(command) 236 } 237 238 logrus.SetOutput(w) 239 logrus.Debugf("command: %s", command) 240 241 cmd := exec.Command("bash", "-c", command) 242 243 return term.New( 244 term.WithDir(sls.App.Path), 245 term.WithStdout(w), 246 term.WithStderr(w), 247 ).InteractiveRun(cmd) 248 } 249 250 func (sls *Manager) runRemoveDomain(w io.Writer) error { 251 nvmDir := os.Getenv("NVM_DIR") 252 if len(nvmDir) == 0 { 253 nvmDir = "$HOME/.nvm" 254 } 255 256 command := fmt.Sprintf( 257 `source %s/nvm.sh && \ 258 nvm use %s && \ 259 npx serverless delete_domain \ 260 --verbose \ 261 --region %s \ 262 --aws-profile %s \ 263 --stage %s`, 264 nvmDir, sls.App.NodeVersion, sls.App.AwsRegion, 265 sls.App.AwsProfile, sls.Project.Env) 266 267 if sls.App.UseYarn { 268 command = npmToYarn(command) 269 } 270 271 logrus.SetOutput(w) 272 logrus.Debugf("command: %s", command) 273 274 cmd := exec.Command("bash", "-c", command) 275 276 return term.New( 277 term.WithDir(sls.App.Path), 278 term.WithStdout(w), 279 term.WithStderr(w), 280 ).InteractiveRun(cmd) 281 } 282 283 func npmToYarn(cmd string) string { 284 cmd = strings.ReplaceAll(cmd, "npm", "yarn") 285 return strings.ReplaceAll(cmd, "npx", "yarn") 286 }