archive.js 3.5 KB

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