archive.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. '!_*.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 bundleDistDir = path.join(options.bundleDir, 'dist')
  69. let tmpDir = path.join('tmp/archives', options.archiveName)
  70. let writingPkgs = promisifyVinyl(
  71. src('**', { cwd: bundleDistDir, base: bundleDistDir }).pipe(
  72. dest(path.join(tmpDir, 'lib'))
  73. )
  74. )
  75. let writingOtherExampleFiles = promisifyVinyl(
  76. src(options.exampleOtherFiles, { cwd: 'examples', base: '.' })
  77. .pipe(dest(tmpDir))
  78. )
  79. let writingTopLevelFiles = promisifyVinyl(
  80. src(options.topLevelFiles)
  81. .pipe(dest(tmpDir))
  82. )
  83. return Promise.all([
  84. writingPkgs,
  85. writeExampleHtmlAndVendor(options.exampleHtmlFiles, tmpDir),
  86. writingOtherExampleFiles,
  87. writingTopLevelFiles
  88. ]).then(function() {
  89. return tmpDir
  90. })
  91. }
  92. function writeExampleHtmlAndVendor(exampleHtmlFiles, tmpDir) {
  93. let vendorPaths = []
  94. let writingExampleHtmlFiles = promisifyVinyl(
  95. src(exampleHtmlFiles, { cwd: 'examples', base: '.' })
  96. .pipe(modify(replaceContent))
  97. .pipe(dest(tmpDir))
  98. )
  99. function replaceContent(content) {
  100. return content.replace(
  101. /((?:src|href)=['"])([^'"]*)(['"])/g,
  102. function(m0, m1, m2, m3) {
  103. return m1 + transformResourcePath(m2) + m3
  104. }
  105. )
  106. }
  107. function transformResourcePath(resourcePath) {
  108. resourcePath = resourcePath.replace(
  109. /^\.\.\/packages(-premium)?\/bundle\/dist\b/,
  110. '../lib'
  111. )
  112. resourcePath = resourcePath.replace(
  113. /^\.\.\/(node_modules\/.*\/([^/]+))$/,
  114. function(m0, m1, m2) {
  115. vendorPaths.push(m1) // the path on the filesystem from proj root
  116. return '../vendor/' + m2 // how the html file will reference it
  117. }
  118. )
  119. return resourcePath
  120. }
  121. return writingExampleHtmlFiles.then(function() {
  122. if (vendorPaths.length) {
  123. return promisifyVinyl(
  124. src(vendorPaths)
  125. .pipe(dest(tmpDir + '/vendor'))
  126. )
  127. }
  128. })
  129. }