Skip to content

Commit

Permalink
👌 IMPROVE: Use XDG Specification for history file
Browse files Browse the repository at this point in the history
  • Loading branch information
mstruebing committed May 22, 2021
1 parent 6682184 commit 4b49519
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
8 changes: 3 additions & 5 deletions utils/ask.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
const os = require('os');
const fs = require('fs');
const path = require('path');
const { Input } = require('enquirer');
Expand All @@ -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 (
Expand All @@ -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`)
})
};
}
Expand Down
17 changes: 17 additions & 0 deletions utils/history.js
Original file line number Diff line number Diff line change
@@ -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');
}
}
};

0 comments on commit 4b49519

Please sign in to comment.