Skip to content

Commit

Permalink
add "--wd" command line option
Browse files Browse the repository at this point in the history
lets you set the "working directory" of the fSpy session when launching from the command line. acts as an override for the default path of any open/save dialogs
  • Loading branch information
Mitchell Kehn committed Nov 22, 2020
1 parent 543bea3 commit ad35e32
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 10 deletions.
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"editor.tabSize": 2,
"files.eol": "\n"
"files.eol": "\n",
"typescript.tsdk": "node_modules\\typescript\\lib"
}
40 changes: 31 additions & 9 deletions src/main/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ let mainWindow: Electron.BrowserWindow | null = null
export interface DocumentState {
hasUnsavedChanges: boolean
filePath: string | null,
isExampleProject: boolean
isExampleProject: boolean,
workingDirectory: string | undefined
}

let documentState: DocumentState | null = null
Expand Down Expand Up @@ -73,6 +74,13 @@ function openProject(path: string, window: BrowserWindow) {
)
}

function defaultPath(): string | undefined {
if (documentState) {
return documentState.workingDirectory
}
return undefined
}

function createWindow() {
const minWidth = 800
const minHeight = 768
Expand Down Expand Up @@ -138,7 +146,8 @@ function createWindow() {
filters: [
{ name: 'fSpy project files', extensions: ['fspy'] }
],
properties: ['openFile']
properties: ['openFile'],
defaultPath: defaultPath()
}
).then((result) => {
if (!result.canceled) {
Expand All @@ -153,7 +162,8 @@ function createWindow() {
filters: [
{ name: 'fSpy project files', extensions: ['fspy'] }
],
properties: ['openFile']
properties: ['openFile'],
defaultPath: defaultPath()
}
).then((result) => {
if (!result.canceled) {
Expand All @@ -176,7 +186,9 @@ function createWindow() {
onSaveProjectAs: () => {
dialog.showSaveDialog(
window,
{}
{
defaultPath: defaultPath()
}
).then((result) => {
if (!result.canceled && result.filePath !== undefined) {
window.webContents.send(
Expand All @@ -192,6 +204,7 @@ function createWindow() {
dialog.showOpenDialog(
window,
{
defaultPath: defaultPath(),
properties: ['openFile']
}
).then((result) => {
Expand Down Expand Up @@ -273,7 +286,8 @@ function createWindow() {
documentState = {
hasUnsavedChanges: false,
filePath: null,
isExampleProject: false
isExampleProject: false,
workingDirectory: undefined
}

if (initialOpenMessage) {
Expand All @@ -284,8 +298,8 @@ function createWindow() {
} else {
// parse GUI command line arguments
const args = minimist(process.argv, {
string: 'open',
default: { open: null }
string: ['open', 'wd'],
default: { open: null, wd: undefined }
})

if (args.open) {
Expand Down Expand Up @@ -317,6 +331,10 @@ function createWindow() {
})
}
}

if (documentState) {
documentState.workingDirectory = args.wd
}
}

if (process.env.DEV) {
Expand Down Expand Up @@ -385,7 +403,9 @@ function createWindow() {
// TODO: DRY
dialog.showSaveDialog(
window,
{}
{
defaultPath: defaultPath()
}
).then((result) => {
if (!result.canceled && result.filePath) {
window.webContents.send(
Expand All @@ -402,7 +422,9 @@ function createWindow() {
// TODO: DRY
dialog.showSaveDialog(
window,
{}
{
defaultPath: defaultPath()
}
).then((result) => {
if (!result.canceled && result.filePath) {
let file = openSync(result.filePath, 'w')
Expand Down

0 comments on commit ad35e32

Please sign in to comment.