Нема описа
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.archiveDirectory = void 0;
  4. const archiver = require("archiver");
  5. const filesize = require("filesize");
  6. const fs = require("fs");
  7. const path = require("path");
  8. const tar = require("tar");
  9. const tmp = require("tmp");
  10. const error_1 = require("./error");
  11. const listFiles_1 = require("./listFiles");
  12. const logger_1 = require("./logger");
  13. const fsAsync = require("./fsAsync");
  14. async function archiveDirectory(sourceDirectory, options = {}) {
  15. let postfix = ".tar.gz";
  16. if (options.type === "zip") {
  17. postfix = ".zip";
  18. }
  19. const tempFile = tmp.fileSync({
  20. prefix: "firebase-archive-",
  21. postfix,
  22. });
  23. if (!options.ignore) {
  24. options.ignore = [];
  25. }
  26. let makeArchive;
  27. if (options.type === "zip") {
  28. makeArchive = zipDirectory(sourceDirectory, tempFile, options);
  29. }
  30. else {
  31. makeArchive = tarDirectory(sourceDirectory, tempFile, options);
  32. }
  33. try {
  34. const archive = await makeArchive;
  35. logger_1.logger.debug(`Archived ${filesize(archive.size)} in ${sourceDirectory}.`);
  36. return archive;
  37. }
  38. catch (err) {
  39. if (err instanceof error_1.FirebaseError) {
  40. throw err;
  41. }
  42. throw new error_1.FirebaseError("Failed to create archive.", { original: err });
  43. }
  44. }
  45. exports.archiveDirectory = archiveDirectory;
  46. async function tarDirectory(sourceDirectory, tempFile, options) {
  47. const allFiles = (0, listFiles_1.listFiles)(sourceDirectory, options.ignore);
  48. try {
  49. fs.statSync(sourceDirectory);
  50. }
  51. catch (err) {
  52. if (err.code === "ENOENT") {
  53. throw new error_1.FirebaseError(`Could not read directory "${sourceDirectory}"`);
  54. }
  55. throw err;
  56. }
  57. if (!allFiles.length) {
  58. throw new error_1.FirebaseError(`Cannot create a tar archive with 0 files from directory "${sourceDirectory}"`);
  59. }
  60. await tar.create({
  61. gzip: true,
  62. file: tempFile.name,
  63. cwd: sourceDirectory,
  64. follow: true,
  65. noDirRecurse: true,
  66. portable: true,
  67. }, allFiles);
  68. const stats = fs.statSync(tempFile.name);
  69. return {
  70. file: tempFile.name,
  71. stream: fs.createReadStream(tempFile.name),
  72. manifest: allFiles,
  73. size: stats.size,
  74. source: sourceDirectory,
  75. };
  76. }
  77. async function zipDirectory(sourceDirectory, tempFile, options) {
  78. const archiveFileStream = fs.createWriteStream(tempFile.name, {
  79. flags: "w",
  80. encoding: "binary",
  81. });
  82. const archive = archiver("zip");
  83. const archiveDone = pipeAsync(archive, archiveFileStream);
  84. const allFiles = [];
  85. let files;
  86. try {
  87. files = await fsAsync.readdirRecursive({ path: sourceDirectory, ignore: options.ignore });
  88. }
  89. catch (err) {
  90. if (err.code === "ENOENT") {
  91. throw new error_1.FirebaseError(`Could not read directory "${sourceDirectory}"`, { original: err });
  92. }
  93. throw err;
  94. }
  95. for (const file of files) {
  96. const name = path.relative(sourceDirectory, file.name);
  97. allFiles.push(name);
  98. archive.file(file.name, {
  99. name,
  100. mode: file.mode,
  101. });
  102. }
  103. void archive.finalize();
  104. await archiveDone;
  105. const stats = fs.statSync(tempFile.name);
  106. return {
  107. file: tempFile.name,
  108. stream: fs.createReadStream(tempFile.name),
  109. manifest: allFiles,
  110. size: stats.size,
  111. source: sourceDirectory,
  112. };
  113. }
  114. async function pipeAsync(from, to) {
  115. return new Promise((resolve, reject) => {
  116. to.on("finish", resolve);
  117. to.on("error", reject);
  118. from.pipe(to);
  119. });
  120. }