Skip to content

Commit

Permalink
better way to determine user home path and concatenate bun binary pat…
Browse files Browse the repository at this point in the history
…h, TODO implement `which` and `where`
  • Loading branch information
kmikzjh committed Sep 17, 2023
1 parent 14dcebd commit 04832ff
Show file tree
Hide file tree
Showing 11 changed files with 54 additions and 76 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ To build the app, use `cargo tauri build`

## TODO
- [] Change default icon (right now is used the Tauri icon, hehe)
- [] Save bun binary path
- [] Use `which` / `where` to determine bun path
- [] Re-structure app to improve clean code
- [] Write unit test, ups!
- [] Create new tabs
Expand Down
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Tauri + React + TS</title>
<title>Ninja Script</title>
</head>

<body>
Expand Down
28 changes: 28 additions & 0 deletions src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ tauri-build = { version = "1.4", features = [] }
tauri = { version = "1.4", features = [ "path-all", "fs-all", "shell-open"] }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
dirs = "5.0.1"

[features]
# this feature is used for production builds or when `devPath` points to the filesystem
Expand Down
7 changes: 5 additions & 2 deletions src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use std::fs::File;
use std::fs::create_dir_all;
use std::io::{Read, Write};
use std::env;
use dirs::home_dir;

fn write_temp_js_file(content: &str, dir: &str) -> std::io::Result<()> {
let _ = create_dir_all(dir);
Expand All @@ -17,11 +18,13 @@ fn write_temp_js_file(content: &str, dir: &str) -> std::io::Result<()> {
}

#[tauri::command]
fn exec_bun(input_code: &str, data_path: &str, binary_path: &str) -> String {
fn exec_bun(input_code: &str, data_path: &str) -> String {
let h_dir = home_dir().unwrap();
let home_path: String =String::from(h_dir.to_string_lossy());
let temp_directory = data_path;
let _ = write_temp_js_file(input_code, &temp_directory);
let temp_file = format!("{}temp_js_file.ts", temp_directory);
let mut bun_command = Command::new(&binary_path).arg(temp_file)
let mut bun_command = Command::new(format!("{}/.bun/bin/bun", home_path)).arg(temp_file)
.stdout(std::process::Stdio::piped())
.spawn()
.expect("no se pudo ejecutar el proceso");
Expand Down
39 changes: 13 additions & 26 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import {ChangeEvent, useMemo, useState} from "react";
import {ChangeEvent, useState} from "react";
import {evalInputCode, getAppLocalDataDirPath, getOldFile} from "./Utils";
import CodeEditor from '@uiw/react-textarea-code-editor';
import "./App.css";
import StatusBar from "./components/StatusBar.tsx";
import BunPathContext, {ContextValue} from "./data/bun-binary-context.tsx";
import {bunBinparyPathDefault} from "./constants";

let appLocalDataDir = '';
let lastFile = '';
Expand All @@ -18,31 +16,22 @@ getOldFile().then((res) => {
})

function App() {
const [dataContext, setDataContext] = useState<ContextValue["dataContext"]>(bunBinparyPathDefault);
const contextValue = useMemo(() => ({dataContext, setDataContext}), [dataContext, setDataContext]);
const [codeInput, setCodeInput] = useState(lastFile);
const [codeOutput, setCodeOutput] = useState('');
const [statusCode, setStatusCode] = useState('')
const [statusMessage, setStatusMessage] = useState('')

const handleOnChange = (event: ChangeEvent<HTMLTextAreaElement>) => {
if (dataContext.binaryPath.length == 0) {
console.log('ERROR')
setStatusCode('Error')
setStatusMessage('Ingrese la ruta de bun')
event.preventDefault();
} else {
setStatusCode('Info')
setStatusMessage('Processing...')
setCodeInput(event.target.value)
evalInputCode(event.target.value, appLocalDataDir, dataContext.binaryPath).then((result: any) => {
setCodeOutput(result)
console.log("-> codeInput", codeInput);
console.log("-> codeOutput", codeOutput);
setStatusCode('Ok')
setStatusMessage('')
})
}
setStatusCode('Info')
setStatusMessage('Processing...')
setCodeInput(event.target.value)
evalInputCode(event.target.value, appLocalDataDir).then((result: any) => {
setCodeOutput(result)
console.log("-> codeInput", codeInput);
console.log("-> codeOutput", codeOutput);
setStatusCode('Ok')
setStatusMessage('')
})
}

// if (lastFile.length > 0) {
Expand All @@ -51,9 +40,7 @@ function App() {

return (
<>
<BunPathContext.Provider value={contextValue}>
<StatusBar statusCode={statusCode} statusMessage={statusMessage}/>
</BunPathContext.Provider>
<StatusBar statusCode={statusCode} statusMessage={statusMessage}/>
<section
className='container'
>
Expand All @@ -70,7 +57,7 @@ function App() {
/>
<CodeEditor
language="js"
placeholder="Enter JS/TS code."
placeholder="JS/TS output."
padding={15}
value={codeOutput}
readOnly={true}
Expand Down
11 changes: 2 additions & 9 deletions src/Utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,8 @@ import {invoke} from '@tauri-apps/api'
import {appLocalDataDir} from '@tauri-apps/api/path';
import {BaseDirectory, readTextFile} from "@tauri-apps/api/fs";

export const parseResult = (resultEncode: string) => {
resultEncode = resultEncode.replace('[', '');
resultEncode = resultEncode.replace(']', '');
const resultParts = resultEncode.split(',');
const resultPartsNumber = resultParts.map(Number)
return String.fromCharCode(...resultPartsNumber)
}
export const evalInputCode = async (inputCode: string, dataPath: string, binaryPath: string) => {
return await invoke('exec_bun', {inputCode, dataPath, binaryPath})
export const evalInputCode = async (inputCode: string, dataPath: string) => {
return await invoke('exec_bun', {inputCode, dataPath})
}

export const getAppLocalDataDirPath = async () => await appLocalDataDir();
Expand Down
12 changes: 0 additions & 12 deletions src/components/StatusBar.tsx
Original file line number Diff line number Diff line change
@@ -1,24 +1,12 @@
import BunPathContext, {ContextValue} from "../data/bun-binary-context.tsx";
import {useContext} from "react";

function StatusBar(props: {
statusMessage: string;
statusCode: string;
}) {
const statusMessage = props.statusMessage ?? '';
const statusCode = props.statusCode ?? '';
const { dataContext, setDataContext } = useContext<ContextValue>(BunPathContext);
const { binaryPath } = dataContext;
return (
<section className={"status-bar-container"}>
<div className={"status-bar-elements status-bar-binary-input"}>
<input
placeholder={"Enter bun binary path..."}
value={binaryPath}
onChange={(event) => setDataContext({binaryPath: event.target.value})}
type="text"
/>
</div>
<div className={"status-bar-elements status-bar-status-text"}>
{statusMessage} {statusMessage && "-"} {statusCode}
</div>
Expand Down
3 changes: 0 additions & 3 deletions src/constants/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +0,0 @@
export const bunBinparyPathDefault = {
binaryPath: ''
}
18 changes: 0 additions & 18 deletions src/data/bun-binary-context.tsx

This file was deleted.

7 changes: 3 additions & 4 deletions src/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
-webkit-text-size-adjust: 100%;
--calc-height: calc(100vh - 67px);
--calc-height: calc(100vh - 40px);
}

body {
Expand All @@ -21,7 +21,7 @@ body {
}

.container {
margin: 67px 0 0 0;
margin: 40px 0 0 0;
display: flex;
flex-direction: row;
justify-content: left;
Expand All @@ -37,7 +37,7 @@ body {
flex-grow: 1;
word-break: break-word;
min-height: 100%;
min-width: 50vh;
min-width: 45vw;
}

.status-bar-container {
Expand All @@ -56,7 +56,6 @@ body {
}

.status-bar-status-text {
padding: 0.6em 1.2em;
text-align: right;;
}

Expand Down

0 comments on commit 04832ff

Please sign in to comment.