generate-sri.mjs 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. #!/usr/bin/env node
  2. import crypto from 'node:crypto'
  3. import fs from 'node:fs'
  4. import path from 'node:path'
  5. import { fileURLToPath } from 'node:url'
  6. import sh from 'shelljs'
  7. const __dirname = path.dirname(fileURLToPath(import.meta.url))
  8. sh.config.fatal = true
  9. const configFile = path.join(__dirname, '../_config.yml')
  10. const files = [
  11. {
  12. file: 'dist/css/tabler.min.css',
  13. configPropertyName: 'css_hash'
  14. },
  15. {
  16. file: 'dist/css/tabler.rtl.min.css',
  17. configPropertyName: 'css_rtl_hash'
  18. },
  19. {
  20. file: 'dist/js/tabler.min.js',
  21. configPropertyName: 'js_hash'
  22. },
  23. {
  24. file: 'dist/js/tabler.bundle.min.js',
  25. configPropertyName: 'js_bundle_hash'
  26. }
  27. ]
  28. for (const { file, configPropertyName } of files) {
  29. fs.readFile(file, 'utf8', (error, data) => {
  30. if (error) {
  31. throw error
  32. }
  33. const algorithm = 'sha384'
  34. const hash = crypto.createHash(algorithm).update(data, 'utf8').digest('base64')
  35. const integrity = `${algorithm}-${hash}`
  36. console.log(`${configPropertyName}: ${integrity}`)
  37. sh.sed('-i', new RegExp(`^(\\s+${configPropertyName}:\\s+["'])\\S*(["'])`), `$1${integrity}$2`, configFile)
  38. })
  39. }