Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/desktop/.eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
},
"extends": ["../../.eslintrc.json"],
"rules": {
"no-console": "warn",
"no-console": "off",
"@typescript-eslint/no-explicit-any": "warn",
"@typescript-eslint/no-var-requires": "off"
},
Expand Down
52 changes: 52 additions & 0 deletions packages/desktop/app/AppState.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { action, makeObservable, observable } from 'mobx'
import { MessageType } from '../test/TestIpcMessage'
import { Store } from './javascripts/Main/Store/Store'
import { StoreKeys } from './javascripts/Main/Store/StoreKeys'
import { Paths, Urls } from './javascripts/Main/Types/Paths'
import { UpdateState } from './javascripts/Main/UpdateManager'
import { handleTestMessage } from './javascripts/Main/Utils/Testing'
import { isTesting } from './javascripts/Main/Utils/Utils'
import { WindowState } from './javascripts/Main/Window'

export class AppState {
readonly version: string
readonly store: Store
readonly startUrl = Urls.indexHtml
readonly isPrimaryInstance: boolean
public willQuitApp = false
public lastBackupDate: number | null = null
public windowState?: WindowState
public deepLinkUrl?: string
public readonly updates: UpdateState
public lastRunVersion: string

constructor(app: Electron.App) {
this.version = app.getVersion()
this.store = new Store(Paths.userDataDir)
this.isPrimaryInstance = app.requestSingleInstanceLock()

this.lastRunVersion = this.store.get(StoreKeys.LastRunVersion) || 'unknown'
this.store.set(StoreKeys.LastRunVersion, this.version)

makeObservable(this, {
lastBackupDate: observable,
setBackupCreationDate: action,
})

this.updates = new UpdateState(this)

if (isTesting()) {
handleTestMessage(MessageType.AppStateCall, (method, ...args) => {
;(this as any)[method](...args)
})
}
}

public isRunningVersionForFirstTime(): boolean {
return this.lastRunVersion !== this.version
}

setBackupCreationDate(date: number | null): void {
this.lastBackupDate = date
}
}
60 changes: 12 additions & 48 deletions packages/desktop/app/application.ts
Original file line number Diff line number Diff line change
@@ -1,52 +1,15 @@
import { App, Shell } from 'electron'
import { action, makeObservable, observable } from 'mobx'
import { MessageType } from '../test/TestIpcMessage'
import { AppState } from './AppState'
import { createExtensionsServer } from './javascripts/Main/ExtensionsServer'
import { Keychain } from './javascripts/Main/Keychain/Keychain'
import { Store, StoreKeys } from './javascripts/Main/Store'
import { StoreKeys } from './javascripts/Main/Store/StoreKeys'
import { AppName, initializeStrings } from './javascripts/Main/Strings'
import { Paths, Urls } from './javascripts/Main/Types/Paths'
import { isLinux, isMac, isWindows } from './javascripts/Main/Types/Platforms'
import { UpdateState } from './javascripts/Main/UpdateManager'
import { handleTestMessage } from './javascripts/Main/Utils/Testing'
import { isDev, isTesting } from './javascripts/Main/Utils/Utils'
import { createWindowState, WindowState } from './javascripts/Main/Window'
import { isDev } from './javascripts/Main/Utils/Utils'
import { createWindowState } from './javascripts/Main/Window'

const deepLinkScheme = 'standardnotes'

export class AppState {
readonly version: string
readonly store: Store
readonly startUrl = Urls.indexHtml
readonly isPrimaryInstance: boolean
public willQuitApp = false
public lastBackupDate: number | null = null
public windowState?: WindowState
public deepLinkUrl?: string
public readonly updates: UpdateState

constructor(app: Electron.App) {
this.version = app.getVersion()
this.store = new Store(Paths.userDataDir)
this.isPrimaryInstance = app.requestSingleInstanceLock()
makeObservable(this, {
lastBackupDate: observable,
setBackupCreationDate: action,
})
this.updates = new UpdateState(this)

if (isTesting()) {
handleTestMessage(MessageType.AppStateCall, (method, ...args) => {
;(this as any)[method](...args)
})
}
}

setBackupCreationDate(date: number | null): void {
this.lastBackupDate = date
}
}

export function initializeApplication(args: { app: Electron.App; ipcMain: Electron.IpcMain; shell: Shell }): void {
const { app } = args

Expand Down Expand Up @@ -146,7 +109,6 @@ async function finishApplicationInitialization({ app, shell, state }: { app: App
const keychainWindow = await Keychain.ensureKeychainAccess(state.store)

initializeStrings(app.getLocale())
initializeExtensionsServer(state.store)

const windowState = await createWindowState({
shell,
Expand All @@ -157,6 +119,14 @@ async function finishApplicationInitialization({ app, shell, state }: { app: App
},
})

if (state.isRunningVersionForFirstTime()) {
console.log('Clearing window cache')
await windowState.window.webContents.session.clearCache()
}

const host = createExtensionsServer()
state.store.set(StoreKeys.ExtServerHost, host)

/**
* Close the keychain window after the main window is created, otherwise the
* app will quit automatically
Expand All @@ -171,9 +141,3 @@ async function finishApplicationInitialization({ app, shell, state }: { app: App

void windowState.window.loadURL(state.startUrl)
}

function initializeExtensionsServer(store: Store) {
const host = createExtensionsServer()

store.set(StoreKeys.ExtServerHost, host)
}
3 changes: 2 additions & 1 deletion packages/desktop/app/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import path from 'path'
import './@types/modules'
import { initializeApplication } from './application'
import { enableExperimentalFeaturesForFileAccessFix } from './enableExperimentalWebFeatures'
import { Store, StoreKeys } from './javascripts/Main/Store'
import { Store } from './javascripts/Main/Store/Store'
import { StoreKeys } from './javascripts/Main/Store/StoreKeys'
import { isSnap } from './javascripts/Main/Types/Constants'
import { Paths } from './javascripts/Main/Types/Paths'
import { setupTesting } from './javascripts/Main/Utils/Testing'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import { dialog, shell, WebContents } from 'electron'
import { promises as fs } from 'fs'
import path from 'path'
import { AppMessageType, MessageType } from '../../../../test/TestIpcMessage'
import { AppState } from '../../../application'
import { AppState } from '../../../AppState'
import { MessageToWebApp } from '../../Shared/IpcMessages'
import { StoreKeys } from '../Store'
import { StoreKeys } from '../Store/StoreKeys'
import { backups as str } from '../Strings'
import { Paths } from '../Types/Paths'
import {
Expand Down
4 changes: 3 additions & 1 deletion packages/desktop/app/javascripts/Main/ExtensionsServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { URL } from 'url'
import { extensions as str } from './Strings'
import { Paths } from './Types/Paths'
import { FileDoesNotExist } from './Utils/FileUtils'
import { app } from 'electron'

const Protocol = 'http'

Expand Down Expand Up @@ -61,7 +62,8 @@ async function handleRequest(request: IncomingMessage, response: ServerResponse)
const mimeType = mime.lookup(path.parse(filePath).ext)

response.setHeader('Access-Control-Allow-Origin', '*')
response.setHeader('Cache-Control', 'max-age=604800')
response.setHeader('Cache-Control', 'no-cache')
response.setHeader('ETag', app.getVersion())
response.setHeader('Content-Type', `${mimeType}; charset=utf-8`)

const data = fs.readFileSync(filePath)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { FileBackupsDevice, FileBackupsMapping } from '@web/Application/Device/DesktopSnjsExports'
import { AppState } from 'app/application'
import { AppState } from 'app/AppState'
import { shell } from 'electron'
import { StoreKeys } from '../Store'
import { StoreKeys } from '../Store/StoreKeys'
import path from 'path'
import {
deleteFile,
Expand Down
3 changes: 2 additions & 1 deletion packages/desktop/app/javascripts/Main/Keychain/Keychain.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { app, BrowserWindow, ipcMain } from 'electron'
import keytar from 'keytar'
import { MessageToMainProcess } from '../../Shared/IpcMessages'
import { Store, StoreKeys } from '../Store'
import { Store } from '../Store/Store'
import { StoreKeys } from '../Store/StoreKeys'
import { AppName } from '../Strings'
import { keychainAccessIsUserConfigurable } from '../Types/Constants'
import { Paths, Urls } from '../Types/Paths'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { BrowserWindow } from 'electron'
import { Store } from '../Store'
import { Store } from '../Store/Store'

export interface KeychainInterface {
ensureKeychainAccess(store: Store): Promise<BrowserWindow | undefined>
Expand Down
5 changes: 3 additions & 2 deletions packages/desktop/app/javascripts/Main/Menus/Menus.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AppState } from 'app/application'
import { AppState } from 'app/AppState'
import {
app,
BrowserWindow,
Expand All @@ -10,7 +10,8 @@ import {
WebContents,
} from 'electron'
import { autorun } from 'mobx'
import { Store, StoreKeys } from '../Store'
import { Store } from '../Store/Store'
import { StoreKeys } from '../Store/StoreKeys'
import { appMenu as str, contextMenu } from '../Strings'
import { TrayManager } from '../TrayManager'
import { autoUpdatingAvailable } from '../Types/Constants'
Expand Down
11 changes: 7 additions & 4 deletions packages/desktop/app/javascripts/Main/Packages/PackageManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ import { downloadFile, getJSON } from './Networking'
import { Component, MappingFile, PackageInfo, PackageManagerInterface, SyncTask } from './PackageManagerInterface'

function logMessage(...message: any) {
log.info('PackageManager:', ...message)
log.info('PackageManager[Info]:', ...message)
}

function logError(...message: any) {
console.error('PackageManager:', ...message)
console.error('PackageManager[Error]:', ...message)
}

/**
Expand Down Expand Up @@ -357,10 +357,13 @@ async function installComponent(

function pathsForComponent(component: Pick<Component, 'content'>) {
const relativePath = path.join(Paths.extensionsDirRelative, component.content!.package_info.identifier)
const absolutePath = path.join(Paths.userDataDir, relativePath)
const downloadPath = path.join(Paths.tempDir, AppName, 'downloads', component.content!.name + '.zip')

return {
relativePath,
absolutePath: path.join(Paths.userDataDir, relativePath),
downloadPath: path.join(Paths.tempDir, AppName, 'downloads', component.content!.name + '.zip'),
absolutePath,
downloadPath,
}
}

Expand Down
3 changes: 2 additions & 1 deletion packages/desktop/app/javascripts/Main/Remote/RemoteBridge.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { CrossProcessBridge } from '../../Renderer/CrossProcessBridge'
import { Store, StoreKeys } from '../Store'
import { Store } from '../Store/Store'
import { StoreKeys } from '../Store/StoreKeys'

const path = require('path')
const rendererPath = path.join('file://', __dirname, '/renderer.js')
Expand Down
3 changes: 2 additions & 1 deletion packages/desktop/app/javascripts/Main/SpellcheckerManager.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* eslint-disable no-inline-comments */
import { Store, StoreKeys } from './Store'
import { Store } from './Store/Store'
import { StoreKeys } from './Store/StoreKeys'
import { isMac } from './Types/Platforms'
import { isDev } from './Utils/Utils'

Expand Down
Loading