Correcting type errors

This commit is contained in:
49lf
2024-07-31 16:54:32 -04:00
parent 638217311b
commit aa4e9cbc64
15 changed files with 91 additions and 47 deletions

38
interface.d.ts vendored Normal file
View File

@ -0,0 +1,38 @@
import fs from 'node:fs/promises'
import path from 'path'
import { dialog, shell } from 'electron'
export interface IElectronAPI {
open: typeof dialog.showOpenDialog
save: typeof dialog.showSaveDialog
openExternal: typeof shell.openExternal
showInFolder: typeof shell.showItemInFolder
login: (host: string) => Promise<string>
platform: typeof process.env.platform
arch: typeof process.env.arch
version: typeof process.env.version
readFile: (path: string) => ReturnType<fs.readFile>
writeFile: (path: string, data: string | Uint8Array) => ReturnType<fs.writeFile>
readdir: (path: string) => ReturnType<fs.readdir>
getPath: (name: string) => Promise<string>
rm: typeof fs.rm
stat: (path: string) => ReturnType<fs.stat>
statIsDirectory: (path: string) => Promise<boolean>
path: typeof path,
mkdir: typeof fs.mkdir,
rename: (prev: string, next: string) => typeof fs.rename,
packageJson: {
name: string,
},
process: {
env: {
BASE_URL: (value?: string) => string,
}
}
}
declare global {
interface Window {
electron: IElectronAPI
}
}

View File

@ -32,8 +32,8 @@ class FileSystemManager {
.catch((error) => { .catch((error) => {
return Promise.reject(new Error(`Error reading file: ${error}`)) return Promise.reject(new Error(`Error reading file: ${error}`))
}) })
.then((file) => { .then((filePath) => {
return window.electron.readFile(filepath) return window.electron.readFile(filePath)
}) })
} }

View File

@ -65,10 +65,10 @@ export class CoreDumpManager {
getOsInfo(): Promise<string> { getOsInfo(): Promise<string> {
if (this.isDesktop()) { if (this.isDesktop()) {
const osinfo: OsInfo = { const osinfo: OsInfo = {
platform: window.electron.platform, platform: window.electron.platform ?? null,
arch: window.electron.arch, arch: window.electron.arch ?? null,
browser: 'desktop', browser: 'desktop',
version: window.electron.version, version: window.electron.version ?? null,
} }
return new Promise((resolve) => resolve(JSON.stringify(osinfo))) return new Promise((resolve) => resolve(JSON.stringify(osinfo)))
// (lf94) I'm not sure if this comment is specific to tauri or just desktop... // (lf94) I'm not sure if this comment is specific to tauri or just desktop...

View File

@ -82,9 +82,7 @@ export async function ensureProjectDirectoryExists(
await window.electron.stat(projectDir) await window.electron.stat(projectDir)
} catch (e) { } catch (e) {
if (e === 'ENOENT') { if (e === 'ENOENT') {
await window.electron.mkdir(projectDir, { recursive: true }, (e) => { await window.electron.mkdir(projectDir, { recursive: true })
console.log(e)
})
} }
} }
@ -389,7 +387,7 @@ export const getInitialDefaultDir = async () => {
export const readProjectSettingsFile = async ( export const readProjectSettingsFile = async (
projectPath: string projectPath: string
): ProjectConfiguration => { ): Promise<ProjectConfiguration> => {
let settingsPath = await getProjectSettingsFilePath(projectPath) let settingsPath = await getProjectSettingsFilePath(projectPath)
// Check if this file exists. // Check if this file exists.
@ -398,7 +396,7 @@ export const readProjectSettingsFile = async (
} catch (e) { } catch (e) {
if (e === 'ENOENT') { if (e === 'ENOENT') {
// Return the default configuration. // Return the default configuration.
return {} return { settings: {} }
} }
} }
@ -446,7 +444,7 @@ export const setState = async (
export const getUser = async ( export const getUser = async (
token: string, token: string,
hostname: string hostname: string
): Models['User_type'] => { ): Promise<Models['User_type']> => {
// Use the host passed in if it's set. // Use the host passed in if it's set.
// Otherwise, use the default host. // Otherwise, use the default host.
const host = !hostname ? DEFAULT_HOST : hostname const host = !hostname ? DEFAULT_HOST : hostname

View File

@ -8,11 +8,11 @@ const save = (args: any) => ipcRenderer.invoke('dialog.showSaveDialog', args)
const openExternal = (url: any) => ipcRenderer.invoke('shell.openExternal', url) const openExternal = (url: any) => ipcRenderer.invoke('shell.openExternal', url)
const showInFolder = (path: string) => const showInFolder = (path: string) =>
ipcRenderer.invoke('shell.showItemInFolder', path) ipcRenderer.invoke('shell.showItemInFolder', path)
const login = (host: string) => ipcRenderer.invoke('login', host) const login = (host: string): Promise<string> => ipcRenderer.invoke('login', host)
const readFile = (path: string) => fs.readFile(path, 'utf-8') const readFile = (path: string) => fs.readFile(path, 'utf-8')
const rename = (prev: string, next: string) => fs.rename(prev, next) const rename = (prev: string, next: string) => fs.rename(prev, next)
const writeFile = (path: string, data: string) => const writeFile = (path: string, data: string | Uint8Array) =>
fs.writeFile(path, data, 'utf-8') fs.writeFile(path, data, 'utf-8')
const readdir = (path: string) => fs.readdir(path, 'utf-8') const readdir = (path: string) => fs.readdir(path, 'utf-8')
const stat = (path: string) => const stat = (path: string) =>

View File

@ -1,9 +1,11 @@
import { isDesktop } from 'lib/isDesktop' import { isDesktop } from 'lib/isDesktop'
export const openExternalBrowserIfDesktop = (to) => export const openExternalBrowserIfDesktop = (to?: string) =>
function (e) { function (e: Event) {
if (isDesktop()) { if (isDesktop()) {
window.electron.openExternal(to || e.currentTarget.href) // Ignoring because currentTarget could be a few different things
// @ts-ignore
window.electron.openExternal(to || e.currentTarget?.href)
e.preventDefault() e.preventDefault()
e.stopPropagation() e.stopPropagation()
return false return false

View File

@ -47,6 +47,11 @@ export async function getProjectMetaByRouteId(
if (err(configuration)) return Promise.reject(configuration) if (err(configuration)) return Promise.reject(configuration)
// Should not be possible but I guess logically it could be
if (configuration === undefined) {
return Promise.reject(new Error('No configuration found'))
}
const route = parseProjectRoute(configuration, id) const route = parseProjectRoute(configuration, id)
if (err(route)) return Promise.reject(route) if (err(route)) return Promise.reject(route)

View File

@ -86,7 +86,7 @@ export const fileLoader: LoaderFunction = async (
const { project_name, project_path, current_file_name, current_file_path } = const { project_name, project_path, current_file_name, current_file_path } =
projectPathData projectPathData
const urlObj = URL.parse(routerData.request.url) const urlObj = new URL(routerData.request.url)
let code = '' let code = ''
if (!urlObj.pathname.endsWith('/settings')) { if (!urlObj.pathname.endsWith('/settings')) {
@ -120,7 +120,7 @@ export const fileLoader: LoaderFunction = async (
const projectData: IndexLoaderData = { const projectData: IndexLoaderData = {
code, code,
project: isDesktop() project: isDesktop()
? await getProjectInfo(project_path, configuration) ? await getProjectInfo(project_path)
: { : {
name: project_name, name: project_name,
path: project_path, path: project_path,
@ -131,8 +131,8 @@ export const fileLoader: LoaderFunction = async (
default_file: project_path, default_file: project_path,
}, },
file: { file: {
name: current_file_name, name: current_file_name || '',
path: current_file_path?.split('/').slice(0, -1).join('/'), path: current_file_path?.split('/').slice(0, -1).join('/') ?? '',
children: [], children: [],
}, },
} }

View File

@ -62,7 +62,7 @@ export class Setting<T = unknown> {
get user(): T | undefined { get user(): T | undefined {
return this._user return this._user
} }
set user(v: T) { set user(v: T | undefined) {
this._user = this.validate(v) ? v : this._user this._user = this.validate(v) ? v : this._user
this.current = this.resolve() this.current = this.resolve()
} }

View File

@ -146,7 +146,7 @@ async function getUser(context: UserContext) {
}) })
.then((res) => res.json()) .then((res) => res.json())
.catch((err) => console.error('error from Browser getUser', err)) .catch((err) => console.error('error from Browser getUser', err))
: getUserDesktop(context.token, VITE_KC_API_BASE_URL) : getUserDesktop(context.token ?? '', VITE_KC_API_BASE_URL)
const user = await userPromise const user = await userPromise

View File

@ -4,6 +4,7 @@
import { Configuration } from 'wasm-lib/kcl/bindings/Configuration' import { Configuration } from 'wasm-lib/kcl/bindings/Configuration'
import { app, BrowserWindow, ipcMain, dialog, shell } from 'electron' import { app, BrowserWindow, ipcMain, dialog, shell } from 'electron'
import path from 'path' import path from 'path'
import fs from 'node:fs/promises'
import { Issuer } from 'openid-client' import { Issuer } from 'openid-client'
// Handle creating/removing shortcuts on Windows when installing/uninstalling. // Handle creating/removing shortcuts on Windows when installing/uninstalling.
@ -71,8 +72,9 @@ ipcMain.handle('shell.openExternal', (event, data) => {
}) })
ipcMain.handle('login', async (event, host) => { ipcMain.handle('login', async (event, host) => {
console.log('Logging in...')
// Do an OAuth 2.0 Device Authorization Grant dance to get a token. // Do an OAuth 2.0 Device Authorization Grant dance to get a token.
// We quiet ts because we are not using this in the standard way.
// @ts-ignore
const issuer = new Issuer({ const issuer = new Issuer({
device_authorization_endpoint: `${host}/oauth2/device/auth`, device_authorization_endpoint: `${host}/oauth2/device/auth`,
token_endpoint: `${host}/oauth2/device/token`, token_endpoint: `${host}/oauth2/device/token`,
@ -103,9 +105,9 @@ ipcMain.handle('login', async (event, host) => {
if (process.env.TEMP) { if (process.env.TEMP) {
temp = process.env.TEMP temp = process.env.TEMP
} }
let path = path.join(temp, 'kittycad_user_code') let tmpkcuc = path.join(temp, 'kittycad_user_code')
console.log(`Writing to ${path}`) console.log(`Writing to ${tmpkcuc}`)
await fs.writeFile(path, handle.user_code) await fs.writeFile(tmpkcuc, handle.user_code)
} else { } else {
shell.openExternal(handle.verification_uri_complete) shell.openExternal(handle.verification_uri_complete)
} }

View File

@ -913,7 +913,7 @@ mod tests {
.unwrap(); .unwrap();
assert_eq!(state.project.file.name, name); assert_eq!(state.project.file.name, name);
assert_eq!(state.project.path, tmp_project_dir.display().to_string()); assert_eq!(state.project.file.path, tmp_project_dir.display().to_string());
assert_eq!( assert_eq!(
state.current_file, state.current_file,
Some(tmp_project_dir.join("main.kcl").display().to_string()) Some(tmp_project_dir.join("main.kcl").display().to_string())
@ -938,7 +938,7 @@ mod tests {
.unwrap(); .unwrap();
assert_eq!(state.project.file.name, name); assert_eq!(state.project.file.name, name);
assert_eq!(state.project.path, tmp_project_dir.display().to_string()); assert_eq!(state.project.file.path, tmp_project_dir.display().to_string());
assert_eq!( assert_eq!(
state.current_file, state.current_file,
Some(tmp_project_dir.join("main.kcl").display().to_string()) Some(tmp_project_dir.join("main.kcl").display().to_string())
@ -963,7 +963,7 @@ mod tests {
.unwrap(); .unwrap();
assert_eq!(state.project.file.name, name); assert_eq!(state.project.file.name, name);
assert_eq!(state.project.path, tmp_project_dir.display().to_string()); assert_eq!(state.project.file.path, tmp_project_dir.display().to_string());
assert_eq!( assert_eq!(
state.current_file, state.current_file,
Some(tmp_project_dir.join("main.kcl").display().to_string()) Some(tmp_project_dir.join("main.kcl").display().to_string())
@ -988,7 +988,7 @@ mod tests {
.unwrap(); .unwrap();
assert_eq!(state.project.file.name, name); assert_eq!(state.project.file.name, name);
assert_eq!(state.project.path, tmp_project_dir.display().to_string()); assert_eq!(state.project.file.path, tmp_project_dir.display().to_string());
assert_eq!( assert_eq!(
state.current_file, state.current_file,
Some(tmp_project_dir.join("thing.kcl").display().to_string()) Some(tmp_project_dir.join("thing.kcl").display().to_string())
@ -1013,7 +1013,7 @@ mod tests {
.unwrap(); .unwrap();
assert_eq!(state.project.file.name, name); assert_eq!(state.project.file.name, name);
assert_eq!(state.project.path, tmp_project_dir.display().to_string()); assert_eq!(state.project.file.path, tmp_project_dir.display().to_string());
assert_eq!( assert_eq!(
state.current_file, state.current_file,
Some(tmp_project_dir.join("model.obj.kcl").display().to_string()) Some(tmp_project_dir.join("model.obj.kcl").display().to_string())

View File

@ -967,9 +967,9 @@ color = 1567.4"#;
.await .await
.unwrap(); .unwrap();
assert_eq!(project.name, project_name); assert_eq!(project.file.name, project_name);
assert_eq!( assert_eq!(
project.path, project.file.path,
settings settings
.settings .settings
.project .project
@ -981,7 +981,7 @@ color = 1567.4"#;
assert_eq!(project.directory_count, 0); assert_eq!(project.directory_count, 0);
assert_eq!( assert_eq!(
project.default_file, project.default_file,
std::path::Path::new(&project.path) std::path::Path::new(&project.file.path)
.join(super::DEFAULT_PROJECT_KCL_FILE) .join(super::DEFAULT_PROJECT_KCL_FILE)
.to_string_lossy() .to_string_lossy()
); );
@ -1017,9 +1017,9 @@ color = 1567.4"#;
.await .await
.unwrap(); .unwrap();
assert_eq!(project.name, project_name); assert_eq!(project.file.name, project_name);
assert_eq!( assert_eq!(
project.path, project.file.path,
settings settings
.settings .settings
.project .project
@ -1031,7 +1031,7 @@ color = 1567.4"#;
assert_eq!(project.directory_count, 0); assert_eq!(project.directory_count, 0);
assert_eq!( assert_eq!(
project.default_file, project.default_file,
std::path::Path::new(&project.path) std::path::Path::new(&project.file.path)
.join(super::DEFAULT_PROJECT_KCL_FILE) .join(super::DEFAULT_PROJECT_KCL_FILE)
.to_string_lossy() .to_string_lossy()
); );
@ -1057,8 +1057,8 @@ color = 1567.4"#;
let projects = settings.list_projects().await.unwrap(); let projects = settings.list_projects().await.unwrap();
assert_eq!(projects.len(), 1); assert_eq!(projects.len(), 1);
assert_eq!(projects[0].name, project_name); assert_eq!(projects[0].file.name, project_name);
assert_eq!(projects[0].path, project.path); assert_eq!(projects[0].file.path, project.file.path);
assert_eq!(projects[0].kcl_file_count, 1); assert_eq!(projects[0].kcl_file_count, 1);
assert_eq!(projects[0].directory_count, 0); assert_eq!(projects[0].directory_count, 0);
assert_eq!(projects[0].default_file, project.default_file); assert_eq!(projects[0].default_file, project.default_file);
@ -1084,8 +1084,8 @@ color = 1567.4"#;
let projects = settings.list_projects().await.unwrap(); let projects = settings.list_projects().await.unwrap();
assert_eq!(projects.len(), 1); assert_eq!(projects.len(), 1);
assert_eq!(projects[0].name, project_name); assert_eq!(projects[0].file.name, project_name);
assert_eq!(projects[0].path, project.path); assert_eq!(projects[0].file.path, project.file.path);
assert_eq!(projects[0].kcl_file_count, 1); assert_eq!(projects[0].kcl_file_count, 1);
assert_eq!(projects[0].directory_count, 0); assert_eq!(projects[0].directory_count, 0);
assert_eq!(projects[0].default_file, project.default_file); assert_eq!(projects[0].default_file, project.default_file);
@ -1111,8 +1111,8 @@ color = 1567.4"#;
let projects = settings.list_projects().await.unwrap(); let projects = settings.list_projects().await.unwrap();
assert_eq!(projects.len(), 1); assert_eq!(projects.len(), 1);
assert_eq!(projects[0].name, project_name); assert_eq!(projects[0].file.name, project_name);
assert_eq!(projects[0].path, project.path); assert_eq!(projects[0].file.path, project.file.path);
assert_eq!(projects[0].kcl_file_count, 1); assert_eq!(projects[0].kcl_file_count, 1);
assert_eq!(projects[0].directory_count, 0); assert_eq!(projects[0].directory_count, 0);
assert_eq!(projects[0].default_file, project.default_file); assert_eq!(projects[0].default_file, project.default_file);
@ -1138,8 +1138,8 @@ color = 1567.4"#;
let projects = settings.list_projects().await.unwrap(); let projects = settings.list_projects().await.unwrap();
assert_eq!(projects.len(), 1); assert_eq!(projects.len(), 1);
assert_eq!(projects[0].name, project_name); assert_eq!(projects[0].file.name, project_name);
assert_eq!(projects[0].path, project.path); assert_eq!(projects[0].file.path, project.file.path);
assert_eq!(projects[0].kcl_file_count, 1); assert_eq!(projects[0].kcl_file_count, 1);
assert_eq!(projects[0].directory_count, 0); assert_eq!(projects[0].directory_count, 0);
assert_eq!(projects[0].default_file, project.default_file); assert_eq!(projects[0].default_file, project.default_file);

View File

@ -7,7 +7,6 @@ use std::{
use futures::stream::TryStreamExt; use futures::stream::TryStreamExt;
use kcl_lib::{coredump::CoreDump, engine::EngineManager, executor::ExecutorSettings, lint::checks}; use kcl_lib::{coredump::CoreDump, engine::EngineManager, executor::ExecutorSettings, lint::checks};
use serde::{Deserialize, Serialize};
use serde_wasm_bindgen; use serde_wasm_bindgen;
use tower_lsp::{LspService, Server}; use tower_lsp::{LspService, Server};
use wasm_bindgen::prelude::*; use wasm_bindgen::prelude::*;

View File

@ -33,7 +33,7 @@
"noEmit": true, "noEmit": true,
"jsx": "react-jsx" "jsx": "react-jsx"
}, },
"include": ["src", "e2e", "packages", "./*.ts"], "include": ["src", "e2e", "packages", "./*.ts", "package.json"],
"exclude": ["node_modules", "./*.grammar"], "exclude": ["node_modules", "./*.grammar"],
"references": [{ "path": "./tsconfig.node.json" }] "references": [{ "path": "./tsconfig.node.json" }]
} }