Fix kcl file opening on Windows (double click) on second instance (#5420)

* WIP: Double-clicking on .kcl file on Windows redirects to the home page if the app is already open
Fixes #5412

* Add deep link test case for linux

* Add mac tests

* Lint and win tests

* Fix e2e tests

* Logs everywhere

* windows weird? yup

* More logzzz maybe it's not windows

* Remove :/// replacement. Add catch log

* Fix and clean up

* FIx lint

* A snapshot a day keeps the bugs away! 📷🐛 (OS: namespace-profile-ubuntu-8-cores)

* A snapshot a day keeps the bugs away! 📷🐛 (OS: namespace-profile-ubuntu-8-cores)

* More lint

* A snapshot a day keeps the bugs away! 📷🐛 (OS: namespace-profile-ubuntu-8-cores)

* A snapshot a day keeps the bugs away! 📷🐛 (OS: namespace-profile-ubuntu-8-cores)

* A snapshot a day keeps the bugs away! 📷🐛 (OS: namespace-profile-ubuntu-8-cores)

* Clean up tests further

---------

Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
This commit is contained in:
Pierre Jacquier
2025-02-20 15:23:39 -05:00
committed by GitHub
parent 9f5003cafc
commit 30029a63a1
5 changed files with 129 additions and 21 deletions

View File

@ -17,23 +17,27 @@ import { Bonjour, Service } from 'bonjour-service'
// @ts-ignore: TS1343
import * as kittycad from '@kittycad/lib/import'
import electronUpdater, { type AppUpdater } from 'electron-updater'
import minimist from 'minimist'
import getCurrentProjectFile from 'lib/getCurrentProjectFile'
import os from 'node:os'
import { reportRejection } from 'lib/trap'
import { ZOO_STUDIO_PROTOCOL } from 'lib/constants'
import argvFromYargs from './commandLineArgs'
import {
argvFromYargs,
getPathOrUrlFromArgs,
parseCLIArgs,
} from './commandLineArgs'
import * as packageJSON from '../package.json'
let mainWindow: BrowserWindow | null = null
// Check the command line arguments for a project path
const args = parseCLIArgs()
const args = parseCLIArgs(process.argv)
// @ts-ignore: TS1343
const viteEnv = import.meta.env
const NODE_ENV = process.env.NODE_ENV || viteEnv.MODE
const IS_PLAYWRIGHT = process.env.IS_PLAYWRIGHT
// dotenv override when present
dotenv.config({ path: [`.env.${NODE_ENV}.local`, `.env.${NODE_ENV}`] })
@ -50,7 +54,8 @@ process.env.VITE_KC_CONNECTION_TIMEOUT_MS ??=
viteEnv.VITE_KC_CONNECTION_TIMEOUT_MS
// Likely convenient to keep for debugging
console.log('process.env', process.env)
console.log('Environment vars', process.env)
console.log('Parsed CLI args', args)
/// Register our application to handle all "zoo-studio:" protocols.
const singleInstanceLock = app.requestSingleInstanceLock()
@ -68,7 +73,7 @@ if (process.defaultApp) {
// Must be done before ready event.
// Checking against this lock is needed for Windows and Linux, see
// https://www.electronjs.org/docs/latest/tutorial/launch-app-from-url-in-another-app#windows-and-linux-code
if (!singleInstanceLock && !process.env.IS_PLAYWRIGHT) {
if (!singleInstanceLock && !IS_PLAYWRIGHT) {
app.quit()
} else {
registerStartupListeners()
@ -100,12 +105,14 @@ const createWindow = (pathToOpen?: string, reuse?: boolean): BrowserWindow => {
}
// Deep Link: Case of a cold start from Windows or Linux
const zooProtocolArg = process.argv.find((a) =>
a.startsWith(ZOO_STUDIO_PROTOCOL + '://')
)
if (!pathToOpen && zooProtocolArg) {
pathToOpen = zooProtocolArg
console.log('Retrieved deep link from argv', pathToOpen)
const pathOrUrl = getPathOrUrlFromArgs(args)
if (
!pathToOpen &&
pathOrUrl &&
pathOrUrl.startsWith(ZOO_STUDIO_PROTOCOL + '://')
) {
pathToOpen = pathOrUrl
console.log('Retrieved deep link from CLI args', pathToOpen)
}
// Deep Link: Case of a second window opened for macOS
@ -431,7 +438,8 @@ const getProjectPathAtStartup = async (
// If we are in development mode, we don't want to load a project at
// startup.
// Since the args passed are always '.'
if (NODE_ENV !== 'production') {
// aka Forge for yarn tron:start live dev or playwright tests, but not dev packaged apps
if (MAIN_WINDOW_VITE_DEV_SERVER_URL || IS_PLAYWRIGHT) {
return null
}
@ -484,17 +492,18 @@ const getProjectPathAtStartup = async (
return null
}
function parseCLIArgs(): minimist.ParsedArgs {
return minimist(process.argv, {})
}
function registerStartupListeners() {
// Linux and Windows from https://www.electronjs.org/docs/latest/tutorial/launch-app-from-url-in-another-app
app.on('second-instance', (event, commandLine, workingDirectory) => {
// Deep Link: second instance for Windows and Linux
const url = commandLine.pop()?.slice(0, -1)
console.log('Retrieved deep link from commandLine', url)
createWindow(url)
// Likely convenient to keep for debugging
console.log(
'Parsed CLI args from second instance',
parseCLIArgs(commandLine)
)
const pathOrUrl = getPathOrUrlFromArgs(parseCLIArgs(commandLine))
console.log('Retrieved path or deep link from second-instance', pathOrUrl)
createWindow(pathOrUrl)
})
/**