Skip to content

Commit 0854528

Browse files
committed
[WIP] Add option to skip versioning of js & css files
Test the approach mentioned in #12
1 parent 8b2836d commit 0854528

File tree

4 files changed

+123
-13
lines changed

4 files changed

+123
-13
lines changed

glide.lock

Lines changed: 44 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

glide.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,9 @@ package: .
22
import:
33
- package: github.com/aws/aws-sdk-go
44
version: ~1.8.29
5+
- package: github.com/spf13/afero
6+
version: ^1.2.0
7+
- package: golang.org/x/text
8+
version: ^0.3.0
9+
subpackages:
10+
- transform

main.go

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func fatal(format string, a ...interface{}) {
4343
}
4444

4545
// GetFileMd5 returns a checksum for a given file
46-
func GetFileMd5(file *os.File) (string, error) {
46+
func GetFileMd5(file io.Reader) (string, error) {
4747
var fileHash string
4848
hash := md5.New()
4949
if _, err := io.Copy(hash, file); err != nil {
@@ -143,13 +143,31 @@ func UploadFile(file *os.File, filename string, bucket string) (err error) {
143143
return nil
144144
}
145145

146+
func ShouldVersionFile(filename string, skipVersioning bool) (bool) {
147+
ext := filepath.Ext(filename)
148+
return !skipVersioning && (ext == ".js" || ext == ".css")
149+
}
150+
151+
func GetUploadFilename(file io.Reader, filename string, skipVersioning bool) (string, error) {
152+
uploadFilename := filename
153+
if ShouldVersionFile(filename, skipVersioning) {
154+
checksum, errMd5 := GetFileMd5(file)
155+
if errMd5 != nil {
156+
return "", errMd5
157+
}
158+
uploadFilename = GetVersionedFilename(filename, checksum)
159+
}
160+
return uploadFilename, nil
161+
}
162+
146163
// VersionAndUploadFiles will verion files and upload them to s3 and return
147164
// a map of filenames and their version hashes
148165
func VersionAndUploadFiles(
149166
bucket string,
150167
directory string,
151168
filenames []string,
152169
dryRun bool,
170+
skipVersioning bool,
153171
) (map[string]string, error) {
154172
fileVersions := map[string]string{}
155173

@@ -162,14 +180,9 @@ func VersionAndUploadFiles(
162180
}
163181
defer file.Close()
164182

165-
ext := filepath.Ext(filename)
166-
uploadFilename := filename
167-
if ext == ".js" || ext == ".css" {
168-
checksum, errMd5 := GetFileMd5(file)
169-
if errMd5 != nil {
170-
return fileVersions, errMd5
171-
}
172-
uploadFilename = GetVersionedFilename(filename, checksum)
183+
uploadFilename, err := GetUploadFilename(file, filename, skipVersioning)
184+
if err != nil {
185+
return fileVersions, err
173186
}
174187
bucketFilename := path.Join(directory, uploadFilename)
175188
fileURL := GetFileURL(bucket, bucketFilename)
@@ -220,6 +233,7 @@ func main() {
220233
format := flag.String("format", "json", "format of the output [json,csv]")
221234
dryRun := flag.Bool("dry-run", false, "print the output only, skip file uploads and manifest creation")
222235
printVersion := flag.Bool("v", false, "print the current buffer-static-upload version")
236+
skipVersioning := flag.Bool("skip-versioning", false, "skip versioning uploaded files")
223237
flag.Parse()
224238

225239
if *printVersion {
@@ -239,7 +253,7 @@ func main() {
239253
fmt.Printf("Found %d files to upload and version:\n", len(files))
240254

241255
SetupS3Uploader()
242-
fileVersions, err := VersionAndUploadFiles(*s3Bucket, *directory, files, *dryRun)
256+
fileVersions, err := VersionAndUploadFiles(*s3Bucket, *directory, files, *dryRun, *skipVersioning)
243257
if err != nil {
244258
fatal("failed to upload files %s", err)
245259
}

main_test.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package main
22

33
import (
4+
"os"
5+
"fmt"
46
"testing"
7+
"github.com/spf13/afero"
58
)
69

710
func TestGetFileURL(t *testing.T) {
@@ -21,3 +24,49 @@ func TestGetFileURL(t *testing.T) {
2124
}
2225
}
2326
}
27+
28+
29+
func TestShouldVersionFile(t *testing.T) {
30+
tests := []struct {
31+
filename string
32+
skipVersioning bool
33+
expected bool
34+
} {
35+
{ "bundle.js", false, true },
36+
{ "bundle.js", true, false },
37+
{ "assets.css", false, true },
38+
{ "assets.css", true, false },
39+
{ "another.file", false, false },
40+
}
41+
42+
for _, test := range tests {
43+
actual := ShouldVersionFile(test.filename, test.skipVersioning)
44+
if actual != test.expected {
45+
t.Errorf("ShouldVersionFile result was incorrect, got %t, expected %t for filename %s", actual, test.expected, test.filename)
46+
}
47+
}
48+
}
49+
50+
func TestGetUploadFilename(t *testing.T) {
51+
var AppFs = afero.NewMemMapFs()
52+
filename := "bundle.js"
53+
file, _ := AppFs.OpenFile(filename, os.O_CREATE, 0600)
54+
file.WriteString("some JS content")
55+
tests := []struct {
56+
file afero.File
57+
filename string
58+
skipVersioning bool
59+
expected string
60+
} {
61+
{ file, filename, false, "bundle.d41d8cd98f00b204e9800998ecf8427e.js" },
62+
{ file, filename, true, "bundle.js" },
63+
}
64+
65+
for _, test := range tests {
66+
fmt.Print(file.Name())
67+
actual, _ := GetUploadFilename(test.file, test.filename, test.skipVersioning)
68+
if actual != test.expected {
69+
t.Errorf("GetUploadFilename result was incorrect, got %s, expected %s", actual, test.expected)
70+
}
71+
}
72+
}

0 commit comments

Comments
 (0)