Skip to content

Commit

Permalink
Merge pull request #254 from cdnjs/sven/fix-duplicated-file-output
Browse files Browse the repository at this point in the history
fix: ignore compression for file already in output
  • Loading branch information
xtuc authored Apr 15, 2022
2 parents e448458 + 23da46a commit 2b7cddc
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 14 deletions.
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ bin/git-sync:
bin/process-version-host:
go build $(GO_BUILD_ARGS) -o bin/process-version-host ./cmd/process-version-host

bin/process-version:
go build $(GO_BUILD_ARGS) -o bin/process-version ./cmd/process-version

.PHONY: schema
schema:
./bin/packages human > schema_human.json
Expand Down
16 changes: 12 additions & 4 deletions cmd/process-version/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,20 @@ func (j optimizeJob) emitFromWorkspace(src string) {

if _, ok := doNotCompress[ext]; !ok {
outBr := fmt.Sprintf("%s.br", dest)
compress.Brotli11CLI(j.Ctx, src, outBr)
log.Printf("br %s -> %s\n", src, outBr)
if _, err := os.Stat(outBr); err == nil {
log.Printf("file %s already exists at the output\n", outBr)
} else {
compress.Brotli11CLI(j.Ctx, src, outBr)
log.Printf("br %s -> %s\n", src, outBr)
}

outGz := fmt.Sprintf("%s.gz", dest)
compress.Gzip9Native(j.Ctx, src, outGz)
log.Printf("gz %s -> %s\n", src, outGz)
if _, err := os.Stat(outGz); err == nil {
log.Printf("file %s already exists at the output\n", outGz)
} else {
compress.Gzip9Native(j.Ctx, src, outGz)
log.Printf("gz %s -> %s\n", src, outGz)
}
} else {
if err := copyFile(src, dest); err != nil {
log.Fatalf("failed to copy file: %s", err)
Expand Down
14 changes: 6 additions & 8 deletions compress/js.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,16 @@ var (

// Js performs a compression of the file.
func Js(ctx context.Context, file string) *string {
if strings.HasSuffix(file, ".min.js") {
log.Printf("%s is already compressed\n", file)
return nil
}

ext := path.Ext(file)
outfile := file[0:len(file)-len(ext)] + ".min.js"

// compressed file already exists, ignore
if _, err := os.Stat(outfile); err == nil {
log.Printf("compressed file already exists: %s\n", outfile)
return nil
}

// Already minified, ignore
if strings.HasSuffix(file, ".min.js") {
log.Printf("%s.min.js compressed file already exists\n", file)
log.Printf("%s already has corresponding compressed file\n", outfile)
return nil
}

Expand Down
3 changes: 1 addition & 2 deletions scripts/test-process-version.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ echo "processing $package $version"

export DOCKER_BUILDKIT=1
mkdir -p /tmp/input /tmp/output

rm -rf /tmp/output/*
rm -rf /tmp/output/* /tmp/input/*

echo "loading new version files"
curl --fail https://storage.googleapis.com/cdnjs-incoming-prod/$package-$version.tgz > /tmp/input/new-version.tgz
Expand Down

0 comments on commit 2b7cddc

Please sign in to comment.