createWebApp.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. const fs = require('fs-extra');
  2. const webApp = require('../dist/public.json');
  3. const ZipReader = require('./core/ZipReader');
  4. module.exports = async(config) => {
  5. const verFile = `${config.publicDir}/version.txt`;
  6. const zipFile = `${config.tempDir}/public.zip`;
  7. if (await fs.pathExists(verFile)) {
  8. const curPublicVersion = await fs.readFile(verFile, 'utf8');
  9. if (curPublicVersion == config.version)
  10. return;
  11. }
  12. //сохраним files
  13. const filesDir = `${config.publicDir}/files`;
  14. let tmpFilesDir = '';
  15. if (await fs.pathExists(filesDir)) {
  16. tmpFilesDir = `${config.dataDir}/files`;
  17. if (!await fs.pathExists(tmpFilesDir))
  18. await fs.move(filesDir, tmpFilesDir);
  19. }
  20. await fs.remove(config.publicDir);
  21. //извлекаем новый webApp
  22. await fs.writeFile(zipFile, webApp.data, {encoding: 'base64'});
  23. const zipReader = new ZipReader();
  24. await zipReader.open(zipFile);
  25. try {
  26. await zipReader.extractAllToDir(config.publicDir);
  27. } finally {
  28. await zipReader.close();
  29. }
  30. //восстановим files
  31. if (tmpFilesDir)
  32. await fs.move(tmpFilesDir, filesDir);
  33. await fs.writeFile(verFile, config.version);
  34. await fs.remove(zipFile);
  35. };