Skip to content

Commit

Permalink
Make it possible to inject the user settings path
Browse files Browse the repository at this point in the history
  • Loading branch information
c3er committed Nov 24, 2021
1 parent 76f2880 commit 4135298
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 37 deletions.
2 changes: 1 addition & 1 deletion app/lib/encoding/encodingMain.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
58 changes: 58 additions & 0 deletions app/lib/main/cli.js
Original file line number Diff line number Diff line change
@@ -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
}
16 changes: 7 additions & 9 deletions app/lib/main/storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}

Expand All @@ -28,7 +30,7 @@ class StorageBase {
}

static _initStorageDir(storageDir) {
fs.mkdirSync(storageDir ?? getDefaultDir(), { recursive: true })
fs.mkdirSync(storageDir ?? _dataDir, { recursive: true })
}

static _initData(storagePath) {
Expand Down Expand Up @@ -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 ??
Expand All @@ -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
40 changes: 14 additions & 26 deletions app/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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({
Expand All @@ -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()
})
Expand Down Expand Up @@ -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
)

Expand Down Expand Up @@ -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)
}
})
Expand Down
2 changes: 1 addition & 1 deletion test/integrationSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 4135298

Please sign in to comment.