parseListDOS.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.transformList = exports.parseLine = exports.testLine = void 0;
  4. const FileInfo_1 = require("./FileInfo");
  5. /**
  6. * This parser is based on the FTP client library source code in Apache Commons Net provided
  7. * under the Apache 2.0 license. It has been simplified and rewritten to better fit the Javascript language.
  8. *
  9. * https://github.com/apache/commons-net/blob/master/src/main/java/org/apache/commons/net/ftp/parser/NTFTPEntryParser.java
  10. */
  11. const RE_LINE = new RegExp("(\\S+)\\s+(\\S+)\\s+" // MM-dd-yy whitespace hh:mma|kk:mm swallow trailing spaces
  12. + "(?:(<DIR>)|([0-9]+))\\s+" // <DIR> or ddddd swallow trailing spaces
  13. + "(\\S.*)" // First non-space followed by rest of line (name)
  14. );
  15. /**
  16. * Returns true if a given line might be a DOS-style listing.
  17. *
  18. * - Example: `12-05-96 05:03PM <DIR> myDir`
  19. */
  20. function testLine(line) {
  21. return /^\d{2}/.test(line) && RE_LINE.test(line);
  22. }
  23. exports.testLine = testLine;
  24. /**
  25. * Parse a single line of a DOS-style directory listing.
  26. */
  27. function parseLine(line) {
  28. const groups = line.match(RE_LINE);
  29. if (groups === null) {
  30. return undefined;
  31. }
  32. const name = groups[5];
  33. if (name === "." || name === "..") { // Ignore parent directory links
  34. return undefined;
  35. }
  36. const file = new FileInfo_1.FileInfo(name);
  37. const fileType = groups[3];
  38. if (fileType === "<DIR>") {
  39. file.type = FileInfo_1.FileType.Directory;
  40. file.size = 0;
  41. }
  42. else {
  43. file.type = FileInfo_1.FileType.File;
  44. file.size = parseInt(groups[4], 10);
  45. }
  46. file.rawModifiedAt = groups[1] + " " + groups[2];
  47. return file;
  48. }
  49. exports.parseLine = parseLine;
  50. function transformList(files) {
  51. return files;
  52. }
  53. exports.transformList = transformList;