index.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. var os = require('os')
  2. var path = require('path')
  3. var homedir = require('os-homedir')
  4. function posix (id) {
  5. var cacheHome = process.env.XDG_CACHE_HOME || path.join(homedir(), '.cache')
  6. return path.join(cacheHome, id)
  7. }
  8. function darwin (id) {
  9. return path.join(homedir(), 'Library', 'Caches', id)
  10. }
  11. function win32 (id) {
  12. var appData = process.env.LOCALAPPDATA || path.join(homedir(), 'AppData', 'Local')
  13. return path.join(appData, id, 'Cache')
  14. }
  15. var implementation = (function () {
  16. switch (os.platform()) {
  17. case 'android': return posix
  18. case 'darwin': return darwin
  19. case 'freebsd': return posix
  20. case 'linux': return posix
  21. case 'win32': return win32
  22. default: throw new Error('Your OS "' + os.platform() + '" is currently not supported by node-cachedir.')
  23. }
  24. }())
  25. module.exports = function (id) {
  26. if (typeof id !== 'string') {
  27. throw new TypeError('id is not a string')
  28. }
  29. if (id.length === 0) {
  30. throw new Error('id cannot be empty')
  31. }
  32. if (/[^0-9a-zA-Z-]/.test(id)) {
  33. throw new Error('id cannot contain special characters')
  34. }
  35. return implementation(id)
  36. }