-
Notifications
You must be signed in to change notification settings - Fork 9
/
webpack-generate-widget-hash.js
85 lines (76 loc) · 2.51 KB
/
webpack-generate-widget-hash.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
const fs = require('fs')
const crypto = require('crypto')
const execSync = require('child_process').execSync
const { sources } = require('webpack');
class GenerateWidgetHash {
constructor(options = {}) {
this.widget = options.widget;
this.output = options.output;
}
apply(compiler) {
const { webpack } = compiler;
const { Compilation } = webpack;
const { RawSource } = webpack.sources;
compiler.hooks.thisCompilation.tap('GenerateWidgetHash', (compilation) => {
// if widget isnt in options or it isnt in the output, just warn and exit
compilation.hooks.processAssets.tapAsync({
name: 'GenerateWidgetHash',
stage: Compilation.PROCESS_ASSETS_STAGE_REPORT, // see below for more stages
},
(assets, callback) => {
if (typeof this.widget == 'undefined' || typeof assets[this.widget] == 'undefined') {
console.warn('Widget Hash generator couldnt locate ' + this.widget)
return
}
// console.log('Assets:')
// Object.entries(assets).forEach(([pathname, source]) => {
// console.log(`— ${pathname}: ${source.size()} bytes`);
// });
const wigtData = assets[this.widget].source()
// calculate hashes based on the wigt file
var hashmd5 = crypto.createHash('md5').update(wigtData).digest('hex')
var hashSha1 = crypto.createHash('sha1').update(wigtData).digest('hex')
var hashsha256 = crypto.createHash('sha256').update(wigtData).digest('hex')
// get some build environment information
var date = new Date()
var gitCommit = 'unknown'
var email = 'unknown'
var user = 'unknown'
var gitRemote = 'unknown'
try {
gitCommit = execSync('git rev-parse HEAD').toString()
gitRemote = execSync('git remote get-url origin').toString()
email = execSync('git config user.email').toString()
user = execSync('git config user.name').toString()
} catch (error) {
console.log('Error getting build data')
console.warn(error)
}
// build checksum content
var checksumYAML =
`build_date: ${date.toISOString()}` +
'\r\n' +
`git: ${gitRemote.trim()}` +
'\r\n' +
`git_version: ${gitCommit.trim()}` +
'\r\n' +
`git_user: ${user.trim()}` +
'\r\n' +
`git_user_email: ${email.trim()}` +
'\r\n' +
`sha1: ${hashSha1.trim()}` +
'\r\n' +
`sha256: ${hashsha256.trim()}` +
'\r\n' +
`md5: ${hashmd5.trim()}` +
'\r\n'
compilation.emitAsset(
this.output,
new RawSource(checksumYAML)
)
callback()
})
})
}
}
module.exports = GenerateWidgetHash