archive.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. const path = require('path')
  2. const { src, dest, parallel } = require('gulp')
  3. const modify = require('gulp-modify-file')
  4. const rename = require('gulp-rename')
  5. const zip = require('gulp-zip')
  6. const { promisifyVinyl } = require('./util')
  7. /*
  8. assumes everything already built
  9. */
  10. exports.archive = parallel(
  11. writeStandardArchive,
  12. writePremiumArchive
  13. )
  14. function writeStandardArchive() {
  15. return writeArchive({
  16. archiveName: 'fullcalendar',
  17. bundleDir: 'packages/bundle',
  18. exampleHtmlFiles: [
  19. '*.html',
  20. '!*+(resource|timeline)*.html',
  21. '!_*.html'
  22. ],
  23. exampleOtherFiles: [
  24. 'js/*',
  25. 'json/*',
  26. '!json/*resource*.json',
  27. 'php/*'
  28. ],
  29. topLevelFiles: [
  30. 'README.md',
  31. 'LICENSE.txt'
  32. ]
  33. })
  34. }
  35. /*
  36. TODO: for examples, instead of looking for (resource|timeline) in the filename,
  37. leverage whether the html file includes packages-premium/bundle or not.
  38. */
  39. function writePremiumArchive() {
  40. return writeArchive({
  41. archiveName: 'fullcalendar-scheduler',
  42. bundleDir: 'packages-premium/bundle',
  43. exampleHtmlFiles: [
  44. '*+(resource|timeline)*.html',
  45. 'timegrid-views-hscroll.html', // TEMPORARY. TODO: exclude this file from non-premium
  46. '!_*.html'
  47. ],
  48. exampleOtherFiles: [
  49. 'js/*',
  50. 'json/*'
  51. ],
  52. topLevelFiles: [
  53. 'packages-premium/README.md',
  54. 'packages-premium/LICENSE.md'
  55. ]
  56. })
  57. }
  58. function writeArchive(options) {
  59. let version = require(path.join(process.cwd(), 'package.json')).version
  60. return writeArchiveFiles(options).then(function(tmpDir) {
  61. return promisifyVinyl(
  62. src(tmpDir + '/**')
  63. .pipe(zip(options.archiveName + '-' + version + '.zip'))
  64. .pipe(dest('archives'))
  65. )
  66. })
  67. }
  68. function writeArchiveFiles(options) {
  69. let bundleDistDir = path.join(options.bundleDir, 'dist')
  70. let tmpDir = path.join('tmp/archives', options.archiveName)
  71. let writingPkgs = promisifyVinyl(
  72. src('**', { cwd: bundleDistDir, base: bundleDistDir }).pipe(
  73. dest(path.join(tmpDir, 'lib'))
  74. )
  75. )
  76. let writingOtherExampleFiles = promisifyVinyl(
  77. src(options.exampleOtherFiles, { cwd: 'examples', base: '.' })
  78. .pipe(dest(tmpDir))
  79. )
  80. let writingTopLevelFiles = promisifyVinyl(
  81. src(options.topLevelFiles)
  82. .pipe(dest(tmpDir))
  83. )
  84. return Promise.all([
  85. writingPkgs,
  86. writeExampleHtmlAndVendor(options.exampleHtmlFiles, tmpDir),
  87. writingOtherExampleFiles,
  88. writingTopLevelFiles
  89. ]).then(function() {
  90. return tmpDir
  91. })
  92. }
  93. function writeExampleHtmlAndVendor(exampleHtmlFiles, tmpDir) {
  94. let vendorPaths = []
  95. let writingExampleHtmlFiles = promisifyVinyl(
  96. src(exampleHtmlFiles, { cwd: 'examples', base: '.' })
  97. .pipe(modify(replaceContent))
  98. .pipe(dest(tmpDir))
  99. )
  100. function replaceContent(content) {
  101. return content.replace(
  102. /((?:src|href)=['"])([^'"]*)(['"])/g,
  103. function(m0, m1, m2, m3) {
  104. return m1 + transformResourcePath(m2) + m3
  105. }
  106. )
  107. }
  108. function transformResourcePath(resourcePath) {
  109. resourcePath = resourcePath.replace(
  110. /^\.\.\/packages(-premium)?\/bundle\/dist\b/,
  111. '../lib'
  112. )
  113. resourcePath = resourcePath.replace(
  114. /^\.\.\/(node_modules\/.*\/([^/]+))$/,
  115. function(m0, m1, m2) {
  116. vendorPaths.push(m1) // the path on the filesystem from proj root
  117. return '../vendor/' + m2 // how the html file will reference it
  118. }
  119. )
  120. return resourcePath
  121. }
  122. return writingExampleHtmlFiles.then(function() {
  123. if (vendorPaths.length) {
  124. return promisifyVinyl(
  125. src(vendorPaths)
  126. .pipe(dest(tmpDir + '/vendor'))
  127. )
  128. }
  129. })
  130. }