Skip to content

Commit 6e16706

Browse files
committed
fix: match electron apis with tauri api output and convention
1 parent 4377dc4 commit 6e16706

8 files changed

Lines changed: 105 additions & 8 deletions

src-electron/main.js

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,18 @@ ipcMain.handle('get-app-path', () => {
112112

113113
// Directory APIs
114114
ipcMain.handle('get-documents-dir', () => {
115-
return path.join(os.homedir(), 'Documents');
115+
// Match Tauri's documentDir which ends with a trailing slash
116+
return path.join(os.homedir(), 'Documents') + path.sep;
117+
});
118+
119+
ipcMain.handle('get-home-dir', () => {
120+
// Match Tauri's homeDir which ends with a trailing slash
121+
const home = os.homedir();
122+
return home.endsWith(path.sep) ? home : home + path.sep;
123+
});
124+
125+
ipcMain.handle('get-temp-dir', () => {
126+
return os.tmpdir();
116127
});
117128

118129
ipcMain.handle('get-app-data-dir', () => {
@@ -121,14 +132,18 @@ ipcMain.handle('get-app-data-dir', () => {
121132
// macOS: ~/Library/Application Support/fs.phcode/
122133
// Windows: %LOCALAPPDATA%/fs.phcode/
123134
const home = os.homedir();
135+
let appDataDir;
124136
switch (process.platform) {
125137
case 'darwin':
126-
return path.join(home, 'Library', 'Application Support', APP_IDENTIFIER);
138+
appDataDir = path.join(home, 'Library', 'Application Support', APP_IDENTIFIER);
139+
break;
127140
case 'win32':
128-
return path.join(process.env.LOCALAPPDATA || path.join(home, 'AppData', 'Local'), APP_IDENTIFIER);
141+
appDataDir = path.join(process.env.LOCALAPPDATA || path.join(home, 'AppData', 'Local'), APP_IDENTIFIER);
142+
break;
129143
default:
130-
return path.join(process.env.XDG_DATA_HOME || path.join(home, '.local', 'share'), APP_IDENTIFIER);
144+
appDataDir = path.join(process.env.XDG_DATA_HOME || path.join(home, '.local', 'share'), APP_IDENTIFIER);
131145
}
146+
return appDataDir + path.sep;
132147
});
133148

134149
// Get Windows drive letters (returns null on non-Windows platforms)

src-electron/preload.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,17 @@ contextBridge.exposeInMainWorld('electronAPI', {
1717
// Flag to identify Electron environment
1818
isElectron: true,
1919

20+
// Path utilities
21+
path: {
22+
sep: process.platform === 'win32' ? '\\' : '/'
23+
},
24+
2025
// CLI and paths
2126
getCliArgs: () => ipcRenderer.invoke('get-cli-args'),
2227
getAppPath: () => ipcRenderer.invoke('get-app-path'),
2328
documentDir: () => ipcRenderer.invoke('get-documents-dir'),
29+
homeDir: () => ipcRenderer.invoke('get-home-dir'),
30+
tempDir: () => ipcRenderer.invoke('get-temp-dir'),
2431
appLocalDataDir: () => ipcRenderer.invoke('get-app-data-dir'),
2532
getWindowsDrives: () => ipcRenderer.invoke('get-windows-drives'),
2633
showOpenDialog: (options) => ipcRenderer.invoke('show-open-dialog', options),

test/test-copy.browser.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ function _setupTests(testTypeSrc, testTypeDst) {
1717
return window.__TAURI__.path.appLocalDataDir();
1818
}
1919
if(window.__ELECTRON__) {
20-
return await window.electronAPI.appLocalDataDir() + "/";
20+
return window.electronAPI.appLocalDataDir();
2121
}
2222
throw new Error("No native environment detected");
2323
}

test/test-dir.browser.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ function _setupTests(testType) {
2222
return window.__TAURI__.path.appLocalDataDir();
2323
}
2424
if(window.__ELECTRON__) {
25-
return await window.electronAPI.appLocalDataDir() + "/";
25+
return window.electronAPI.appLocalDataDir();
2626
}
2727
throw new Error("No native environment detected");
2828
}

test/test-file.browser.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ function _setupTests(testType) {
2525
return window.__TAURI__.path.appLocalDataDir();
2626
}
2727
if(window.__ELECTRON__) {
28-
return await window.electronAPI.appLocalDataDir() + "/";
28+
return window.electronAPI.appLocalDataDir();
2929
}
3030
throw new Error("No native environment detected");
3131
}

test/test-getPlatformPath-api.browser.js

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,4 +114,78 @@ if(window.__TAURI__ || window.__ELECTRON__){
114114
});
115115
}
116116
});
117+
118+
describe(`test homeDir api`, function () {
119+
async function getHomeDir() {
120+
if (window.__TAURI__) {
121+
return window.__TAURI__.path.homeDir();
122+
} else if (window.electronAPI) {
123+
return window.electronAPI.homeDir();
124+
}
125+
return null;
126+
}
127+
128+
it(`should homeDir return a valid path ending with separator`, async function () {
129+
let homeDir = await getHomeDir();
130+
expect(homeDir).to.be.a('string');
131+
expect(homeDir.length).to.be.greaterThan(0);
132+
if(IS_WINDOWS){
133+
// Windows home dir should contain a drive letter like C:\Users\...
134+
expect(homeDir.charAt(1)).to.eql(':');
135+
expect(homeDir.endsWith('\\')).to.be.true;
136+
} else {
137+
// Unix home dir should start and end with /
138+
expect(homeDir.startsWith('/')).to.be.true;
139+
expect(homeDir.endsWith('/')).to.be.true;
140+
}
141+
});
142+
});
143+
144+
describe(`test tempDir api`, function () {
145+
async function getTempDir() {
146+
if (window.__TAURI__) {
147+
return window.__TAURI__.os.tempdir();
148+
} else if (window.electronAPI) {
149+
return window.electronAPI.tempDir();
150+
}
151+
return null;
152+
}
153+
154+
it(`should tempDir return a valid path without trailing separator`, async function () {
155+
let tempDir = await getTempDir();
156+
expect(tempDir).to.be.a('string');
157+
expect(tempDir.length).to.be.greaterThan(0);
158+
if(IS_WINDOWS){
159+
// Windows temp dir should contain a drive letter like C:\...
160+
expect(tempDir.charAt(1)).to.eql(':');
161+
expect(tempDir.endsWith('\\')).to.be.false;
162+
} else {
163+
// Unix temp dir should start with / but not end with /
164+
expect(tempDir.startsWith('/')).to.be.true;
165+
expect(tempDir.endsWith('/')).to.be.false;
166+
}
167+
});
168+
});
169+
170+
describe(`test path.sep api`, function () {
171+
function getPathSep() {
172+
if (window.__TAURI__) {
173+
return window.__TAURI__.path.sep;
174+
} else if (window.electronAPI) {
175+
return window.electronAPI.path.sep;
176+
}
177+
return null;
178+
}
179+
180+
it(`should path.sep return correct separator for platform`, function () {
181+
let sep = getPathSep();
182+
expect(sep).to.be.a('string');
183+
expect(sep.length).to.eql(1);
184+
if(IS_WINDOWS){
185+
expect(sep).to.eql('\\');
186+
} else {
187+
expect(sep).to.eql('/');
188+
}
189+
});
190+
});
117191
}

test/test-watcher.browser.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ function _setupTests(testType) {
1919
return window.__TAURI__.path.appLocalDataDir();
2020
}
2121
if(window.__ELECTRON__) {
22-
return await window.electronAPI.appLocalDataDir() + "/";
22+
return await window.electronAPI.appLocalDataDir();
2323
}
2424
throw new Error("No native environment detected");
2525
}

test/test.worker.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ function _setupTests(testType) {
6969
}
7070

7171
before(async function () {
72+
this.timeout(10000); // Allow extra time for worker initialization
7273
await _setupTestPath();
7374
await _clean();
7475
await _init();

0 commit comments

Comments
 (0)