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'); + } + } +};