Skip to content

Commit bd2329e

Browse files
authored
build: fix release scripts (#460)
1 parent 07446fc commit bd2329e

File tree

3 files changed

+82
-0
lines changed

3 files changed

+82
-0
lines changed

scripts/readme.js

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
var fs = require('fs');
2+
var path = require('path');
3+
4+
// Update the README with the minified snippet.
5+
var cwd = process.cwd();
6+
var readmeFilename = path.join(cwd, 'README.md');
7+
var readme = fs.readFileSync(readmeFilename, 'utf-8');
8+
9+
var snippetFilename = path.join(cwd, 'amplitude-snippet.min.js');
10+
var snippet = fs.readFileSync(snippetFilename, 'utf-8');
11+
var script =
12+
' <script type="text/javascript">\n' +
13+
snippet.trim().replace(/^/gm, ' ') +
14+
'\n\n' +
15+
' amplitude.getInstance().init("YOUR_API_KEY_HERE");\n' +
16+
' </script>';
17+
18+
var updated = readme.replace(/ +<script[\s\S]+?script>/, script);
19+
fs.writeFileSync(readmeFilename, updated);
20+
21+
console.log('Updated README.md');

scripts/release.js

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
var fs = require('fs-extra');
2+
var path = require('path');
3+
const { version } = require('../package');
4+
var exec = require('child_process').exec;
5+
6+
var cwd = process.cwd();
7+
8+
var file = path.join(cwd, 'dist', 'amplitude-' + version + '.js');
9+
var minfile = path.join(cwd, 'dist', 'amplitude-' + version + '-min.js');
10+
var mingzfile = path.join(cwd, 'dist', 'amplitude-' + version + '-min.gz.js');
11+
12+
fs.copySync(path.join(cwd, 'amplitude.js'), file);
13+
fs.copySync(path.join(cwd, 'amplitude.min.js'), minfile);
14+
exec('gzip < ' + minfile + ' > ' + mingzfile);
15+
16+
const umdFile = path.join(cwd, 'dist', 'amplitude-' + version + '.umd.js');
17+
const umdMinfile = path.join(cwd, 'dist', 'amplitude-' + version + '-min.umd.js');
18+
const umdMingzfile = path.join(cwd, 'dist', 'amplitude-' + version + '-min.umd.gz.js');
19+
20+
fs.copySync(path.join(cwd, 'amplitude.umd.js'), umdFile);
21+
fs.copySync(path.join(cwd, 'amplitude.min.js'), umdMinfile);
22+
exec('gzip < ' + umdMinfile + ' > ' + umdMingzfile);

scripts/version.js

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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

Comments
 (0)