-
-
Notifications
You must be signed in to change notification settings - Fork 25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Save cached config to standard directories #38
Changes from 9 commits
9bffb32
2b9f846
eaca545
f11246a
dff7efb
7e95c90
103a424
c5d1bdf
ce8959a
5171dd9
2830504
9d30f74
3ff22be
819f5fa
fb42dd6
6730801
fef2c76
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
const homedir = require('user-home'); | ||
const path = require('path'); | ||
|
||
const env = process.env; | ||
|
||
const macos = function (name) { | ||
const library = path.join(homedir, 'Library'); | ||
|
||
return path.join(library, 'Caches', name); | ||
}; | ||
|
||
const windows = function (name) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Prefer named functions over function assignments There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of taking a |
||
const appData = env.LOCALAPPDATA || path.join(homedir, 'AppData', 'Local'); | ||
|
||
return path.join(appData, name, 'Cache'); | ||
}; | ||
|
||
// https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html | ||
const linux = function (name) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Prefer named functions over function assignments There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of taking a |
||
const username = path.basename(homedir); | ||
|
||
return path.join(env.XDG_CACHE_HOME || path.join(homedir, '.cache'), name); | ||
}; | ||
|
||
module.exports = function (name) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of calling this as a function, it should probably just export the proper path based on platform |
||
if (process.platform === 'darwin') { | ||
return macos(name); | ||
} | ||
|
||
if (process.platform === 'win32') { | ||
return windows(name); | ||
} | ||
|
||
return linux(name); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,8 +10,10 @@ const execFile = require('child_process').execFile; | |
const env = process.env; | ||
const user = env.LOGNAME || env.USER || env.LNAME || env.USERNAME || ''; | ||
const exclusions = ['--help']; | ||
const cachePaths = require('./cache-paths.js'); | ||
|
||
const configfile = '.v8flags.'+process.versions.v8+'.'+crypto.createHash('md5').update(user).digest('hex')+'.json'; | ||
const configPath = cachePaths('js-v8flags'); | ||
|
||
const failureMessage = [ | ||
'Unable to cache a config file for v8flags to a your home directory', | ||
|
@@ -33,10 +35,12 @@ function openConfig (cb) { | |
return tryOpenConfig(path.join(os.tmpdir(), configfile), cb); | ||
} | ||
|
||
tryOpenConfig(path.join(userHome, configfile), function (err, fd) { | ||
if (err) return tryOpenConfig(path.join(os.tmpdir(), configfile), cb); | ||
return cb(null, fd); | ||
}); | ||
fs.mkdir(configPath, function () { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wouldn't this need to be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good question, I'm not sure what the proper solution is here. It should only create one directory unless the user deleted a default OS directory which would probably also break other apps... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can definitely count on some environments not being able to make this directory. It's a crazy world out there. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @tkellen I understand but regardless of an error or not There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To clarify, by "multiple" I think @phated meant nested. It doesn't seem unlikely that the user would define a nested directory in config, in which case you would either need to recreate the logic in also, is there a reason the error isn't being handled? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes I understand @phated meant nested directories. With my reply I meant that it's nested inside a default OS directory so I'm wondering if the case needs to be handled. Regarding error handling, if for some reason we can't create the directory the next function There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you handle the error (and it's not EEXIST - which you wouldn't need to worry about with mkdirp) you could avoid an extra There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes that's true. Do you think using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Even if it isn't worth the added dependency (not sure if so), checking the error might still be worth it. |
||
tryOpenConfig(path.join(configPath, configfile), function (err, fd) { | ||
if (err) return tryOpenConfig(path.join(os.tmpdir(), configfile), cb); | ||
return cb(null, fd); | ||
}); | ||
}) | ||
} | ||
|
||
function tryOpenConfig (configpath, cb) { | ||
|
@@ -131,3 +135,4 @@ module.exports = function (cb) { | |
}; | ||
|
||
module.exports.configfile = configfile; | ||
module.exports.configPath = configPath; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Prefer named functions over function assignments
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of taking a
name
, can just have'js-v8flags'
assigned as a variable at the top of file.