move mkdir to init (#7085)

* move to init

* fix tests

* fix tests
This commit is contained in:
Jack Kavanagh 2024-02-13 10:16:57 +01:00 committed by GitHub
parent 444877e1e1
commit 8ecf47c0a7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 8 additions and 35 deletions

View File

@ -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

View File

@ -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));

View File

@ -96,8 +96,8 @@ export const cancelCurlRequest = (id: string) => cancelCurlRequestHandlers[id]()
export const curlRequest = (options: CurlRequestOptions) => new Promise<CurlRequestOutput>(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;

View File

@ -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));

View File

@ -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<string> {
// 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<string>((resolve, reject) => {
// fs.writeFile(timelinePath, timelineStr, err => {
// if (err != null) {
// return reject(err);
// }
// resolve(timelinePath);
// });
// });
// }