#!/usr/bin/env node

/*!
 * Script to create the built examples zip archive;
 * requires the `zip` command to be present!
 * Copyright 2020 The Bootstrap Authors
 * Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
 */

'use strict'

const path = require('path')
const sh = require('shelljs')

const { version, version_short: versionShort } = require('../package.json')

const folderName = `bootstrap-${version}-examples`

sh.config.fatal = true

if (!sh.test('-d', '_gh_pages')) {
  throw new Error('The _gh_pages folder does not exist, did you forget building the docs?')
}

// switch to the root dir
sh.cd(path.join(__dirname, '..'))

// remove any previously created folder with the same name
sh.rm('-rf', folderName)
sh.mkdir('-p', folderName)

// copy the examples and dist folders; for the examples we use `*`
// so that its content are copied to the root dist dir
sh.cp('-Rf', [
  `_gh_pages/docs/${versionShort}/examples/*`,
  `_gh_pages/docs/${versionShort}/dist/`
], folderName)
sh.rm(`${folderName}/index.html`)

// sed-fu
sh.find(`${folderName}/**/*.html`).forEach(file => {
  sh.sed('-i', new RegExp(`"/docs/${versionShort}/`, 'g'), '"../', file)
  sh.sed('-i', /(<link href="\.\.\/.*) integrity=".*>/g, '$1>', file)
  sh.sed('-i', /(<script src="\.\.\/.*) integrity=".*>/g, '$1></script>', file)
})

// create the zip file
sh.exec(`zip -r9 "${folderName}.zip" "${folderName}"`, { fatal: true })

// remove the folder we created
sh.rm('-rf', folderName)

@@ -523,16 +523,17 @@
 or('The 
+%22
 _gh_page
@@ -533,16 +533,17 @@
 gh_pages
+%22
  folder 
@@ -746,157 +746,327 @@
 me)%0A
-sh.mkdir('-p', folderName)%0A%0A// copy the examples and dist folders; for the examples we use %60*%60%0A// so that its content are copied to the root dist dir
+// create any folders so that %60cp%60 works%0Ash.mkdir('-p', folderName)%0Ash.mkdir('-p', %60$%7BfolderName%7D/assets/brand/%60)%0A%0Ash.cp('-Rf', %60_gh_pages/docs/$%7BversionShort%7D/examples/*%60, folderName)%0Ash.cp('-Rf', %60_gh_pages/docs/$%7BversionShort%7D/dist/%60, %60$%7BfolderName%7D/assets/%60)%0A// also copy the two brand images we use in the examples
 %0Ash.
@@ -1066,25 +1066,24 @@
 les%0Ash.cp('-
-R
 f', %5B%0A  %60_gh
@@ -1110,26 +1110,50 @@
 nShort%7D/
-examples/*
+assets/brand/bootstrap-outline.svg
 %60,%0A  %60_g
@@ -1185,18 +1185,48 @@
 rt%7D/
-dist/
+assets/brand/bootstrap-solid.svg
 %60%0A%5D, 
+%60$%7B
 fold
@@ -1227,24 +1227,40 @@
 $%7BfolderName
+%7D/assets/brand/%60
 )%0Ash.rm(%60$%7Bf
@@ -1291,14 +1291,36 @@
 %0A// 
-sed-fu
+get all examples' HTML files
 %0Ash.
@@ -1375,94 +1375,185 @@
 %7B%0A  
-sh.sed('-i', new RegExp(%60%22/docs/$%7BversionShort%7D/%60, 'g'), '%22../', file)%0A  sh.sed('-i', 
+const fileContents = sh.cat(file)%0A    .toString()%0A    .replace(new RegExp(%60%22/docs/$%7BversionShort%7D/%60, 'g'), '%22../')%0A    .replace(/%22..%5C/dist%5C//g, '%22../assets/dist/')%0A    .replace(
 /(%3Cl
@@ -1599,31 +1599,23 @@
 $1%3E'
-, file)%0A  sh.sed('-i', 
+)%0A    .replace(
 /(%3Cs
@@ -1667,18 +1667,120 @@
 script%3E'
-, 
+)%0A    .replace(/( +)%3C!-- favicons(.%7C%5Cn)+%3Cstyle%3E/i, '    %3Cstyle%3E')%0A  new sh.ShellString(fileContents).to(
 file)%0A%7D)

Improve build/zip-examples.js (#30759)