70 lines
2.7 KiB
JavaScript
70 lines
2.7 KiB
JavaScript
const fs = require('node:fs')
|
|
const path = require('node:path')
|
|
const { NtExecutable, NtExecutableResource, Data, Resource } = require('resedit')
|
|
|
|
exports.default = async function afterPack(context) {
|
|
if (context.electronPlatformName !== 'win32') return
|
|
|
|
const productFilename = context.packager.appInfo.productFilename
|
|
const exePath = path.join(context.appOutDir, `${productFilename}.exe`)
|
|
const icoPath = path.resolve(__dirname, '../resources/icon.ico')
|
|
|
|
if (!fs.existsSync(exePath) || !fs.existsSync(icoPath)) {
|
|
throw new Error(`afterPack missing target executable (${exePath}) or icon (${icoPath})`)
|
|
}
|
|
|
|
const exe = NtExecutable.from(fs.readFileSync(exePath))
|
|
const res = NtExecutableResource.from(exe)
|
|
const hadAsarIntegrity = res.entries.some(
|
|
(entry) => String(entry.type) === 'INTEGRITY' && String(entry.id) === 'ELECTRONASAR',
|
|
)
|
|
|
|
const iconFile = Data.IconFile.from(fs.readFileSync(icoPath))
|
|
const existingGroups = Resource.IconGroupEntry.fromEntries(res.entries)
|
|
const iconGroupId = existingGroups[0]?.id ?? 1
|
|
const iconLang = existingGroups[0]?.lang ?? 1033
|
|
|
|
Resource.IconGroupEntry.replaceIconsForResource(
|
|
res.entries,
|
|
iconGroupId,
|
|
iconLang,
|
|
iconFile.icons.map((item) => item.data),
|
|
)
|
|
|
|
const versions = Resource.VersionInfo.fromEntries(res.entries)
|
|
if (versions.length > 0) {
|
|
const versionInfo = versions[0]
|
|
const rawVersion = String(context.packager.appInfo.version || '0.0.0')
|
|
const [major = 0, minor = 0, patch = 0] = rawVersion
|
|
.split('-')[0]
|
|
.split('.')
|
|
.map((n) => Number.parseInt(n, 10) || 0)
|
|
const languages = versionInfo.getAllLanguagesForStringValues()
|
|
const targetLangs = languages.length > 0 ? languages : [{ lang: 1033, codepage: 1200 }]
|
|
for (const langInfo of targetLangs) {
|
|
versionInfo.setStringValues(langInfo, {
|
|
ProductName: 'Planet Earth',
|
|
FileDescription: 'Planet Earth',
|
|
CompanyName: 'Linkong Intelligent Media Lab',
|
|
LegalCopyright: 'Copyright © 2026 Linkong Intelligent Media Lab',
|
|
OriginalFilename: `${productFilename}.exe`,
|
|
InternalName: 'Planet Earth',
|
|
})
|
|
versionInfo.setFileVersion(major, minor, patch, 0, langInfo.lang)
|
|
versionInfo.setProductVersion(major, minor, patch, 0, langInfo.lang)
|
|
}
|
|
versionInfo.outputToResourceEntries(res.entries)
|
|
}
|
|
|
|
if (
|
|
hadAsarIntegrity &&
|
|
!res.entries.some((entry) => String(entry.type) === 'INTEGRITY' && String(entry.id) === 'ELECTRONASAR')
|
|
) {
|
|
throw new Error(`afterPack lost INTEGRITY:ELECTRONASAR resource in ${exePath}`)
|
|
}
|
|
|
|
res.outputResource(exe)
|
|
fs.writeFileSync(exePath, Buffer.from(exe.generate()))
|
|
console.log(` • patched Windows PE icon & version info -> ${exePath}`)
|
|
}
|