Provide better XDG compliance for Node.js

We want to store the Node REPL history in XDG_DATA_HOME, and we also
want to make sure we always have an npm config file that sets up npm
with its appropriate XDG paths. (By default, lots of stuff ends up in
~/.npm if no configuration is provided.)

As I mention in the diff, we can't simply commit the npm config file to
version control, because it contains things like authorisation tokens
that shouldn't be committed. We can however generate a default config
file that sets up the XDG paths we need, allowing the user to then go
ahead and add tokens to the generated file later on, like so.
This commit is contained in:
Danielle McLean 2021-10-01 11:32:16 +10:00
parent f06071e7c7
commit 2255703a21
Signed by: 00dani
GPG key ID: 9DDE1EDE01E3A605

View file

@ -1,4 +1,23 @@
#! zsh #! zsh
: ${N_PREFIX:=$HOME/.local} : ${N_PREFIX:=$HOME/.local}
: ${NPM_CONFIG_USERCONFIG:=$XDG_CONFIG_HOME/npm/config} : ${NPM_CONFIG_USERCONFIG:=$XDG_CONFIG_HOME/npm/config}
export N_PREFIX NPM_CONFIG_USERCONFIG : ${NODE_REPL_HISTORY:=$XDG_DATA_HOME/node/repl_history}
# The npm configuration file can't be safely committed to version control,
# because things like authorisation tokens for publishing packages to npmjs.org
# live in there. Instead, let's generate a basic npm config here if it doesn't
# already exist.
if [[ ! -a $NPM_CONFIG_USERCONFIG ]]; then
mkdir -p $NPM_CONFIG_USERCONFIG:h
> $NPM_CONFIG_USERCONFIG {
echo cache=$XDG_CACHE_HOME/npm
echo prefix=$HOME/.local
echo store-dir=$XDG_CACHE_HOME/pnpm-store
}
fi
# Node won't create the directory to store its own REPL history if it isn't
# there? It'll just fail to store REPL history. So let's ensure the directory
# exists by ourselves.
mkdir -p $NODE_REPL_HISTORY:h
export N_PREFIX NPM_CONFIG_USERCONFIG NODE_REPL_HISTORY