Skip to content

Commit

Permalink
Add a simple script to generate SRI hashes.
Browse files Browse the repository at this point in the history
  • Loading branch information
XhmikosR committed Nov 17, 2017
1 parent 8df34b0 commit fb1ea5f
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 0 deletions.
53 changes: 53 additions & 0 deletions build/generate-sri.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#!/usr/bin/env node

/*!
* Script to generate SRI hashes for use in our docs.
* Copyright 2017 The Bootstrap Authors
* Copyright 2017 Twitter, Inc.
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
*/

'use strict'

const fs = require('fs')
const path = require('path')
const sriToolbox = require('sri-toolbox')
const sh = require('shelljs')
const sed = sh.sed

sh.config.fatal = true

const configFile = path.join(__dirname, '..', '_config.yml')

const files = [
{
file: 'dist/css/bootstrap.min.css',
yamlName: 'css_hash'
},
{
file: 'dist/js/bootstrap.min.js',
yamlName: 'js_hash'
},
{
file: 'assets/js/vendor/jquery-slim.min.js',
yamlName: 'jquery_hash'
},
{
file: 'assets/js/vendor/popper.min.js',
yamlName: 'popper_hash'
}
]

files.forEach((file) => {
fs.readFile(file.file, 'utf8', (err, data) => {
if (err) {
throw err
}

const integrity = sriToolbox.generate({
algorithms: ['sha384']
}, data)

sed('-i', new RegExp(`(\\s${file.yamlName}:\\s+"|')(\\S+)("|')`), '$1' + integrity + '$3', configFile)
})
})
6 changes: 6 additions & 0 deletions build/ship.sh
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ printf "\n${magenta}Compile latest CSS and JS...${end}"
printf "\n${magenta}=======================================================\n${end}"
npm run dist

# Generate the SRI hashes
printf "\n${magenta}=======================================================${end}"
printf "\n${magenta}Generate the SRI hashes...${end}"
printf "\n${magenta}=======================================================\n${end}"
npm run release-sri

# Compress the dist files
printf "\n${magenta}=======================================================${end}"
printf "\n${magenta}Compressing the dist files...${end}"
Expand Down
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
"docs-upload-preview": "build/upload-preview.sh",
"docs-workbox-precache": "node build/workbox.js",
"maintenance-dependencies": "ncu -a -x jquery && npm update && bundle update && shx echo 'Manually update assets/js/vendor/*, js/tests/vendor/* and .travis.yml'",
"release-sri": "node build/generate-sri.js",
"release-version": "node build/change-version.js",
"release-zip": "cd dist/ && zip -r9 bootstrap-$npm_package_version-dist.zip * && shx mv bootstrap-$npm_package_version-dist.zip ..",
"dist": "npm-run-all --parallel css js",
Expand Down Expand Up @@ -106,6 +107,7 @@
"rollup-plugin-node-resolve": "^3.0.0",
"shelljs": "^0.7.8",
"shx": "^0.2.2",
"sri-toolbox": "^0.2.0",
"stylelint": "^8.2.0",
"stylelint-config-recommended-scss": "^2.0.0",
"stylelint-config-standard": "^17.0.0",
Expand Down

0 comments on commit fb1ea5f

Please sign in to comment.