sync.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.fromSnapshotSync = exports.toSnapshotSync = void 0;
  4. const toSnapshotSync = ({ fs, path = '/', separator = '/' }) => {
  5. const stats = fs.lstatSync(path);
  6. if (stats.isDirectory()) {
  7. const list = fs.readdirSync(path);
  8. const entries = {};
  9. const dir = path.endsWith(separator) ? path : path + separator;
  10. for (const child of list) {
  11. const childSnapshot = (0, exports.toSnapshotSync)({ fs, path: `${dir}${child}`, separator });
  12. if (childSnapshot)
  13. entries['' + child] = childSnapshot;
  14. }
  15. return [0 /* SnapshotNodeType.Folder */, {}, entries];
  16. }
  17. else if (stats.isFile()) {
  18. const buf = fs.readFileSync(path);
  19. const uint8 = new Uint8Array(buf.buffer, buf.byteOffset, buf.byteLength);
  20. return [1 /* SnapshotNodeType.File */, {}, uint8];
  21. }
  22. else if (stats.isSymbolicLink()) {
  23. return [
  24. 2 /* SnapshotNodeType.Symlink */,
  25. {
  26. target: fs.readlinkSync(path).toString(),
  27. },
  28. ];
  29. }
  30. return null;
  31. };
  32. exports.toSnapshotSync = toSnapshotSync;
  33. const fromSnapshotSync = (snapshot, { fs, path = '/', separator = '/' }) => {
  34. if (!snapshot)
  35. return;
  36. switch (snapshot[0]) {
  37. case 0 /* SnapshotNodeType.Folder */: {
  38. if (!path.endsWith(separator))
  39. path = path + separator;
  40. const [, , entries] = snapshot;
  41. fs.mkdirSync(path, { recursive: true });
  42. for (const [name, child] of Object.entries(entries))
  43. (0, exports.fromSnapshotSync)(child, { fs, path: `${path}${name}`, separator });
  44. break;
  45. }
  46. case 1 /* SnapshotNodeType.File */: {
  47. const [, , data] = snapshot;
  48. fs.writeFileSync(path, data);
  49. break;
  50. }
  51. case 2 /* SnapshotNodeType.Symlink */: {
  52. const [, { target }] = snapshot;
  53. fs.symlinkSync(target, path);
  54. break;
  55. }
  56. }
  57. };
  58. exports.fromSnapshotSync = fromSnapshotSync;
  59. //# sourceMappingURL=sync.js.map