deploy.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /**
  2. * Copyright (c) 2019 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  5. */
  6. const git = require('simple-git');
  7. const path = require('path');
  8. const fs = require("fs");
  9. const fse = require("fs-extra");
  10. const remoteUrl = "https://github.com/molstar/molstar.github.io.git";
  11. const buildDir = path.resolve(__dirname, '../build/');
  12. const deployDir = path.resolve(buildDir, 'deploy/');
  13. const localPath = path.resolve(deployDir, 'molstar.github.io/');
  14. const analyticsTag = /<!-- __MOLSTAR_ANALYTICS__ -->/g;
  15. const analyticsCode = `<!-- Cloudflare Web Analytics --><script defer src='https://static.cloudflareinsights.com/beacon.min.js' data-cf-beacon='{"token": "c414cbae2d284ea995171a81e4a3e721"}'></script><!-- End Cloudflare Web Analytics -->`;
  16. function log(command, stdout, stderr) {
  17. if (command) {
  18. console.log('\n###', command);
  19. stdout.pipe(process.stdout);
  20. stderr.pipe(process.stderr);
  21. }
  22. }
  23. function addAnalytics(path) {
  24. const data = fs.readFileSync(path, 'utf8');
  25. const result = data.replace(analyticsTag, analyticsCode);
  26. fs.writeFileSync(path, result, 'utf8');
  27. }
  28. function copyViewer() {
  29. console.log('\n###', 'copy viewer files');
  30. const viewerBuildPath = path.resolve(buildDir, '../build/viewer/');
  31. const viewerDeployPath = path.resolve(localPath, 'viewer/');
  32. fse.copySync(viewerBuildPath, viewerDeployPath, { overwrite: true });
  33. addAnalytics(path.resolve(viewerDeployPath, 'index.html'));
  34. }
  35. if (!fs.existsSync(localPath)) {
  36. console.log('\n###', 'create localPath');
  37. fs.mkdirSync(localPath, { recursive: true });
  38. }
  39. process.chdir(localPath);
  40. if (!fs.existsSync(path.resolve(localPath, '.git/'))) {
  41. console.log('\n###', 'clone repository');
  42. git()
  43. .outputHandler(log)
  44. .clone(remoteUrl, localPath)
  45. .fetch(['--all'])
  46. .exec(copyViewer)
  47. .add(['-A'])
  48. .commit('updated viewer')
  49. .push();
  50. } else {
  51. console.log('\n###', 'update repository');
  52. git()
  53. .outputHandler(log)
  54. .fetch(['--all'])
  55. .reset(['--hard', 'origin/master'])
  56. .exec(copyViewer)
  57. .add(['-A'])
  58. .commit('updated viewer')
  59. .push();
  60. }