@ciberus/find-steam-app
Version:
Find location of an installed Steam app
168 lines • 8.43 kB
JavaScript
;
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (_) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.findSteamPath = exports.SteamNotFoundError = void 0;
var execa_1 = __importDefault(require("execa"));
var fs_extra_1 = __importDefault(require("fs-extra"));
var path_1 = __importDefault(require("path"));
var utils_1 = require("./utils");
var REG_TREE_PATH = "HKCU\\Software\\Valve\\Steam";
var REG_KEY_NOT_FOUND = "The system was unable to find the specified registry key or value";
var SteamNotFoundError = /** @class */ (function (_super) {
__extends(SteamNotFoundError, _super);
function SteamNotFoundError() {
var _this = _super.call(this, "Steam installation directory not found") || this;
_this.name = "SteamNotFoundError";
return _this;
}
return SteamNotFoundError;
}(Error));
exports.SteamNotFoundError = SteamNotFoundError;
var pathIfExists = function (name) { return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, fs_extra_1.default.pathExists(name)];
case 1: return [2 /*return*/, (_a.sent()) ? name : undefined];
}
}); }); };
var getRegExePath = function () {
return process.platform === "win32" && process.env.windir != null
? path_1.default.join(process.env.windir, "System32", "reg.exe")
: "REG";
};
function windows() {
var _a;
return __awaiter(this, void 0, void 0, function () {
var programFiles, _b, output, matches, err_1;
return __generator(this, function (_c) {
switch (_c.label) {
case 0:
programFiles = process.env["ProgramFiles(x86)"];
if (programFiles == null)
programFiles = process.env.ProgramFiles;
_b = programFiles != null;
if (!_b) return [3 /*break*/, 2];
return [4 /*yield*/, fs_extra_1.default.pathExists("".concat(programFiles, "/Steam/Steam.exe"))];
case 1:
_b = (_c.sent());
_c.label = 2;
case 2:
if (_b) {
return [2 /*return*/, "".concat(programFiles, "/Steam")];
}
_c.label = 3;
case 3:
_c.trys.push([3, 5, , 6]);
return [4 /*yield*/, execa_1.default.stdout(getRegExePath(), ["QUERY", REG_TREE_PATH, "/v", "SteamPath"], { cwd: undefined })];
case 4:
output = _c.sent();
matches = output.match(/SteamPath\s+[A-Z_]+\s+(.+)/);
if (!matches || matches[1] === "")
throw new Error("Unexpected output:\n".concat(output.trim()));
return [2 /*return*/, pathIfExists(matches[1])];
case 5:
err_1 = _c.sent();
// @ts-ignore
if (!((_a = err_1 === null || err_1 === void 0 ? void 0 : err_1.message) === null || _a === void 0 ? void 0 : _a.includes(REG_KEY_NOT_FOUND))) {
throw err_1;
}
return [3 /*break*/, 6];
case 6: throw new Error("Unable to find Steam installation directory");
}
});
});
}
/**
* Searches for Steam.
*
* @returns Location of Steam. `undefined` if Steam wasn't found.
*/
function findSteamPath() {
return __awaiter(this, void 0, void 0, function () {
var result, _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
_a = process.platform;
switch (_a) {
case "win32": return [3 /*break*/, 1];
case "linux": return [3 /*break*/, 3];
case "darwin": return [3 /*break*/, 5];
}
return [3 /*break*/, 7];
case 1: return [4 /*yield*/, windows()];
case 2:
result = _b.sent();
return [3 /*break*/, 8];
case 3: return [4 /*yield*/, pathIfExists("".concat(process.env.HOME, "/.local/share/Steam"))];
case 4:
result = _b.sent();
return [3 /*break*/, 8];
case 5: return [4 /*yield*/, pathIfExists("".concat(process.env.HOME, "/Library/Application Support/Steam"))];
case 6:
result = _b.sent();
return [3 /*break*/, 8];
case 7: throw new Error("Steam finding isn't implemented for ".concat(process.platform));
case 8:
if (result) {
result = (0, utils_1.joinAndNormalize)(result);
}
return [2 /*return*/, result];
}
});
});
}
exports.findSteamPath = findSteamPath;
//# sourceMappingURL=steam.js.map