import { app , session } from 'electron' ; import debugFactory from 'debug' ; const debug = debugFactory ( 'ccm:common' ) ; function checkIfCalledViaCLI ( args ) { if ( args [ 0 ] ?. endsWith ( '.app/Contents/MacOS/clash-config-manager' ) ) { const restArgs = args . slice ( 1 ) . filter ( ( arg ) => ! [ '--inspect' , '--inspect-brk' ] . includes ( arg ) ) ; const isCli = ! ! restArgs . length ; return { isCli , restArgs } ; } const scriptIndex = args . findIndex ( ( arg ) => arg ?. endsWith ( 'bundle/development/main/index.js' ) ) ; if ( scriptIndex === - 1 ) { return { isCli : false } ; } else { const restArgs = args . slice ( scriptIndex + 1 ) . filter ( ( arg ) => ! [ '--inspect' , '--inspect-brk' ] . includes ( arg ) ) ; const isCli = ! ! restArgs . length ; return { isCli , restArgs } ; } } export const isCli = process . env . CCM_RUN_MODE === 'cli' ; const { restArgs } = checkIfCalledViaCLI ( process . argv ) ; export { restArgs } ; debug ( 'process.argv: %O' , { 'process.argv' : process . argv , isCli , restArgs , } ) ; export async function initCommon ( ) { await app . whenReady ( ) ; session . defaultSession . webRequest . onBeforeSendHeaders ( ( details , callback ) => { let extraHeaders = { } ; if ( details . requestHeaders [ 'x-extra-headers' ] ) { try { extraHeaders = JSON . parse ( details . requestHeaders [ 'x-extra-headers' ] ) ; } catch ( e ) { } } callback ( { cancel : false , requestHeaders : { ... details . requestHeaders , ... extraHeaders } } ) ; } ) ; }