#!/usr/bin/env node /*! Copyright Twitter Inc. and other contributors. Licensed under MIT *//* https://github.com/twitter/twemoji/blob/gh-pages/LICENSE */ const fs = require('fs'); const path = require('path'); const { spawnSync } = require('child_process'); function file(...which) { return path.join(__dirname, '..', ...which); } fs.writeFileSync( file('twemoji.npm.js'), [ 'var location = global.location || {};', fs.readFileSync(file('twemoji.js')), 'if (!location.protocol) {', ' twemoji.base = twemoji.base.replace(/^http:/, "");', '}', 'module.exports = twemoji;' ].join('\n') ); fs.writeFileSync( file('twemoji.amd.js'), 'define(function () {\n' + fs.readFileSync(file('twemoji.js')).toString().replace( /^(.)/gm, ' $1' ) + '\n return twemoji;\n});' ); spawnSync( 'yarnpkg', [ 'uglifyjs', '--verbose', file('twemoji.js'), '-o', file('twemoji.tmp.js') ] ); fs.writeFileSync( file('twemoji.min.js'), '/*! Copyright Twitter Inc. and other contributors. Licensed under MIT */\n' + fs.readFileSync(file('twemoji.tmp.js')) ); fs.unlinkSync(file('twemoji.tmp.js')); const algorithm = 'sha384'; const digest = spawnSync('openssl', ['dgst', `-${algorithm}`, '-binary', file('twemoji.min.js')]); if (digest.status || digest.signal){ throw new Error(digest.stderr.toString('utf8')); } const integrity = `integrity="${algorithm}-${digest.stdout.toString('base64')}"`; function updateIntegrity(filename) { fs.writeFileSync( filename, fs.readFileSync(filename).toString('utf8').replace(/integrity="[^ ]*"/, integrity)); } updateIntegrity(file('..', 'README.md')); updateIntegrity(file('templates', 'preview.html'));