Skip to content

Commit

Permalink
[WIP] Add option to skip versioning of js & css files
Browse files Browse the repository at this point in the history
Test the approach mentioned in #12
  • Loading branch information
msanroman committed Jan 22, 2019
1 parent 8b2836d commit 0854528
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 13 deletions.
47 changes: 44 additions & 3 deletions glide.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions glide.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,9 @@ package: .
import:
- package: github.com/aws/aws-sdk-go
version: ~1.8.29
- package: github.com/spf13/afero
version: ^1.2.0
- package: golang.org/x/text
version: ^0.3.0
subpackages:
- transform
34 changes: 24 additions & 10 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func fatal(format string, a ...interface{}) {
}

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

func ShouldVersionFile(filename string, skipVersioning bool) (bool) {
ext := filepath.Ext(filename)
return !skipVersioning && (ext == ".js" || ext == ".css")
}

func GetUploadFilename(file io.Reader, filename string, skipVersioning bool) (string, error) {
uploadFilename := filename
if ShouldVersionFile(filename, skipVersioning) {
checksum, errMd5 := GetFileMd5(file)
if errMd5 != nil {
return "", errMd5
}
uploadFilename = GetVersionedFilename(filename, checksum)
}
return uploadFilename, nil
}

// VersionAndUploadFiles will verion files and upload them to s3 and return
// a map of filenames and their version hashes
func VersionAndUploadFiles(
bucket string,
directory string,
filenames []string,
dryRun bool,
skipVersioning bool,
) (map[string]string, error) {
fileVersions := map[string]string{}

Expand All @@ -162,14 +180,9 @@ func VersionAndUploadFiles(
}
defer file.Close()

ext := filepath.Ext(filename)
uploadFilename := filename
if ext == ".js" || ext == ".css" {
checksum, errMd5 := GetFileMd5(file)
if errMd5 != nil {
return fileVersions, errMd5
}
uploadFilename = GetVersionedFilename(filename, checksum)
uploadFilename, err := GetUploadFilename(file, filename, skipVersioning)
if err != nil {
return fileVersions, err
}
bucketFilename := path.Join(directory, uploadFilename)
fileURL := GetFileURL(bucket, bucketFilename)
Expand Down Expand Up @@ -220,6 +233,7 @@ func main() {
format := flag.String("format", "json", "format of the output [json,csv]")
dryRun := flag.Bool("dry-run", false, "print the output only, skip file uploads and manifest creation")
printVersion := flag.Bool("v", false, "print the current buffer-static-upload version")
skipVersioning := flag.Bool("skip-versioning", false, "skip versioning uploaded files")
flag.Parse()

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

SetupS3Uploader()
fileVersions, err := VersionAndUploadFiles(*s3Bucket, *directory, files, *dryRun)
fileVersions, err := VersionAndUploadFiles(*s3Bucket, *directory, files, *dryRun, *skipVersioning)
if err != nil {
fatal("failed to upload files %s", err)
}
Expand Down
49 changes: 49 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package main

import (
"os"
"fmt"
"testing"
"github.com/spf13/afero"
)

func TestGetFileURL(t *testing.T) {
Expand All @@ -21,3 +24,49 @@ func TestGetFileURL(t *testing.T) {
}
}
}


func TestShouldVersionFile(t *testing.T) {
tests := []struct {
filename string
skipVersioning bool
expected bool
} {
{ "bundle.js", false, true },
{ "bundle.js", true, false },
{ "assets.css", false, true },
{ "assets.css", true, false },
{ "another.file", false, false },
}

for _, test := range tests {
actual := ShouldVersionFile(test.filename, test.skipVersioning)
if actual != test.expected {
t.Errorf("ShouldVersionFile result was incorrect, got %t, expected %t for filename %s", actual, test.expected, test.filename)
}
}
}

func TestGetUploadFilename(t *testing.T) {
var AppFs = afero.NewMemMapFs()
filename := "bundle.js"
file, _ := AppFs.OpenFile(filename, os.O_CREATE, 0600)
file.WriteString("some JS content")
tests := []struct {
file afero.File
filename string
skipVersioning bool
expected string
} {
{ file, filename, false, "bundle.d41d8cd98f00b204e9800998ecf8427e.js" },
{ file, filename, true, "bundle.js" },
}

for _, test := range tests {
fmt.Print(file.Name())
actual, _ := GetUploadFilename(test.file, test.filename, test.skipVersioning)
if actual != test.expected {
t.Errorf("GetUploadFilename result was incorrect, got %s, expected %s", actual, test.expected)
}
}
}

0 comments on commit 0854528

Please sign in to comment.