diff --git a/app/lib/encoding/encodingMain.js b/app/lib/encoding/encodingMain.js index 394c5c8..5512139 100644 --- a/app/lib/encoding/encodingMain.js +++ b/app/lib/encoding/encodingMain.js @@ -79,7 +79,7 @@ exports.ENCODINGS = [ exports.init = (mainMenu, storageDir, electronMock) => { electron = electronMock ?? require("electron") _mainMenu = mainMenu - _storageDir = storageDir ?? storage.getDefaultDir() + _storageDir = storageDir ?? storage.dataDir electron.ipcMain.on(ipc.messages.changeEncoding, (_, filePath, encoding) => changeEncoding(filePath, encoding) diff --git a/app/lib/main/cli.js b/app/lib/main/cli.js new file mode 100644 index 0000000..f307acc --- /dev/null +++ b/app/lib/main/cli.js @@ -0,0 +1,58 @@ +const path = require("path") + +let electron + +const DEFAULT_FILE = path.join(__dirname, "..", "..", "..", "README.md") + +function extractInternalTarget(args) { + return args.find(arg => arg.startsWith("#")) +} + +function extractFilePath(args, storageDirArgIndex) { + return ( + args.find( + arg => + arg !== process.execPath && + arg !== "." && + arg !== electron.app.getAppPath() && + arg !== "data:," && + !arg.startsWith("-") && + !arg.startsWith("#") && + args.indexOf(arg) !== storageDirArgIndex + ) ?? DEFAULT_FILE + ) +} + +function parseTestArgs(args) { + const testArgIndex = args.indexOf("--test") + const isTest = testArgIndex >= 0 + + let storageDirArgIndex = -1 + if (isTest && testArgIndex < args.length - 1) { + storageDirArgIndex = testArgIndex + 1 + } + + return { + isTest: isTest, + storageDir: + storageDirArgIndex >= 0 + ? args[storageDirArgIndex] + : path.join(electron.app.getPath("userData"), "storage"), + storageDirArgIndex: storageDirArgIndex, + } +} + +exports.init = electronMock => (electron = electronMock ?? require("electron")) + +exports.parse = args => { + console.debug(args) + const { isTest, storageDir, storageDirArgIndex } = parseTestArgs(args) + const parsedArgs = { + filePath: extractFilePath(args, storageDirArgIndex), + internalTarget: extractInternalTarget(args), + isTest: isTest, + storageDir: storageDir, + } + console.debug(parsedArgs) + return parsedArgs +} diff --git a/app/lib/main/storage.js b/app/lib/main/storage.js index c138ea4..10534f7 100644 --- a/app/lib/main/storage.js +++ b/app/lib/main/storage.js @@ -6,6 +6,8 @@ let electron const APPLICATION_SETTINGS_FILE = "app-settings.json" const DOCUMENT_SETTINGS_FILE = "doc-settings.json" +let _dataDir + let _applicationSettings let _documentSettings = {} @@ -28,7 +30,7 @@ class StorageBase { } static _initStorageDir(storageDir) { - fs.mkdirSync(storageDir ?? getDefaultDir(), { recursive: true }) + fs.mkdirSync(storageDir ?? _dataDir, { recursive: true }) } static _initData(storagePath) { @@ -127,16 +129,14 @@ class DocumentSettings extends StorageBase { } } -function getDefaultDir() { - // electron.app.getPath does not work in tests - return path.join(electron.app.getPath("userData"), "storage") -} - exports.APPLICATION_SETTINGS_FILE = APPLICATION_SETTINGS_FILE exports.DOCUMENT_SETTINGS_FILE = DOCUMENT_SETTINGS_FILE -exports.init = electronMock => (electron = electronMock ?? require("electron")) +exports.init = (dataDir, electronMock) => { + electron = electronMock ?? require("electron") + exports.dataDir = _dataDir = dataDir +} exports.loadApplicationSettings = (storageDir, storageFile) => _applicationSettings ?? @@ -145,5 +145,3 @@ exports.loadApplicationSettings = (storageDir, storageFile) => exports.loadDocumentSettings = (storageDir, storageFile, documentPath) => _documentSettings[documentPath] ?? (_documentSettings[documentPath] = new DocumentSettings(storageDir, storageFile, documentPath)) - -exports.getDefaultDir = getDefaultDir diff --git a/app/main.js b/app/main.js index 5a7ec01..005daec 100644 --- a/app/main.js +++ b/app/main.js @@ -8,6 +8,7 @@ const childProcess = require("child_process") const electron = require("electron") const remote = require("@electron/remote/main") +const cli = require("./lib/main/cli") const common = require("./lib/common") const contentBlocking = require("./lib/contentBlocking/contentBlockingMain") const encodingLib = require("./lib/encoding/encodingMain") @@ -21,6 +22,7 @@ const UPDATE_INTERVAL = 1000 // ms const UPDATE_FILE_TIME_NAV_ID = "update-file-time" const ZOOM_STEP = 0.1 +let _cliArgs let _isTest = false let _mainWindow @@ -82,22 +84,10 @@ function openFile(filePath, internalTarget, encoding) { } } -function extractInternalTarget(args) { - return args.find(arg => arg.startsWith("#")) -} - -function determineCurrentFilePath(args) { +function determineCurrentFilePath() { return navigation.hasCurrentLocation() ? navigation.getCurrentLocation().filePath - : args.find( - arg => - arg !== process.execPath && - arg !== "." && - arg !== electron.app.getAppPath() && - arg !== "data:," && - !arg.startsWith("-") && - !arg.includes("spectron-menu-addon-v2") - ) ?? DEFAULT_FILE + : _cliArgs.filePath } function createChildWindow(filePath, internalTarget) { @@ -145,9 +135,9 @@ function resetZoom() { function createWindow() { const windowPosition = storage.loadDocumentSettings( - storage.getDefaultDir(), + storage.dataDir, storage.DOCUMENT_SETTINGS_FILE, - determineCurrentFilePath(process.argv) + determineCurrentFilePath() ).windowPosition const mainWindow = new electron.BrowserWindow({ @@ -164,9 +154,9 @@ function createWindow() { }) mainWindow.on("close", () => { const documentSettings = storage.loadDocumentSettings( - storage.getDefaultDir(), + storage.dataDir, storage.DOCUMENT_SETTINGS_FILE, - determineCurrentFilePath(process.argv) + determineCurrentFilePath() ) documentSettings.windowPosition = mainWindow.getBounds() }) @@ -369,11 +359,12 @@ function createWindow() { } electron.app.on("ready", () => { - _isTest = process.argv.includes("--test") + cli.init() + _cliArgs = cli.parse(process.argv) - storage.init() + storage.init(_cliArgs.storageDir) _applicationSettings = storage.loadApplicationSettings( - storage.getDefaultDir(), + storage.dataDir, storage.APPLICATION_SETTINGS_FILE ) @@ -403,11 +394,8 @@ electron.app.on("activate", () => { }) electron.ipcMain.on(ipc.messages.finishLoad, () => { - const args = process.argv - console.debug(args) - - const filePath = determineCurrentFilePath(args) - if (openFile(filePath, extractInternalTarget(args), encodingLib.load(filePath))) { + const filePath = _cliArgs.filePath + if (openFile(filePath, _cliArgs.internalTarget, encodingLib.load(filePath))) { setZoom(_applicationSettings.zoom) } }) diff --git a/test/integrationSpec.js b/test/integrationSpec.js index 4897532..f9d1932 100644 --- a/test/integrationSpec.js +++ b/test/integrationSpec.js @@ -98,7 +98,7 @@ describe("Integration tests with single app instance", () => { beforeEach(() => { mocking.resetElectron() - storage.init(mocking.electron) + storage.init(mocking.dataDir, mocking.electron) applicationSettings = storage.loadApplicationSettings( mocking.dataDir, storage.APPLICATION_SETTINGS_FILE