UNPKG

installed-win-apps

Version:

Get the paths of installed programs on Windows

128 lines (95 loc) 4.36 kB
const lsDir = require('list-files-in-dir'); const os = require("os"); const path = require('path'); const lnkParser = require('./lnkParser') const codePage = require("./win-codepage.js"); const fs = require("fs"); const myExport = { async _getpath(folder, lnkOnly=false) { const myCodePage = await codePage() let files = []; try { files = await lsDir.listFiles(folder); } catch (error) { console.error("Error reading directory:", error); } const getTargetPath = async (myItem) => { try { if (!lnkOnly) { const lnkObj = await lnkParser(myItem, myCodePage) return {lnk: myItem, target: lnkObj.targetPath} } else { return {lnk: myItem, target: ""} } }catch (e) { return "" } } const promises = []; for (const item of files) { let ext = path.extname(item); if (ext.toLowerCase() === ".lnk") { promises.push(getTargetPath(item)) } } return (await Promise.all(promises)).filter(ele=>ele!=="") }, /** * * @param {String} keyword @default {empty string}: Filter for an application. Print out all applications when left empty. * @param {Boolean} lnkOnly @default {false}: Print out lnk files only. Do not extract targets. Fast way to find out if an application is installed. * @returns {Array} Array of objects {lnk: "", target: ""} . e.g. [{lnk: "PATH/myapp.lnk", target: "PATH/myapp.exe"},{lnk: "PATH/yourapp.lnk", target: "PATH/yourapp.exe"}] */ async getAllPaths(keyword = "", lnkOnly=false) { const homePath = os.homedir(); const usersPath = path.dirname(homePath); const drivePath = path.dirname(usersPath); const folders = [ homePath + "\\AppData\\Roaming\\Microsoft\\Windows\\Start Menu\\Programs", usersPath + "\\Default\\AppData\\Roaming\\Microsoft\\Windows\\Start Menu\\Programs", drivePath + "ProgramData\\Microsoft\\Windows\\Start Menu\\Programs" ] const promises = [this._getpath(folders[0], lnkOnly), this._getpath(folders[1], lnkOnly), this._getpath(folders[2], lnkOnly)]; const allPaths = await Promise.all(promises); const pathsInfo =[...allPaths[0], ...allPaths[1], ...allPaths[2]]; if (keyword ==="") { return pathsInfo; } const filteredPaths = pathsInfo.filter((x)=>{ if (path.basename(x.lnk).includes(keyword)) { return true; } if (path.basename(x.target).includes(keyword)) { return true; } }) if (filteredPaths.length ===0) { return false; } else { //running 32bit node on 64bit windows may output mistaken paths. const isNode32 = !process.arch.includes("64"); const isWindows = (process.platform === "win32"); console.log(isNode32, isWindows) if (isNode32 && isWindows) { // "C:\Program Files (x86)" const newPath = filteredPaths.map(ele=>{ if (ele.target.includes("Program Files (x86)")) { if (!fs.existsSync(ele.target)) { const newTarget = ele.target.replace("Program Files (x86)\\", "Program Files\\") ele.target = newTarget; } } return ele }) return newPath } else { return filteredPaths; } } } } module.exports = myExport // const installedPaths = myExport // installedPaths.getAllPaths("Microsoft Word", false).then (paths=>{ // console.log(paths) //paths is an array that contains the paths of all installed apps // })