Migrate app to TypeScript (#1637)

* Initial setup and migrated src/common

* WIP

* WIP

* WIP

* Main module basically finished

* Renderer process migrated

* Added CI step and some fixes

* Fixed remainder of issues and added proper ESLint config

* Fixed a couple issues

* Progress!

* Some more fixes

* Fixed a test

* Fix build step

* PR feedback
This commit is contained in:
Devin Binnie
2021-06-28 09:51:23 -04:00
committed by GitHub
parent 422673a740
commit 1b3d0eac8f
115 changed files with 16246 additions and 9921 deletions

84
src/main/utils.ts Normal file
View File

@@ -0,0 +1,84 @@
// Copyright (c) 2015-2016 Yuya Ochiai
// Copyright (c) 2016-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import electron, {app, BrowserWindow} from 'electron';
import path from 'path';
import {Args} from 'types/args';
import {PRODUCTION} from 'common/utils/constants';
import Utils from 'common/utils/util';
const TAB_BAR_HEIGHT = 40;
const BACK_BAR_HEIGHT = 36;
export function shouldBeHiddenOnStartup(parsedArgv: Args) {
if (parsedArgv.hidden) {
return true;
}
if (process.platform === 'darwin') {
if (app.getLoginItemSettings().wasOpenedAsHidden) {
return true;
}
}
return false;
}
export function getWindowBoundaries(win: BrowserWindow, hasBackBar = false) {
const {width, height} = win.getContentBounds();
return getAdjustedWindowBoundaries(width, height, hasBackBar);
}
export function getAdjustedWindowBoundaries(width: number, height: number, hasBackBar = false) {
return {
x: 0,
y: TAB_BAR_HEIGHT + (hasBackBar ? BACK_BAR_HEIGHT : 0),
width,
height: height - TAB_BAR_HEIGHT - (hasBackBar ? BACK_BAR_HEIGHT : 0),
};
}
export function getLocalURLString(urlPath: string, query?: Map<string, string>, isMain?: boolean) {
const localURL = getLocalURL(urlPath, query, isMain);
return localURL.href;
}
export function getLocalURL(urlPath: string, query?: Map<string, string>, isMain?: boolean) {
let pathname;
const processPath = isMain ? '' : '/renderer';
const mode = Utils.runMode();
const protocol = 'file';
const hostname = '';
const port = '';
if (mode === PRODUCTION) {
pathname = path.join(electron.app.getAppPath(), `${processPath}/${urlPath}`);
} else {
pathname = path.resolve(__dirname, `../../dist/${processPath}/${urlPath}`); // TODO: find a better way to work with webpack on this
}
const localUrl = new URL(`${protocol}://${hostname}${port}`);
localUrl.pathname = pathname;
if (query) {
query.forEach((value: string, key: string) => {
localUrl.searchParams.append(encodeURIComponent(key), encodeURIComponent(value));
});
}
return localUrl;
}
export function getLocalPreload(file: string) {
if (Utils.runMode() === PRODUCTION) {
return path.join(electron.app.getAppPath(), `${file}`);
}
return path.resolve(__dirname, `../../dist/${file}`);
}
export function composeUserAgent() {
const baseUserAgent = app.userAgentFallback.split(' ');
// filter out the Mattermost tag that gets added earlier on
const filteredUserAgent = baseUserAgent.filter((ua) => !ua.startsWith('Mattermost'));
return `${filteredUserAgent.join(' ')} Mattermost/${app.getVersion()}`;
}