Finally got testing working!

This commit is contained in:
marley 2023-05-15 13:39:30 -07:00
parent 2a97cb358f
commit 87b631efff
9 changed files with 4205 additions and 74 deletions

1
.idea/ed-safari.iml Executable file → Normal file
View file

@ -8,5 +8,6 @@
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="@types/mocha" level="application" />
</component>
</module>

View file

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="JavaScriptLibraryMappings">
<file url="file://$PROJECT_DIR$/test" libraries="{@types/mocha}" />
<excludedPredefinedLibrary name="HTML" />
</component>
</project>

6
babel.config.js Normal file
View file

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

10
jest.config.js Normal file
View file

@ -0,0 +1,10 @@
/** @type {import('ts-jest').JestConfigWithTsJest} */
module.exports = {
testEnvironment: 'node',
"transformIgnorePatterns": [
"/node_modules/(?!lodash-es/.*)"
],
transform: {
'\\.[tj]sx?$': ['babel-jest', { rootMode: 'upward' }],
}
};

4095
package-lock.json generated

File diff suppressed because it is too large Load diff

View file

@ -9,7 +9,8 @@
"package": "electron-forge package",
"make": "electron-forge make",
"publish": "electron-forge publish",
"lint": "echo \"No linting configured\""
"lint": "echo \"No linting configured\"",
"test": "jest"
},
"keywords": [],
"author": {
@ -22,6 +23,9 @@
"node 18"
],
"devDependencies": {
"@babel/core": "^7.21.8",
"@babel/preset-env": "^7.21.5",
"@babel/preset-typescript": "^7.21.5",
"@electron-forge/cli": "^6.1.1",
"@electron-forge/maker-deb": "^6.1.1",
"@electron-forge/maker-rpm": "^6.1.1",
@ -30,12 +34,15 @@
"@electron-forge/plugin-vite": "^6.1.1",
"@tsconfig/node-lts": "^18.12.2",
"@tsconfig/node20": "^1.0.0",
"@types/jest": "^29.5.1",
"@types/lodash-es": "^4.17.7",
"@types/node": "^20.1.2",
"@types/tail": "^2.2.1",
"autoprefixer": "^10.4.14",
"babel-jest": "^29.5.0",
"browserslist": "^4.21.5",
"electron": "24.2.0",
"jest": "^29.5.0",
"sass": "^1.62.1",
"typescript": "^5.0.4"
},

View file

@ -9,89 +9,92 @@ import { Journal } from "./Journal";
import { Log } from "./Log";
export class Safari {
static #instance: Safari;
#journalDir?: string;
#journalPattern?: string;
journal?: Journal;
static #instance: Safari;
#watcher?: any;
readonly #journalDir?: string;
readonly #journalPattern?: string;
journal?: Journal;
private constructor(isPackaged: boolean) {
if (!isPackaged && os.platform() === 'linux') { // Account for WSL during development
this.#journalDir = "/mnt/c/Users/marle/Saved\ Games/Frontier\ Developments/Elite\ Dangerous/";
#watcher?: any;
} else if (os.platform() === 'win32') { // Windows
this.#journalDir = path.join(
os.homedir(),
'Saved Games',
'Frontier Developments',
'Elite Dangerous'
);
private constructor(isPackaged: boolean) {
if (!isPackaged && os.platform() === 'linux') { // Account for WSL during development
this.#journalDir = "/mnt/c/Users/marle/Saved\ Games/Frontier\ Developments/Elite\ Dangerous/";
} else if (os.platform() === 'linux') { // Linux
this.#journalDir = path.join(
os.homedir(),
'.local',
'share',
'Steam',
'steamapps',
'compatdata',
'359320',
'pfx',
'drive_c',
'steamuser',
'Saved Games',
'Frontier Developments',
'Elite Dangerous',
);
} else {
Log.write(`ERROR: Journal files not found. OS: ${os.platform()}.`);
}
} else if (os.platform() === 'win32') { // Windows
this.#journalDir = path.join(
os.homedir(),
'Saved Games',
'Frontier Developments',
'Elite Dangerous'
);
if (this.#journalDir) {
this.#journalPattern = path.join(this.#journalDir, 'Journal.*.log');
this.journal = this.#getLatestJournal();
}
} else if (os.platform() === 'linux') { // Linux
this.#journalDir = path.join(
os.homedir(),
'.local',
'share',
'Steam',
'steamapps',
'compatdata',
'359320',
'pfx',
'drive_c',
'steamuser',
'Saved Games',
'Frontier Developments',
'Elite Dangerous',
);
} else {
Log.write(`ERROR: Journal files not found. OS: ${os.platform()}.`);
}
static start(isPackaged: boolean): Safari {
if (!Safari.#instance) {
Safari.#instance = new Safari(isPackaged);
}
if (this.#journalDir) {
this.#journalPattern = path.join(this.#journalDir, 'Journal.*.log');
this.journal = this.#getLatestJournal();
}
}
return Safari.#instance;
static start(isPackaged: boolean): Safari {
if (!Safari.#instance) {
Safari.#instance = new Safari(isPackaged);
}
/* ------------------------------------------------------------------- #getLatestJournal ---- */
return Safari.#instance;
}
// https://stackoverflow.com/questions/15696218/get-the-most-recent-file-in-a-directory-node-js
#getLatestJournal(): Journal|undefined {
const journals = globSync(this.#journalPattern, {windowsPathsNoEscape: true});
const journalPath: string|undefined = _.maxBy(journals, file => fs.statSync(file).mtime);
/* ------------------------------------------------------------------- #getLatestJournal ---- */
if (journalPath) {
Log.write(`New journal file found, now watching ${path.basename(journalPath)}.`);
return new Journal(journalPath);
} else {
Log.write('ERROR: Unable to find latest journal.');
return;
}
// https://stackoverflow.com/questions/15696218/get-the-most-recent-file-in-a-directory-node-js
#getLatestJournal(): Journal|undefined {
// @ts-ignore
const journals = globSync(this.#journalPattern, {windowsPathsNoEscape: true});
const journalPath: string|undefined = _.maxBy(journals, file => fs.statSync(file).mtime);
if (journalPath) {
Log.write(`New journal file found, now watching ${path.basename(journalPath)}.`);
return new Journal(journalPath);
} else {
Log.write('ERROR: Unable to find latest journal.');
return;
}
}
/* --------------------------------------------------------------------- watchJournalDir ---- */
/* --------------------------------------------------------------------- watchJournalDir ---- */
watchJournalDir(): void {
const options = {usePolling: true, persistent: true, ignoreInitial: true};
this.#watcher = chokidar.watch(this.#journalPattern, options);
watchJournalDir(): void {
const options = {usePolling: true, persistent: true, ignoreInitial: true};
// @ts-ignore
this.#watcher = chokidar.watch(this.#journalPattern, options);
this.#watcher.on('ready', () => Log.write('Watching journal folder for changes...'));
this.#watcher.on('add', () => this.journal = this.#getLatestJournal());
}
this.#watcher.on('ready', () => Log.write('Watching journal folder for changes...'));
this.#watcher.on('add', () => this.journal = this.#getLatestJournal());
}
/* ---------------------------------------------------------------------------- shutdown ---- */
/* ---------------------------------------------------------------------------- shutdown ---- */
async shutdown(): Promise<void> {
this.journal?.shutdown();
await this.#watcher.close();
}
}
async shutdown(): Promise<void> {
this.journal?.shutdown();
await this.#watcher.close();
}
}

8
test/Safari.test.ts Normal file
View file

@ -0,0 +1,8 @@
import {Safari} from '../src/models/Safari';
describe('Safari', function () {
it('should return itself', () => {
const safari = Safari.start(false);
expect(safari).toBeDefined();
});
});

View file

@ -5,5 +5,5 @@
"lib": ["DOM"]
},
"include": ["src"],
"exclude": ["node_modules"]
}
"exclude": ["node_modules"],
}