archive.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. pkgFiles: [
  18. 'packages/*/dist/**'
  19. ],
  20. exampleHtmlFiles: [
  21. '*.html',
  22. '!*+(resource|timeline)*.html',
  23. '!_*.html'
  24. ],
  25. exampleOtherFiles: [
  26. 'js/*',
  27. 'json/*',
  28. '!json/*resource*.json',
  29. 'php/*'
  30. ],
  31. topLevelFiles: [
  32. 'README.md',
  33. 'LICENSE.txt'
  34. ]
  35. })
  36. }
  37. function writePremiumArchive() {
  38. return writeArchive({
  39. archiveName: 'fullcalendar-scheduler',
  40. pkgFiles: [
  41. 'packages?(-premium)/*/dist/**'
  42. ],
  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 tmpDir = path.join('tmp/archives', options.archiveName)
  69. // packages/whatever/dist/file.js -> packages/whatever/file.js
  70. let writingPkgs = promisifyVinyl(
  71. src(
  72. options.pkgFiles.concat([ '!**/dist' ]), // hack to prevent empty dist dir
  73. { base: '.' }
  74. ).pipe(
  75. rename((pathParts) => {
  76. pathParts.dirname = transformPkgPath(pathParts.dirname)
  77. })
  78. ).pipe(dest(tmpDir))
  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. if (resourcePath.indexOf('../packages') === 0) { // one of our package files
  114. resourcePath = transformPkgPath(resourcePath)
  115. }
  116. resourcePath = resourcePath.replace(
  117. /^\.\.\/(node_modules\/.*\/([^/]+))$/,
  118. function(m0, m1, m2) {
  119. vendorPaths.push(m1) // the path on the filesystem from proj root
  120. return '../vendor/' + m2 // how the html file will reference it
  121. }
  122. )
  123. return resourcePath
  124. }
  125. return writingExampleHtmlFiles.then(function() {
  126. if (vendorPaths.length) {
  127. return promisifyVinyl(
  128. src(vendorPaths)
  129. .pipe(dest(tmpDir + '/vendor'))
  130. )
  131. }
  132. })
  133. }
  134. function transformPkgPath(path) {
  135. return path.replace(/(^|\/)dist(\/|$)/, '$2')
  136. }