double-click to open / open from cli (#3643)
* fixes Signed-off-by: Jess Frazelle <github@jessfraz.com> * add tests Signed-off-by: Jess Frazelle <github@jessfraz.com> * updates Signed-off-by: Jess Frazelle <github@jessfraz.com> * Look at this (photo)Graph *in the voice of Nickelback* * remove unneeded rust Signed-off-by: Jess Frazelle <github@jessfraz.com> * remove dep on clap Signed-off-by: Jess Frazelle <github@jessfraz.com> * fixups for imports Signed-off-by: Jess Frazelle <github@jessfraz.com> * updates Signed-off-by: Jess Frazelle <github@jessfraz.com> * fix types Signed-off-by: Jess Frazelle <github@jessfraz.com> * bump Signed-off-by: Jess Frazelle <github@jessfraz.com> --------- Signed-off-by: Jess Frazelle <github@jessfraz.com> Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
This commit is contained in:
108
src/main.ts
108
src/main.ts
@ -8,6 +8,11 @@ import { Issuer } from 'openid-client'
|
||||
import { Bonjour, Service } from 'bonjour-service'
|
||||
// @ts-ignore: TS1343
|
||||
import * as kittycad from '@kittycad/lib/import'
|
||||
import minimist from 'minimist'
|
||||
import getCurrentProjectFile from 'lib/getCurrentProjectFile'
|
||||
|
||||
// Check the command line arguments for a project path
|
||||
const args = parseCLIArgs()
|
||||
|
||||
// If it's not set, scream.
|
||||
const NODE_ENV = process.env.NODE_ENV || 'production'
|
||||
@ -22,6 +27,10 @@ if (require('electron-squirrel-startup')) {
|
||||
app.quit()
|
||||
}
|
||||
|
||||
// Global app listeners
|
||||
// Must be done before ready event
|
||||
registerListeners()
|
||||
|
||||
const createWindow = () => {
|
||||
const mainWindow = new BrowserWindow({
|
||||
autoHideMenuBar: true,
|
||||
@ -159,3 +168,102 @@ ipcMain.handle('find_machine_api', () => {
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
ipcMain.handle('loadProjectAtStartup', 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') {
|
||||
return null
|
||||
}
|
||||
|
||||
let projectPath: string | null = null
|
||||
// macOS: open-file events that were received before the app is ready
|
||||
const macOpenFiles: string[] = (global as any).macOpenFiles
|
||||
if (macOpenFiles && macOpenFiles && macOpenFiles.length > 0) {
|
||||
projectPath = macOpenFiles[0] // We only do one project at a time
|
||||
}
|
||||
// Reset this so we don't accidentally use it again.
|
||||
const macOpenFilesEmpty: string[] = []
|
||||
// @ts-ignore
|
||||
global['macOpenFiles'] = macOpenFilesEmpty
|
||||
|
||||
// macOS: open-url events that were received before the app is ready
|
||||
const getOpenUrls: string[] = ((global as any).getOpenUrls() ||
|
||||
[]) as string[]
|
||||
if (getOpenUrls && getOpenUrls.length > 0) {
|
||||
projectPath = getOpenUrls[0] // We only do one project at a
|
||||
}
|
||||
// Reset this so we don't accidentally use it again.
|
||||
// @ts-ignore
|
||||
global['getOpenUrls'] = function () {
|
||||
return []
|
||||
}
|
||||
|
||||
// Check if we have a project path in the command line arguments
|
||||
// If we do, we will load the project at that path
|
||||
if (args._.length > 1) {
|
||||
if (args._[1].length > 0) {
|
||||
projectPath = args._[1]
|
||||
// Reset all this value so we don't accidentally use it again.
|
||||
args._[1] = ''
|
||||
}
|
||||
}
|
||||
|
||||
if (projectPath) {
|
||||
// We have a project path, load the project information.
|
||||
console.log(`Loading project at startup: ${projectPath}`)
|
||||
try {
|
||||
const currentFile = await getCurrentProjectFile(projectPath)
|
||||
console.log(`Project loaded: ${currentFile}`)
|
||||
return currentFile
|
||||
} catch (e) {
|
||||
console.error(e)
|
||||
}
|
||||
|
||||
return null
|
||||
}
|
||||
|
||||
return null
|
||||
})
|
||||
|
||||
function parseCLIArgs(): minimist.ParsedArgs {
|
||||
return minimist(process.argv, {})
|
||||
}
|
||||
|
||||
function registerListeners() {
|
||||
/**
|
||||
* macOS: when someone drops a file to the not-yet running VSCode, the open-file event fires even before
|
||||
* the app-ready event. We listen very early for open-file and remember this upon startup as path to open.
|
||||
*/
|
||||
const macOpenFiles: string[] = []
|
||||
// @ts-ignore
|
||||
global['macOpenFiles'] = macOpenFiles
|
||||
app.on('open-file', function (event, path) {
|
||||
macOpenFiles.push(path)
|
||||
})
|
||||
|
||||
/**
|
||||
* macOS: react to open-url requests.
|
||||
*/
|
||||
const openUrls: string[] = []
|
||||
const onOpenUrl = function (
|
||||
event: { preventDefault: () => void },
|
||||
url: string
|
||||
) {
|
||||
event.preventDefault()
|
||||
|
||||
openUrls.push(url)
|
||||
}
|
||||
|
||||
app.on('will-finish-launching', function () {
|
||||
app.on('open-url', onOpenUrl)
|
||||
})
|
||||
|
||||
// @ts-ignore
|
||||
global['getOpenUrls'] = function () {
|
||||
app.removeListener('open-url', onOpenUrl)
|
||||
|
||||
return openUrls
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user