manifest.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. "use strict";
  2. const http = require("http");
  3. const unzip = require("unzip");
  4. var targz = require('tar.gz');
  5. const url = require("url");
  6. const fs = require("fs");
  7. class ManifestPreparer {
  8. constructor(options) {
  9. this.steps = options.steps;
  10. this.download = options.download;
  11. this._validateSteps();
  12. }
  13. prepare() {
  14. var unzipStep = this.steps[0]["unzip"];
  15. var untarStep = this.steps[1]["untar"];
  16. let fileName = `tmp/${this.download.split("/").pop()}`;
  17. const downloadRequest = http.get(this.download, (response) => {
  18. var body = "";
  19. response.pipe(unzip.Parse()).on('entry', (entry) => {
  20. const fileName = entry.path;
  21. if (unzipStep.files.indexOf(fileName) !== -1) {
  22. entry.pipe(targz().createParseStream()).on('entry', (tarEntry) => {
  23. const fileName = tarEntry.path.split("/").pop();
  24. if (untarStep.files.indexOf(fileName) !== -1 ) {
  25. tarEntry.pipe(fs.createWriteStream(`tmp/${fileName}`));
  26. }
  27. });
  28. } else {
  29. entry.autodrain();
  30. }
  31. });
  32. response.on("error", (e) => console.error(e));
  33. });
  34. }
  35. unzip(source, files) {
  36. }
  37. untar(source, files) {
  38. }
  39. flash() {
  40. }
  41. /**
  42. * Checks if the step functionality from the manifest.json file exists
  43. * in the {ManifestPreparer}
  44. * @private
  45. */
  46. _validateSteps() {
  47. this.steps
  48. .map(this._getStepName)
  49. .forEach(step => {
  50. if (!(typeof this[step] === "function")) {
  51. throw `${step} is not a valid step`;
  52. }
  53. });
  54. }
  55. _getStepName(step) {
  56. return Object.keys(step)[0];
  57. }
  58. }
  59. module.exports = {ManifestPreparer};