cli
                                
                                
                                
                                    cli copied to clipboard
                            
                            
                            
                        fix(pack, publish): default foreground-scripts to true
Fixes #6816
This fixes a regression in both npm 9 and 10.
An alternative approach to adding a constructor could be, add something in BaseCommand that runs super, and then reads the default from the derived command class, and then sets the default - that way it'd be more declarative. Happy to change to something like that, if preferred.
Check out the save config for how we are overriding the defaultDescription to describe subtleties like this.
Let's put a pin in the base command update, since we'd want to include the other things for which the default is different (like save).
Smoke tests will likely need snapshots updated.
How do I update smoke test snapshots?
npm run snap -w smoke-tests
                                    
                                    
                                    
                                
I've updated the smoke test snapshots but they're still failing.
Smoke tests were failing because they made an assumption that the output of npm pack would only include the name of the resulting tarball which is no longer true with this change.
I fixed this in #7256. Once that lands, this PR can be rebased and should go green.
For reference here's what node . pack now looks like with this PR:
❯ node . pack 2> /dev/null
> [email protected] prepack
> node . run build -w docs
> @npmcli/[email protected] build
> node bin/build.js
Wrote 251 files
npm-10.4.0.tgz
                                    
                                    
                                    
                                
This change is problematic imo and it actually broke our tooling, because the stdout even with the --json flag now also contains the stdout of the pre/post hooks.
With --json i'd assume that you would be able to pipe the stdout and it only contains the command's json output, not some random output of lifecycle hooks.
(We were actually using the fact that it just used to print the tarball name to stdout, and I can see how that is maybe not the most robust idea since it's not a guaranteed API but with the --json flag I somehow expected it to work).
Edit: I just realized that --json doesn't print into a single line, so my initial idea of fixing it by piping through tail -n 1 doesn't work either. This change makes it very hard to separate the npm pack command's output with the one from the lifecycle hooks. ~Does adding sth like a --silent flag make sense?~ There's actually the (global?) --silent flag, and using it to prevent printing stdout of hooks would probably make sense to me.
Update: should have looked up before posting, there's already a bug linked there that was closed and mentions using --foreground-scripts=false which actually does what i need (but somehow isn't documented in the command's help).
@simonhaenisch the hooks are under your control - you can silence their output however you like unrelated to npm. You can also set --no-foreground-scripts if you want it to be false and restore the previous behavior, as you indicated.