Skip to content

Commit

Permalink
重新设计 getSystemApp
Browse files Browse the repository at this point in the history
  • Loading branch information
SOVLOOKUP committed Oct 19, 2023
1 parent abba931 commit 7c91428
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 60 deletions.
16 changes: 2 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,22 +113,10 @@ pnpm add rubick-native

##### 入参 eg

- callback: (apps: Apps) => void | Promise<void> 用于接收应用信息的回调函数
- extraPath: string 额外需要检索的文件夹

##### 出参 eg

异步迭代器

```js
let appNum = 0;
for await (const app of getSystemApp()) {
console.log(app);
appNum++;
}
console.log("Total", appNum, "App installed.");
```

app object:
Apps object:

```json
{
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "rubick-native",
"version": "0.0.14-beta",
"version": "0.0.14",
"type": "module",
"main": "dist/index.cjs",
"module": "dist/index.js",
Expand Down
6 changes: 4 additions & 2 deletions src/sysapp/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { shortcutWin } from "./windows"
import { platform } from "os"
import { exeLookBase64 } from "../../addon"

export type CallBack = (apps: Apps) => void | Promise<void>

export interface Apps {
name: string;
description: string;
Expand All @@ -11,10 +13,10 @@ export interface Apps {
}

// todo linux/macos
export const getSystemApp = (extraPath?: string[]) => {
export const getSystemApp = async (callback: CallBack, extraPath?: string[]) => {
switch (platform()) {
case "win32":
return shortcutWin(extraPath)
return await shortcutWin(callback, extraPath)

// case "linux":
// break;
Expand Down
77 changes: 37 additions & 40 deletions src/sysapp/windows.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,45 +2,42 @@ import { join, parse } from "path";
import { homedir } from "os"
import { fdir } from "fdir";
import { parseLnkFallback, parseLnk } from "../../addon"
import { Apps } from ".";
import { CallBack } from ".";

export const shortcutWin = (extraPath: string[] = []) => ({
[Symbol.asyncIterator]: async function* () {
const hdir = homedir()
const f = new fdir().glob("./**/*.lnk").withFullPaths()
const defaultPaths = [
join(process.env.ProgramData, "/Microsoft/Windows/Start Menu/Programs"),
join(process.env.AppData, "/Microsoft/Windows/Start Menu/Programs"),
join(process.env.PUBLIC, 'Desktop'),
join(hdir, 'Desktop'),
...extraPath
]
for (const p of defaultPaths) {
const o = await f.crawl(p).withPromise()
for (const t of o) {
const { name, dir } = parse(t)
try {
const data = parseLnk(t)
const d = JSON.parse(data)
yield ({
name,
description: d.name_string ?? null,
execPath: d.target_full_path,
shortCutPath: t,
workingDir: d.working_dir ?? null,
}) as Apps
} catch {
const d = parseLnkFallback(t)
const execPath = join(dir, d.relativePath ?? '')
yield ({
name,
description: d.nameString ?? null,
execPath,
shortCutPath: t,
workingDir: d.workingDir ?? null,
}) as Apps
}
export const shortcutWin = async (callback: CallBack, extraPath: string[] = []) => {
const hdir = homedir()
const f = new fdir().glob("./**/*.lnk").withFullPaths()
.filter((t) => {
const { name, dir } = parse(t)
try {
const data = parseLnk(t)
const d = JSON.parse(data)
callback({
name,
description: d.name_string ?? null,
execPath: d.target_full_path,
shortCutPath: t,
workingDir: d.working_dir ?? null,
})
} catch {
const d = parseLnkFallback(t)
const execPath = join(dir, d.relativePath ?? '')
callback({
name,
description: d.nameString ?? null,
execPath,
shortCutPath: t,
workingDir: d.workingDir ?? null,
})
}
}
}
})
return true
})
const defaultPaths = [
join(process.env.ProgramData, "/Microsoft/Windows/Start Menu/Programs"),
join(process.env.AppData, "/Microsoft/Windows/Start Menu/Programs"),
join(process.env.PUBLIC, 'Desktop'),
join(hdir, 'Desktop'),
...extraPath
]
await Promise.allSettled(defaultPaths.map(path => f.crawl(path).withPromise()))
}
11 changes: 8 additions & 3 deletions test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
import { sendKeyboardSimulation } from "./src"
sendKeyboardSimulation("{+sdfsd}")
console.log(1);
import { getSystemApp } from "./src"

console.time("1")
getSystemApp((e) => {
console.log(e);

})
console.timeEnd("1")

0 comments on commit 7c91428

Please sign in to comment.