forked from ucfopen/Materia-Widget-Dev-Kit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack-generate-widget-hash.js
79 lines (67 loc) · 2.11 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
const fs = require('fs')
const crypto = require('crypto')
const execSync = require('child_process').execSync
function GenerateWidgetHash(options) {
const apply = function(compiler) {
compiler.plugin('emit', function(compilation, callback) {
// if widget isnt in options or it isnt in the output, just warn and exit
if (typeof options.widget == 'undefined' || typeof compilation.assets[options.widget] == 'undefined') {
console.warn('Widget Hash generator couldnt locate ' + options.widget)
callback()
return
}
const wigtData = compilation.assets[options.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 = 'unkown'
var email = 'unkown'
var user = 'unkown'
var gitRemote = 'unkown'
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'
// add checksum to the output (this is how webpack gets assets)
compilation.assets[options.output] = {
source: function() {
return checksumYAML
},
size: function() {
return checksumYAML.length
}
}
callback()
})
}
return {
apply: apply
}
}
module.exports = GenerateWidgetHash