Skip to content

Commit

Permalink
auto open qz tray
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolaslopezj committed Aug 8, 2023
1 parent 844a77e commit a65ced9
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 1 deletion.
3 changes: 3 additions & 0 deletions app/App/offline/ipc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {restartPrintManager} from '../print/restartPrintManager'
import {checkForUpdates, getAppVersion} from '../lifecycle'
import {installQZTray} from '../qztray/install'
import {getQZDigitalCertificate, getQZSignature} from '../qztray/ipc'
import {openQZTray} from '../qztray/open'

export interface IntraSyncAPI {
getDeviceIP: () => Promise<string>
Expand All @@ -29,6 +30,7 @@ export interface IntraSyncAPI {
installQZTray: () => Promise<{success: boolean; message: string}>
getQZDigitalCertificate: () => Promise<string>
getQZSignature: (toSign: string) => Promise<string>
openQZTray: () => Promise<{success: boolean; message: string}>

// Intrasync
acceptConnection: (token: string) => void
Expand All @@ -55,6 +57,7 @@ export function registerIntraSync() {
handleEvent('installQZTray', installQZTray)
handleEvent('getQZDigitalCertificate', getQZDigitalCertificate)
handleEvent('getQZSignature', getQZSignature)
handleEvent('openQZTray', openQZTray)
handleEvent('respondToRequest', respondToRequest)
handleEvent('acceptConnection', acceptConnection)
handleEvent('rejectConnection', rejectConnection)
Expand Down
1 change: 1 addition & 0 deletions app/App/offline/preload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export const methods = [
'installQZTray',
'getQZDigitalCertificate',
'getQZSignature',
'openQZTray',
// Intrasync
'acceptConnection',
'rejectConnection',
Expand Down
18 changes: 18 additions & 0 deletions app/App/qztray/execPromise.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import {exec} from 'child_process'

export async function execPromise(script: string) {
await new Promise<string>((resolve, reject) => {
console.log(`Will run exec promise: ${script}`)
exec(script, (error, stdout, stderr) => {
if (error) {
console.log(`Error in sudo promise: ${error}`, stdout, stderr)
reject(`Error: ${error.message}`)
} else {
console.log('Sudo promise success', {
stdout
})
resolve(stdout.toString())
}
})
})
}
70 changes: 70 additions & 0 deletions app/App/qztray/open.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import {spawn} from 'child_process'
import {dialog} from 'electron'
import fs from 'fs'
import Main from '..'
import {installQZTray} from './install'

const isMac = process.platform === 'darwin'

let isOpening = false

export async function openQZTray(): Promise<{success: boolean; message: string}> {
if (isOpening) {
return {
success: false,
message: 'Ya se está abriendo'
}
}

isOpening = true

try {
const path = {
mac: '/Applications/QZ Tray.app/Contents/MacOS/QZ Tray',
win: `${process.env.PROGRAMFILES}\\QZ Tray\\qz-tray-console.exe`
}[isMac ? 'mac' : 'win']

// file exists
if (!fs.existsSync(path)) {
const choice = dialog.showMessageBoxSync(Main.mainWindow, {
type: 'question',
buttons: ['Yes', 'No'],
title: 'Instalar QZ Tray',
message: 'No se encontró QZ Tray, ¿Quieres instalarlo?'
})
if (choice === 0) {
installQZTray()
}
isOpening = false
return {
success: false,
message: 'No se encontró QZ Tray'
}
}

const script = {
mac: '"/Applications/QZ Tray.app/Contents/MacOS/QZ Tray"',
win: `"%PROGRAMFILES%\\QZ Tray\\qz-tray.exe"`
}[isMac ? 'mac' : 'win']

const subprocess = spawn(script, [], {
detached: true,
stdio: 'ignore',
shell: true
})

subprocess.unref()

isOpening = false
return {
success: true,
message: 'Se abrió con éxito'
}
} catch (error) {
isOpening = false
return {
success: false,
message: error.message
}
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "justo-crisp",
"version": "0.7.3",
"version": "0.7.5",
"license": "MIT",
"description": "Crisp POS",
"author": "nicolaslopezj",
Expand Down

0 comments on commit a65ced9

Please sign in to comment.