Skip to content

Commit 45e073b

Browse files
committed
review: make zip extensions more generic
1 parent 5197817 commit 45e073b

7 files changed

Lines changed: 181 additions & 393 deletions

File tree

cmd/browsers.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1940,7 +1940,7 @@ func (b BrowsersCmd) ExtensionsUpload(ctx context.Context, in BrowsersExtensions
19401940
tempZipPath := filepath.Join(os.TempDir(), fmt.Sprintf("kernel-ext-%s.zip", extName))
19411941

19421942
pterm.Info.Printf("Zipping %s as %s...\n", extPath, extName)
1943-
if err := util.ZipDirectory(extPath, tempZipPath); err != nil {
1943+
if err := util.ZipDirectory(extPath, tempZipPath, nil); err != nil {
19441944
pterm.Error.Printf("Failed to zip %s: %v\n", extPath, err)
19451945
return nil
19461946
}

cmd/deploy.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ func runDeploy(cmd *cobra.Command, args []string) (err error) {
236236
}
237237
tmpFile := filepath.Join(os.TempDir(), fmt.Sprintf("kernel_%d.zip", time.Now().UnixNano()))
238238
logger.Debug("compressing files", logger.Args("sourceDir", sourceDir, "tmpFile", tmpFile))
239-
if err := util.ZipDirectory(sourceDir, tmpFile); err != nil {
239+
if err := util.ZipDirectory(sourceDir, tmpFile, nil); err != nil {
240240
if spinner != nil {
241241
spinner.Fail("Failed to compress files")
242242
}

cmd/extensions.go

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,28 @@ import (
1919
)
2020

2121
const (
22-
// MaxExtensionSize is the maximum allowed size for extension bundles from API (50MB)
23-
MaxExtensionSize = 50 * 1024 * 1024
22+
MaxExtensionSizeBytes = 50 * 1024 * 1024 // 50MB
2423
)
2524

25+
// defaultExtensionExclusions contains patterns for files that are not needed
26+
// when zipping Chrome extensions
27+
var defaultExtensionExclusions = util.ZipOptions{
28+
ExcludeDirectories: []string{
29+
"node_modules",
30+
".git",
31+
"__tests__",
32+
"coverage",
33+
},
34+
ExcludeFilenamePatterns: []string{
35+
"*.test.js",
36+
"*.test.ts",
37+
"*.spec.js",
38+
"*.spec.ts",
39+
"*.log",
40+
"*.swp",
41+
},
42+
}
43+
2644
// ExtensionsService defines the subset of the Kernel SDK extension client that we use.
2745
type ExtensionsService interface {
2846
List(ctx context.Context, opts ...option.RequestOption) (res *[]kernel.ExtensionListResponse, err error)
@@ -310,30 +328,22 @@ func (e ExtensionsCmd) Upload(ctx context.Context, in ExtensionsUploadInput) err
310328
pterm.Info.Println("Zipping extension directory...")
311329
}
312330

313-
// Use new extension-specific zip function
314-
stats, err := util.ZipExtensionDirectory(absDir, tmpFile, &util.ExtensionZipOptions{
315-
ExcludeDefaults: false, // Apply defaults
316-
})
317-
if err != nil {
331+
if err := util.ZipDirectory(absDir, tmpFile, &defaultExtensionExclusions); err != nil {
318332
pterm.Error.Println("Failed to zip directory")
319333
return err
320334
}
321335
defer os.Remove(tmpFile)
322336

323-
// Get zip file size
324337
fileInfo, err := os.Stat(tmpFile)
325338
if err != nil {
326339
return fmt.Errorf("failed to stat zip: %w", err)
327340
}
328341

329342
if in.Output != "json" {
330-
pterm.Success.Printf("Created bundle: %s (%d files)\n",
331-
util.FormatBytes(fileInfo.Size()), stats.FilesIncluded)
343+
pterm.Success.Printf("Created bundle: %s\n", util.FormatBytes(fileInfo.Size()))
332344
}
333345

334-
// Final size validation
335-
336-
if fileInfo.Size() > MaxExtensionSize {
346+
if fileInfo.Size() > MaxExtensionSizeBytes {
337347
pterm.Error.Printf("Extension bundle is too large: %s (max: 50MB)\n",
338348
util.FormatBytes(fileInfo.Size()))
339349
pterm.Info.Println("\nSuggestions to reduce size:")
@@ -343,14 +353,12 @@ func (e ExtensionsCmd) Upload(ctx context.Context, in ExtensionsUploadInput) err
343353
return fmt.Errorf("bundle exceeds maximum size")
344354
}
345355

346-
// Open file for upload
347356
f, err := os.Open(tmpFile)
348357
if err != nil {
349358
return fmt.Errorf("failed to open temp zip: %w", err)
350359
}
351360
defer f.Close()
352361

353-
// Upload
354362
if in.Output != "json" {
355363
pterm.Info.Println("Uploading extension...")
356364
}
@@ -365,7 +373,6 @@ func (e ExtensionsCmd) Upload(ctx context.Context, in ExtensionsUploadInput) err
365373
return util.CleanedUpSdkError{Err: err}
366374
}
367375

368-
// Display results
369376
if in.Output == "json" {
370377
return util.PrintPrettyJSON(item)
371378
}

pkg/util/zip.go

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,16 @@ import (
1111
"github.com/boyter/gocodewalker"
1212
)
1313

14+
// ZipOptions which directories and files to exclude from the zip
15+
type ZipOptions struct {
16+
// ExcludeDirectories: exact directory names to exclude (case-sensitive)
17+
ExcludeDirectories []string
18+
// ExcludeFilenamePatterns: glob patterns for filename exclusion (e.g., "*.test.js")
19+
ExcludeFilenamePatterns []string
20+
}
21+
1422
// ZipDirectory compresses the given source directory into the destination file path.
15-
func ZipDirectory(srcDir, destZip string) error {
23+
func ZipDirectory(srcDir, destZip string, opts *ZipOptions) error {
1624
zipFile, err := os.Create(destZip)
1725
if err != nil {
1826
return err
@@ -28,9 +36,16 @@ func ZipDirectory(srcDir, destZip string) error {
2836
// Include hidden files (to match previous behaviour) but still respect .gitignore rules
2937
walker.IncludeHidden = true
3038

31-
// Start walking in a separate goroutine so we can process files as they arrive
39+
// Apply directory exclusions to walker
40+
if opts != nil {
41+
walker.ExcludeDirectory = append(walker.ExcludeDirectory, opts.ExcludeDirectories...)
42+
}
43+
44+
defer walker.Terminate()
45+
46+
errChan := make(chan error, 1)
3247
go func() {
33-
_ = walker.Start()
48+
errChan <- walker.Start()
3449
}()
3550

3651
// Track directories we've already added to the zip archive so we don't duplicate entries
@@ -44,6 +59,22 @@ func ZipDirectory(srcDir, destZip string) error {
4459
}
4560
relPath = filepath.ToSlash(relPath)
4661

62+
// Check against pattern-based exclusions if provided
63+
if opts != nil && len(opts.ExcludeFilenamePatterns) > 0 {
64+
filename := filepath.Base(f.Location)
65+
shouldExclude := false
66+
for _, pattern := range opts.ExcludeFilenamePatterns {
67+
matched, err := filepath.Match(pattern, filename)
68+
if err == nil && matched {
69+
shouldExclude = true
70+
break
71+
}
72+
}
73+
if shouldExclude {
74+
continue
75+
}
76+
}
77+
4778
// Ensure parent directories exist in the archive
4879
if dir := filepath.Dir(relPath); dir != "." && dir != "" {
4980
// Walk up the directory tree ensuring each level exists
@@ -115,6 +146,10 @@ func ZipDirectory(srcDir, destZip string) error {
115146
}
116147
}
117148

149+
if err := <-errChan; err != nil {
150+
return fmt.Errorf("directory walk failed: %w", err)
151+
}
152+
118153
return nil
119154
}
120155

0 commit comments

Comments
 (0)