Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. var common = require('./common');
  2. var os = require('os');
  3. var fs = require('fs');
  4. // Returns false if 'dir' is not a writeable directory, 'dir' otherwise
  5. function writeableDir(dir) {
  6. if (!dir || !fs.existsSync(dir))
  7. return false;
  8. if (!fs.statSync(dir).isDirectory())
  9. return false;
  10. var testFile = dir+'/'+common.randomFileName();
  11. try {
  12. fs.writeFileSync(testFile, ' ');
  13. common.unlinkSync(testFile);
  14. return dir;
  15. } catch (e) {
  16. return false;
  17. }
  18. }
  19. //@
  20. //@ ### tempdir()
  21. //@
  22. //@ Examples:
  23. //@
  24. //@ ```javascript
  25. //@ var tmp = tempdir(); // "/tmp" for most *nix platforms
  26. //@ ```
  27. //@
  28. //@ Searches and returns string containing a writeable, platform-dependent temporary directory.
  29. //@ Follows Python's [tempfile algorithm](http://docs.python.org/library/tempfile.html#tempfile.tempdir).
  30. function _tempDir() {
  31. var state = common.state;
  32. if (state.tempDir)
  33. return state.tempDir; // from cache
  34. state.tempDir = writeableDir(os.tempDir && os.tempDir()) || // node 0.8+
  35. writeableDir(process.env['TMPDIR']) ||
  36. writeableDir(process.env['TEMP']) ||
  37. writeableDir(process.env['TMP']) ||
  38. writeableDir(process.env['Wimp$ScrapDir']) || // RiscOS
  39. writeableDir('C:\\TEMP') || // Windows
  40. writeableDir('C:\\TMP') || // Windows
  41. writeableDir('\\TEMP') || // Windows
  42. writeableDir('\\TMP') || // Windows
  43. writeableDir('/tmp') ||
  44. writeableDir('/var/tmp') ||
  45. writeableDir('/usr/tmp') ||
  46. writeableDir('.'); // last resort
  47. return state.tempDir;
  48. }
  49. module.exports = _tempDir;