diff --git a/package-lock.json b/package-lock.json index c2c6f18..94e0cf2 100644 --- a/package-lock.json +++ b/package-lock.json @@ -20,6 +20,8 @@ "@ark120202/typescript-config": "^2.0.0", "@types/execa": "^0.9.0", "@types/fs-extra": "^5.0.5", + "@types/node": "^10.12.18", + "tslint": "^5.16.0", "@typescript-eslint/eslint-plugin": "^4.31.2", "@typescript-eslint/parser": "^4.31.2", "eslint": "^7.32.0", diff --git a/package.json b/package.json index b64fe28..10248f5 100644 --- a/package.json +++ b/package.json @@ -18,7 +18,7 @@ "main": "lib/index.js", "types": "lib/index.d.ts", "scripts": { - "build": "tsc", + "build": "tsc --build", "dev": "tsc --watch", "lint": "run-s lint:prettier lint:tslint", "lint:prettier": "prettier --check **/*.{ts,md,yml,json}", @@ -44,6 +44,7 @@ "@ark120202/typescript-config": "^2.0.0", "@types/execa": "^0.9.0", "@types/fs-extra": "^5.0.5", + "@types/node": "^10.12.18", "@typescript-eslint/eslint-plugin": "^4.31.2", "@typescript-eslint/parser": "^4.31.2", "eslint": "^7.32.0", diff --git a/src/libraries.ts b/src/libraries.ts index db4654d..9b6dd6e 100644 --- a/src/libraries.ts +++ b/src/libraries.ts @@ -2,12 +2,19 @@ import fs from 'fs-extra'; import path from 'path'; import vdf from 'vdf-extra'; +interface FolderDetails { + path: string + label: string + contentid: number + totalsize: number + update_clean_bytes_tally: number + time_last_update_corruption: number + apps: Object +} + interface LibraryFolders { - [id: string]: - | { - path: string; - } - | string; + '0': FolderDetails + contentstatsid: string } export async function loadSteamLibraries(steam: string) { @@ -18,9 +25,7 @@ export async function loadSteamLibraries(steam: string) { const libraries = Object.entries(libraryFoldersData) .filter(([id]) => !isNaN(Number(id))) - .map(([, libPath]) => - path.join(typeof libPath === 'string' ? libPath : libPath.path, 'steamapps'), - ); + .map(([_, libPath]) => path.join(libPath.path, 'steamapps')); return [mainSteamApps, ...libraries]; } diff --git a/src/steam.ts b/src/steam.ts index 2193ad0..757de27 100644 --- a/src/steam.ts +++ b/src/steam.ts @@ -1,6 +1,7 @@ -import execa from 'execa'; -import fs from 'fs-extra'; -import path from 'path'; +import * as execa from 'execa'; +import * as fs from 'fs-extra'; +import * as path from 'path'; +import * as os from 'os'; const pathIfExists = async (name: string) => ((await fs.pathExists(name)) ? name : undefined); const getRegExePath = () => @@ -10,13 +11,8 @@ const getRegExePath = () => const REG_TREE_PATH = 'HKCU\\Software\\Valve\\Steam'; const REG_KEY_NOT_FOUND = 'The system was unable to find the specified registry key or value'; -async function windows() { - let programFiles = process.env['ProgramFiles(x86)']; - if (programFiles == null) programFiles = process.env.ProgramFiles; - if (programFiles != null && (await fs.pathExists(`${programFiles}/Steam/Steam.exe`))) { - return `${programFiles}/Steam`; - } +async function getPathHKCU () { try { const output = await execa.stdout( getRegExePath(), @@ -34,6 +30,35 @@ async function windows() { } } +async function windows() { + let programFiles = process.env['ProgramFiles(x86)']; + if (programFiles == null) programFiles = process.env.ProgramFiles; + if (programFiles != null && (await fs.pathExists(`${programFiles}\\Steam\\Steam.exe`))) { + return `${programFiles}\\Steam`; + } + + return getPathHKCU(); +} + +const releaseName = os.release(); +const isWSL = () => process.env.WSL_DISTRO_NAME != null && releaseName.includes('microsoft'); + +async function wsl (): Promise { + const mountDir = '/mnt'; + const mountableDrives = fs.readdirSync(mountDir) + // NOTE: WSL does not support 32 bit windows systems + const steamDir = 'Program Files (x86)/Steam' + const defaultPath = `${mountDir}/c/${steamDir}` + + for (const drive of mountableDrives) { + const steamPath = path.join(mountDir, drive, steamDir) + if (await fs.pathExists(steamPath) === true) { + return steamPath + } + } + + return pathIfExists(defaultPath) +} /** * Searches for Steam. * @@ -44,7 +69,9 @@ export async function findSteam() { case 'win32': return windows(); case 'linux': - return pathIfExists(`${process.env.HOME}/.local/share/Steam`); + return isWSL() === true + ? wsl() + : pathIfExists(`${process.env.HOME}/.local/share/Steam`); case 'darwin': return pathIfExists(`${process.env.HOME}/Library/Application Support/Steam`); default: