Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds Support for WSL Users #2

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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}",
Expand All @@ -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",
Expand Down
21 changes: 13 additions & 8 deletions src/libraries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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];
}
47 changes: 37 additions & 10 deletions src/steam.ts
Original file line number Diff line number Diff line change
@@ -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 = () =>
Expand All @@ -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(),
Expand All @@ -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<string|undefined> {
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.
*
Expand All @@ -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:
Expand Down