Skip to content

Commit 04832ff

Browse files
committed
better way to determine user home path and concatenate bun binary path, TODO implement which and where
1 parent 14dcebd commit 04832ff

File tree

11 files changed

+54
-76
lines changed

11 files changed

+54
-76
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ To build the app, use `cargo tauri build`
3838

3939
## TODO
4040
- [] Change default icon (right now is used the Tauri icon, hehe)
41-
- [] Save bun binary path
41+
- [] Use `which` / `where` to determine bun path
4242
- [] Re-structure app to improve clean code
4343
- [] Write unit test, ups!
4444
- [] Create new tabs

index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<meta charset="UTF-8" />
55
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
66
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
7-
<title>Tauri + React + TS</title>
7+
<title>Ninja Script</title>
88
</head>
99

1010
<body>

src-tauri/Cargo.lock

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

src-tauri/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ tauri-build = { version = "1.4", features = [] }
1616
tauri = { version = "1.4", features = [ "path-all", "fs-all", "shell-open"] }
1717
serde = { version = "1.0", features = ["derive"] }
1818
serde_json = "1.0"
19+
dirs = "5.0.1"
1920

2021
[features]
2122
# this feature is used for production builds or when `devPath` points to the filesystem

src-tauri/src/main.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use std::fs::File;
66
use std::fs::create_dir_all;
77
use std::io::{Read, Write};
88
use std::env;
9+
use dirs::home_dir;
910

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

1920
#[tauri::command]
20-
fn exec_bun(input_code: &str, data_path: &str, binary_path: &str) -> String {
21+
fn exec_bun(input_code: &str, data_path: &str) -> String {
22+
let h_dir = home_dir().unwrap();
23+
let home_path: String =String::from(h_dir.to_string_lossy());
2124
let temp_directory = data_path;
2225
let _ = write_temp_js_file(input_code, &temp_directory);
2326
let temp_file = format!("{}temp_js_file.ts", temp_directory);
24-
let mut bun_command = Command::new(&binary_path).arg(temp_file)
27+
let mut bun_command = Command::new(format!("{}/.bun/bin/bun", home_path)).arg(temp_file)
2528
.stdout(std::process::Stdio::piped())
2629
.spawn()
2730
.expect("no se pudo ejecutar el proceso");

src/App.tsx

Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
import {ChangeEvent, useMemo, useState} from "react";
1+
import {ChangeEvent, useState} from "react";
22
import {evalInputCode, getAppLocalDataDirPath, getOldFile} from "./Utils";
33
import CodeEditor from '@uiw/react-textarea-code-editor';
44
import "./App.css";
55
import StatusBar from "./components/StatusBar.tsx";
6-
import BunPathContext, {ContextValue} from "./data/bun-binary-context.tsx";
7-
import {bunBinparyPathDefault} from "./constants";
86

97
let appLocalDataDir = '';
108
let lastFile = '';
@@ -18,31 +16,22 @@ getOldFile().then((res) => {
1816
})
1917

2018
function App() {
21-
const [dataContext, setDataContext] = useState<ContextValue["dataContext"]>(bunBinparyPathDefault);
22-
const contextValue = useMemo(() => ({dataContext, setDataContext}), [dataContext, setDataContext]);
2319
const [codeInput, setCodeInput] = useState(lastFile);
2420
const [codeOutput, setCodeOutput] = useState('');
2521
const [statusCode, setStatusCode] = useState('')
2622
const [statusMessage, setStatusMessage] = useState('')
2723

2824
const handleOnChange = (event: ChangeEvent<HTMLTextAreaElement>) => {
29-
if (dataContext.binaryPath.length == 0) {
30-
console.log('ERROR')
31-
setStatusCode('Error')
32-
setStatusMessage('Ingrese la ruta de bun')
33-
event.preventDefault();
34-
} else {
35-
setStatusCode('Info')
36-
setStatusMessage('Processing...')
37-
setCodeInput(event.target.value)
38-
evalInputCode(event.target.value, appLocalDataDir, dataContext.binaryPath).then((result: any) => {
39-
setCodeOutput(result)
40-
console.log("-> codeInput", codeInput);
41-
console.log("-> codeOutput", codeOutput);
42-
setStatusCode('Ok')
43-
setStatusMessage('')
44-
})
45-
}
25+
setStatusCode('Info')
26+
setStatusMessage('Processing...')
27+
setCodeInput(event.target.value)
28+
evalInputCode(event.target.value, appLocalDataDir).then((result: any) => {
29+
setCodeOutput(result)
30+
console.log("-> codeInput", codeInput);
31+
console.log("-> codeOutput", codeOutput);
32+
setStatusCode('Ok')
33+
setStatusMessage('')
34+
})
4635
}
4736

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

5241
return (
5342
<>
54-
<BunPathContext.Provider value={contextValue}>
55-
<StatusBar statusCode={statusCode} statusMessage={statusMessage}/>
56-
</BunPathContext.Provider>
43+
<StatusBar statusCode={statusCode} statusMessage={statusMessage}/>
5744
<section
5845
className='container'
5946
>
@@ -70,7 +57,7 @@ function App() {
7057
/>
7158
<CodeEditor
7259
language="js"
73-
placeholder="Enter JS/TS code."
60+
placeholder="JS/TS output."
7461
padding={15}
7562
value={codeOutput}
7663
readOnly={true}

src/Utils/index.ts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,8 @@ import {invoke} from '@tauri-apps/api'
22
import {appLocalDataDir} from '@tauri-apps/api/path';
33
import {BaseDirectory, readTextFile} from "@tauri-apps/api/fs";
44

5-
export const parseResult = (resultEncode: string) => {
6-
resultEncode = resultEncode.replace('[', '');
7-
resultEncode = resultEncode.replace(']', '');
8-
const resultParts = resultEncode.split(',');
9-
const resultPartsNumber = resultParts.map(Number)
10-
return String.fromCharCode(...resultPartsNumber)
11-
}
12-
export const evalInputCode = async (inputCode: string, dataPath: string, binaryPath: string) => {
13-
return await invoke('exec_bun', {inputCode, dataPath, binaryPath})
5+
export const evalInputCode = async (inputCode: string, dataPath: string) => {
6+
return await invoke('exec_bun', {inputCode, dataPath})
147
}
158

169
export const getAppLocalDataDirPath = async () => await appLocalDataDir();

src/components/StatusBar.tsx

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,12 @@
1-
import BunPathContext, {ContextValue} from "../data/bun-binary-context.tsx";
2-
import {useContext} from "react";
31

42
function StatusBar(props: {
53
statusMessage: string;
64
statusCode: string;
75
}) {
86
const statusMessage = props.statusMessage ?? '';
97
const statusCode = props.statusCode ?? '';
10-
const { dataContext, setDataContext } = useContext<ContextValue>(BunPathContext);
11-
const { binaryPath } = dataContext;
128
return (
139
<section className={"status-bar-container"}>
14-
<div className={"status-bar-elements status-bar-binary-input"}>
15-
<input
16-
placeholder={"Enter bun binary path..."}
17-
value={binaryPath}
18-
onChange={(event) => setDataContext({binaryPath: event.target.value})}
19-
type="text"
20-
/>
21-
</div>
2210
<div className={"status-bar-elements status-bar-status-text"}>
2311
{statusMessage} {statusMessage && "-"} {statusCode}
2412
</div>

src/constants/index.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +0,0 @@
1-
export const bunBinparyPathDefault = {
2-
binaryPath: ''
3-
}

src/data/bun-binary-context.tsx

Lines changed: 0 additions & 18 deletions
This file was deleted.

0 commit comments

Comments
 (0)