From 2255703a21da64ba64f5e076287397e1ea0c6fe6 Mon Sep 17 00:00:00 2001 From: Danielle McLean Date: Fri, 1 Oct 2021 11:32:16 +1000 Subject: [PATCH] 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. --- config/zsh/login/npm | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/config/zsh/login/npm b/config/zsh/login/npm index 5f8b56b..351ab02 100644 --- a/config/zsh/login/npm +++ b/config/zsh/login/npm @@ -1,4 +1,23 @@ #! zsh : ${N_PREFIX:=$HOME/.local} : ${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