diff --git a/README.md b/README.md index 96dbd5d..4588b45 100644 --- a/README.md +++ b/README.md @@ -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 : 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 diff --git a/docker.go b/docker.go index b63458a..d40130d 100644 --- a/docker.go +++ b/docker.go @@ -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, "") diff --git a/main.go b/main.go index 98517d6..e44d8bf 100644 --- a/main.go +++ b/main.go @@ -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() { @@ -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"},