begin converting WakatimeCore

This commit is contained in:
Vu Nguyen
2021-01-16 20:04:53 -06:00
parent d194bcfe60
commit 0c39fbbc79
25 changed files with 1205 additions and 55 deletions

View File

@@ -2,4 +2,5 @@ assets
public public
tests tests
coverage coverage
gulpfile.js gulpfile.js
dist

15
babel.config.js Normal file
View File

@@ -0,0 +1,15 @@
// babel.config.js
module.exports = {
presets: [
'@babel/preset-react',
[
'@babel/preset-env',
{
targets: {
node: 'current',
},
},
],
'@babel/preset-typescript',
],
};

View File

@@ -6,22 +6,17 @@
export default { export default {
// All imported modules in your tests should be mocked automatically // All imported modules in your tests should be mocked automatically
// automock: false, // automock: false,
// Stop running tests after `n` failures // Stop running tests after `n` failures
// bail: 0, // bail: 0,
// The directory where Jest should store its cached dependency information // The directory where Jest should store its cached dependency information
// cacheDirectory: "/private/var/folders/sz/2htrw56x4fg4kmlpq6ssdw5m0000gn/T/jest_dx", // cacheDirectory: "/private/var/folders/sz/2htrw56x4fg4kmlpq6ssdw5m0000gn/T/jest_dx",
// Automatically clear mock calls and instances between every test // Automatically clear mock calls and instances between every test
clearMocks: true, clearMocks: true,
// Indicates whether the coverage information should be collected while executing the test // Indicates whether the coverage information should be collected while executing the test
// collectCoverage: false, // collectCoverage: false,
// An array of glob patterns indicating a set of files for which coverage information should be collected // An array of glob patterns indicating a set of files for which coverage information should be collected
// collectCoverageFrom: undefined, // collectCoverageFrom: undefined,
// The directory where Jest should output its coverage files // The directory where Jest should output its coverage files
coverageDirectory: 'coverage', coverageDirectory: 'coverage',
@@ -29,10 +24,11 @@ export default {
// coveragePathIgnorePatterns: [ // coveragePathIgnorePatterns: [
// "/node_modules/" // "/node_modules/"
// ], // ],
// Indicates which provider should be used to instrument code for coverage // Indicates which provider should be used to instrument code for coverage
coverageProvider: 'v8', coverageProvider: 'v8',
preset: 'ts-jest', preset: 'ts-jest',
setupFilesAfterEnv: ['./setupTests.ts'],
// A list of reporter names that Jest uses when writing coverage reports // A list of reporter names that Jest uses when writing coverage reports
// coverageReporters: [ // coverageReporters: [
// "json", // "json",

1082
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -34,10 +34,13 @@
"react": "^16.2.0", "react": "^16.2.0",
"react-dom": "^16.2.0", "react-dom": "^16.2.0",
"react-transition-group": "^1.0.0", "react-transition-group": "^1.0.0",
"webextension-polyfill": "^0.4.0",
"webextension-polyfill-ts": "^0.22.0" "webextension-polyfill-ts": "^0.22.0"
}, },
"devDependencies": { "devDependencies": {
"@babel/core": "^7.8.7",
"@babel/preset-env": "^7.8.7",
"@babel/preset-react": "^7.8.3",
"@babel/preset-typescript": "^7.8.3",
"@testing-library/dom": "^7.29.4", "@testing-library/dom": "^7.29.4",
"@testing-library/jest-dom": "^5.11.9", "@testing-library/jest-dom": "^5.11.9",
"@testing-library/react": "^11.2.3", "@testing-library/react": "^11.2.3",
@@ -55,7 +58,9 @@
"@typescript-eslint/eslint-plugin": "^4.13.0", "@typescript-eslint/eslint-plugin": "^4.13.0",
"@typescript-eslint/parser": "^4.13.0", "@typescript-eslint/parser": "^4.13.0",
"@xarc/run": "^1.0.4", "@xarc/run": "^1.0.4",
"axios": "^0.21.1",
"babel-jest": "^22.1.0", "babel-jest": "^22.1.0",
"babel-loader": "^8.2.2",
"browserify": "^17.0.0", "browserify": "^17.0.0",
"chai": "^4.1.2", "chai": "^4.1.2",
"clean-webpack-plugin": "^3.0.0", "clean-webpack-plugin": "^3.0.0",
@@ -84,8 +89,6 @@
"mocha": "^5.0.0", "mocha": "^5.0.0",
"mocha-sinon": "^2.0.0", "mocha-sinon": "^2.0.0",
"mocha-traceur": "^2.1.0", "mocha-traceur": "^2.1.0",
"mockzilla": "^0.9.0",
"mockzilla-webextension": "^0.9.0",
"phantomjs": "^2.1.7", "phantomjs": "^2.1.7",
"popper.js": "^1.14.6", "popper.js": "^1.14.6",
"prettier": "^2.2.1", "prettier": "^2.2.1",

View File

@@ -13,7 +13,7 @@ class BrowserMock {
}, },
}; };
} }
//TODO: Improve mocking
export const browser = new BrowserMock(); // eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
export default { browser }; window.browser = new BrowserMock();

View File

@@ -1,5 +1,4 @@
import React from 'react'; import React from 'react';
import { browser } from 'webextension-polyfill-ts';
export interface MainListProps { export interface MainListProps {
disableLogging: (event: React.MouseEvent<HTMLAnchorElement, MouseEvent>) => void; disableLogging: (event: React.MouseEvent<HTMLAnchorElement, MouseEvent>) => void;

View File

@@ -1,5 +1,3 @@
import { browser } from 'webextension-polyfill-ts';
/** /**
* Logging * Logging
*/ */

35
src/core/WakaTimeCore.ts Normal file
View File

@@ -0,0 +1,35 @@
import axios, { AxiosResponse } from 'axios';
import moment from 'moment';
import { Tabs } from 'webextension-polyfill-ts';
import { User } from '../types/user';
import config from '../config';
import { SummariesPayload, GrandTotal } from '../types/summaries';
class WakaTimeCore {
tabsWithDevtoolsOpen: Tabs.Tab[];
constructor() {
this.tabsWithDevtoolsOpen = [];
}
setTabsWithDevtoolsOpen(tabs: Tabs.Tab[]): void {
this.tabsWithDevtoolsOpen = tabs;
}
async getTotalTimeLoggedToday(): Promise<GrandTotal> {
const today = moment().format('YYYY-MM-DD');
const summariesAxiosPayload: AxiosResponse<SummariesPayload> = await axios.get(
config.summariesApiUrl,
{
data: {
end: today,
start: today,
},
},
);
return summariesAxiosPayload.data.data[0].grand_total;
}
async checkAuth(): Promise<User> {
const userPayload: AxiosResponse<User> = await axios.get(config.currentUserApiUrl);
return userPayload.data;
}
}
export default new WakaTimeCore();

13
src/devtools.ts Normal file
View File

@@ -0,0 +1,13 @@
// // Create a connection to the background page
// const backgroundPageConnection = browser.runtime.connect({
// name: 'devtools-page',
// });
// // Send a message to background page with the current active tabId
// backgroundPageConnection.postMessage({
// name: 'init',
// tabId: browser.devtools.inspectedWindow.tabId,
// });
// eslint-disable-next-line @typescript-eslint/no-floating-promises
browser.devtools.panels.create('Wakatime', 'test.png', 'WakatimeDevPanel.html');

View File

@@ -0,0 +1,15 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>WakaTime options</title>
<link href="public/css/app.css" rel="stylesheet" />
</head>
<body>
<div id="wakatime-devtools"></div>
<script src="public/js/browser-polyfill.min.js"></script>
</body>
</html>

3
src/html/devtools.html Normal file
View File

@@ -0,0 +1,3 @@
<!DOCTYPE html>
<script src="public/js/browser-polyfill.min.js"></script>
<script src="devtools.js"></script>

View File

@@ -10,7 +10,7 @@
</head> </head>
<body> <body>
<div id="wakatime-options"></div> <div id="wakatime-options"></div>
<script src="public/js/browser-polyfill.min.js"></script>
<script src="options.js"></script> <script src="options.js"></script>
</body> </body>
</html> </html>

View File

@@ -10,7 +10,7 @@
</head> </head>
<body> <body>
<div id="wakatime"></div> <div id="wakatime"></div>
<script src="public/js/browser-polyfill.min.js"></script>
<script src="popup.js"></script> <script src="popup.js"></script>
</body> </body>
</html> </html>

View File

@@ -1,6 +0,0 @@
import jestConfig from '../jest.config';
export default {
...jestConfig,
testEnvironment: 'node',
};

View File

@@ -1,7 +1,7 @@
{ {
"background": { "background": {
"persistent": false, "persistent": false,
"scripts": ["background.js"] "scripts": ["public/js/browser-polyfill.min.js", "background.js"]
}, },
"browser_action": { "browser_action": {
"default_icon": { "default_icon": {
@@ -11,7 +11,6 @@
"default_popup": "popup.html", "default_popup": "popup.html",
"default_title": "WakaTime" "default_title": "WakaTime"
}, },
"browser_specific_settings": {},
"description": "Automatic time tracking for Chrome.", "description": "Automatic time tracking for Chrome.",
"devtools_page": "devtools.html", "devtools_page": "devtools.html",
"homepage_url": "https://wakatime.com", "homepage_url": "https://wakatime.com",

View File

@@ -1,7 +1,7 @@
{ {
"background": { "background": {
"persistent": false, "persistent": false,
"scripts": ["background.js"] "scripts": ["public/js/browser-polyfill.min.js", "background.js"]
}, },
"browser_action": { "browser_action": {
"default_icon": { "default_icon": {

View File

@@ -0,0 +1,6 @@
import React from 'react';
import ReactDOM from 'react-dom';
const container = document.getElementById('wakatime-options');
ReactDOM.render(<h1>OPTIONS GO HERE</h1>, container);

View File

@@ -1 +1,14 @@
// Stub for port of previous extension import React from 'react';
import ReactDOM from 'react-dom';
const container = document.getElementById('wakatime');
const openOptions = async (): Promise<void> => {
await browser.runtime.openOptionsPage();
};
ReactDOM.render(
<>
<h1>POPUP GO HERE</h1>
<div onClick={openOptions}>Open options</div>
</>,
container,
);

View File

@@ -1,7 +1,7 @@
/** /**
* Returns domain from given URL. * Returns domain from given URL.
*/ */
export function getDomainFromUrl(url: string): string { export default function getDomainFromUrl(url: string): string {
const parts = url.split('/'); const parts = url.split('/');
return parts[0] + '//' + parts[2]; return parts[0] + '//' + parts[2];

View File

@@ -1,7 +1,7 @@
/** /**
* Returns boolean if needle is found in haystack or not. * Returns boolean if needle is found in haystack or not.
*/ */
export function in_array<T>(needle: T, haystack: T[]): boolean { export default function in_array<T>(needle: T, haystack: T[]): boolean {
for (let i = 0; i < haystack.length; i++) { for (let i = 0; i < haystack.length; i++) {
if (needle == haystack[i]) { if (needle == haystack[i]) {
return true; return true;

View File

@@ -13,11 +13,16 @@ const graphicsFolder = join(__dirname, 'graphics');
const srcFolder = join(__dirname, 'src'); const srcFolder = join(__dirname, 'src');
const htmlFolder = join(srcFolder, 'html'); const htmlFolder = join(srcFolder, 'html');
const manifestFolder = join(srcFolder, 'manifests'); const manifestFolder = join(srcFolder, 'manifests');
const browserPolyfill = join(
__dirname,
'node_modules/webextension-polyfill/dist/browser-polyfill.min.js',
);
const getConfigByBrowser = (isProd: boolean, browser: BrowserTypes): webpack.Configuration => { const getConfigByBrowser = (isProd: boolean, browser: BrowserTypes): webpack.Configuration => {
const cfg: webpack.Configuration = { const cfg: webpack.Configuration = {
devtool: isProd ? 'none' : 'inline-source-map',
entry: { entry: {
background: [join(srcFolder, 'background.ts')], background: [join(srcFolder, 'background.ts')],
devtools: [join(srcFolder, 'devtools.ts')],
options: [join(srcFolder, 'options.tsx')], options: [join(srcFolder, 'options.tsx')],
popup: [join(srcFolder, 'popup.tsx')], popup: [join(srcFolder, 'popup.tsx')],
}, },
@@ -26,7 +31,7 @@ const getConfigByBrowser = (isProd: boolean, browser: BrowserTypes): webpack.Con
rules: [ rules: [
{ {
test: /\.(js|jsx|ts|tsx)$/, test: /\.(js|jsx|ts|tsx)$/,
use: 'ts-loader', use: 'babel-loader',
}, },
], ],
}, },
@@ -44,8 +49,25 @@ const getConfigByBrowser = (isProd: boolean, browser: BrowserTypes): webpack.Con
{ from: htmlFolder }, { from: htmlFolder },
// TODO: Create a mechanism to have a firefox manifest vs chrome // TODO: Create a mechanism to have a firefox manifest vs chrome
{ from: join(manifestFolder, `${browser}.json`), to: 'manifest.json' }, { from: join(manifestFolder, `${browser}.json`), to: 'manifest.json' },
{
from: browserPolyfill,
to: 'public/js/browser-polyfill.min.js',
},
], ],
}), }),
new webpack.DefinePlugin({
['process.env.CURRENT_USER_API_URL']: JSON.stringify(
'https://wakatime.com/api/v1/users/current',
),
['process.env.HEART_BEAT_API_URL']: JSON.stringify(
'https://wakatime.com/api/v1/users/current/heartbeats',
),
['process.env.LOGOUT_USER_URL']: JSON.stringify('https://wakatime.com/logout'),
['process.env.NODE_ENV']: JSON.stringify(isProd ? 'production' : 'development'),
['process.env.SUMMARIES_API_URL']: JSON.stringify(
'https://wakatime.com/api/v1/users/current/summaries',
),
}),
], ],
resolve: { resolve: {
extensions: ['.js', '.jsx', '.ts', '.tsx'], extensions: ['.js', '.jsx', '.ts', '.tsx'],