Skip to content

Commit

Permalink
Merge pull request #35 from TheDevMinerTV/feat/npm-cache-reuse
Browse files Browse the repository at this point in the history
feat: NPM cache reuse
  • Loading branch information
TheDevMinerTV authored Jul 20, 2024
2 parents bf56cba + 4156849 commit 13aa98f
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 16 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ The following additional flags are available:
```
--short : Print a shorter version of the package report, ideal for posts to Twitter
--no-cleanup : Don't remove the temporary directory after the calculation
--npm-cache <DIRECTORY>: Use the specified directory as the NPM cache, defaults to generating a temporary directory
--npm-cache-read-write : Mount the NPM cache directory as read-write, defaults to true, only honored if --npm-cache is specified
```

## License
Expand Down
22 changes: 10 additions & 12 deletions docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,19 +104,17 @@ func runContainer(ctx context.Context, cmd []string, tmpDir internal.TmpDir) err
},
}

if MountNPMCache != "" {
if !filepath.IsAbs(MountNPMCache) {
return fmt.Errorf("MountNPMCache must be an absolute path")
}

hostConfig.Mounts = append(hostConfig.Mounts, docker_mount.Mount{
Type: docker_mount.TypeBind,
Source: MountNPMCache,
Target: "/root/.npm",
ReadOnly: true,
})
hostConfig.Mounts = append(hostConfig.Mounts, docker_mount.Mount{
Type: docker_mount.TypeBind,
Source: string(npmCache),
Target: "/root/.cache/npm",
ReadOnly: npmCacheRO,
})

log.Info().Str("path", MountNPMCache).Msg("Mounting readonly NPM cache")
if npmCacheRO {
log.Info().Str("path", string(npmCache)).Msg("Mounting readonly NPM cache")
} else {
log.Info().Str("path", string(npmCache)).Msg("Mounting NPM cache")
}

c, err := dockerC.ContainerCreate(ctx, &config, &hostConfig, nil, nil, "")
Expand Down
35 changes: 31 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,20 @@ import (
)

const (
BaseImage = "node:22"
MountNPMCache = ""
BaseImage = "node:22"
)

var (
npmClient *npm.Client
dockerC *docker_client.Client

fShortMode = flag.Bool("short", false, "Print a shorter version of the package report, ideal for posts to Twitter")
fNoCleanup = flag.Bool("no-cleanup", false, "Do not cleanup the temporary directories after the execution")
npmCache internal.TmpDir
npmCacheRO = false

fShortMode = flag.Bool("short", false, "Print a shorter version of the package report, ideal for posts to Twitter")
fNoCleanup = flag.Bool("no-cleanup", false, "Do not cleanup the temporary directories after the execution")
fNPMCache = flag.String("npm-cache", "", "Use the specified directory as the NPM cache")
fNPMCacheRW = flag.Bool("npm-cache-rw", true, "Mount the NPM cache directory as read-write")
)

func main() {
Expand All @@ -48,6 +52,29 @@ func main() {
log.Fatal().Err(err).Msg("Failed to download Node 22 image")
}

if *fNPMCache == "" {
npmCache, err = internal.NewTmpDir("npm_cache_*")
if err != nil {
log.Fatal().Err(err).Msg("Failed to create temporary directory for NPM cache")
}
defer func() {
if !*fNoCleanup {
return
}

npmCache.Remove()

log.Debug().Str("dir", npmCache.String()).Msg("Cleaned up temporary directory for NPM cache")
}()

log.Debug().Str("dir", npmCache.String()).Msg("Created temporary directory for NPM cache")
} else {
npmCache = internal.TmpDir(*fNPMCache)
npmCacheRO = !*fNPMCacheRW

log.Debug().Str("dir", npmCache.String()).Bool("readonly", npmCacheRO).Msg("Using specified directory as NPM cache")
}

variant, _, err := internal.RunSelect(&promptui.Select{
Label: "Select variant",
Items: []string{"Calculate size differences for replacing/removing dependencies", "Calculate size difference between package versions"},
Expand Down

0 comments on commit 13aa98f

Please sign in to comment.