From 4b495190925fd40a4eed298026e8bd097a02ca5c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Max=20Str=C3=BCbing?= Date: Sat, 22 May 2021 11:43:54 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=91=8C=20IMPROVE:=20Use=20XDG=20Specifica?= =?UTF-8?q?tion=20for=20history=20file?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit closes #4 --- utils/ask.js | 8 +++----- utils/history.js | 17 +++++++++++++++++ 2 files changed, 20 insertions(+), 5 deletions(-) create mode 100644 utils/history.js diff --git a/utils/ask.js b/utils/ask.js index c6ad933..bbdb914 100644 --- a/utils/ask.js +++ b/utils/ask.js @@ -1,4 +1,3 @@ -const os = require('os'); const fs = require('fs'); const path = require('path'); const { Input } = require('enquirer'); @@ -7,6 +6,8 @@ const handleError = require('cli-handle-error'); const shouldCancel = require('cli-should-cancel'); const { Store } = require('data-store'); +const { getHistoryDirectory } = require('./history'); + module.exports = async ({ name, message, hint, initial }) => { let history = false; if ( @@ -18,10 +19,7 @@ module.exports = async ({ name, message, hint, initial }) => { history = { autosave: true, store: new Store({ - path: path.join( - os.homedir(), - `.history/create-node-cli/${name}.json` - ) + path: path.join(getHistoryDirectory(), `${name}.json`) }) }; } diff --git a/utils/history.js b/utils/history.js new file mode 100644 index 0000000..7061842 --- /dev/null +++ b/utils/history.js @@ -0,0 +1,17 @@ +const os = require('os'); +const path = require('path'); + +module.exports = { + getHistoryDirectory: () => { + const XDG_CACHE_HOME = process.env.XDG_CACHE_HOME; + const FALLBACK_CACHE_DIRECTORY = path.join(os.homedir(), '.cache'); + + if (XDG_CACHE_HOME) { + return path.join(XDG_CACHE_HOME, 'create-node-cli'); + } + + if (FALLBACK_CACHE_DIRECTORY) { + return path.join(FALLBACK_CACHE_DIRECTORY, 'create-node-cli'); + } + } +};