fattr.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. const pth = require("path");
  2. module.exports = function (/*String*/ path, /*Utils object*/ { fs }) {
  3. var _path = path || "",
  4. _obj = newAttr(),
  5. _stat = null;
  6. function newAttr() {
  7. return {
  8. directory: false,
  9. readonly: false,
  10. hidden: false,
  11. executable: false,
  12. mtime: 0,
  13. atime: 0
  14. };
  15. }
  16. if (_path && fs.existsSync(_path)) {
  17. _stat = fs.statSync(_path);
  18. _obj.directory = _stat.isDirectory();
  19. _obj.mtime = _stat.mtime;
  20. _obj.atime = _stat.atime;
  21. _obj.executable = (0o111 & _stat.mode) !== 0; // file is executable who ever har right not just owner
  22. _obj.readonly = (0o200 & _stat.mode) === 0; // readonly if owner has no write right
  23. _obj.hidden = pth.basename(_path)[0] === ".";
  24. } else {
  25. console.warn("Invalid path: " + _path);
  26. }
  27. return {
  28. get directory() {
  29. return _obj.directory;
  30. },
  31. get readOnly() {
  32. return _obj.readonly;
  33. },
  34. get hidden() {
  35. return _obj.hidden;
  36. },
  37. get mtime() {
  38. return _obj.mtime;
  39. },
  40. get atime() {
  41. return _obj.atime;
  42. },
  43. get executable() {
  44. return _obj.executable;
  45. },
  46. decodeAttributes: function () {},
  47. encodeAttributes: function () {},
  48. toJSON: function () {
  49. return {
  50. path: _path,
  51. isDirectory: _obj.directory,
  52. isReadOnly: _obj.readonly,
  53. isHidden: _obj.hidden,
  54. isExecutable: _obj.executable,
  55. mTime: _obj.mtime,
  56. aTime: _obj.atime
  57. };
  58. },
  59. toString: function () {
  60. return JSON.stringify(this.toJSON(), null, "\t");
  61. }
  62. };
  63. };