Changed the paths to utils for free access across the project

This commit is contained in:
Core 2022-02-02 10:28:26 +00:00
parent de5d41ef36
commit b972a0e40a
No known key found for this signature in database
GPG key ID: FE9BF1B547F8F3C6
2 changed files with 60 additions and 25 deletions

View file

@ -18,14 +18,7 @@ import {utils} from './utils'
export class BrowserWindow { export class BrowserWindow {
public static win: any | undefined = null; public static win: any | undefined = null;
private devMode: boolean = !app.isPackaged; private devMode: boolean = !app.isPackaged;
private paths: any = {
srcPath: path.join(__dirname, "../../src"),
resourcePath: path.join(__dirname, "../../resources"),
ciderCache: path.resolve(app.getPath("userData"), "CiderCache"),
themes: path.resolve(app.getPath("userData"), "Themes"),
plugins: path.resolve(app.getPath("userData"), "Plugins"),
};
private audioStream: any = new Stream.PassThrough(); private audioStream: any = new Stream.PassThrough();
private clientPort: number = 0; private clientPort: number = 0;
private remotePort: number = 6942; private remotePort: number = 6942;
@ -37,7 +30,7 @@ export class BrowserWindow {
}; };
private options: any = { private options: any = {
icon: path.join( icon: path.join(
this.paths.resourcePath, utils.getPath('resourcePath'),
`icons/icon.` + (process.platform === "win32" ? "ico" : "png") `icons/icon.` + (process.platform === "win32" ? "ico" : "png")
), ),
width: 1024, width: 1024,
@ -62,7 +55,7 @@ export class BrowserWindow {
plugins: true, plugins: true,
nodeIntegrationInWorker: false, nodeIntegrationInWorker: false,
webSecurity: false, webSecurity: false,
preload: path.join(this.paths.srcPath, "./preload/cider-preload.js"), preload: path.join(utils.getPath('srcPath'), "./preload/cider-preload.js"),
}, },
}; };
@ -122,7 +115,7 @@ export class BrowserWindow {
} }
} }
for (let i = 0; i < expectedFiles.length; i++) { for (let i = 0; i < expectedFiles.length; i++) {
const file = path.join(this.paths.ciderCache, expectedFiles[i]); const file = path.join(utils.getPath('ciderCache'), expectedFiles[i]);
if (!fs.existsSync(file)) { if (!fs.existsSync(file)) {
fs.writeFileSync(file, JSON.stringify([])); fs.writeFileSync(file, JSON.stringify([]));
} }
@ -135,8 +128,8 @@ export class BrowserWindow {
private startWebServer(): void { private startWebServer(): void {
const app = express(); const app = express();
app.use(express.static(path.join(this.paths.srcPath, "./renderer/"))); app.use(express.static(path.join(utils.getPath('srcPath'), "./renderer/")));
app.set("views", path.join(this.paths.srcPath, "./renderer/views")); app.set("views", path.join(utils.getPath('srcPath'), "./renderer/views"));
app.set("view engine", "ejs"); app.set("view engine", "ejs");
let firstRequest = true; let firstRequest = true;
app.use((req, res, next) => { app.use((req, res, next) => {
@ -188,8 +181,8 @@ export class BrowserWindow {
* https://github.com/ciderapp/Apple-Music-Electron/blob/818ed18940ff600d76eb59d22016723a75885cd5/resources/functions/handler.js#L1173 * https://github.com/ciderapp/Apple-Music-Electron/blob/818ed18940ff600d76eb59d22016723a75885cd5/resources/functions/handler.js#L1173
*/ */
const remote = express(); const remote = express();
remote.use(express.static(path.join(this.paths.srcPath, "./web-remote/"))) remote.use(express.static(path.join(utils.getPath('srcPath'), "./web-remote/")))
remote.set("views", path.join(this.paths.srcPath, "./web-remote/views")); remote.set("views", path.join(utils.getPath('srcPath'), "./web-remote/views"));
remote.set("view engine", "ejs"); remote.set("view engine", "ejs");
getPort({port: 6942}).then((port) => { getPort({port: 6942}).then((port) => {
this.remotePort = port; this.remotePort = port;
@ -312,42 +305,42 @@ export class BrowserWindow {
ipcMain.on("put-library-songs", (_event, arg) => { ipcMain.on("put-library-songs", (_event, arg) => {
fs.writeFileSync( fs.writeFileSync(
path.join(this.paths.ciderCache, "library-songs.json"), path.join(utils.getPath('ciderCache'), "library-songs.json"),
JSON.stringify(arg) JSON.stringify(arg)
); );
}); });
ipcMain.on("put-library-artists", (_event, arg) => { ipcMain.on("put-library-artists", (_event, arg) => {
fs.writeFileSync( fs.writeFileSync(
path.join(this.paths.ciderCache, "library-artists.json"), path.join(utils.getPath('ciderCache'), "library-artists.json"),
JSON.stringify(arg) JSON.stringify(arg)
); );
}); });
ipcMain.on("put-library-albums", (_event, arg) => { ipcMain.on("put-library-albums", (_event, arg) => {
fs.writeFileSync( fs.writeFileSync(
path.join(this.paths.ciderCache, "library-albums.json"), path.join(utils.getPath('ciderCache'), "library-albums.json"),
JSON.stringify(arg) JSON.stringify(arg)
); );
}); });
ipcMain.on("put-library-playlists", (_event, arg) => { ipcMain.on("put-library-playlists", (_event, arg) => {
fs.writeFileSync( fs.writeFileSync(
path.join(this.paths.ciderCache, "library-playlists.json"), path.join(utils.getPath('ciderCache'), "library-playlists.json"),
JSON.stringify(arg) JSON.stringify(arg)
); );
}); });
ipcMain.on("put-library-recentlyAdded", (_event, arg) => { ipcMain.on("put-library-recentlyAdded", (_event, arg) => {
fs.writeFileSync( fs.writeFileSync(
path.join(this.paths.ciderCache, "library-recentlyAdded.json"), path.join(utils.getPath('ciderCache'), "library-recentlyAdded.json"),
JSON.stringify(arg) JSON.stringify(arg)
); );
}); });
ipcMain.on("get-library-songs", (event) => { ipcMain.on("get-library-songs", (event) => {
let librarySongs = fs.readFileSync( let librarySongs = fs.readFileSync(
path.join(this.paths.ciderCache, "library-songs.json"), path.join(utils.getPath('ciderCache'), "library-songs.json"),
"utf8" "utf8"
); );
event.returnValue = JSON.parse(librarySongs); event.returnValue = JSON.parse(librarySongs);
@ -355,7 +348,7 @@ export class BrowserWindow {
ipcMain.on("get-library-artists", (event) => { ipcMain.on("get-library-artists", (event) => {
let libraryArtists = fs.readFileSync( let libraryArtists = fs.readFileSync(
path.join(this.paths.ciderCache, "library-artists.json"), path.join(utils.getPath('ciderCache'), "library-artists.json"),
"utf8" "utf8"
); );
event.returnValue = JSON.parse(libraryArtists); event.returnValue = JSON.parse(libraryArtists);
@ -363,7 +356,7 @@ export class BrowserWindow {
ipcMain.on("get-library-albums", (event) => { ipcMain.on("get-library-albums", (event) => {
let libraryAlbums = fs.readFileSync( let libraryAlbums = fs.readFileSync(
path.join(this.paths.ciderCache, "library-albums.json"), path.join(utils.getPath('ciderCache'), "library-albums.json"),
"utf8" "utf8"
); );
event.returnValue = JSON.parse(libraryAlbums); event.returnValue = JSON.parse(libraryAlbums);
@ -371,7 +364,7 @@ export class BrowserWindow {
ipcMain.on("get-library-playlists", (event) => { ipcMain.on("get-library-playlists", (event) => {
let libraryPlaylists = fs.readFileSync( let libraryPlaylists = fs.readFileSync(
path.join(this.paths.ciderCache, "library-playlists.json"), path.join(utils.getPath('ciderCache'), "library-playlists.json"),
"utf8" "utf8"
); );
event.returnValue = JSON.parse(libraryPlaylists); event.returnValue = JSON.parse(libraryPlaylists);
@ -379,7 +372,7 @@ export class BrowserWindow {
ipcMain.on("get-library-recentlyAdded", (event) => { ipcMain.on("get-library-recentlyAdded", (event) => {
let libraryRecentlyAdded = fs.readFileSync( let libraryRecentlyAdded = fs.readFileSync(
path.join(this.paths.ciderCache, "library-recentlyAdded.json"), path.join(utils.getPath('ciderCache'), "library-recentlyAdded.json"),
"utf8" "utf8"
); );
event.returnValue = JSON.parse(libraryRecentlyAdded); event.returnValue = JSON.parse(libraryRecentlyAdded);

View file

@ -3,9 +3,30 @@ import * as path from "path";
import {jsonc} from "jsonc"; import {jsonc} from "jsonc";
import {Store} from "./store"; import {Store} from "./store";
import {BrowserWindow as bw} from "./browserwindow"; import {BrowserWindow as bw} from "./browserwindow";
import {app} from "electron";
export class utils { export class utils {
/**
* Paths for the application to use
*/
private static paths: any = {
srcPath: path.join(__dirname, "../../src"),
resourcePath: path.join(__dirname, "../../resources"),
ciderCache: path.resolve(app.getPath("userData"), "CiderCache"),
themes: path.resolve(app.getPath("userData"), "Themes"),
plugins: path.resolve(app.getPath("userData"), "Plugins"),
};
/**
* Get the path
* @returns {string}
* @param name
*/
static getPath(name: string): string {
return this.paths[name];
}
/** /**
* Fetches the i18n locale for the given language. * Fetches the i18n locale for the given language.
* @param language {string} The language to fetch the locale for. * @param language {string} The language to fetch the locale for.
@ -58,4 +79,25 @@ export class utils {
static getWindow(): Electron.BrowserWindow { static getWindow(): Electron.BrowserWindow {
return bw.win return bw.win
} }
/**
* Playback Functions
*/
static playback = {
pause: () => {
bw.win.webContents.send("MusicKitInterop.pause()")
},
play: () => {
bw.win.webContents.send("MusicKitInterop.play()")
},
playPause: () => {
bw.win.webContents.send("MusicKitInterop.playPause()")
},
next: () => {
bw.win.webContents.send("MusicKitInterop.next()")
},
previous: () => {
bw.win.webContents.send("MusicKitInterop.previous()")
}
}
} }