11import path from "path" ;
22import { unescape } from "querystring" ;
33import fs from "fs" ;
4+ import os from "os" ;
45import { app , dialog , ipcMain } from "electron" ;
56/** ACTIONS */
67import startBackgroundProcess from "./actions/startBackgroundProcess" ;
@@ -35,6 +36,13 @@ const getFileCategory = (fileExtension) => {
3536 }
3637} ;
3738
39+ const createFsResourceItem = ( name , fsResourcePath , type ) => ( {
40+ name,
41+ path : fsResourcePath ,
42+ type,
43+ ...( type === "directory" && { contents : [ ] } ) ,
44+ } ) ;
45+
3846export async function trackRecentlyAccessedFile ( filePath ) {
3947 const fileExtension = path . extname ( filePath ) ;
4048
@@ -263,19 +271,21 @@ export const registerMainProcessEventsForWebAppWindow = (webAppWindow) => {
263271export const registerMainProcessCommonEvents = ( ) => {
264272 ipcMain . handle ( "open-file-dialog" , async ( event , options ) => {
265273 const fileDialogPromise = dialog . showOpenDialog ( options ?? { } ) ;
266- return fileDialogPromise . then ( result => {
267- const { canceled, filePaths } = result ;
268- if ( canceled || ! filePaths ?. length ) {
269- return { canceled : true , files : [ ] } ;
270- }
271- const files = [ ]
272- for ( const filePath of filePaths ) {
273- const { size } = fs . statSync ( filePath ) ;
274- const name = path . basename ( filePath ) ;
275- files . push ( { path : filePath , name, size } ) ;
276- }
277- return { canceled, files } ;
278- } ) . catch ( console . error )
274+ return fileDialogPromise
275+ . then ( ( result ) => {
276+ const { canceled, filePaths } = result ;
277+ if ( canceled || ! filePaths ?. length ) {
278+ return { canceled : true , files : [ ] } ;
279+ }
280+ const files = [ ] ;
281+ for ( const filePath of filePaths ) {
282+ const { size } = fs . statSync ( filePath ) ;
283+ const name = path . basename ( filePath ) ;
284+ files . push ( { path : filePath , name, size } ) ;
285+ }
286+ return { canceled, files } ;
287+ } )
288+ . catch ( console . error ) ;
279289 } ) ;
280290
281291 ipcMain . handle ( "open-folder-dialog" , async ( event , options = { } ) => {
@@ -286,4 +296,96 @@ export const registerMainProcessCommonEvents = () => {
286296 const folderDialogPromise = await dialog . showOpenDialog ( dialogOptions ) ;
287297 return folderDialogPromise ;
288298 } ) ;
299+
300+ ipcMain . handle (
301+ "get-workspace-folder-preview" ,
302+ async ( event , payload = { } ) => {
303+ try {
304+ let { folderPath } = payload ;
305+
306+ if ( ! folderPath ) {
307+ const homeDir = os . homedir ( ) ;
308+ const documentsPath = path . join ( homeDir , "Documents" ) ;
309+
310+ if (
311+ fs . existsSync ( documentsPath ) &&
312+ fs . statSync ( documentsPath ) . isDirectory ( )
313+ ) {
314+ folderPath = documentsPath ;
315+ } else {
316+ folderPath = homeDir ;
317+ }
318+ }
319+
320+ // Checking if workspace folder exists
321+ const folderExists = fs . existsSync ( folderPath ) ;
322+ const existingContents = [ ] ;
323+
324+ if ( folderExists ) {
325+ // Check if it's a directory
326+ const stats = fs . statSync ( folderPath ) ;
327+ if ( ! stats . isDirectory ( ) ) {
328+ throw new Error ( "Folder path is not a directory" ) ;
329+ }
330+
331+ // Read existing directory contents at root level of workspace
332+ const items = fs . readdirSync ( folderPath ) ;
333+
334+ for ( const item of items ) {
335+ const itemPath = path . join ( folderPath , item ) ;
336+ const itemStats = fs . statSync ( itemPath ) ;
337+
338+ existingContents . push (
339+ createFsResourceItem (
340+ item ,
341+ itemPath ,
342+ itemStats . isDirectory ( ) ? "directory" : "file"
343+ )
344+ ) ;
345+ }
346+
347+ existingContents . sort ( ( a , b ) => {
348+ const aIsDir = a . type === "directory" ;
349+ const bIsDir = b . type === "directory" ;
350+ if ( aIsDir && ! bIsDir ) return - 1 ;
351+ if ( ! aIsDir && bIsDir ) return 1 ;
352+ return a . name . localeCompare ( b . name ) ;
353+ } ) ;
354+ }
355+
356+ const newAdditions = {
357+ name : "Workspace folder" ,
358+ path : folderPath ,
359+ type : "directory" ,
360+ contents : [
361+ {
362+ name : "environments" ,
363+ path : path . join ( folderPath , "environments" ) ,
364+ type : "directory" ,
365+ contents : [ ] ,
366+ } ,
367+ {
368+ name : "requestly.json" ,
369+ path : path . join ( folderPath , "requestly.json" ) ,
370+ type : "file" ,
371+ } ,
372+ ] ,
373+ } ;
374+
375+ return {
376+ success : true ,
377+ folderPath,
378+ existingContents,
379+ newAdditions,
380+ } ;
381+ } catch ( error ) {
382+ console . error ( "Error getting folder preview:" , error ) ;
383+ return {
384+ success : false ,
385+ error : error . message ,
386+ folderPath : payload . folderPath ,
387+ } ;
388+ }
389+ }
390+ ) ;
289391} ;
0 commit comments