From 8ecf47c0a7303a616a061eeace9ef3c56b37ee38 Mon Sep 17 00:00:00 2001 From: Jack Kavanagh Date: Tue, 13 Feb 2024 10:16:57 +0100 Subject: [PATCH] move mkdir to init (#7085) * move to init * fix tests * fix tests --- packages/insomnia/src/main.development.ts | 14 ++++------- packages/insomnia/src/main/network/curl.ts | 1 - .../src/main/network/libcurl-promise.ts | 4 ++-- .../insomnia/src/main/network/websocket.ts | 1 - packages/insomnia/src/network/network.ts | 23 +------------------ 5 files changed, 8 insertions(+), 35 deletions(-) diff --git a/packages/insomnia/src/main.development.ts b/packages/insomnia/src/main.development.ts index 6787a7c71..36be374b9 100644 --- a/packages/insomnia/src/main.development.ts +++ b/packages/insomnia/src/main.development.ts @@ -3,6 +3,7 @@ import electron, { app, ipcMain, session } from 'electron'; import { BrowserWindow } from 'electron'; import contextMenu from 'electron-context-menu'; import installExtension, { REACT_DEVELOPER_TOOLS } from 'electron-devtools-installer'; +import fs from 'fs/promises'; import path from 'path'; import { userDataFolder } from '../config/config.json'; @@ -39,15 +40,8 @@ log.info(`Running version ${getAppVersion()}`); // Override the Electron userData path // This makes Chromium use this folder for eg localStorage -const envDataPath = process.env.INSOMNIA_DATA_PATH; -if (envDataPath) { - app.setPath('userData', envDataPath); -} else { - // Explicitly set userData folder from config because it's sketchy to rely on electron-builder to use productName, which could be changed by accident. - const defaultPath = app.getPath('userData'); - const newPath = path.join(defaultPath, '../', isDevelopment() ? 'insomnia-app' : userDataFolder); - app.setPath('userData', newPath); -} +const dataPath = process.env.INSOMNIA_DATA_PATH || path.join(app.getPath('userData'), '../', isDevelopment() ? 'insomnia-app' : userDataFolder); +app.setPath('userData', dataPath); // So if (window) checks don't throw global.window = global.window || undefined; @@ -102,6 +96,8 @@ app.on('ready', async () => { // Init the rest await updates.init(); + // recursive = ignore already exists error + await fs.mkdir(path.join(dataPath, 'responses'), { recursive: true }); }); // Set as default protocol diff --git a/packages/insomnia/src/main/network/curl.ts b/packages/insomnia/src/main/network/curl.ts index c48f15092..1ffdd3e63 100644 --- a/packages/insomnia/src/main/network/curl.ts +++ b/packages/insomnia/src/main/network/curl.ts @@ -109,7 +109,6 @@ const openCurlConnection = async ( } const responsesDir = path.join(process.env['INSOMNIA_DATA_PATH'] || electron.app.getPath('userData'), 'responses'); - fs.mkdirSync(responsesDir, { recursive: true }); const responseBodyPath = path.join(responsesDir, uuidV4() + '.response'); eventLogFileStreams.set(options.requestId, fs.createWriteStream(responseBodyPath)); diff --git a/packages/insomnia/src/main/network/libcurl-promise.ts b/packages/insomnia/src/main/network/libcurl-promise.ts index 71b9e64e0..7b9d162d9 100644 --- a/packages/insomnia/src/main/network/libcurl-promise.ts +++ b/packages/insomnia/src/main/network/libcurl-promise.ts @@ -96,8 +96,8 @@ export const cancelCurlRequest = (id: string) => cancelCurlRequestHandlers[id]() export const curlRequest = (options: CurlRequestOptions) => new Promise(async resolve => { try { const responsesDir = path.join(getDataDirectory(), 'responses'); - fs.mkdirSync(responsesDir, { recursive: true }); - + // TODO: remove this check, its only used for network.test.ts + await fs.promises.mkdir(responsesDir, { recursive: true }); const responseBodyPath = path.join(responsesDir, uuidv4() + '.response'); const { requestId, req, finalUrl, settings, certificates, caCertficatePath, socketPath, authHeader } = options; diff --git a/packages/insomnia/src/main/network/websocket.ts b/packages/insomnia/src/main/network/websocket.ts index 66944f607..d7d39c62f 100644 --- a/packages/insomnia/src/main/network/websocket.ts +++ b/packages/insomnia/src/main/network/websocket.ts @@ -118,7 +118,6 @@ const openWebSocketConnection = async ( } const responsesDir = path.join(process.env['INSOMNIA_DATA_PATH'] || electron.app.getPath('userData'), 'responses'); - fs.mkdirSync(responsesDir, { recursive: true }); const responseBodyPath = path.join(responsesDir, uuidV4() + '.response'); eventLogFileStreams.set(options.requestId, fs.createWriteStream(responseBodyPath)); diff --git a/packages/insomnia/src/network/network.ts b/packages/insomnia/src/network/network.ts index 579951f3b..b4de50175 100644 --- a/packages/insomnia/src/network/network.ts +++ b/packages/insomnia/src/network/network.ts @@ -65,7 +65,7 @@ export const fetchRequestData = async (requestId: string) => { const clientCertificates = await models.clientCertificate.findByParentId(workspaceId); const caCert = await models.caCertificate.findByParentId(workspaceId); const responseId = generateId('res'); - const responsesDir = pathJoin(process.env['INSOMNIA_DATA_PATH'] || (process.type === 'renderer' ? window : require('electron')).app.getPath('userData'), 'responses'); + const responsesDir = pathJoin((process.type === 'renderer' ? window : require('electron')).app.getPath('userData'), 'responses'); const timelinePath = pathJoin(responsesDir, responseId + '.timeline'); return { request, environment, settings, clientCertificates, caCert, activeEnvironmentId, timelinePath, responseId }; }; @@ -337,24 +337,3 @@ async function _applyResponsePluginHooks( } } - -// export function storeTimeline(timeline: ResponseTimelineEntry[]): Promise { -// const timelineStr = JSON.stringify(timeline, null, '\t'); -// const timelineHash = uuidv4(); -// const responsesDir = pathJoin(process.env['INSOMNIA_DATA_PATH'] || (process.type === 'renderer' ? window : electron).app.getPath('userData'), 'responses'); - -// fs.mkdirSync(responsesDir, { recursive: true }); - -// const timelinePath = pathJoin(responsesDir, timelineHash + '.timeline'); -// if (process.type === 'renderer') { -// return window.main.writeFile({ path: timelinePath, content: timelineStr }); -// } -// return new Promise((resolve, reject) => { -// fs.writeFile(timelinePath, timelineStr, err => { -// if (err != null) { -// return reject(err); -// } -// resolve(timelinePath); -// }); -// }); -// }