|
| 1 | +const fs = require('fs'); |
| 2 | +const path = require('path'); |
| 3 | +const { version } = require('../package'); |
| 4 | +const crypto = require('crypto'); |
| 5 | + |
| 6 | +const cwd = process.cwd(); |
| 7 | + |
| 8 | +function replaceTextInFile(filepath, match, replacement) { |
| 9 | + var filename = path.join(cwd, filepath); |
| 10 | + |
| 11 | + const updatedText = fs.readFileSync(filename, 'utf-8').replace(match, replacement); |
| 12 | + |
| 13 | + if (updatedText.indexOf(replacement) === -1) { |
| 14 | + throw new Error(`Failed to update text in ${filepath}`); |
| 15 | + } |
| 16 | + |
| 17 | + fs.writeFileSync(filename, updatedText); |
| 18 | + |
| 19 | + console.log(`Updated ${filepath}: ${replacement}`); |
| 20 | +} |
| 21 | + |
| 22 | +// Update version in snippet |
| 23 | +replaceTextInFile( |
| 24 | + path.join('src', 'amplitude-snippet.js'), |
| 25 | + /cdn\.amplitude\.com\/libs\/amplitude-[0-9]+\.[0-9]+\.[0-9]+-min\.gz\.js/, |
| 26 | + `cdn.amplitude.com/libs/amplitude-${version}-min.gz.js`, |
| 27 | +); |
| 28 | + |
| 29 | +// Update integrity hash in snippet |
| 30 | +// Provides extra layer of security. If script changes, it will fail to load |
| 31 | +const sdkText = fs.readFileSync(path.join('.', `amplitude.min.js`), 'utf-8'); |
| 32 | +const hash = crypto.createHash('sha384').update(sdkText).digest('base64'); |
| 33 | +replaceTextInFile( |
| 34 | + path.join('src', 'amplitude-snippet.js'), |
| 35 | + /as.integrity = 'sha384-[a-zA-Z0-9+/]+';/, |
| 36 | + `as.integrity = 'sha384-${hash}';`, |
| 37 | +); |
| 38 | + |
| 39 | +console.log(`Updated version to ${version}`); |
0 commit comments