Skip to content

Commit f37ac8c

Browse files
committed
chore: application root for tests
Signed-off-by: Pawel Psztyc <[email protected]>
1 parent 260eec6 commit f37ac8c

File tree

9 files changed

+90
-330
lines changed

9 files changed

+90
-330
lines changed

src/io/AmfParserConnector.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { app, ipcMain } from 'electron';
22
import path from 'path';
33
import { v4 } from 'uuid';
44
import { fork } from 'child_process';
5+
import { fileURLToPath } from 'url';
56
import { logger } from './Logger.js';
67

78
/** @typedef {import('child_process').ChildProcess} ChildProcess */
@@ -43,8 +44,8 @@ export class AmfParserConnector {
4344
}
4445

4546
serviceUrl() {
46-
const base = app.getAppPath();
47-
return path.join(base, 'services', 'AmfParserService.js');
47+
const base = path.dirname(fileURLToPath(import.meta.url));
48+
return path.join(base, '..', '..', 'services', 'AmfParserService.js');
4849
}
4950

5051
/**

src/io/ApplicationDefaults.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ThemeDefaults } from './defaults/ThemeDefaultsNew.js';
1+
import { ThemeDefaults } from './defaults/ThemeDefaults.js';
22
import { WorkspaceDefaults } from './defaults/WorkspaceDefaults.js';
33
import { DataStore } from './defaults/DataStore.js';
44

src/io/ApplicationMenu.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { EventEmitter } from 'events';
33
import { app, Menu, MenuItem } from 'electron';
44
import fs from 'fs-extra';
55
import path from 'path';
6+
import { fileURLToPath } from 'url';
67
import { logger } from './Logger.js';
78
import { WorkspaceHistory } from './models/WorkspaceHistory.js';
89

@@ -65,7 +66,8 @@ export class ApplicationMenu extends EventEmitter {
6566
async getTemplate() {
6667
const osName = this.platformToName();
6768
const name = `${osName}.json`;
68-
const file = path.join(app.getAppPath(), 'src', 'menus', name);
69+
const base = path.dirname(fileURLToPath(import.meta.url));
70+
const file = path.join(base, '..', '..', 'src', 'menus', name);
6971
logger.debug(`Menu template location: ${file}`);
7072
return fs.readJson(file);
7173
}

src/io/ApplicationUpdater.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { autoUpdater } from 'electron-updater';
22
import { EventEmitter } from 'events';
33
import { dialog, nativeImage, ipcMain, BrowserWindow, app } from 'electron';
44
import path from 'path';
5+
import { fileURLToPath } from 'url';
56
import { logger } from './Logger.js';
67

78
autoUpdater.logger = logger;
@@ -257,7 +258,8 @@ export class ApplicationUpdater extends EventEmitter {
257258
detail,
258259
};
259260
if (!isError) {
260-
const imgPath = path.join(app.getAppPath(), 'assets', 'icon.iconset', 'icon_512x512.png');
261+
const base = path.dirname(fileURLToPath(import.meta.url));
262+
const imgPath = path.join(base, '..', '..', 'assets', 'icon.iconset', 'icon_512x512.png');
261263
dialogOpts.icon = nativeImage.createFromPath(imgPath);
262264
}
263265
dialog.showMessageBox(dialogOpts);

src/io/EsmProtocol.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
/* eslint-disable no-bitwise */
2-
import { session, protocol, app } from 'electron';
2+
import { session, protocol } from 'electron';
33
import path from 'path';
44
import fs from 'fs-extra';
55
import mime from 'mime-types';
6+
import { fileURLToPath } from 'url';
67
import { logger } from './Logger.js';
78
import { MainWindowPersist, TaskManagerWindowPersist } from '../common/Constants.js';
89

@@ -32,11 +33,12 @@ const locationPrefixes = ['web_modules', 'node_modules', 'src'];
3233
export class EsmProtocol {
3334
constructor() {
3435
this[requestHandler] = this[requestHandler].bind(this);
36+
const base = path.dirname(fileURLToPath(import.meta.url));
3537
/**
3638
* Base path to the application folder.
37-
* @type {String}
39+
* @type {string}
3840
*/
39-
this.basePath = app.getAppPath();
41+
this.basePath = path.join(base, '..', '..');
4042
}
4143

4244
/**

src/io/WindowsManager.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,8 @@ export class WindowsManager {
310310
},
311311
});
312312
if (preload) {
313-
options.webPreferences.preload = path.join(app.getAppPath(), 'src', 'preload', preload);
313+
const base = path.dirname(url.fileURLToPath(import.meta.url));
314+
options.webPreferences.preload = path.join(base, '..', '..', 'src', 'preload', preload);
314315
}
315316
return options;
316317
}

src/io/defaults/ThemeDefaults.js

Lines changed: 36 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
/* eslint-disable no-param-reassign */
22
/* eslint-disable no-await-in-loop */
3-
import fs from 'fs-extra';
3+
import fs from 'fs/promises';
4+
import fsSync from 'fs';
45
import path from 'path';
56
import semver from 'semver';
7+
import { fileURLToPath } from 'url';
68
import { logger } from '../Logger.js';
9+
import * as FsUtils from '../lib/FsUtils.js';
710

811
/** @typedef {import('@advanced-rest-client/events').Theme.ArcThemeStore} ArcThemeStore */
912

@@ -19,6 +22,11 @@ import { logger } from '../Logger.js';
1922
* A class that is responsible for setting up theme defaults.
2023
*/
2124
export class ThemeDefaults {
25+
constructor() {
26+
const base = path.dirname(fileURLToPath(import.meta.url));
27+
this.appPath = path.join(base, '..', '..', '..');
28+
}
29+
2230
/**
2331
* Sets defaults if the defaults are not yet set.
2432
* It copies anypoint and default theme to theme location
@@ -41,8 +49,8 @@ export class ThemeDefaults {
4149
*/
4250
async _readDefaultThemesPackages() {
4351
logger.silly('Reading local (app) themes info file...');
44-
const source = path.join(__dirname, '..', '..', '..', 'appresources', 'themes');
45-
logger.silly('Searching for default themes...');
52+
const source = path.join(this.appPath, 'appresources', 'themes');
53+
logger.silly(`Searching for default themes in ${source}...`);
4654
const themes = await this._listThemePackages(source);
4755
if (themes) {
4856
logger.silly(`Found ${themes.length} default themes.`);
@@ -56,13 +64,16 @@ export class ThemeDefaults {
5664
* @returns {Promise<DefaultThemeInfo[]|undefined>}
5765
*/
5866
async _listThemePackages(themePath, parent) {
67+
/** @type string[]|undefined */
5968
let items;
6069
try {
61-
items = await fs.readdir(themePath, 'utf8');
70+
fsSync.readdirSync(themePath);
71+
items = await fs.readdir(themePath);
6272
} catch (e) {
63-
logger.error(`Unable to read themes path ${themePath}. Skipping themes initialization.`);
64-
logger.error(e);
65-
logger.error(e.stack);
73+
logger.error(`Unable to read themes path ${themePath}. Skipping themes initialization.\n${e.message}`);
74+
if (e.stack) {
75+
logger.error(e.stack);
76+
}
6677
return undefined;
6778
}
6879
let themePaths = [];
@@ -71,9 +82,9 @@ export class ThemeDefaults {
7182
const stats = await fs.stat(loc);
7283
if (stats.isDirectory()) {
7384
const pkgFile = path.join(loc, 'package.json');
74-
const hasPackage = await fs.pathExists(pkgFile);
85+
const hasPackage = await FsUtils.canRead(pkgFile);
7586
if (hasPackage) {
76-
const pkgContent = await fs.readJSON(pkgFile, { throws: false });
87+
const pkgContent = /** @type DefaultThemeInfo */ (await FsUtils.readJson(pkgFile, { throws: false }));
7788
const main = this._readMainFile(pkgContent, name);
7889
if (parent) {
7990
name = path.join(parent, name);
@@ -146,15 +157,15 @@ export class ThemeDefaults {
146157
*/
147158
async _ensureTheme(info) {
148159
const file = path.join(process.env.ARC_THEMES, info.name, info.main);
149-
const exists = await fs.pathExists(file);
160+
const exists = await FsUtils.canRead(file);
150161
if (!exists) {
151162
await this._copyThemeFiles(info);
152163
return;
153164
}
154165
const localPkgFile = path.join(info.location, 'package.json');
155-
const localPkg = await fs.readJson(localPkgFile);
166+
const localPkg = /** @type any */ (await FsUtils.readJson(localPkgFile));
156167
const installedPkgFile = path.join(process.env.ARC_THEMES, info.name, 'package.json');
157-
const installedPkg = await fs.readJson(installedPkgFile);
168+
const installedPkg = /** @type any */ (await FsUtils.readJson(installedPkgFile));
158169
const localVersion = localPkg.version;
159170
const installedVersion = installedPkg.version;
160171
if (semver.gt(localVersion, installedVersion)) {
@@ -173,8 +184,8 @@ export class ThemeDefaults {
173184
logger.debug(`Creating ${info.name} theme...`);
174185
const dest = path.join(process.env.ARC_THEMES, info.name);
175186
try {
176-
await fs.emptyDir(dest);
177-
await fs.copy(info.location, dest);
187+
await FsUtils.emptyDir(dest);
188+
await FsUtils.copy(info.location, dest);
178189
await this._updateThemeVersion(info);
179190
} catch (cause) {
180191
logger.error(`Unable to copy default theme from ${info.location} to ${dest}`);
@@ -188,7 +199,7 @@ export class ThemeDefaults {
188199
*/
189200
async _setThemeInfo() {
190201
const file = path.join(process.env.ARC_THEMES, 'themes-info.json');
191-
const exists = await fs.pathExists(file);
202+
const exists = await FsUtils.canRead(file);
192203
if (exists) {
193204
logger.debug(`${file} exists. Ensuring info scheme.`);
194205
await this._ensureThemesInfoVersion(file);
@@ -203,7 +214,7 @@ export class ThemeDefaults {
203214
* @returns {Promise<void>}
204215
*/
205216
async _ensureThemesInfoVersion(file) {
206-
const data = await fs.readJson(file, { throws: false });
217+
const data = /** @type any */ (await FsUtils.readJson(file, { throws: false }));
207218
if (!data) {
208219
return this._copyInfoFile();
209220
}
@@ -229,7 +240,7 @@ export class ThemeDefaults {
229240
* @return {string} Location of theme info file in local resources.
230241
*/
231242
get localThemeInfoFile() {
232-
return path.join(__dirname, '..', '..', '..', 'appresources', 'themes', 'themes-info.json');
243+
return path.join(this.appPath, 'appresources', 'themes', 'themes-info.json');
233244
}
234245

235246
/**
@@ -238,10 +249,10 @@ export class ThemeDefaults {
238249
*/
239250
async _copyInfoFile() {
240251
const dest = process.env.ARC_THEMES_SETTINGS;
241-
await fs.ensureDir(process.env.ARC_THEMES);
242-
let info = await fs.readJson(this.localThemeInfoFile, { throws: false })
252+
await FsUtils.ensureDir(process.env.ARC_THEMES);
253+
let info = await FsUtils.readJson(this.localThemeInfoFile, { throws: false })
243254
info = info || {};
244-
await fs.writeJson(dest, info);
255+
await FsUtils.writeJson(dest, info);
245256
}
246257

247258
/**
@@ -255,7 +266,7 @@ export class ThemeDefaults {
255266
* @returns {Promise<void>}
256267
*/
257268
async _upgradeInfoFile(file, installed) {
258-
let info = await fs.readJson(this.localThemeInfoFile, { throws: false });
269+
let info = /** @type any */ (await FsUtils.readJson(this.localThemeInfoFile, { throws: false }));
259270
if (!info || !info.themes) {
260271
info = { themes: [] };
261272
}
@@ -266,7 +277,7 @@ export class ThemeDefaults {
266277
info.themes.push(item);
267278
});
268279
info.systemPreferred = false;
269-
await fs.writeJson(file, info);
280+
await FsUtils.writeJson(file, info);
270281
}
271282

272283
/**
@@ -275,16 +286,16 @@ export class ThemeDefaults {
275286
*/
276287
async _updateThemeVersion(info) {
277288
const dbFile = process.env.ARC_THEMES_SETTINGS;
278-
const db = /** @type ArcThemeStore */ (await fs.readJson(dbFile));
289+
const db = /** @type ArcThemeStore */ (await FsUtils.readJson(dbFile));
279290
// name contains path separator that is different on different platforms.
280291
const normalizedName = info.name.replace(/[\\/]/g, '');
281292
const theme = db.themes.find((i) => i.name.replace(/[\\/]/g, '') === normalizedName);
282293
if (!theme) {
283294
return;
284295
}
285296
const localPkgFile = path.join(info.location, 'package.json');
286-
const localPkg = await fs.readJson(localPkgFile);
297+
const localPkg = /** @type any */ (await FsUtils.readJson(localPkgFile));
287298
theme.version = localPkg.version;
288-
await fs.writeJson(dbFile, db);
299+
await FsUtils.writeJson(dbFile, db);
289300
}
290301
}

0 commit comments

Comments
 (0)