Skip to content

Commit 6067b28

Browse files
author
gagik
committed
REALMC-9363 - Removed client-side transpilation.
1 parent 1d37fd6 commit 6067b28

File tree

5 files changed

+87
-137
lines changed

5 files changed

+87
-137
lines changed

internal/cloud/realm/client.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ type Client interface {
3030
ImportDependencies(groupID, appID, uploadPath string) error
3131
Diff(groupID, appID string, appData interface{}) ([]string, error)
3232
DiffDependencies(groupID, appID, uploadPath string) (DependenciesDiff, error)
33+
GetDependenciesStatus(groupID, appID string) (DependenciesStatus, error)
3334

3435
CreateApp(groupID, name string, meta AppMeta) (App, error)
3536
DeleteApp(groupID, appID string) error

internal/cloud/realm/dependencies.go

Lines changed: 64 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"mime/multipart"
1111
"net/http"
1212
"os"
13+
"time"
1314

1415
"github.com/10gen/realm-cli/internal/utils/api"
1516
)
@@ -18,10 +19,42 @@ const (
1819
dependenciesPathPattern = appPathPattern + "/dependencies"
1920
dependenciesArchivePathPattern = dependenciesPathPattern + "/archive"
2021
dependenciesDiffPathPattern = dependenciesPathPattern + "/diff"
22+
dependenciesStatusPathPattern = dependenciesPathPattern + "/status"
2123

2224
paramFile = "file"
2325
)
2426

27+
// AppDependenciesInfo is used to get information from a dependencies status request
28+
type AppDependenciesInfo struct {
29+
Status DependenciesStatus `json:"status"`
30+
}
31+
32+
// represents possible dependency statuses
33+
type DependenciesStatus string
34+
35+
// set of known dependencies statuses
36+
const (
37+
DependenciesStatusCreated DependenciesStatus = "created"
38+
DependenciesStatusSuccessful DependenciesStatus = "successful"
39+
DependenciesStatusFailed DependenciesStatus = "failed"
40+
)
41+
42+
func (c *client) GetDependenciesStatus(groupID, appID string) (DependenciesStatus, error) {
43+
res, err := c.do(
44+
http.MethodGet,
45+
fmt.Sprintf(dependenciesStatusPathPattern, groupID, appID),
46+
api.RequestOptions{},
47+
)
48+
if err != nil {
49+
return "", err
50+
}
51+
var transpilation AppDependenciesInfo
52+
if err := json.NewDecoder(res.Body).Decode(&transpilation); err != nil {
53+
return "", err
54+
}
55+
return transpilation.Status, nil
56+
}
57+
2558
func (c *client) ImportDependencies(groupID, appID, uploadPath string) error {
2659
file, fileErr := os.Open(uploadPath)
2760
if fileErr != nil {
@@ -37,9 +70,9 @@ func (c *client) ImportDependencies(groupID, appID, uploadPath string) error {
3770
body := &bytes.Buffer{}
3871
w := multipart.NewWriter(body)
3972

40-
form, formErr := w.CreateFormFile(paramFile, fileInfo.Name())
41-
if formErr != nil {
42-
return formErr
73+
form, err := w.CreateFormFile(paramFile, fileInfo.Name())
74+
if err != nil {
75+
return err
4376
}
4477

4578
if _, err := io.Copy(form, file); err != nil {
@@ -49,20 +82,43 @@ func (c *client) ImportDependencies(groupID, appID, uploadPath string) error {
4982
return err
5083
}
5184

52-
res, resErr := c.do(
53-
http.MethodPost,
85+
res, err := c.do(
86+
http.MethodPut,
5487
fmt.Sprintf(dependenciesPathPattern, groupID, appID),
5588
api.RequestOptions{
5689
Body: body,
5790
ContentType: w.FormDataContentType(),
5891
},
5992
)
60-
if resErr != nil {
61-
return resErr
93+
if err != nil {
94+
return err
6295
}
6396
if res.StatusCode != http.StatusNoContent {
64-
return api.ErrUnexpectedStatusCode{"import dependencies", res.StatusCode}
97+
return fmt.Errorf("import got invalid status code %d", res.StatusCode)
98+
}
99+
100+
waitForTranspilation := func() error {
101+
status, err := c.GetDependenciesStatus(groupID, appID)
102+
if err != nil {
103+
return err
104+
}
105+
for status == DependenciesStatusCreated {
106+
time.Sleep(time.Second)
107+
status, err = c.GetDependenciesStatus(groupID, appID)
108+
if err != nil {
109+
return err
110+
}
111+
if status == DependenciesStatusFailed {
112+
return fmt.Errorf("failed to transpile dependencies")
113+
}
114+
}
115+
return nil
116+
}
117+
118+
if err := waitForTranspilation(); err != nil {
119+
return err
65120
}
121+
66122
return nil
67123
}
68124

internal/cloud/realm/dependencies_test.go

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,15 @@ func TestRealmDependencies(t *testing.T) {
2828
assert.Nil(t, err)
2929

3030
uploadPath := filepath.Join(wd, "testdata/dependencies_upload.zip")
31+
32+
_, err = client.GetDependenciesStatus(groupID, app.ID)
33+
assert.NotNilf(t, err, "dependency status must return error before import")
3134
assert.Nil(t, client.ImportDependencies(groupID, app.ID, uploadPath))
3235

36+
status, err := client.GetDependenciesStatus(groupID, app.ID)
37+
assert.Equalf(t, status, realm.DependenciesStatusSuccessful, "must wait until dependency status is successful during import")
38+
assert.Nil(t, err)
39+
3340
t.Run("and wait for those dependencies to be deployed to the app", func(t *testing.T) {
3441
deployments, err := client.Deployments(groupID, app.ID)
3542
assert.Nil(t, err)
@@ -78,7 +85,8 @@ func TestRealmDependencies(t *testing.T) {
7885
assert.Nil(t, expectedDepsErr)
7986
defer expectedDeps.Close()
8087

81-
assert.Equalf(t, parseZipArchive(t, expectedDeps), parseZipArchive(t, actualDeps), "expected archives to match")
88+
// make sure same files are present; contents may be different because of transpilation
89+
assert.Equal(t, getZipFileNames(t, expectedDeps), getZipFileNames(t, actualDeps))
8290
})
8391
})
8492

@@ -97,7 +105,7 @@ func TestRealmDependencies(t *testing.T) {
97105
})
98106
}
99107

100-
func parseZipArchive(t *testing.T, file *os.File) map[string]string {
108+
func getZipFileNames(t *testing.T, file *os.File) map[string]bool {
101109
t.Helper()
102110

103111
fileInfo, err := file.Stat()
@@ -106,5 +114,13 @@ func parseZipArchive(t *testing.T, file *os.File) map[string]string {
106114
zipPkg, err := zip.NewReader(file, fileInfo.Size())
107115
assert.Nil(t, err)
108116

109-
return parseZipPkg(t, zipPkg)
117+
fileNames := make(map[string]bool, len(zipPkg.File))
118+
119+
for _, file := range zipPkg.File {
120+
if file.FileInfo().IsDir() {
121+
continue
122+
}
123+
fileNames[file.Name] = true
124+
}
125+
return fileNames
110126
}

internal/local/dependencies.go

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package local
22

33
import (
44
"archive/zip"
5-
"context"
65
"fmt"
76
"io"
87
"io/ioutil"
@@ -11,7 +10,6 @@ import (
1110
"time"
1211

1312
"github.com/10gen/realm-cli/internal/terminal"
14-
1513
"github.com/briandowns/spinner"
1614
)
1715

@@ -50,7 +48,7 @@ func FindAppDependencies(path string) (Dependencies, error) {
5048
}
5149

5250
// PrepareUpload prepares a dependencies upload package by creating a .zip file
53-
// containing the specified archive's transpiled file contents in a tempmorary directory
51+
// containing the specified archive's file contents in a temporary directory
5452
// and returns that file path
5553
func (d Dependencies) PrepareUpload() (string, error) {
5654
file, err := os.Open(d.ArchivePath)
@@ -64,11 +62,6 @@ func (d Dependencies) PrepareUpload() (string, error) {
6462
return "", err
6563
}
6664

67-
transpiler, err := newDefaultTranspiler()
68-
if err != nil {
69-
return "", err
70-
}
71-
7265
dir, err := ioutil.TempDir("", "") // uses os.TempDir and guarantees existence and proper permissions
7366
if err != nil {
7467
return "", err
@@ -129,16 +122,8 @@ func (d Dependencies) PrepareUpload() (string, error) {
129122
}
130123
}
131124

132-
ctx, cancel := context.WithCancel(context.Background())
133-
defer cancel()
134-
135-
transpiledSources, err := transpiler.Transpile(ctx, jsSources...)
136-
if err != nil {
137-
return "", err
138-
}
139-
140-
for i, transpiledSource := range transpiledSources {
141-
if err := writeFile(jsPaths[i], []byte(transpiledSource)); err != nil {
125+
for i, jsSource := range jsSources {
126+
if err := writeFile(jsPaths[i], []byte(jsSource)); err != nil {
142127
return "", err
143128
}
144129
}

internal/local/transpiler.go

Lines changed: 0 additions & 108 deletions
This file was deleted.

0 commit comments

Comments
 (0)