zip-examples.mjs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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-2025 The Bootstrap Authors
  6. * Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
  7. */
  8. import fs from 'node:fs/promises'
  9. import path from 'node:path'
  10. import { fileURLToPath } from 'node:url'
  11. import sh from 'shelljs'
  12. const __dirname = path.dirname(fileURLToPath(import.meta.url))
  13. const pkgJson = path.join(__dirname, '../package.json')
  14. const pkg = JSON.parse(await fs.readFile(pkgJson, 'utf8'))
  15. const versionShort = pkg.config.version_short
  16. const distFolder = `bootstrap-${pkg.version}-examples`
  17. const rootDocsDir = '_site'
  18. const docsDir = `${rootDocsDir}/docs/${versionShort}/`
  19. // these are the files we need in the examples
  20. const cssFiles = [
  21. 'bootstrap.min.css',
  22. 'bootstrap.min.css.map',
  23. 'bootstrap.rtl.min.css',
  24. 'bootstrap.rtl.min.css.map'
  25. ]
  26. const jsFiles = [
  27. 'bootstrap.bundle.min.js',
  28. 'bootstrap.bundle.min.js.map'
  29. ]
  30. const imgFiles = [
  31. 'bootstrap-logo.svg',
  32. 'bootstrap-logo-white.svg'
  33. ]
  34. const staticJsFiles = [
  35. 'color-modes.js'
  36. ]
  37. sh.config.fatal = true
  38. if (!sh.test('-d', rootDocsDir)) {
  39. throw new Error(`The "${rootDocsDir}" folder does not exist, did you forget building the docs?`)
  40. }
  41. // switch to the root dir
  42. sh.cd(path.join(__dirname, '..'))
  43. // remove any previously created folder/zip with the same name
  44. sh.rm('-rf', [distFolder, `${distFolder}.zip`])
  45. // create any folders so that `cp` works
  46. sh.mkdir('-p', [
  47. distFolder,
  48. `${distFolder}/assets/brand/`,
  49. `${distFolder}/assets/dist/css/`,
  50. `${distFolder}/assets/dist/js/`,
  51. `${distFolder}/assets/js/`
  52. ])
  53. sh.cp('-Rf', `${docsDir}/examples/*`, distFolder)
  54. for (const file of cssFiles) {
  55. sh.cp('-f', `${docsDir}/dist/css/${file}`, `${distFolder}/assets/dist/css/`)
  56. }
  57. for (const file of jsFiles) {
  58. sh.cp('-f', `${docsDir}/dist/js/${file}`, `${distFolder}/assets/dist/js/`)
  59. }
  60. for (const file of imgFiles) {
  61. sh.cp('-f', `${docsDir}/assets/brand/${file}`, `${distFolder}/assets/brand/`)
  62. }
  63. for (const file of staticJsFiles) {
  64. sh.cp('-f', `${docsDir}/assets/js/${file}`, `${distFolder}/assets/js/`)
  65. }
  66. sh.rm(`${distFolder}/index.html`)
  67. // get all examples' HTML files
  68. for (const file of sh.find(`${distFolder}/**/*.html`)) {
  69. const fileContents = sh.cat(file)
  70. .toString()
  71. .replace(new RegExp(`"/docs/${versionShort}/`, 'g'), '"../')
  72. .replace(/"..\/dist\//g, '"../assets/dist/')
  73. .replace(/(<link href="\.\.\/.*) integrity=".*>/g, '$1>')
  74. .replace(/(<script src="\.\.\/.*) integrity=".*>/g, '$1></script>')
  75. .replace(/( +)<!-- favicons(.|\n)+<style>/i, ' <style>')
  76. new sh.ShellString(fileContents).to(file)
  77. }
  78. // create the zip file
  79. sh.exec(`zip -qr9 "${distFolder}.zip" "${distFolder}"`)
  80. // remove the folder we created
  81. sh.rm('-rf', distFolder)