From c1be741df2eb44ff74943c20856f885b61f74519 Mon Sep 17 00:00:00 2001 From: Elijah Mooring <45398751+Vehmloewff@users.noreply.github.com> Date: Fri, 7 Feb 2020 02:46:55 -0600 Subject: [PATCH] Fix the case where the entry file is in a subdirectory (#10) --- index.js | 10 ++++++---- package.json | 5 +++-- test/{ => main}/index.js | 4 ++-- 3 files changed, 11 insertions(+), 8 deletions(-) rename test/{ => main}/index.js (69%) diff --git a/index.js b/index.js index a8cd6cc..dcf21c1 100644 --- a/index.js +++ b/index.js @@ -6,9 +6,9 @@ const chokidar = require('chokidar'); const isDev = require('electron-is-dev'); const dateTime = require('date-time'); const chalk = require('chalk'); +const findUp = require('find-up'); -function getMainProcessPaths(topModuleObject) { - const cwd = path.dirname(topModuleObject.filename); +function getMainProcessPaths(topModuleObject, cwd) { const paths = new Set([topModuleObject.filename]); const getPaths = moduleObject => { @@ -43,8 +43,10 @@ module.exports = (moduleObj, options) => { ...options }; - const cwd = path.dirname(moduleObj.filename); - const mainProcessPaths = getMainProcessPaths(moduleObj); + const mainProcessDir = path.dirname(moduleObj.filename); + const packageDir = findUp.sync('package.json', {cwd: mainProcessDir}); + const cwd = packageDir ? path.dirname(packageDir) : mainProcessDir; + const mainProcessPaths = getMainProcessPaths(moduleObj, cwd); const watchPaths = options.watchRenderer ? cwd : [...mainProcessPaths]; const watcher = chokidar.watch(watchPaths, { diff --git a/package.json b/package.json index 3951b70..1b6b4a6 100644 --- a/package.json +++ b/package.json @@ -11,7 +11,7 @@ }, "scripts": { "test": "xo", - "start": "electron test" + "start": "electron test/main" }, "files": [ "index.js" @@ -39,7 +39,8 @@ "chalk": "^2.4.2", "chokidar": "^3.0.0", "date-time": "^3.1.0", - "electron-is-dev": "^1.1.0" + "electron-is-dev": "^1.1.0", + "find-up": "^4.1.0" }, "devDependencies": { "electron": "^5.0.1", diff --git a/test/index.js b/test/main/index.js similarity index 69% rename from test/index.js rename to test/main/index.js index bc8af9e..a23b563 100644 --- a/test/index.js +++ b/test/main/index.js @@ -2,7 +2,7 @@ const path = require('path'); const {app, BrowserWindow} = require('electron'); -require('..')(module, { +require('../..')(module, { debug: true }); @@ -12,5 +12,5 @@ let mainWindow; await app.whenReady(); mainWindow = new BrowserWindow(); - await mainWindow.loadFile(path.join(__dirname, 'index.html')); + await mainWindow.loadFile(path.join(__dirname, '../index.html')); })();