| 1 |
#!/usr/bin/env node |
| 2 |
|
| 3 |
'use strict'; |
| 4 |
|
| 5 |
/*! |
| 6 |
* Script to generate SRI hashes for use in our docs. |
| 7 |
* Remember to use the same vendor files as the CDN ones, |
| 8 |
* otherwise the hashes won't match! |
| 9 |
* |
| 10 |
* Copyright 2017-2019 The Bootstrap Authors |
| 11 |
* Copyright 2017-2019 Twitter, Inc. |
| 12 |
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE) |
| 13 |
*/ |
| 14 |
|
| 15 |
var crypto = require('crypto'); |
| 16 |
var fs = require('fs'); |
| 17 |
var path = require('path'); |
| 18 |
var replace = require('replace-in-file'); |
| 19 |
|
| 20 |
var configFile = path.join(__dirname, '../_config.yml'); |
| 21 |
|
| 22 |
// Array of objects which holds the files to generate SRI hashes for. |
| 23 |
// `file` is the path from the root folder |
| 24 |
// `configPropertyName` is the _config.yml variable's name of the file |
| 25 |
var files = [ |
| 26 |
{ |
| 27 |
file: 'dist/css/bootstrap.min.css', |
| 28 |
configPropertyName: 'css_hash' |
| 29 |
}, |
| 30 |
{ |
| 31 |
file: 'dist/css/bootstrap-theme.min.css', |
| 32 |
configPropertyName: 'css_theme_hash' |
| 33 |
}, |
| 34 |
{ |
| 35 |
file: 'dist/js/bootstrap.min.js', |
| 36 |
configPropertyName: 'js_hash' |
| 37 |
} |
| 38 |
]; |
| 39 |
|
| 40 |
files.forEach(function (file) { |
| 41 |
fs.readFile(file.file, 'utf8', function (err, data) { |
| 42 |
if (err) { |
| 43 |
throw err; |
| 44 |
} |
| 45 |
|
| 46 |
var algo = 'sha384'; |
| 47 |
var hash = crypto.createHash(algo).update(data, 'utf8').digest('base64'); |
| 48 |
var integrity = algo + '-' + hash; |
| 49 |
|
| 50 |
console.log(file.configPropertyName + ': ' + integrity); |
| 51 |
|
| 52 |
try { |
| 53 |
replace.sync({ |
| 54 |
files: configFile, |
| 55 |
from: new RegExp('(\\s' + file.configPropertyName + ':\\s+"|\')(\\S+)("|\')'), |
| 56 |
to: '$1' + integrity + '$3' |
| 57 |
}); |
| 58 |
} catch (error) { |
| 59 |
console.error('Error occurred:', error); |
| 60 |
} |
| 61 |
}); |
| 62 |
}); |