Skip to content

Commit 4b49519

Browse files
committed
👌 IMPROVE: Use XDG Specification for history file
closes ahmadawais#4
1 parent 6682184 commit 4b49519

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

utils/ask.js

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
const os = require('os');
21
const fs = require('fs');
32
const path = require('path');
43
const { Input } = require('enquirer');
@@ -7,6 +6,8 @@ const handleError = require('cli-handle-error');
76
const shouldCancel = require('cli-should-cancel');
87
const { Store } = require('data-store');
98

9+
const { getHistoryDirectory } = require('./history');
10+
1011
module.exports = async ({ name, message, hint, initial }) => {
1112
let history = false;
1213
if (
@@ -18,10 +19,7 @@ module.exports = async ({ name, message, hint, initial }) => {
1819
history = {
1920
autosave: true,
2021
store: new Store({
21-
path: path.join(
22-
os.homedir(),
23-
`.history/create-node-cli/${name}.json`
24-
)
22+
path: path.join(getHistoryDirectory(), `${name}.json`)
2523
})
2624
};
2725
}

utils/history.js

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
const os = require('os');
2+
const path = require('path');
3+
4+
module.exports = {
5+
getHistoryDirectory: () => {
6+
const XDG_CACHE_HOME = process.env.XDG_CACHE_HOME;
7+
const FALLBACK_CACHE_DIRECTORY = path.join(os.homedir(), '.cache');
8+
9+
if (XDG_CACHE_HOME) {
10+
return path.join(XDG_CACHE_HOME, 'create-node-cli');
11+
}
12+
13+
if (FALLBACK_CACHE_DIRECTORY) {
14+
return path.join(FALLBACK_CACHE_DIRECTORY, 'create-node-cli');
15+
}
16+
}
17+
};

0 commit comments

Comments
 (0)