@gtkx/cli 2.0.0-beta.1 → 2.0.0-beta.2

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.
Files changed (165) hide show
  1. package/dist/builder.d.ts +1 -0
  2. package/dist/builder.d.ts.map +1 -1
  3. package/dist/builder.js +17 -5
  4. package/dist/builder.js.map +1 -1
  5. package/dist/cli.js +2 -0
  6. package/dist/cli.js.map +1 -1
  7. package/dist/codegen/run-codegen.d.ts +4 -2
  8. package/dist/codegen/run-codegen.d.ts.map +1 -1
  9. package/dist/codegen/run-codegen.js +13 -8
  10. package/dist/codegen/run-codegen.js.map +1 -1
  11. package/dist/codegen/store-resolver.d.ts +1 -1
  12. package/dist/codegen/store-resolver.d.ts.map +1 -1
  13. package/dist/codegen/store-resolver.js +2 -2
  14. package/dist/codegen/store-resolver.js.map +1 -1
  15. package/dist/commands/build.d.ts +5 -0
  16. package/dist/commands/build.d.ts.map +1 -1
  17. package/dist/commands/build.js +83 -10
  18. package/dist/commands/build.js.map +1 -1
  19. package/dist/commands/codegen.d.ts +1 -0
  20. package/dist/commands/codegen.d.ts.map +1 -1
  21. package/dist/commands/codegen.js +6 -5
  22. package/dist/commands/codegen.js.map +1 -1
  23. package/dist/commands/deploy.d.ts +1 -0
  24. package/dist/commands/deploy.d.ts.map +1 -1
  25. package/dist/commands/deploy.js +4 -2
  26. package/dist/commands/deploy.js.map +1 -1
  27. package/dist/commands/dev.d.ts +8 -0
  28. package/dist/commands/dev.d.ts.map +1 -1
  29. package/dist/commands/dev.js +19 -1
  30. package/dist/commands/dev.js.map +1 -1
  31. package/dist/deploy/freedesktop/metainfo.d.ts.map +1 -1
  32. package/dist/deploy/freedesktop/metainfo.js +41 -2
  33. package/dist/deploy/freedesktop/metainfo.js.map +1 -1
  34. package/dist/deploy/nfpm/config.d.ts.map +1 -1
  35. package/dist/deploy/nfpm/config.js +5 -2
  36. package/dist/deploy/nfpm/config.js.map +1 -1
  37. package/dist/deploy/nfpm/contents.d.ts.map +1 -1
  38. package/dist/deploy/nfpm/contents.js +8 -0
  39. package/dist/deploy/nfpm/contents.js.map +1 -1
  40. package/dist/deploy/node-runtime/elf.d.ts +2 -1
  41. package/dist/deploy/node-runtime/elf.d.ts.map +1 -1
  42. package/dist/deploy/node-runtime/elf.js +32 -5
  43. package/dist/deploy/node-runtime/elf.js.map +1 -1
  44. package/dist/deploy/node-runtime/index.d.ts +2 -1
  45. package/dist/deploy/node-runtime/index.d.ts.map +1 -1
  46. package/dist/deploy/node-runtime/index.js +73 -4
  47. package/dist/deploy/node-runtime/index.js.map +1 -1
  48. package/dist/deploy/notices/node-runtime.js +2 -2
  49. package/dist/deploy/notices/node-runtime.js.map +1 -1
  50. package/dist/deploy/payload/launcher.d.ts.map +1 -1
  51. package/dist/deploy/payload/launcher.js +14 -1
  52. package/dist/deploy/payload/launcher.js.map +1 -1
  53. package/dist/deploy/payload/stage.js +1 -1
  54. package/dist/deploy/payload/stage.js.map +1 -1
  55. package/dist/deploy/run-deploy.d.ts +1 -0
  56. package/dist/deploy/run-deploy.d.ts.map +1 -1
  57. package/dist/deploy/run-deploy.js +100 -14
  58. package/dist/deploy/run-deploy.js.map +1 -1
  59. package/dist/deploy/settings/build-manifest.d.ts +6 -1
  60. package/dist/deploy/settings/build-manifest.d.ts.map +1 -1
  61. package/dist/deploy/settings/build-manifest.js +15 -4
  62. package/dist/deploy/settings/build-manifest.js.map +1 -1
  63. package/dist/deploy/settings/extra-files.d.ts +1 -1
  64. package/dist/deploy/settings/extra-files.d.ts.map +1 -1
  65. package/dist/deploy/settings/extra-files.js +22 -1
  66. package/dist/deploy/settings/extra-files.js.map +1 -1
  67. package/dist/deploy/settings/index.d.ts +1 -0
  68. package/dist/deploy/settings/index.d.ts.map +1 -1
  69. package/dist/deploy/settings/index.js +2 -1
  70. package/dist/deploy/settings/index.js.map +1 -1
  71. package/dist/deploy/settings/paths.d.ts +2 -1
  72. package/dist/deploy/settings/paths.d.ts.map +1 -1
  73. package/dist/deploy/settings/paths.js +113 -5
  74. package/dist/deploy/settings/paths.js.map +1 -1
  75. package/dist/deploy/targets/flatpak-source.d.ts.map +1 -1
  76. package/dist/deploy/targets/flatpak-source.js +11 -2
  77. package/dist/deploy/targets/flatpak-source.js.map +1 -1
  78. package/dist/deploy/types.d.ts +1 -0
  79. package/dist/deploy/types.d.ts.map +1 -1
  80. package/dist/deploy/types.js.map +1 -1
  81. package/dist/dev/headless.d.ts +3 -0
  82. package/dist/dev/headless.d.ts.map +1 -0
  83. package/dist/dev/headless.js +35 -0
  84. package/dist/dev/headless.js.map +1 -0
  85. package/dist/dev/runner-deps.js +1 -1
  86. package/dist/dev/runner-deps.js.map +1 -1
  87. package/dist/i18n/source-messages.d.ts.map +1 -1
  88. package/dist/i18n/source-messages.js +835 -1
  89. package/dist/i18n/source-messages.js.map +1 -1
  90. package/dist/internal/build-manifest.d.ts +5 -2
  91. package/dist/internal/build-manifest.d.ts.map +1 -1
  92. package/dist/internal/build-manifest.js +15 -2
  93. package/dist/internal/build-manifest.js.map +1 -1
  94. package/dist/internal/build-output.d.ts +8 -0
  95. package/dist/internal/build-output.d.ts.map +1 -0
  96. package/dist/internal/build-output.js +69 -0
  97. package/dist/internal/build-output.js.map +1 -0
  98. package/dist/internal/entry-arg.d.ts +4 -1
  99. package/dist/internal/entry-arg.d.ts.map +1 -1
  100. package/dist/internal/entry-arg.js +7 -1
  101. package/dist/internal/entry-arg.js.map +1 -1
  102. package/dist/internal/output-directory.d.ts +15 -0
  103. package/dist/internal/output-directory.d.ts.map +1 -0
  104. package/dist/internal/output-directory.js +490 -0
  105. package/dist/internal/output-directory.js.map +1 -0
  106. package/dist/internal/prepare-project.d.ts +2 -0
  107. package/dist/internal/prepare-project.d.ts.map +1 -1
  108. package/dist/internal/prepare-project.js +3 -3
  109. package/dist/internal/prepare-project.js.map +1 -1
  110. package/dist/internal/source-imports.js +1 -1
  111. package/dist/internal/source-imports.js.map +1 -1
  112. package/dist/mcp/client.js +2 -2
  113. package/dist/mcp/client.js.map +1 -1
  114. package/dist/vite-plugins/build-manifest.d.ts +5 -1
  115. package/dist/vite-plugins/build-manifest.d.ts.map +1 -1
  116. package/dist/vite-plugins/build-manifest.js +2 -1
  117. package/dist/vite-plugins/build-manifest.js.map +1 -1
  118. package/dist/vite-plugins/i18n.d.ts.map +1 -1
  119. package/dist/vite-plugins/i18n.js +58 -6
  120. package/dist/vite-plugins/i18n.js.map +1 -1
  121. package/dist/vite-plugins/index.d.ts +8 -1
  122. package/dist/vite-plugins/index.d.ts.map +1 -1
  123. package/dist/vite-plugins/index.js +6 -2
  124. package/dist/vite-plugins/index.js.map +1 -1
  125. package/dist/vite-plugins/self-contained.d.ts.map +1 -1
  126. package/dist/vite-plugins/self-contained.js +159 -158
  127. package/dist/vite-plugins/self-contained.js.map +1 -1
  128. package/package.json +9 -9
  129. package/src/builder.ts +18 -5
  130. package/src/cli.ts +3 -0
  131. package/src/codegen/run-codegen.ts +15 -7
  132. package/src/codegen/store-resolver.ts +2 -2
  133. package/src/commands/build.ts +13 -2
  134. package/src/commands/codegen.ts +6 -5
  135. package/src/commands/deploy.ts +4 -2
  136. package/src/commands/dev.ts +20 -1
  137. package/src/deploy/freedesktop/metainfo.ts +73 -21
  138. package/src/deploy/nfpm/config.ts +5 -2
  139. package/src/deploy/nfpm/contents.ts +8 -0
  140. package/src/deploy/node-runtime/elf.ts +43 -5
  141. package/src/deploy/node-runtime/index.ts +105 -5
  142. package/src/deploy/notices/node-runtime.ts +2 -2
  143. package/src/deploy/payload/launcher.ts +20 -1
  144. package/src/deploy/payload/stage.ts +1 -1
  145. package/src/deploy/run-deploy.ts +56 -16
  146. package/src/deploy/settings/build-manifest.ts +28 -3
  147. package/src/deploy/settings/extra-files.ts +26 -2
  148. package/src/deploy/settings/index.ts +3 -1
  149. package/src/deploy/settings/paths.ts +70 -5
  150. package/src/deploy/targets/flatpak-source.ts +13 -2
  151. package/src/deploy/types.ts +1 -0
  152. package/src/dev/headless.ts +40 -0
  153. package/src/dev/runner-deps.ts +1 -1
  154. package/src/i18n/source-messages.ts +1220 -2
  155. package/src/internal/build-manifest.ts +25 -1
  156. package/src/internal/build-output.ts +99 -0
  157. package/src/internal/entry-arg.ts +8 -1
  158. package/src/internal/output-directory.ts +699 -0
  159. package/src/internal/prepare-project.ts +5 -5
  160. package/src/internal/source-imports.ts +1 -1
  161. package/src/mcp/client.ts +2 -2
  162. package/src/vite-plugins/build-manifest.ts +7 -1
  163. package/src/vite-plugins/i18n.ts +86 -5
  164. package/src/vite-plugins/index.ts +14 -7
  165. package/src/vite-plugins/self-contained.ts +184 -185
@@ -1,7 +1,8 @@
1
1
  import { info } from "@gtkx/utils";
2
2
  import { defineCommand } from "citty";
3
3
  import { build as buildApp } from "../builder.js";
4
- import { entryArg } from "../internal/entry-arg.js";
4
+ import { prepareBuildOutDir, resolveBuildOutDir } from "../internal/build-output.js";
5
+ import { configArg, entryArg } from "../internal/entry-arg.js";
5
6
  import { prepareProject } from "../internal/prepare-project.js";
6
7
 
7
8
  const BUILD_MODE = "production";
@@ -13,18 +14,28 @@ const build = defineCommand({
13
14
  },
14
15
  args: {
15
16
  ...entryArg,
17
+ ...configArg,
18
+ out: {
19
+ type: "string",
20
+ description: "Output directory below the project root (default: dist)",
21
+ },
16
22
  },
17
23
  async run({ args }) {
18
- const { cwd, entry } = await prepareProject(args, BUILD_MODE);
24
+ const { cwd, entry, configFile } = await prepareProject(args, BUILD_MODE);
25
+ const outDir = resolveBuildOutDir(cwd, args.out);
26
+ using preparedOutput = prepareBuildOutDir(cwd, outDir);
19
27
  info(`Building ${entry}`);
20
28
 
21
29
  const bundlePath = await buildApp({
22
30
  entry,
31
+ configFile,
23
32
  vite: {
24
33
  root: cwd,
34
+ build: { outDir: preparedOutput.path, emptyOutDir: false },
25
35
  },
26
36
  });
27
37
 
38
+ preparedOutput.commit();
28
39
  info(`Build complete: ${bundlePath}`);
29
40
  },
30
41
  });
@@ -2,7 +2,7 @@ import { info } from "@gtkx/utils";
2
2
  import { defineCommand } from "citty";
3
3
  import { formatCodegenResult } from "../codegen/report.js";
4
4
  import { ensureGenerated, isCodegenDisabled, runCodegen } from "../codegen/run-codegen.js";
5
- import { cwdArg, resolveCwd } from "../internal/entry-arg.js";
5
+ import { configArg, cwdArg, resolveCwd } from "../internal/entry-arg.js";
6
6
 
7
7
  const FORCED_WHILE_DISABLED_MESSAGE =
8
8
  "codegen is disabled for this project, so --force has no store to regenerate here. " +
@@ -20,29 +20,30 @@ const codegen = defineCommand({
20
20
  description: "Wipe the generated store and regenerate unconditionally (recover a corrupted store)",
21
21
  default: false,
22
22
  },
23
+ ...configArg,
23
24
  ...cwdArg,
24
25
  },
25
26
  async run({ args }) {
26
27
  const cwd = resolveCwd(args);
27
- const isDisabled = await isCodegenDisabled(cwd);
28
+ const isDisabled = await isCodegenDisabled(cwd, undefined, args.config);
28
29
  checkForce(isDisabled, args.force);
29
30
 
30
31
  if (isDisabled) {
31
- await runCodegen({ cwd });
32
+ await runCodegen({ cwd, configFile: args.config });
32
33
  info("codegen: disabled for this project; reusing an installed binding store");
33
34
 
34
35
  return;
35
36
  }
36
37
 
37
38
  if (!args.force) {
38
- const isRan = await ensureGenerated(cwd);
39
+ const isRan = await ensureGenerated(cwd, { configFile: args.config });
39
40
  info(isRan ? "codegen: regenerated stale bindings" : "codegen: bindings up to date");
40
41
 
41
42
  return;
42
43
  }
43
44
 
44
45
  const startedAt = Date.now();
45
- const result = await runCodegen({ cwd, isForced: true });
46
+ const result = await runCodegen({ cwd, configFile: args.config, isForced: true });
46
47
  const lines = formatCodegenResult(result, Date.now() - startedAt);
47
48
 
48
49
  for (const line of lines) {
@@ -1,7 +1,7 @@
1
1
  import { defineCommand } from "citty";
2
2
  import { KNOWN_NAMES } from "../deploy/registry.js";
3
3
  import { runDeploy } from "../deploy/run-deploy.js";
4
- import { entryArg } from "../internal/entry-arg.js";
4
+ import { configArg, entryArg } from "../internal/entry-arg.js";
5
5
  import { resolveProject } from "../internal/prepare-project.js";
6
6
 
7
7
  const DEPLOY_MODE = "production";
@@ -13,6 +13,7 @@ const deploy = defineCommand({
13
13
  },
14
14
  args: {
15
15
  ...entryArg,
16
+ ...configArg,
16
17
  target: {
17
18
  type: "string",
18
19
  description: `Comma-separated package formats to build (${KNOWN_NAMES})`,
@@ -31,11 +32,12 @@ const deploy = defineCommand({
31
32
  },
32
33
  },
33
34
  async run({ args }) {
34
- const { cwd, entry } = await resolveProject(args, DEPLOY_MODE);
35
+ const { cwd, entry, configFile } = await resolveProject(args, DEPLOY_MODE);
35
36
 
36
37
  await runDeploy({
37
38
  entry,
38
39
  cwd,
40
+ configFile,
39
41
  targets: args.target,
40
42
  outDir: args.out,
41
43
  shouldPrintManifests: args["print-manifests"] === true,
@@ -1,5 +1,6 @@
1
1
  import { defineCommand } from "citty";
2
2
  import { resolveConfigWatch } from "../codegen/run-codegen.js";
3
+ import { startHeadlessDevDisplay } from "../dev/headless.js";
3
4
  import { type DevWatch, runDevSupervisor } from "../dev/supervisor.js";
4
5
  import { splitApplicationArgs } from "../internal/application-args.js";
5
6
  import { entryArg } from "../internal/entry-arg.js";
@@ -14,12 +15,30 @@ const dev = defineCommand({
14
15
  },
15
16
  args: {
16
17
  ...entryArg,
18
+ headless: {
19
+ type: "boolean",
20
+ description: "Run on an isolated headless Wayland display",
21
+ },
22
+ size: {
23
+ type: "string",
24
+ description: "Headless display size as WIDTHxHEIGHT",
25
+ },
17
26
  },
18
27
  async run({ args }) {
28
+ if (args.size !== undefined && !args.headless) {
29
+ throw new Error("--size requires --headless");
30
+ }
31
+
19
32
  const { cwd, entry: entryPath } = await prepareProject(args, DEV_MODE);
20
33
  const watch: DevWatch | undefined = await resolveConfigWatch(cwd, DEV_MODE);
21
34
  const { applicationArgs } = splitApplicationArgs(process.argv.slice(2));
22
- await runDevSupervisor({ entryPath, cwd, args: applicationArgs, watch });
35
+ const stopHeadless = args.headless ? await startHeadlessDevDisplay(args.size) : undefined;
36
+
37
+ try {
38
+ await runDevSupervisor({ entryPath, cwd, args: applicationArgs, watch });
39
+ } finally {
40
+ stopHeadless?.();
41
+ }
23
42
  },
24
43
  });
25
44
 
@@ -14,6 +14,54 @@ import { element, renderDocument, text, type XmlNode } from "./xml.js";
14
14
 
15
15
  const COMPONENT_TYPE = "desktop-application";
16
16
  const DESKTOP_SUFFIX = ".desktop";
17
+ const COMPONENT_CLOSE = "</component>\n";
18
+ const PROVIDES_CLOSE = " </provides>\n";
19
+ const PROVIDES_FRAGMENT = /^<provides\s*>([\s\S]*)<\/provides>$/;
20
+
21
+ const normalizeExtraFragment = (fragment: string, index: number): string => {
22
+ const normalized = fragment.trim();
23
+
24
+ if (normalized.length === 0) {
25
+ throw new Error(`Cannot render deploy.metainfoExtra entry ${String(index + 1)}: it is empty`);
26
+ }
27
+
28
+ return normalized;
29
+ };
30
+
31
+ const indentFragment = (fragment: string, depth: number): string => {
32
+ const indentation = " ".repeat(depth * 4);
33
+
34
+ return fragment.split(/\r?\n/).map((line) => `${indentation}${line}`).join("\n");
35
+ };
36
+
37
+ const appendExtraFragments = (document: string, fragments: string[]): string => {
38
+ if (fragments.length === 0) {
39
+ return document;
40
+ }
41
+
42
+ let merged = document;
43
+ const componentFragments: string[] = [];
44
+
45
+ for (const [index, rawFragment] of fragments.entries()) {
46
+ const fragment = normalizeExtraFragment(rawFragment, index);
47
+ const provides = PROVIDES_FRAGMENT.exec(fragment)?.[1]?.trim();
48
+
49
+ if (provides === undefined) {
50
+ componentFragments.push(fragment);
51
+ continue;
52
+ }
53
+
54
+ merged = merged.replace(PROVIDES_CLOSE, () => `${indentFragment(provides, 2)}\n${PROVIDES_CLOSE}`);
55
+ }
56
+
57
+ if (componentFragments.length === 0) {
58
+ return merged;
59
+ }
60
+
61
+ const extra = componentFragments.map((fragment) => indentFragment(fragment, 1)).join("\n");
62
+
63
+ return merged.replace(COMPONENT_CLOSE, () => `${extra}\n${COMPONENT_CLOSE}`);
64
+ };
17
65
 
18
66
  const launchable = (applicationId: string): XmlNode => ({
19
67
  tag: "launchable",
@@ -21,26 +69,30 @@ const launchable = (applicationId: string): XmlNode => ({
21
69
  text: `${applicationId}${DESKTOP_SUFFIX}`,
22
70
  });
23
71
 
24
- const renderMetainfo = (settings: DeploySettings): string =>
25
- renderDocument(
26
- element("component", { type: COMPONENT_TYPE }, [
27
- text("id", settings.applicationId),
28
- text("name", settings.name),
29
- text("summary", settings.summary),
30
- text("metadata_license", settings.metadataLicense),
31
- text("project_license", settings.license),
32
- ...developerSection(settings),
33
- ...descriptionSection(settings),
34
- launchable(settings.applicationId),
35
- ...providesSection(settings),
36
- ...listSection("categories", "category", settings.categories),
37
- ...listSection("keywords", "keyword", settings.keywords),
38
- ...urlsSection(settings),
39
- ...screenshotsSection(settings),
40
- ...releasesSection(settings),
41
- ...contentRatingSection(settings),
42
- ...brandingSection(settings),
43
- ]),
44
- );
72
+ const metainfoRoot = (settings: DeploySettings): XmlNode =>
73
+ element("component", { type: COMPONENT_TYPE }, [
74
+ text("id", settings.applicationId),
75
+ text("name", settings.name),
76
+ text("summary", settings.summary),
77
+ text("metadata_license", settings.metadataLicense),
78
+ text("project_license", settings.license),
79
+ ...developerSection(settings),
80
+ ...descriptionSection(settings),
81
+ launchable(settings.applicationId),
82
+ ...providesSection(settings),
83
+ ...listSection("categories", "category", settings.categories),
84
+ ...listSection("keywords", "keyword", settings.keywords),
85
+ ...urlsSection(settings),
86
+ ...screenshotsSection(settings),
87
+ ...releasesSection(settings),
88
+ ...contentRatingSection(settings),
89
+ ...brandingSection(settings),
90
+ ]);
91
+
92
+ const renderMetainfo = (settings: DeploySettings): string => {
93
+ const document = renderDocument(metainfoRoot(settings));
94
+
95
+ return appendExtraFragments(document, settings.deploy.metainfoExtra ?? []);
96
+ };
45
97
 
46
98
  export { renderMetainfo };
@@ -1,5 +1,6 @@
1
1
  import type { DeployPayload, DeploySettings, StagedFile } from "../types.js";
2
2
  import { resolveDepends } from "../depends.js";
3
+ import { glibcMinimumForFiles } from "../node-runtime/elf.js";
3
4
  import { groupForCategories, sectionForCategories } from "../settings/categories.js";
4
5
  import { nfpmContents } from "./contents.js";
5
6
  import { debDescription, rpmDescription } from "./description.js";
@@ -125,7 +126,9 @@ const descriptionFor = (settings: DeploySettings, packager: NfpmPackager): strin
125
126
  const renderNfpmConfig = (payload: DeployPayload, packager: NfpmPackager): NfpmConfig => {
126
127
  const settings = payload.settings;
127
128
  const isRpm = packager === "rpm";
128
- const depends = resolveDepends(settings, payload.node?.glibcMinimum ?? null);
129
+ const staged = stagedFilesFor(payload, packager);
130
+ const glibcMinimum = glibcMinimumForFiles(staged.map((file) => file.abs));
131
+ const depends = resolveDepends(settings, glibcMinimum);
129
132
 
130
133
  return {
131
134
  name: packageNameFor(settings, packager),
@@ -146,7 +149,7 @@ const renderNfpmConfig = (payload: DeployPayload, packager: NfpmPackager): NfpmC
146
149
  ...relationsFor(settings, packager),
147
150
  ...optional("scripts", scriptsFor(settings)),
148
151
  [packager]: packagerSettings(settings, packager),
149
- contents: nfpmContents(PREFIX, stagedFilesFor(payload, packager), isRpm),
152
+ contents: nfpmContents(PREFIX, staged, isRpm),
150
153
  };
151
154
  };
152
155
 
@@ -13,10 +13,16 @@ const FILESYSTEM_OWNED_PREFIXES = new Set([
13
13
  "/usr",
14
14
  "/usr/bin",
15
15
  "/usr/lib",
16
+ "/usr/lib/systemd",
17
+ "/usr/lib/systemd/system",
18
+ "/usr/lib/systemd/user",
19
+ "/usr/libexec",
16
20
  "/usr/share",
17
21
  "/usr/share/applications",
18
22
  "/usr/share/dbus-1",
19
23
  "/usr/share/dbus-1/services",
24
+ "/usr/share/dbus-1/system-services",
25
+ "/usr/share/dbus-1/system.d",
20
26
  "/usr/share/doc",
21
27
  "/usr/share/glib-2.0",
22
28
  "/usr/share/glib-2.0/schemas",
@@ -25,6 +31,8 @@ const FILESYSTEM_OWNED_PREFIXES = new Set([
25
31
  "/usr/share/metainfo",
26
32
  "/usr/share/mime",
27
33
  "/usr/share/mime/packages",
34
+ "/usr/share/polkit-1",
35
+ "/usr/share/polkit-1/actions",
28
36
  ]);
29
37
 
30
38
  const ICON_THEME_PREFIX = "/usr/share/icons/";
@@ -1,4 +1,4 @@
1
- import { readFileSync } from "node:fs";
1
+ import { closeSync, openSync, readFileSync, readSync } from "node:fs";
2
2
 
3
3
  type ElfSection = {
4
4
  name: string;
@@ -17,6 +17,7 @@ type DynamicEntry = {
17
17
  };
18
18
 
19
19
  const ELF_MAGIC = "ELF";
20
+ const ELF_MAGIC_SIZE = 4;
20
21
  const CLASS_64 = 2;
21
22
  const LITTLE_ENDIAN = 1;
22
23
  const OFFSET_CLASS = 4;
@@ -35,8 +36,11 @@ const DYNAMIC_VALUE_OFFSET = 8;
35
36
  const GLIBC_VERSION = /GLIBC_(\d+)\.(\d+)/g;
36
37
  const VERSION_RADIX = 1000;
37
38
 
39
+ const hasElfMagic = (buffer: Buffer): boolean =>
40
+ buffer.length >= OFFSET_CLASS && buffer.subarray(0, 4).toString("latin1") === ELF_MAGIC;
41
+
38
42
  const assertElf64 = (buffer: Buffer, path: string): void => {
39
- if (buffer.length < OFFSET_SECTION_NAME_INDEX || buffer.subarray(0, 4).toString("latin1") !== ELF_MAGIC) {
43
+ if (buffer.length < OFFSET_SECTION_NAME_INDEX || !hasElfMagic(buffer)) {
40
44
  throw new Error(`Cannot read ${path}: it is not an ELF binary`);
41
45
  }
42
46
 
@@ -130,8 +134,7 @@ const readGlibcMinimum = (strings: Buffer): string | null => {
130
134
  const stringTable = (buffer: Buffer, dynstr: ElfSection | undefined): Buffer =>
131
135
  dynstr === undefined ? Buffer.alloc(0) : buffer.subarray(dynstr.offset, dynstr.offset + dynstr.size);
132
136
 
133
- const readElfInfo = (path: string): ElfInfo => {
134
- const buffer = readFileSync(path);
137
+ const readElfBuffer = (buffer: Buffer, path: string): ElfInfo => {
135
138
  assertElf64(buffer, path);
136
139
  const sections = readSections(buffer);
137
140
  const strings = stringTable(buffer, sectionFor(sections, ".dynstr"));
@@ -142,4 +145,39 @@ const readElfInfo = (path: string): ElfInfo => {
142
145
  };
143
146
  };
144
147
 
145
- export { type ElfInfo, readElfInfo };
148
+ const readElfInfo = (path: string): ElfInfo => readElfBuffer(readFileSync(path), path);
149
+
150
+ const readFilePrefix = (descriptor: number, size: number): Buffer => {
151
+ const buffer = Buffer.alloc(size);
152
+ const bytesRead = readSync(descriptor, buffer, 0, size, 0);
153
+
154
+ return buffer.subarray(0, bytesRead);
155
+ };
156
+
157
+ const readOptionalElfInfo = (path: string): ElfInfo | null => {
158
+ const descriptor = openSync(path, "r");
159
+
160
+ try {
161
+ const prefix = readFilePrefix(descriptor, ELF_MAGIC_SIZE);
162
+
163
+ return hasElfMagic(prefix) ? readElfBuffer(readFileSync(descriptor), path) : null;
164
+ } finally {
165
+ closeSync(descriptor);
166
+ }
167
+ };
168
+
169
+ const glibcMinimumForFiles = (paths: Iterable<string>): string | null => {
170
+ const versions: number[][] = [];
171
+
172
+ for (const path of paths) {
173
+ const minimum = readOptionalElfInfo(path)?.glibcMinimum;
174
+
175
+ if (minimum !== undefined && minimum !== null) {
176
+ versions.push(minimum.split(".").map(Number));
177
+ }
178
+ }
179
+
180
+ return formatVersion(highestVersion(versions));
181
+ };
182
+
183
+ export { type ElfInfo, glibcMinimumForFiles, readElfInfo };
@@ -1,4 +1,6 @@
1
+ import { MINIMUM_NODE_VERSION } from "@gtkx/config/internal";
1
2
  import { info, tryResolveExecutable } from "@gtkx/utils";
3
+ import { execFileSync } from "node:child_process";
2
4
  import { chmodSync, copyFileSync, mkdirSync, statSync } from "node:fs";
3
5
  import { join, resolve } from "node:path";
4
6
  import type { DeploySettings, NodeRuntime } from "../types.js";
@@ -13,13 +15,92 @@ type StagedRuntime = {
13
15
  licenseFile: string | null;
14
16
  };
15
17
 
18
+ type NodeSource = "download" | "host" | "path";
19
+
16
20
  const BYTES_PER_MIB = 1024 * 1024;
17
21
  const EXECUTABLE_MODE = 0o755;
18
22
  const NODE_FILENAME = "node";
23
+ const NODE_VERSION_PATTERN = /^v?(\d+)\.(\d+)\.(\d+)$/;
24
+ const VERSION_PROBE_TIMEOUT_MS = 10_000;
25
+ const VERSION_PROBE_MAX_BUFFER = 1024;
26
+
27
+ const nodeSourceFor = (settings: DeploySettings): NodeSource => settings.deploy.node?.source ?? "download";
28
+
29
+ const parseNodeVersion = (value: string, subject: string): { parts: number[]; version: string } => {
30
+ const match = NODE_VERSION_PATTERN.exec(value.trim());
31
+
32
+ if (match === null) {
33
+ throw new Error(`Cannot determine the Node.js version for ${subject}: expected a version such as 26.7.0`);
34
+ }
35
+
36
+ const parts = match.slice(1).map(Number);
37
+
38
+ return { parts, version: parts.map(String).join(".") };
39
+ };
40
+
41
+ const compareVersions = (left: number[], right: number[]): number => {
42
+ for (let index = 0; index < Math.max(left.length, right.length); index += 1) {
43
+ const difference = (left[index] ?? 0) - (right[index] ?? 0);
44
+
45
+ if (difference !== 0) {
46
+ return difference;
47
+ }
48
+ }
49
+
50
+ return 0;
51
+ };
52
+
53
+ const supportedNodeVersion = (value: string, subject: string): string => {
54
+ const parsed = parseNodeVersion(value, subject);
55
+ const minimum = parseNodeVersion(MINIMUM_NODE_VERSION, "GTKX");
56
+
57
+ if (compareVersions(parsed.parts, minimum.parts) < 0) {
58
+ throw new Error(
59
+ `Cannot bundle Node.js ${parsed.version} for ${subject}: GTKX requires ${MINIMUM_NODE_VERSION} or newer`,
60
+ );
61
+ }
62
+
63
+ return parsed.version;
64
+ };
65
+
66
+ const probeNodeVersion = (path: string): string => {
67
+ try {
68
+ const output = execFileSync(path, ["--version"], {
69
+ encoding: "utf8",
70
+ env: { ...process.env, NODE_OPTIONS: "" },
71
+ maxBuffer: VERSION_PROBE_MAX_BUFFER,
72
+ timeout: VERSION_PROBE_TIMEOUT_MS,
73
+ });
74
+
75
+ return supportedNodeVersion(output, path);
76
+ } catch (error) {
77
+ if (error instanceof Error && error.message.startsWith("Cannot bundle Node.js")) {
78
+ throw error;
79
+ }
80
+
81
+ throw new Error(`Cannot determine the Node.js version from ${path}`, { cause: error });
82
+ }
83
+ };
84
+
85
+ const assertExpectedVersion = (configured: string | undefined, actual: string, source: NodeSource): string => {
86
+ if (configured === undefined) {
87
+ return actual;
88
+ }
89
+
90
+ const expected = parseNodeVersion(configured, `deploy.node.source: "${source}"`).version;
91
+
92
+ if (expected !== actual) {
93
+ throw new Error(
94
+ `The Node.js runtime from deploy.node.source "${source}" is ${actual}, not the configured ${expected}`,
95
+ );
96
+ }
97
+
98
+ return actual;
99
+ };
19
100
 
20
101
  const sourcePathFor = (settings: DeploySettings): string => {
21
102
  const node = settings.deploy.node ?? {};
22
- const source = node.source ?? "download";
103
+ const source = nodeSourceFor(settings);
23
104
 
24
105
  if (source === "host") {
25
106
  return process.execPath;
@@ -32,6 +113,21 @@ const sourcePathFor = (settings: DeploySettings): string => {
32
113
  return resolve(settings.paths.root, node.path);
33
114
  };
34
115
 
116
+ const resolveNodeVersion = (settings: DeploySettings): string => {
117
+ const node = settings.deploy.node ?? {};
118
+ const source = nodeSourceFor(settings);
119
+
120
+ if (source === "download") {
121
+ return supportedNodeVersion(node.version ?? MINIMUM_NODE_VERSION, 'deploy.node.source: "download"');
122
+ }
123
+
124
+ const actual = source === "host"
125
+ ? supportedNodeVersion(process.versions.node, 'deploy.node.source: "host"')
126
+ : probeNodeVersion(sourcePathFor(settings));
127
+
128
+ return assertExpectedVersion(node.version, actual, source);
129
+ };
130
+
35
131
  const didStripBinary = (path: string): boolean => {
36
132
  if (tryResolveExecutable("strip") === undefined) {
37
133
  return false;
@@ -60,7 +156,11 @@ const stageDownloadedNode = async (settings: DeploySettings, version: string): P
60
156
  return downloaded;
61
157
  };
62
158
 
63
- const stageFromSource = async (settings: DeploySettings, version: string, source: string): Promise<StagedRuntime> => {
159
+ const stageFromSource = async (
160
+ settings: DeploySettings,
161
+ version: string,
162
+ source: NodeSource,
163
+ ): Promise<StagedRuntime> => {
64
164
  if (source === "download") {
65
165
  return stageDownloadedNode(settings, version);
66
166
  }
@@ -73,8 +173,8 @@ const stageFromSource = async (settings: DeploySettings, version: string, source
73
173
 
74
174
  const resolveNodeRuntime = async (settings: DeploySettings): Promise<NodeRuntime> => {
75
175
  const node = settings.deploy.node ?? {};
76
- const source = node.source ?? "download";
77
- const version = node.version ?? process.versions.node;
176
+ const source = nodeSourceFor(settings);
177
+ const version = resolveNodeVersion(settings);
78
178
  const staged = await stageFromSource(settings, version, source);
79
179
  const isStripped = node.shouldStrip === false ? false : didStripBinary(staged.path);
80
180
  const elf = readElfInfo(staged.path);
@@ -83,4 +183,4 @@ const resolveNodeRuntime = async (settings: DeploySettings): Promise<NodeRuntime
83
183
  return { ...staged, version, glibcMinimum: elf.glibcMinimum, isStripped };
84
184
  };
85
185
 
86
- export { resolveNodeRuntime, sourcePathFor };
186
+ export { resolveNodeRuntime, resolveNodeVersion, sourcePathFor };
@@ -1,6 +1,6 @@
1
1
  import { warn } from "@gtkx/utils";
2
2
  import type { DeploySettings, NodeRuntime, NoticeSection } from "../types.js";
3
- import { sourcePathFor } from "../node-runtime/index.js";
3
+ import { resolveNodeVersion, sourcePathFor } from "../node-runtime/index.js";
4
4
  import { licenseBesideNode } from "../node-runtime/license.js";
5
5
  import { NODE_FILENAME } from "../payload/launcher.js";
6
6
  import { nodeLicenseDestination } from "../payload/stage.js";
@@ -32,7 +32,7 @@ const MISSING_LICENSE =
32
32
  const isSourceMode = (settings: DeploySettings): boolean => settings.deploy.flatpak?.mode === "source";
33
33
 
34
34
  const versionFor = (settings: DeploySettings, node: NodeRuntime | null): string =>
35
- node?.version ?? settings.deploy.node?.version ?? process.versions.node;
35
+ node?.version ?? resolveNodeVersion(settings);
36
36
 
37
37
  const licenseFileFor = (settings: DeploySettings, node: NodeRuntime | null): string | null => {
38
38
  if (node !== null) {
@@ -2,6 +2,8 @@ import type { DeploySettings } from "../types.js";
2
2
  import { BUNDLE_FILENAME } from "../../vite-plugins/esm-extension.js";
3
3
 
4
4
  const NODE_FILENAME = "node";
5
+ const SHELL_SINGLE_QUOTE = "'";
6
+ const SHELL_SINGLE_QUOTE_ESCAPE = "'\"'\"'";
5
7
 
6
8
  const compileCacheLines = (binaryName: string): string[] => [
7
9
  "cache_home=${XDG_CACHE_HOME:-${HOME:-/tmp}/.cache}",
@@ -9,6 +11,22 @@ const compileCacheLines = (binaryName: string): string[] => [
9
11
  "export NODE_COMPILE_CACHE",
10
12
  ];
11
13
 
14
+ const shellQuote = (value: string): string =>
15
+ SHELL_SINGLE_QUOTE + value.split(SHELL_SINGLE_QUOTE).join(SHELL_SINGLE_QUOTE_ESCAPE) + SHELL_SINGLE_QUOTE;
16
+
17
+ const launcherEnvLines = (settings: DeploySettings): string[] =>
18
+ Object.entries(settings.deploy.launcherEnv ?? {})
19
+ .toSorted(([left], [right]) => left.localeCompare(right))
20
+ .map(([name, value]) => `export ${name}=${shellQuote(value)}`);
21
+
22
+ const launcherCommand = (settings: DeploySettings, libDir: string): string =>
23
+ [
24
+ `"${libDir}/${NODE_FILENAME}"`,
25
+ ...(settings.deploy.nodeFlags ?? []).map((flag) => shellQuote(flag)),
26
+ `"${libDir}/${BUNDLE_FILENAME}"`,
27
+ '"$@"',
28
+ ].join(" ");
29
+
12
30
  const renderLauncher = (settings: DeploySettings): string => {
13
31
  const libDir = `$prefix/lib/${settings.binaryName}`;
14
32
  const shouldUseCompileCache = settings.deploy.node?.shouldUseCompileCache !== false;
@@ -21,7 +39,8 @@ const renderLauncher = (settings: DeploySettings): string => {
21
39
  'GTKX_LOCALE_DIR="$prefix/share/locale"',
22
40
  "export GTKX_LOCALE_DIR",
23
41
  ...(shouldUseCompileCache ? compileCacheLines(settings.binaryName) : []),
24
- `exec "${libDir}/${NODE_FILENAME}" "${libDir}/${BUNDLE_FILENAME}" "$@"`,
42
+ ...launcherEnvLines(settings),
43
+ `exec ${launcherCommand(settings, libDir)}`,
25
44
  "",
26
45
  ].join("\n");
27
46
  };
@@ -108,10 +108,10 @@ const stagePayload = ({ settings, node, metadata }: StageRequest): StagedFile[]
108
108
  ...stageNodeBinary(settings, root, node),
109
109
  ...stageRuntimeFiles(settings, root),
110
110
  ...stageCatalogs(settings, root),
111
+ ...stageExtraFiles(settings, root),
111
112
  ...stageMetadata(settings, root, metadata),
112
113
  ...stageIcons(settings, root),
113
114
  ...stageSchemas(settings, root),
114
- ...stageExtraFiles(settings, root),
115
115
  ]);
116
116
  };
117
117