util.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.testDirectoryIsWritable = exports.pathToLocation = void 0;
  4. const pathToLocation = (path) => {
  5. if (path[0] === "/" /* FsaToNodeConstants.Separator */)
  6. path = path.slice(1);
  7. if (path[path.length - 1] === "/" /* FsaToNodeConstants.Separator */)
  8. path = path.slice(0, -1);
  9. const lastSlashIndex = path.lastIndexOf("/" /* FsaToNodeConstants.Separator */);
  10. if (lastSlashIndex === -1)
  11. return [[], path];
  12. const file = path.slice(lastSlashIndex + 1);
  13. const folder = path.slice(0, lastSlashIndex).split("/" /* FsaToNodeConstants.Separator */);
  14. return [folder, file];
  15. };
  16. exports.pathToLocation = pathToLocation;
  17. const testDirectoryIsWritable = async (dir) => {
  18. const testFileName = '__memfs_writable_test_file_' + Math.random().toString(36).slice(2) + Date.now();
  19. try {
  20. await dir.getFileHandle(testFileName, { create: true });
  21. return true;
  22. }
  23. catch (_a) {
  24. return false;
  25. }
  26. finally {
  27. try {
  28. await dir.removeEntry(testFileName);
  29. }
  30. catch (e) { }
  31. }
  32. };
  33. exports.testDirectoryIsWritable = testDirectoryIsWritable;
  34. //# sourceMappingURL=util.js.map