-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
82 lines (76 loc) · 3.26 KB
/
index.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
'use strict'
const fs = require('fs')
const run = require('child_process').execSync
const path = require('path')
/**
* Change the icon of an executable file.
* @param {string} exe - The name of the executable file.
* @param {string} img - The name of the image to be used as an icon.
* You must supply an ICO image.
*/
async function icon(exe, img) {
if (img.endsWith('.ico')) {
let icon = img
console.log(`Changing icon of ${exe} to ${icon} ...`)
return new Promise((resolve, reject) => {
let args = `-open "${exe}" -save "${exe}" -action addoverwrite -res "${icon}" -mask ICONGROUP,MAINICON,`
run(`"${path.join(__dirname, 'resource_hacker', 'ResourceHacker.exe')}" ${args}`)
console.log('Icon successfully changed!')
resolve()
})
} else {
console.error('You must use an .ico format sadly')
}
}
/**
* Change the VersionInfo of an executable file, such as CompanyName, App Description.
* @param {string} exe - The name of the executable file.
* @param {{
* CompanyName: string,
* FileDescription: string,
* FileVersion: string,
* LegalCopyright: string,
* OriginalFilename: string,
* ProductName: string,
* ProductVersion: string
* }} metaData - The desired metadata. FileVersion and ProductVersion must be formatted like 1.2.3.4
*/
async function versionInfo(exe, metaData) {
console.log('Generating VersionInfo script file from the following data:')
console.log(metaData)
await writeScript(metaData)
console.log('Compiling VersionInfo script file into resource file ...')
await compileScript(path.join(__dirname, 'VersionInfo.rc'))
console.log('Deleting VersionInfo script file ...')
await fs.unlinkSync(path.join(__dirname, 'VersionInfo.rc'))
console.log(`Changing VersionInfo of ${exe} ...`)
await changeMetaData(exe, path.join(__dirname, 'VersionInfo.res'))
console.log('Deleting VersionInfo resource file ...')
await fs.unlinkSync(path.join(__dirname, 'VersionInfo.res'))
console.log('VersionInfo updated!')
}
async function writeScript(metaData) {
metaData.FileVersion = metaData.FileVersion || '1.0.0.0'
metaData.ProductVersion = metaData.ProductVersion || '1.0.0.0'
let FILEVERSION = metaData.FileVersion.replace(/\./g, ',')
let PRODUCTVERSION = metaData.ProductVersion.replace(/\./g, ',')
let keys = Object.keys(metaData)
keys.forEach(key => { metaData[key] = ` VALUE "${key}", "${metaData[key]}"` })
let block = Object.values(metaData).join(`\n`)
let scriptContents = fs
.readFileSync(path.join(__dirname, 'template.rc'), 'utf8')
.replace('${FILEVERSION}', FILEVERSION)
.replace('${PRODUCTVERSION}', PRODUCTVERSION)
.replace('${block}', block)
fs.writeFileSync(path.join(__dirname, 'VersionInfo.rc'), scriptContents)
}
async function compileScript(scriptFileName) {
let args = `-open "${scriptFileName}" -save "${scriptFileName.replace('.rc', '.res')}" -action compile -res`
run(`"${path.join(__dirname, 'resource_hacker', 'ResourceHacker.exe')}" ${args}`)
}
async function changeMetaData(exe, resFileName) {
let args = `-open "${exe}" -save "${exe}" -action addoverwrite -res "${resFileName}" -mask versioninfo,1`
run(`"${path.join(__dirname, 'resource_hacker', 'ResourceHacker.exe')}" ${args}`)
}
module.exports.icon = icon;
module.exports.versionInfo = versionInfo;