11const child_process = require ( "child_process" ) ;
22const fs = require ( "fs" ) ;
3- const { default : path } = require ( "path" ) ;
3+ const path = require ( "path" ) ;
44
5- module . exports = function ( filePath ) {
6- const { WINDOWS_SIGN_EXECUTABLE } = process . env ;
7-
8- const stats = fs . lstatSync ( filePath ) ;
9- console . log ( filePath , stats ) ;
5+ const LOG_LOCATION = "c:\\ev_signer_trilium\\ev_signer_trilium.err.log" ;
6+ const { WINDOWS_SIGN_EXECUTABLE } = process . env ;
107
8+ module . exports = function ( sourcePath ) {
119 if ( ! WINDOWS_SIGN_EXECUTABLE ) {
1210 console . warn ( "[Sign] Skip signing due to missing environment variable." ) ;
1311 return ;
1412 }
1513
1614 const outputDir = path . join ( __dirname , "sign" ) ;
17- console . log ( "Output dir is " , path . resolve ( outputDir ) ) ;
1815 if ( ! fs . existsSync ( outputDir ) ) {
1916 fs . mkdirSync ( outputDir ) ;
2017 }
2118
22- fs . copyFileSync ( sourcePath , destPath ) ;
19+ try {
20+ const destPath = path . resolve ( path . join ( outputDir , path . basename ( sourcePath ) ) ) ;
21+ fs . copyFileSync ( sourcePath , destPath ) ;
22+ console . log ( [ "[Sign] Copying file to signing location" , destPath ] . join ( " " ) ) ;
23+ const command = `${ WINDOWS_SIGN_EXECUTABLE } --executable "${ sourcePath } "` ;
24+ console . log ( `[Sign] ${ command } ` ) ;
25+
26+ child_process . execSync ( command ) ;
27+ } catch ( e ) {
28+ console . error ( "[Sign] Got error while signing " + e . output . toString ( "utf-8" ) ) ;
29+ printSigningErrorLogs ( sourcePath ) ;
30+ process . exit ( 2 ) ;
31+ }
32+ }
33+
34+ function printSigningErrorLogs ( sourcePath ) {
35+ console . log ( "Platform: " , process . platform ) ;
36+ console . log ( "CPU archi:" , process . arch ) ;
37+ console . log ( "DLL archi: " , getDllArchitectureFromFile ( sourcePath ) ) ;
38+ console . log ( "Signer archi: " , getDllArchitectureFromFile ( WINDOWS_SIGN_EXECUTABLE ) ) ;
2339
24- const command = `${ WINDOWS_SIGN_EXECUTABLE } --executable "${ filePath } "` ;
25- console . log ( `[Sign] ${ command } ` ) ;
40+ if ( ! fs . existsSync ( LOG_LOCATION ) ) {
41+ console . warn ( "[Sign] No debug log file found." ) ;
42+ return ;
43+ }
2644
27- const output = child_process . execSync ( command ) ;
28- console . log ( `[Sign] ${ output } ` ) ;
29- }
45+ const logContent = fs . readFileSync ( LOG_LOCATION , "utf-8" ) ;
46+ console . error ( "[Sign] Debug log content:\n" + logContent ) ;
47+ }
48+
49+ function getDllArchitectureFromFile ( filePath ) {
50+ const buffer = fs . readFileSync ( filePath ) ;
51+
52+ // Check for MZ header
53+ if ( buffer [ 0 ] !== 0x4D || buffer [ 1 ] !== 0x5A ) {
54+ return 'Not a PE file (missing MZ header)' ;
55+ }
56+
57+ // Offset to PE header
58+ const peHeaderOffset = buffer . readUInt32LE ( 0x3C ) ;
59+
60+ // Confirm PE signature
61+ const peSig = buffer . toString ( 'utf8' , peHeaderOffset , peHeaderOffset + 4 ) ;
62+ if ( peSig !== 'PE\u0000\u0000' ) {
63+ return 'Invalid PE header' ;
64+ }
65+
66+ // Machine field is 2 bytes at PE header + 4
67+ const machine = buffer . readUInt16LE ( peHeaderOffset + 4 ) ;
68+
69+ const archMap = {
70+ 0x014c : 'x86 (32-bit)' ,
71+ 0x8664 : 'x64 (64-bit)' ,
72+ 0x01c4 : 'ARM (32-bit)' ,
73+ 0xaa64 : 'ARM64' ,
74+ } ;
75+
76+ return archMap [ machine ] || `Unknown (0x${ machine . toString ( 16 ) } )` ;
77+ }
0 commit comments