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

test.js 1.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. var common = require('./common');
  2. var fs = require('fs');
  3. //@
  4. //@ ### test(expression)
  5. //@ Available expression primaries:
  6. //@
  7. //@ + `'-b', 'path'`: true if path is a block device
  8. //@ + `'-c', 'path'`: true if path is a character device
  9. //@ + `'-d', 'path'`: true if path is a directory
  10. //@ + `'-e', 'path'`: true if path exists
  11. //@ + `'-f', 'path'`: true if path is a regular file
  12. //@ + `'-L', 'path'`: true if path is a symboilc link
  13. //@ + `'-p', 'path'`: true if path is a pipe (FIFO)
  14. //@ + `'-S', 'path'`: true if path is a socket
  15. //@
  16. //@ Examples:
  17. //@
  18. //@ ```javascript
  19. //@ if (test('-d', path)) { /* do something with dir */ };
  20. //@ if (!test('-f', path)) continue; // skip if it's a regular file
  21. //@ ```
  22. //@
  23. //@ Evaluates expression using the available primaries and returns corresponding value.
  24. function _test(options, path) {
  25. if (!path)
  26. common.error('no path given');
  27. // hack - only works with unary primaries
  28. options = common.parseOptions(options, {
  29. 'b': 'block',
  30. 'c': 'character',
  31. 'd': 'directory',
  32. 'e': 'exists',
  33. 'f': 'file',
  34. 'L': 'link',
  35. 'p': 'pipe',
  36. 'S': 'socket'
  37. });
  38. var canInterpret = false;
  39. for (var key in options)
  40. if (options[key] === true) {
  41. canInterpret = true;
  42. break;
  43. }
  44. if (!canInterpret)
  45. common.error('could not interpret expression');
  46. if (options.link) {
  47. try {
  48. return fs.lstatSync(path).isSymbolicLink();
  49. } catch(e) {
  50. return false;
  51. }
  52. }
  53. if (!fs.existsSync(path))
  54. return false;
  55. if (options.exists)
  56. return true;
  57. var stats = fs.statSync(path);
  58. if (options.block)
  59. return stats.isBlockDevice();
  60. if (options.character)
  61. return stats.isCharacterDevice();
  62. if (options.directory)
  63. return stats.isDirectory();
  64. if (options.file)
  65. return stats.isFile();
  66. if (options.pipe)
  67. return stats.isFIFO();
  68. if (options.socket)
  69. return stats.isSocket();
  70. } // test
  71. module.exports = _test;