Skip to content

Commit 9b72217

Browse files
committed
Fix electron type to reflect that it could be undefined
1 parent 7235ab4 commit 9b72217

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+775
-399
lines changed

e2e/playwright/fixtures/fixtureSetup.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,12 @@ import { SceneFixture } from '@e2e/playwright/fixtures/sceneFixture'
2323
import { ToolbarFixture } from '@e2e/playwright/fixtures/toolbarFixture'
2424

2525
import { TEST_SETTINGS } from '@e2e/playwright/storageStates'
26-
import { getUtils, settingsToToml, setup } from '@e2e/playwright/test-utils'
26+
import {
27+
assertElectron,
28+
getUtils,
29+
settingsToToml,
30+
setup,
31+
} from '@e2e/playwright/test-utils'
2732

2833
export class AuthenticatedApp {
2934
public readonly page: Page
@@ -228,7 +233,8 @@ export class ElectronZoo {
228233
}, dims)
229234

230235
return this.evaluate(async (dims: { width: number; height: number }) => {
231-
await window.electron.resizeWindow(dims.width, dims.height)
236+
assertElectron()
237+
await window.electron?.resizeWindow(dims.width, dims.height)
232238
window.document.body.style.width = dims.width + 'px'
233239
window.document.body.style.height = dims.height + 'px'
234240
window.document.documentElement.style.width = dims.width + 'px'

e2e/playwright/test-utils.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ export const commonPoints = {
6868
export const editorSelector = '[role="textbox"][data-language="kcl"]'
6969
type PaneId = 'variables' | 'code' | 'files' | 'logs'
7070

71+
export function assertElectron() {
72+
expect(window.electron).toBeDefined()
73+
}
74+
7175
export function runningOnLinux() {
7276
return process.platform === 'linux'
7377
}

interface.d.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import { MachinesListing } from 'components/MachineManagerProvider'
1+
import type { MachinesListing } from 'components/MachineManagerProvider'
22
import 'electron'
3-
import fs from 'node:fs/promises'
4-
import path from 'path'
5-
import { dialog, shell } from 'electron'
3+
import type fs from 'node:fs/promises'
4+
import type path from 'path'
5+
import type { dialog, shell } from 'electron'
66
import type { WebContentSendPayload } from 'menu/channels'
7-
import { ZooLabel } from 'menu/roles'
7+
import type { ZooLabel } from 'menu/roles'
88

99
// Extend the interface with additional custom properties
1010
declare module 'electron' {
@@ -61,7 +61,7 @@ export interface IElectronAPI {
6161
mkdir: typeof fs.mkdir
6262
join: typeof path.join
6363
sep: typeof path.sep
64-
rename: (prev: string, next: string) => typeof fs.rename
64+
rename: (prev: string, next: string) => ReturnType<fs.rename>
6565
packageJson: {
6666
name: string
6767
}
@@ -106,7 +106,9 @@ export interface IElectronAPI {
106106

107107
declare global {
108108
interface Window {
109-
electron: IElectronAPI
110-
openExternalLink: (e: React.MouseEvent<HTMLAnchorElement>) => void
109+
electron: IElectronAPI | undefined
110+
openExternalLink:
111+
| ((e: React.MouseEvent<HTMLAnchorElement>) => void)
112+
| undefined
111113
}
112114
}

package-lock.json

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
"rust/kcl-language-server"
1818
],
1919
"dependencies": {
20+
"@chainner/node-path": "^0.1.1",
2021
"@codemirror/autocomplete": "^6.18.6",
2122
"@codemirror/commands": "^6.8.1",
2223
"@codemirror/language": "^6.11.0",

rust/kcl-lib/src/fs/wasm.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,6 @@ extern "C" {
1616
#[derive(Debug, Clone)]
1717
pub type FileSystemManager;
1818

19-
#[wasm_bindgen(constructor)]
20-
pub fn new() -> FileSystemManager;
21-
2219
#[wasm_bindgen(method, js_name = readFile, catch)]
2320
fn read_file(this: &FileSystemManager, path: String) -> Result<js_sys::Promise, js_sys::Error>;
2421

src/App.tsx

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,11 @@ import env from '@src/env'
7272
// CYCLIC REF
7373
sceneInfra.camControls.engineStreamActor = engineStreamActor
7474

75-
maybeWriteToDisk()
76-
.then(() => {})
77-
.catch(() => {})
75+
if (window.electron) {
76+
maybeWriteToDisk(window.electron)
77+
.then(() => {})
78+
.catch(reportRejection)
79+
}
7880

7981
export function App() {
8082
const { state: modelingState } = useModelingContext()
@@ -241,7 +243,7 @@ export function App() {
241243

242244
// Only create the native file menus on desktop
243245
useEffect(() => {
244-
if (isDesktop()) {
246+
if (window.electron) {
245247
window.electron
246248
.createModelingPageMenu()
247249
.then(() => {

src/components/CommandBar/CommandBarPathInput.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ function CommandBarPathInput({
6565
configuration.filters = arg.filters
6666
}
6767

68+
if (!window.electron) {
69+
return new Error("Can't open file picker without electron")
70+
}
6871
const newPath = await window.electron.open(configuration)
6972
if (newPath.canceled) return
7073
inputRef.current.value = newPath.filePaths[0]
@@ -75,7 +78,7 @@ function CommandBarPathInput({
7578

7679
// Fire on component mount, if outside of e2e test context
7780
useEffect(() => {
78-
window.electron.process.env.NODE_ENV !== 'test' &&
81+
window.electron?.process.env.NODE_ENV !== 'test' &&
7982
toSync(pickFileThroughNativeDialog, reportRejection)()
8083
// eslint-disable-next-line react-hooks/exhaustive-deps -- TODO: blanket-ignored fix me!
8184
}, [])

src/components/Explorer/ProjectExplorer.tsx

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ export const ProjectExplorer = ({
326326
}
327327
},
328328
onOpenInNewWindow: () => {
329-
window.electron.openInNewWindow(row.path)
329+
window.electron?.openInNewWindow(row.path)
330330
},
331331
onRenameStart: () => {
332332
if (readOnly) {
@@ -375,16 +375,19 @@ export const ProjectExplorer = ({
375375
const absolutePathToParentDirectory = getParentAbsolutePath(
376376
row.path
377377
)
378-
const oldPath = window.electron.path.join(
378+
const oldPath = window.electron?.path.join(
379379
absolutePathToParentDirectory,
380380
name
381381
)
382-
const newPath = window.electron.path.join(
382+
const newPath = window.electron?.path.join(
383383
absolutePathToParentDirectory,
384384
requestedName
385385
)
386386
const shouldWeNavigate =
387-
file?.path?.startsWith(oldPath) && canNavigate
387+
oldPath !== undefined &&
388+
newPath !== undefined &&
389+
file?.path?.startsWith(oldPath) &&
390+
canNavigate
388391

389392
if (shouldWeNavigate && file && file.path) {
390393
const requestedFileNameWithExtension =

src/components/FileMachineProvider.tsx

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import { useSettings, useToken } from '@src/lib/singletons'
3535
import { commandBarActor } from '@src/lib/singletons'
3636
import { fileMachine } from '@src/machines/fileMachine'
3737
import { modelingMenuCallbackMostActions } from '@src/menu/register'
38+
import { fsManager } from '@src/lang/std/fileSystemManager'
3839

3940
type MachineContext<T extends AnyStateMachine> = {
4041
state: StateFrom<T>
@@ -127,7 +128,7 @@ export const FileMachineProvider = ({
127128
// TODO: Should this be context.selectedDirectory.path?
128129
// eslint-disable-next-line @typescript-eslint/restrict-plus-operands
129130
context.selectedDirectory +
130-
window.electron.path.sep +
131+
fsManager.path.sep +
131132
// eslint-disable-next-line @typescript-eslint/restrict-plus-operands
132133
event.output.name
133134
)}`
@@ -147,7 +148,7 @@ export const FileMachineProvider = ({
147148
}
148149

149150
commandBarActor.send({ type: 'Close' })
150-
window.electron.openInNewWindow(event.data.name)
151+
window.electron?.openInNewWindow(event.data.name)
151152
},
152153
},
153154
actors: {
@@ -161,6 +162,9 @@ export const FileMachineProvider = ({
161162
}
162163
}),
163164
createAndOpenFile: fromPromise(async ({ input }) => {
165+
if (!window.electron) {
166+
return Promise.reject(new Error('No file system present'))
167+
}
164168
let createdName = input.name.trim() || DEFAULT_FILE_NAME
165169
let createdPath: string
166170

@@ -172,6 +176,7 @@ export const FileMachineProvider = ({
172176
input.makeDir
173177
) {
174178
let { name, path } = getNextDirName({
179+
electron: window.electron,
175180
entryName: input.targetPathToClone
176181
? window.electron.path.basename(input.targetPathToClone)
177182
: createdName,
@@ -187,7 +192,8 @@ export const FileMachineProvider = ({
187192
input.targetPathToClone &&
188193
input.selectedDirectory.path.indexOf(input.targetPathToClone) > -1
189194
if (isTargetPathToCloneASubPath) {
190-
const { name, path } = getNextFileName({
195+
const { name, path } = await getNextFileName({
196+
electron: window.electron,
191197
entryName: input.targetPathToClone
192198
? window.electron.path.basename(input.targetPathToClone)
193199
: createdName,
@@ -198,7 +204,8 @@ export const FileMachineProvider = ({
198204
createdName = name
199205
createdPath = path
200206
} else {
201-
const { name, path } = getNextFileName({
207+
const { name, path } = await getNextFileName({
208+
electron: window.electron,
202209
entryName: input.targetPathToClone
203210
? window.electron.path.basename(input.targetPathToClone)
204211
: createdName,
@@ -229,19 +236,24 @@ export const FileMachineProvider = ({
229236
}
230237
}),
231238
createFile: fromPromise(async ({ input }) => {
239+
if (!window.electron) {
240+
return Promise.reject(new Error('No file system present'))
241+
}
232242
let createdName = input.name.trim() || DEFAULT_FILE_NAME
233243
let createdPath: string
234244

235245
if (input.makeDir) {
236246
let { name, path } = getNextDirName({
247+
electron: window.electron,
237248
entryName: createdName,
238249
baseDir: input.selectedDirectory.path,
239250
})
240251
createdName = name
241252
createdPath = path
242253
await window.electron.mkdir(createdPath)
243254
} else {
244-
const { name, path } = getNextFileName({
255+
const { name, path } = await getNextFileName({
256+
electron: window.electron,
245257
entryName: createdName,
246258
baseDir: input.selectedDirectory.path,
247259
})
@@ -260,6 +272,9 @@ export const FileMachineProvider = ({
260272
}
261273
}),
262274
renameFile: fromPromise(async ({ input }) => {
275+
if (!window.electron) {
276+
return Promise.reject(new Error('No file system present'))
277+
}
263278
const { oldName, newName, isDir } = input
264279
const name = newName
265280
? newName.endsWith(FILE_EXT) || isDir
@@ -319,6 +334,7 @@ export const FileMachineProvider = ({
319334
}
320335
}),
321336
deleteFile: fromPromise(async ({ input }) => {
337+
if (!window.electron) return
322338
const isDir = !!input.children
323339

324340
if (isDir) {
@@ -444,7 +460,7 @@ export const FileMachineProvider = ({
444460

445461
const kclCommandMemo = useMemo(() => {
446462
const providedOptions = []
447-
if (isDesktop() && project?.children && file?.path) {
463+
if (window.electron && project?.children && file?.path) {
448464
const projectPath = project.path
449465
const filePath = file.path
450466
let children = project.children

0 commit comments

Comments
 (0)