Skip to content

Commit

Permalink
Merge pull request #199 from muffinista/fix-built-paths
Browse files Browse the repository at this point in the history
Remove calls to __dirname #194
  • Loading branch information
muffinista authored Apr 3, 2024
2 parents cc91f96 + 550c8a8 commit c5fb52c
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 92 deletions.
12 changes: 1 addition & 11 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,14 @@
"devport": 9081,
"scripts": {
"dev": "node bin/dev-runner.js",
"dev-no-reload": "cross-env DISABLE_RELOAD=1 node bin/dev-runner.js",
"compile": "cross-env NODE_ENV=production webpack --mode production --config webpack.config.js",
"eslint-all": "eslint -c .eslintrc.json src/**/*.js src/**/*.svelte test/**/*.js webpack*.js",
"eslint": "eslint -c .eslintrc.json",
"postinstall": "electron-builder install-app-deps",
"pack": "npm run compile && electron-builder --dir",
"dist": "npm run compile && electron-builder --x64",
"test": "npm run compile && mocha -b test/**/*.js",
"test-ui": "npm run compile && xvfb-maybe mocha test/ui/**/*.js",
"test-ui": "cross-env DISABLE_SENTRY=true npm run compile && xvfb-maybe mocha test/ui/**/*.js",
"test-lib": "mocha test test/lib/**/*.js test/main/**/*.js",
"run-local": "node bin/build-icon.js && npm run compile && cross-env ELECTRON_IS_DEV=0 electron output/main.js",
"grab-screens": "bin/capture-screens.js",
Expand Down Expand Up @@ -115,15 +114,6 @@
"node_modules/**/*",
"package.json"
],
"extraResources": [
{
"from": "data/savers",
"to": "savers",
"filter": [
"**/*"
]
}
],
"appId": "Before Dawn",
"mac": {
"category": "public.app-category.entertainment",
Expand Down
15 changes: 0 additions & 15 deletions src/main/bootstrap.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { readFile } from 'fs/promises';
import { init } from '@sentry/electron';

export default async function bootstrapApp() {
const packageJSON = JSON.parse(
Expand Down Expand Up @@ -38,18 +37,4 @@ export default async function bootstrapApp() {
global.RELEASE_CHECK_URL = `${global.RELEASE_SERVER}/update/${process.platform}/${global.APP_VERSION_BASE}`;
global.PACKAGE_DOWNLOAD_URL = `https://github.com/${global.APP_REPO}/releases/latest`;
}

if ( process.env.TEST_MODE === undefined && process.env.SENTRY_DSN !== undefined ) {
console.log(`setting up sentry with ${process.env.SENTRY_DSN}`);
try {
init({
dsn: process.env.SENTRY_DSN,
// eslint-disable-next-line no-console
onFatalError: console.log
});
}
catch(e) {
console.log(e);
}
}
}
118 changes: 71 additions & 47 deletions src/main/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,21 @@
*/


import { init } from '@sentry/electron';
if ( process.env.TEST_MODE === undefined && process.env.SENTRY_DSN !== undefined ) {
console.log(`setting up sentry with ${process.env.SENTRY_DSN}`);
try {
init({
dsn: process.env.SENTRY_DSN,
// eslint-disable-next-line no-console
onFatalError: console.log
});
}
catch(e) {
console.log(e);
}
}

import {app,
BrowserWindow,
desktopCapturer,
Expand Down Expand Up @@ -70,10 +85,6 @@ import * as autostarter from "./autostarter.js";
import FullScreen from "detect-fullscreen";
const { isFullscreen } = FullScreen;

import { fileURLToPath } from 'url';

const __dirname = path.dirname(fileURLToPath(import.meta.url));

const packageJSON = JSON.parse(
await readFile(
new URL('../../package.json', import.meta.url)
Expand All @@ -82,7 +93,6 @@ const packageJSON = JSON.parse(

var releaseChecker;


// NOTE -- this needs to be global, otherwise the app icon gets
// garbage collected and won't show up in the system tray
let appIcon = null;
Expand Down Expand Up @@ -114,9 +124,6 @@ else {

let exitOnQuit = false;

// load some global CSS we'll inject into running screensavers
const globalCSSCode = fs.readFileSync( path.join(__dirname, "assets", "global.css"), "ascii");

/**
* track some information about windows and preview bounds for the prefs window
* and editor window
Expand Down Expand Up @@ -219,7 +226,7 @@ var listScreens = async function() {
var openGrabberWindow = function() {
return new Promise((resolve) => {
log.info("openGrabberWindow");
const grabberUrl = `file://${__dirname}/assets/grabber.html`;
const grabberUrl = `file://${getAssetsDir()}/grabber.html`;

var grabberWindow = new BrowserWindow({
show: false,
Expand All @@ -230,7 +237,7 @@ var openGrabberWindow = function() {
y: 2000,
webPreferences: {
...defaultWebPreferences,
preload: path.join(__dirname, "assets", "grabber.mjs")
preload: path.join(getAssetsDir(), "grabber.mjs")
}
});
// grabberWindow.noTray = true;
Expand Down Expand Up @@ -266,7 +273,7 @@ var grabScreen = function(s) {
(process.platform === "darwin" && systemPreferences.getMediaAccessStatus("screen") !== "granted" ) ||
testMode === true ) {
resolve({
url: path.join(__dirname, "assets", "color-bars.png")
url: path.join(getAssetsDir(), "color-bars.png")
});
}
else {
Expand Down Expand Up @@ -322,11 +329,11 @@ var openTestShim = function() {
height: 600,
webPreferences: {
...defaultWebPreferences,
preload: path.join(__dirname, "assets", "shim.js")
preload: path.join(getAssetsDir(), "shim.js")
}
});

const shimUrl = `file://${__dirname}/assets/shim.html`;
const shimUrl = `file://${getAssetsDir()}/shim.html`;
testWindow.loadURL(shimUrl);

testWindow.webContents.openDevTools();
Expand Down Expand Up @@ -356,7 +363,6 @@ var openPrefsWindow = function() {
screenshots[primary.id] = grab.url;

const prefsUrl = getUrl("prefs.html");

handles.prefs.window = new BrowserWindow({
show: false,
width: 910,
Expand All @@ -367,9 +373,9 @@ var openPrefsWindow = function() {
resizable: true,
webPreferences: {
...defaultWebPreferences,
preload: path.join(__dirname, "assets", "preload.mjs")
preload: path.join(getAssetsDir(), "preload.mjs")
},
icon: path.join(__dirname, "assets", "iconTemplate.png")
icon: path.join(getAssetsDir(), "iconTemplate.png")
});

if ( !isDev && handles.prefs.window.removeMenu !== undefined ) {
Expand Down Expand Up @@ -412,10 +418,10 @@ var openSettingsWindow = function() {
resizable: true,
parent: handles.prefs.window,
modal: true,
icon: path.join(__dirname, "assets", "iconTemplate.png"),
icon: path.join(getAssetsDir(), "iconTemplate.png"),
webPreferences: {
...defaultWebPreferences,
preload: path.join(__dirname, "assets", "preload.mjs"),
preload: path.join(getAssetsDir(), "preload.mjs"),
}
});

Expand Down Expand Up @@ -458,9 +464,9 @@ var addNewSaver = async function(opts) {
resizable:true,
webPreferences: {
...defaultWebPreferences,
preload: path.join(__dirname, "assets", "preload.mjs"),
preload: path.join(getAssetsDir(), "preload.mjs"),
},
icon: path.join(__dirname, "assets", "iconTemplate.png")
icon: path.join(getAssetsDir(), "iconTemplate.png")
});

handles.addNew.window.on("closed", () => {
Expand All @@ -486,10 +492,10 @@ var openAboutWindow = function() {
width:500,
height:600,
resizable:false,
icon: path.join(__dirname, "assets", "iconTemplate.png"),
icon: path.join(getAssetsDir(), "iconTemplate.png"),
webPreferences: {
...defaultWebPreferences,
preload: path.join(__dirname, "assets", "preload.mjs"),
preload: path.join(getAssetsDir(), "preload.mjs"),
}
});

Expand Down Expand Up @@ -533,7 +539,7 @@ var openEditor = (args) => {
show: false,
webPreferences: {
...defaultWebPreferences,
preload: path.join(__dirname, "assets", "preload.mjs"),
preload: path.join(getAssetsDir(), "preload.mjs"),
},
});
}
Expand Down Expand Up @@ -615,6 +621,9 @@ var applyScreensaverWindowEvents = function(w) {
w.webContents.on("did-finish-load", function() {
log.info("did-finish-load");
if (!w.isDestroyed()) {
// load some global CSS we'll inject into running screensavers
const globalCSSCode = fs.readFileSync( path.join(getAssetsDir(), "global.css"), "ascii");

w.webContents.insertCSS(globalCSSCode);
}
});
Expand Down Expand Up @@ -931,14 +940,33 @@ var getSystemDir = function() {
}

if ( process.env.TEST_MODE ) {
return path.join(__dirname, "..", "..", "output");
return app.getAppPath();
}
if ( app.isPackaged ) {
return path.join(app.getAppPath(), "output");
}

log.info(`dev systemDir: ${__dirname} --> ../../output/${__dirname}`);
return path.join(__dirname, "..", "..", "output");
return path.join(app.getAppPath(), "..", "..", "output");
};


/**
* determine what our assets directory is. This is where global CSS,
* icons, etc, can be found.
*/
let getAssetsDir = function() {
if ( process.env.BEFORE_DAWN_ASSETS_DIR !== undefined ) {
return process.env.BEFORE_DAWN_ASSETS_DIR;
}

if ( app.isPackaged ) {
return path.join(app.getAppPath(), "output", "assets");
}
if ( process.env.TEST_MODE ) {
return path.join(app.getAppPath(), "assets");
}

return path.join(app.getAppPath(), "assets");
};


Expand All @@ -950,30 +978,26 @@ var getSystemDir = function() {
var getUrl = function(dest) {
let baseUrl;
if ( !testMode && isDev ) {
if ( ! process.env.DISABLE_RELOAD ) {
let devPort;
let devPort;

try {
devPort = packageJSON.devport;
}
catch(e) {
devPort = 9080;
}

baseUrl = `http://localhost:${devPort}`;
return new URL(dest, new URL(baseUrl)).toString(); // url.resolve(baseUrl, dest);
try {
devPort = packageJSON.devport;
}
catch(e) {
devPort = 9080;
}

return `${__dirname}/../../output/${dest}`;
baseUrl = `http://localhost:${devPort}`;

return new URL(dest, new URL(baseUrl)).toString();
}
else {
if ( app.isPackaged ) {
return `file://./${dest}`;
}

return `file://${__dirname}/../../output/${dest}`;
log.info(`hey!!! ${app.getAppPath()}`);
if ( testMode ) {
return `file://${app.getAppPath()}/${dest}`;
}

return `file://${app.getAppPath()}/output/${dest}`;
};

var setupForTesting = function() {
Expand Down Expand Up @@ -1311,8 +1335,7 @@ let setupIPC = function() {
ipcMain.handle("create-screensaver", async(_event, attrs) => {
const factory = new SaverFactory();

let systemPath = getSystemDir();
const src = path.join(systemPath, "system-savers", "__template");
const src = path.join(getSystemDir(), "system-savers", "__template");
log.info(`create-screensaver from ${src}`);
const dest = prefs.localSource;
const data = factory.create(src, dest, attrs);
Expand Down Expand Up @@ -1591,7 +1614,7 @@ var bootApp = async function() {
saversDir = process.env.SAVERS_DIR;
}
else if ( isDev ) {
saversDir = path.join(__dirname, "..", "..", "data", "savers");
saversDir = path.join(app.getAppPath(), "..", "..", "data", "savers");
log.info("hello from dev mode, 'node bin/download-screensavers' to grab screensavers");
}
else {
Expand All @@ -1614,7 +1637,7 @@ var bootApp = async function() {
log.info("Loading prefs");
log.info(`baseDir: ${basePath}`);
log.info(`saversDir: ${saversDir}`);
log.info(`system savers: ${systemDir}`);
log.info(`system savers: ${systemDir}/system-savers`);

prefs = new SaverPrefs(basePath, systemDir, saversDir);
savers = new SaverListManager({
Expand Down Expand Up @@ -1967,6 +1990,7 @@ export {
setStateToRunning,
setStateToPaused,
resetState,
getAssetsDir,
getStateManager,
getAppIcon,
getTrayMenu,
Expand Down
26 changes: 12 additions & 14 deletions src/main/menus.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,6 @@ import * as main from "./index.js";
import * as path from "path";
import { nativeImage, shell } from "electron";

import { fileURLToPath } from 'url';

const __dirname = path.dirname(fileURLToPath(import.meta.url));

const icons = {
"win32" : {
active: path.join(__dirname , "assets", "icon.ico"),
paused: path.join(__dirname , "assets", "icon-paused.ico")
},
"default": {
active: path.join(__dirname , "assets", "iconTemplate.png"),
paused: path.join(__dirname , "assets", "icon-pausedTemplate.png")
}
};

var openUrl = (url) => {
try {
Expand Down Expand Up @@ -292,6 +278,17 @@ export const trayMenuTemplate = function() {
* get icons for the current platform
*/
export const getIcons = function() {
const icons = {
"win32" : {
active: path.join(main.getAssetsDir() , "icon.ico"),
paused: path.join(main.getAssetsDir(), "icon-paused.ico")
},
"default": {
active: path.join(main.getAssetsDir() , "iconTemplate.png"),
paused: path.join(main.getAssetsDir() , "icon-pausedTemplate.png")
}
};

if ( icons[process.platform] ) {
return icons[process.platform];
}
Expand All @@ -312,6 +309,7 @@ export const trayIconImage = function() {
iconPath = icons.active;
}

main.log.info(`use icon ${iconPath}`);
return nativeImage.createFromPath(iconPath);
};

Expand Down
Loading

0 comments on commit c5fb52c

Please sign in to comment.