zip-examples.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #!/usr/bin/env node
  2. /*!
  3. * Script to create the built examples zip archive;
  4. * requires the `zip` command to be present!
  5. * Copyright 2020-2021 The Bootstrap Authors
  6. * Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
  7. */
  8. 'use strict'
  9. const path = require('path')
  10. const sh = require('shelljs')
  11. const pkg = require('../package.json')
  12. const versionShort = pkg.config.version_short
  13. const distFolder = `bootstrap-${pkg.version}-examples`
  14. const rootDocsDir = '_site'
  15. const docsDir = `${rootDocsDir}/docs/${versionShort}/`
  16. // these are the files we need in the examples
  17. const cssFiles = [
  18. 'bootstrap.min.css',
  19. 'bootstrap.min.css.map',
  20. 'bootstrap.rtl.min.css',
  21. 'bootstrap.rtl.min.css.map'
  22. ]
  23. const jsFiles = [
  24. 'bootstrap.bundle.min.js',
  25. 'bootstrap.bundle.min.js.map'
  26. ]
  27. const imgFiles = [
  28. 'bootstrap-logo.svg',
  29. 'bootstrap-logo-white.svg'
  30. ]
  31. sh.config.fatal = true
  32. if (!sh.test('-d', rootDocsDir)) {
  33. throw new Error(`The "${rootDocsDir}" folder does not exist, did you forget building the docs?`)
  34. }
  35. // switch to the root dir
  36. sh.cd(path.join(__dirname, '..'))
  37. // remove any previously created folder/zip with the same name
  38. sh.rm('-rf', [distFolder, `${distFolder}.zip`])
  39. // create any folders so that `cp` works
  40. sh.mkdir('-p', [
  41. distFolder,
  42. `${distFolder}/assets/brand/`,
  43. `${distFolder}/assets/dist/css/`,
  44. `${distFolder}/assets/dist/js/`
  45. ])
  46. sh.cp('-Rf', `${docsDir}/examples/*`, distFolder)
  47. cssFiles.forEach(file => {
  48. sh.cp('-f', `${docsDir}/dist/css/${file}`, `${distFolder}/assets/dist/css/`)
  49. })
  50. jsFiles.forEach(file => {
  51. sh.cp('-f', `${docsDir}/dist/js/${file}`, `${distFolder}/assets/dist/js/`)
  52. })
  53. imgFiles.forEach(file => {
  54. sh.cp('-f', `${docsDir}/assets/brand/${file}`, `${distFolder}/assets/brand/`)
  55. })
  56. sh.rm(`${distFolder}/index.html`)
  57. // get all examples' HTML files
  58. sh.find(`${distFolder}/**/*.html`).forEach(file => {
  59. const fileContents = sh.cat(file)
  60. .toString()
  61. .replace(new RegExp(`"/docs/${versionShort}/`, 'g'), '"../')
  62. .replace(/"..\/dist\//g, '"../assets/dist/')
  63. .replace(/(<link href="\.\.\/.*) integrity=".*>/g, '$1>')
  64. .replace(/(<script src="\.\.\/.*) integrity=".*>/g, '$1></script>')
  65. .replace(/( +)<!-- favicons(.|\n)+<style>/i, ' <style>')
  66. new sh.ShellString(fileContents).to(file)
  67. })
  68. // create the zip file
  69. sh.exec(`zip -r9 "${distFolder}.zip" "${distFolder}"`)
  70. // remove the folder we created
  71. sh.rm('-rf', distFolder)