@domicile-desktop/system-apps 0.0.0-alpha-b1041f8fca57 → 0.0.0-alpha-e612f0697e41

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "dependencies": {
3
3
  "@cprussin/option-result": "^2.0.1",
4
- "@domicile-desktop/sdk": "0.0.0-alpha-b1041f8fca57",
4
+ "@domicile-desktop/sdk": "0.0.0-alpha-e612f0697e41",
5
5
  "zod": "^4.5.4"
6
6
  },
7
7
  "description": "Installed applications and bookmarks for a Domicile shell's launcher, read through @domicile-desktop/sdk/system.",
@@ -54,5 +54,5 @@
54
54
  "test:unit": "bun test"
55
55
  },
56
56
  "type": "module",
57
- "version": "0.0.0-alpha-b1041f8fca57"
57
+ "version": "0.0.0-alpha-e612f0697e41"
58
58
  }
package/src/openers.ts CHANGED
@@ -13,15 +13,19 @@ import { desktopEnvironment, setVariable } from "./environment";
13
13
  import { commandOf } from "./exec";
14
14
  import { installedApps } from "./installed";
15
15
  import { defaultApps } from "./mime-apps";
16
+ import { allOk } from "./results";
17
+ import { supertypesOf } from "./supertypes";
16
18
 
17
19
  /** What can open a file. */
18
20
  export type Openers = {
19
21
  /** The file's content type, by name. Nothing when no glob matches. */
20
22
  contentType: Option<string>;
21
23
  /**
22
- * The installed applications for the type: its defaults from
23
- * `mimeapps.list` in their order (see `./mime-apps`), then the rest whose
24
- * `MimeType` lists it, by name. The first is the one to open it with.
24
+ * The installed applications for the type and its supertypes (see
25
+ * `./supertypes`), nearest first: every type's defaults from
26
+ * `mimeapps.list` in their order (see `./mime-apps`), then, type by type,
27
+ * the rest whose `MimeType` lists it, by name. The first is the one to
28
+ * open it with.
25
29
  */
26
30
  apps: DesktopEntry[];
27
31
  /** The argv that opens the file with `app`. */
@@ -32,7 +36,7 @@ export type Openers = {
32
36
  * What can open `path`: relative to home, as every system call's path is, or
33
37
  * absolute.
34
38
  *
35
- * Only `MimeType` and the defaults are read: aliases, subclasses and
39
+ * Only `MimeType`, the defaults and subclasses are read: aliases and
36
40
  * `mimeapps.list`'s added and removed associations are not. A launcher falls
37
41
  * back to `xdg-open` when this offers nothing.
38
42
  */
@@ -66,19 +70,26 @@ const appsFor = async (
66
70
  contentType: string,
67
71
  ): Promise<Result<DesktopEntry[], SystemError>> =>
68
72
  (await installedApps(domicile, dirs)).andThenAsync(async (installed) =>
69
- (await defaultApps(domicile, contentType)).map((defaults) => {
70
- const byId = new Map(installed.map((app) => [app.id, app]));
71
- const first = [...new Set(defaults)].flatMap((id) => {
72
- const app = byId.get(id);
73
- return app === undefined ? [] : [app];
74
- });
75
- const rest = installed
76
- .filter(
77
- (app) => app.mimeTypes.includes(contentType) && !first.includes(app),
78
- )
79
- .toSorted((a, b) => a.name.localeCompare(b.name));
80
- return [...first, ...rest];
81
- }),
73
+ (await supertypesOf(domicile, dirs, contentType)).andThenAsync(
74
+ async (supertypes) => {
75
+ const types = [contentType, ...supertypes];
76
+ return allOk(
77
+ await Promise.all(types.map((type) => defaultApps(domicile, type))),
78
+ ).map((defaults) => {
79
+ const byId = new Map(installed.map((app) => [app.id, app]));
80
+ const first = defaults.flat().flatMap((id) => {
81
+ const app = byId.get(id);
82
+ return app === undefined ? [] : [app];
83
+ });
84
+ const rest = types.flatMap((type) =>
85
+ installed
86
+ .filter((app) => app.mimeTypes.includes(type))
87
+ .toSorted((a, b) => a.name.localeCompare(b.name)),
88
+ );
89
+ return [...new Set([...first, ...rest])];
90
+ });
91
+ },
92
+ ),
82
93
  );
83
94
 
84
95
  const homeOf = (variables: ReadonlyMap<string, string>): string => {
@@ -0,0 +1,70 @@
1
+ // A content type's supertypes, from shared-mime-info's `mime/subclasses`. See
2
+ // https://specifications.freedesktop.org/shared-mime-info-spec/latest/.
3
+
4
+ import type { Result } from "@cprussin/option-result";
5
+ import type { SystemError } from "@domicile-desktop/sdk/system";
6
+ import type { AppsDesktop } from "./apps-desktop";
7
+
8
+ import { allOk, orAbsent } from "./results";
9
+
10
+ /** The type the spec makes every other text/* type a subclass of. */
11
+ const TEXT = "text/plain";
12
+
13
+ /** One `subclasses` line: a type and one of its parents. */
14
+ type Subclass = { type: string; parent: string };
15
+
16
+ /**
17
+ * `contentType`'s supertypes, nearest first, without `contentType` itself.
18
+ *
19
+ * Reads `mime/subclasses` under each of `dataDirs`, missing ones skipped, and
20
+ * follows each parent to its own. Every text/* type is also a text/plain.
21
+ */
22
+ export const supertypesOf = async (
23
+ domicile: AppsDesktop,
24
+ dataDirs: readonly string[],
25
+ contentType: string,
26
+ ): Promise<Result<string[], SystemError>> =>
27
+ allOk(
28
+ await Promise.all(
29
+ dataDirs.map(async (dir) =>
30
+ orAbsent(
31
+ await domicile.files.readText(`${dir}/mime/subclasses`),
32
+ "",
33
+ ).map(subclassesIn),
34
+ ),
35
+ ),
36
+ ).map((subclasses) =>
37
+ ancestorsOf(subclasses.flat(), [contentType], [contentType]).slice(1),
38
+ );
39
+
40
+ /** `seen`, then the parents of `level`'s types, then theirs, each once. */
41
+ const ancestorsOf = (
42
+ subclasses: readonly Subclass[],
43
+ level: readonly string[],
44
+ seen: readonly string[],
45
+ ): readonly string[] => {
46
+ const parents = [
47
+ ...new Set(level.flatMap((type) => parentsOf(subclasses, type))),
48
+ ].filter((type) => !seen.includes(type));
49
+ return parents.length === 0
50
+ ? seen
51
+ : ancestorsOf(subclasses, parents, [...seen, ...parents]);
52
+ };
53
+
54
+ /** `type`'s own parents: those `subclasses` lists, then text/plain for text. */
55
+ const parentsOf = (subclasses: readonly Subclass[], type: string): string[] => [
56
+ ...subclasses
57
+ .filter((subclass) => subclass.type === type)
58
+ .map(({ parent }) => parent),
59
+ ...(type.startsWith("text/") && type !== TEXT ? [TEXT] : []),
60
+ ];
61
+
62
+ /** The pairs `text` lists: `type parent`. */
63
+ const subclassesIn = (text: string): Subclass[] =>
64
+ text
65
+ .split("\n")
66
+ .filter((line) => line !== "")
67
+ .map((line) => {
68
+ const [type = "", parent = ""] = line.split(" ");
69
+ return { parent, type };
70
+ });