@@ -4,7 +4,7 @@ import {exec as _exec, spawn} from "node:child_process";
44import { isLinux , isMac , isWin , platformArch , platformName , platformUUID , platformVersion } from "../../lib/env" ;
55import { Log } from "../log/index" ;
66import iconv from "iconv-lite" ;
7- import { ShellUtil , StrUtil } from "../../lib/util" ;
7+ import { IconvUtil , ShellUtil , StrUtil } from "../../lib/util" ;
88import { AppConfig } from "../../../src/config" ;
99
1010const exec = util . promisify ( _exec )
@@ -16,8 +16,17 @@ const outputStringConvert = (outputEncoding: 'utf8' | 'cp936', data: any) => {
1616 if ( outputEncoding === 'utf8' ) {
1717 return data . toString ( )
1818 }
19- // convert outputEncoding(cp936) to utf8
20- return iconv . decode ( Buffer . from ( data , 'binary' ) , outputEncoding )
19+ let dataEncoding = 'binary'
20+ if ( Buffer . isBuffer ( data ) ) {
21+ dataEncoding = IconvUtil . detect ( data )
22+ if ( 'UTF-8' === dataEncoding ) {
23+ return data . toString ( 'utf8' )
24+ }
25+ }
26+ // dataEncoding UTF-8 cp936
27+ // dataEncoding ISO-8859-1 cp936
28+ // console.log('dataEncoding', dataEncoding, outputEncoding)
29+ return iconv . decode ( Buffer . from ( data , dataEncoding as any ) , outputEncoding )
2130}
2231
2332const shell = async ( command : string , option ?: {
@@ -83,23 +92,16 @@ const spawnShell = async (command: string | string[], option: {
8392 let exitCode = - 1
8493 const stdoutList : string [ ] = [ ]
8594 const stderrList : string [ ] = [ ]
86- const outputStringConvert = ( data : any ) => {
87- if ( option . outputEncoding === 'utf8' ) {
88- return data . toString ( )
89- }
90- // convert outputEncoding(cp936) to utf8
91- return iconv . decode ( Buffer . from ( data , 'binary' ) , option . outputEncoding )
92- }
9395 spawnProcess . stdout ?. on ( 'data' , ( data ) => {
9496 // console.log('App.spawnShell.stdout', data)
95- let dataString = outputStringConvert ( data )
97+ let dataString = outputStringConvert ( option . outputEncoding as any , data )
9698 Log . info ( 'App.spawnShell.stdout' , dataString )
9799 stdoutList . push ( dataString )
98100 option . stdout ?.( dataString , spawnProcess )
99101 } )
100102 spawnProcess . stderr ?. on ( 'data' , ( data ) => {
101103 // console.log('App.spawnShell.stderr', data)
102- let dataString = outputStringConvert ( data )
104+ let dataString = outputStringConvert ( option . outputEncoding as any , data )
103105 Log . info ( 'App.spawnShell.stderr' , dataString )
104106 stderrList . push ( dataString )
105107 option . stderr ?.( dataString , spawnProcess )
@@ -138,10 +140,10 @@ const spawnShell = async (command: string | string[], option: {
138140 encoding : 'binary'
139141 } , ( err , stdout , stderr ) => {
140142 if ( stdout ) {
141- stdout = outputStringConvert ( stdout )
143+ stdout = outputStringConvert ( option . outputEncoding as any , stdout )
142144 }
143145 if ( stderr ) {
144- stderr = outputStringConvert ( stderr )
146+ stderr = outputStringConvert ( option . outputEncoding as any , stderr )
145147 }
146148 Log . info ( 'App.spawnShell.stop.taskkill' , JSON . parse ( JSON . stringify ( { err, stdout, stderr} ) ) )
147149 } )
0 commit comments