diff --git a/apps/server/src/main.ts b/apps/server/src/main.ts index 7aa22316..7186b92a 100644 --- a/apps/server/src/main.ts +++ b/apps/server/src/main.ts @@ -75,32 +75,31 @@ bootstrap(); function generateResourceFolders() { dirs.forEach((dir) => { - access(dir, (error) => { - if (error) mkdirSync(path.join(getSimbridgeDir(), dir), { recursive: true }); + const actualDir = path.join(getSimbridgeDir(), dir); + access(actualDir, (error) => { + if (error) mkdirSync(actualDir, { recursive: true }); }); }); } function generateDefaultProperties() { - const propertiesFilePath = path.join(getSimbridgeDir(), '/resources', '/properties.json'); const defaultProperties = { server: { port: 8380, hidden: true, - closeWithMSFS: false + closeWithMSFS: false, }, printer: { enabled: false, printerName: null, fontSize: 19, - paperSize: "A4", - margin: 30 - } + paperSize: 'A4', + margin: 30, + }, }; - if (!existsSync(propertiesFilePath)) { writeFileSync(propertiesFilePath, JSON.stringify(defaultProperties)); } diff --git a/apps/server/src/terrain.ts b/apps/server/src/terrain.ts index eb2120b1..071bfd29 100644 --- a/apps/server/src/terrain.ts +++ b/apps/server/src/terrain.ts @@ -1,15 +1,12 @@ import * as fs from 'fs'; - -import axios from 'axios'; - import * as path from 'path'; -import * as os from 'os'; - +import axios from 'axios'; -const SIMBRIDGE_FOLDER = path.join(os.homedir() + '/flybywire-externaltools-simbridge'); -const TERRAIN_MAP_FOLDER = path.join(SIMBRIDGE_FOLDER, '/terrain'); -const TERRAIN_MAP_PATH = path.join(TERRAIN_MAP_FOLDER, '/terrain.map'); +import { getSimbridgeDir } from 'apps/server/src/utilities/pathUtil'; +const SIMBRIDGE_FOLDER = getSimbridgeDir(); +const TERRAIN_MAP_FOLDER = path.join(SIMBRIDGE_FOLDER, 'terrain'); +const TERRAIN_MAP_PATH = path.join(TERRAIN_MAP_FOLDER, 'terrain.map'); const TERRAIN_MAP_CDN = 'https://cdn.flybywiresim.com/addons/simbridge/terrain-db-binaries/terrain.map'; @@ -18,28 +15,26 @@ const execute = async () => { // Create the folders if they don't exist if (!fs.existsSync(TERRAIN_MAP_FOLDER)) fs.mkdirSync(TERRAIN_MAP_FOLDER); - // Make sure to unlink the old terrain map so we can update it if needed - // if (fs.existsSync(TERRAIN_MAP_PATH)) fs.unlinkSync(TERRAIN_MAP_PATH); - if (!fs.existsSync(TERRAIN_MAP_PATH)) { - // Terrain map is not cached, download it - console.log('Downloading and caching terrain map'); + console.log('Downloading terrain map'); const terrainResponse = await axios.get(TERRAIN_MAP_CDN, { responseType: 'stream' }); return new Promise((resolve, reject) => { const writer = fs.createWriteStream(TERRAIN_MAP_PATH); terrainResponse.data.pipe(writer); + let error: Error = null; - /* writer.on('error', err => { + writer.on('error', (err) => { + error = err; writer.close(); reject(err); - }); */ + }); - writer.on('close', resolve); + writer.on('close', () => { + if (!error) resolve(0); + }); }); - - } } catch (error) { console.error(error); diff --git a/apps/server/src/utilities/file.service.ts b/apps/server/src/utilities/file.service.ts index f4a79e79..200b245f 100644 --- a/apps/server/src/utilities/file.service.ts +++ b/apps/server/src/utilities/file.service.ts @@ -134,11 +134,11 @@ export class FileService { scale: number = 4, ): Promise { // Some PDFs need external cmaps. - const CMAP_URL = `${join(getSimbridgeDir(), 'node_modules', 'pdfjs-dist', 'cmaps')}/`; + const CMAP_URL = `${join(getExecutablePath(), 'node_modules', 'pdfjs-dist', 'cmaps')}/`; const CMAP_PACKED = true; // Where the standard fonts are located. - const STANDARD_FONT_DATA_URL = `${join(getSimbridgeDir(), 'node_modules', 'pdfjs-dist', 'standard_fonts')}/`; + const STANDARD_FONT_DATA_URL = `${join(getExecutablePath(), 'node_modules', 'pdfjs-dist', 'standard_fonts')}/`; try { const conversionFilePath = join(getSimbridgeDir(), directory, fileName); diff --git a/apps/server/src/utilities/pathUtil.ts b/apps/server/src/utilities/pathUtil.ts index b07d7b3a..1b7cf5dd 100644 --- a/apps/server/src/utilities/pathUtil.ts +++ b/apps/server/src/utilities/pathUtil.ts @@ -1,7 +1,7 @@ -import { homedir } from 'os' +import getPath from 'platform-folders'; import * as path from 'path'; -export const getSimbridgeDir = () => (path.join(homedir() + '/flybywire-externaltools-simbridge')); +export const getSimbridgeDir = () => path.join(getPath('documents'), 'FlyByWireSim', 'Simbridge'); //@ts-expect-error pkg only defined when running as exe export const getExecutablePath = () => (process.pkg ? path.dirname(process.argv[0]) : process.cwd()); diff --git a/apps/server/src/utilities/systray.service.ts b/apps/server/src/utilities/systray.service.ts index 2066fcd9..2fd17026 100644 --- a/apps/server/src/utilities/systray.service.ts +++ b/apps/server/src/utilities/systray.service.ts @@ -4,7 +4,7 @@ import { hideConsole, showConsole } from 'node-hide-console-window'; import open = require('open'); import SysTray, { MenuItem } from 'systray2'; import { join } from 'path'; -import { getSimbridgeDir } from 'apps/server/src/utilities/pathUtil'; +import { getExecutablePath, getSimbridgeDir } from 'apps/server/src/utilities/pathUtil'; import { NetworkService } from './network.service'; import serverConfig from '../config/server.config'; import { ShutDownService } from './shutdown.service'; @@ -29,7 +29,7 @@ export class SysTrayService implements OnApplicationShutdown { tooltip: 'Flybywire SimBridge', items: [this.remoteDisplayItem, this.resourcesFolderItem, this.consoleVisibleItem, this.exitItem], }, - copyDir: getSimbridgeDir(), + copyDir: getExecutablePath(), }); this.sysTray.onClick((action) => { diff --git a/package-lock.json b/package-lock.json index e4b42e62..8ade5feb 100644 --- a/package-lock.json +++ b/package-lock.json @@ -38,6 +38,7 @@ "pdf-to-printer": "5.3.0", "pdfjs-dist": "^2.13.216", "pdfkit": "^0.13.0", + "platform-folders": "^0.6.0", "react": "^17.0.0", "react-dom": "^17.0.0", "react-use-websocket": "^2.9.1", @@ -15132,6 +15133,18 @@ "node": ">=8" } }, + "node_modules/platform-folders": { + "version": "0.6.0", + "resolved": "https://registry.npmjs.org/platform-folders/-/platform-folders-0.6.0.tgz", + "integrity": "sha512-CzaJGN1cbL6kwkge7zM7VbXr/L0Qjkg2Z4IzcprziHHSw8y73oORfB9pMLwjAIhb8JcWUemmWvTXAXvc5hnVlg==", + "hasInstallScript": true, + "dependencies": { + "bindings": "^1.5.0" + }, + "engines": { + "node": "^8.16.0 || >=10" + } + }, "node_modules/pluralize": { "version": "8.0.0", "resolved": "https://registry.npmjs.org/pluralize/-/pluralize-8.0.0.tgz", @@ -31031,6 +31044,14 @@ } } }, + "platform-folders": { + "version": "0.6.0", + "resolved": "https://registry.npmjs.org/platform-folders/-/platform-folders-0.6.0.tgz", + "integrity": "sha512-CzaJGN1cbL6kwkge7zM7VbXr/L0Qjkg2Z4IzcprziHHSw8y73oORfB9pMLwjAIhb8JcWUemmWvTXAXvc5hnVlg==", + "requires": { + "bindings": "^1.5.0" + } + }, "pluralize": { "version": "8.0.0", "resolved": "https://registry.npmjs.org/pluralize/-/pluralize-8.0.0.tgz", diff --git a/package.json b/package.json index ffca673d..83743483 100644 --- a/package.json +++ b/package.json @@ -75,6 +75,7 @@ "pdf-to-printer": "5.3.0", "pdfjs-dist": "^2.13.216", "pdfkit": "^0.13.0", + "platform-folders": "^0.6.0", "react": "^17.0.0", "react-dom": "^17.0.0", "react-use-websocket": "^2.9.1",