pack-options.ts 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import type { EditorPackOptions, EditorPackStats } from "./ipc.js";
  2. import type { EditorProjectYaml } from "./project-yaml.js";
  3. export const DEFAULT_PACK_SUBDIR = "dist-pack";
  4. export function defaultPackOutDir(root: string): string {
  5. return `${root}/${DEFAULT_PACK_SUBDIR}`;
  6. }
  7. export function dialogDefaultsFromProject(
  8. projectYaml: EditorProjectYaml | null,
  9. root: string,
  10. fallbackName: string,
  11. ): EditorPackOptions {
  12. return {
  13. name: projectYaml?.pack?.name ?? projectYaml?.name ?? fallbackName ?? "",
  14. icon: projectYaml?.icon ?? "",
  15. desktop: projectYaml?.pack?.installer === true,
  16. installer: projectYaml?.pack?.installer === true,
  17. mods: projectYaml?.pack?.mods === true,
  18. splitAssets: projectYaml?.pack?.splitAssets === true,
  19. outDir: defaultPackOutDir(root),
  20. };
  21. }
  22. export function projectYamlFromPack(
  23. options: EditorPackOptions,
  24. prev: EditorProjectYaml | null,
  25. ): EditorProjectYaml {
  26. const result: EditorProjectYaml = { ...(prev ?? {}) };
  27. const pack = { ...(result.pack ?? {}) };
  28. const name = options.name.trim();
  29. if (name !== "") pack.name = name;
  30. else delete pack.name;
  31. pack.installer = options.installer;
  32. pack.mods = options.mods;
  33. pack.splitAssets = options.splitAssets;
  34. result.pack = pack;
  35. const icon = options.icon.trim();
  36. if (icon !== "") result.icon = icon;
  37. else delete result.icon;
  38. return result;
  39. }
  40. export interface EditorPackRequest {
  41. contentDir: string;
  42. outDir: string;
  43. desktop: boolean;
  44. installer: boolean;
  45. splitAssets: boolean;
  46. modsDir?: string;
  47. name?: string;
  48. }
  49. export function resolvePackRequest(
  50. root: string,
  51. options: EditorPackOptions,
  52. hasMods: boolean,
  53. ): EditorPackRequest {
  54. const request: EditorPackRequest = {
  55. contentDir: `${root}/content`,
  56. outDir: options.outDir.trim() !== "" ? options.outDir : defaultPackOutDir(root),
  57. desktop: options.installer || options.desktop,
  58. installer: options.installer,
  59. splitAssets: options.splitAssets,
  60. };
  61. const name = options.name.trim();
  62. if (name !== "") request.name = name;
  63. if (options.mods && hasMods) request.modsDir = `${root}/mods`;
  64. return request;
  65. }
  66. export function packHints(stats: EditorPackStats): string[] {
  67. const hints: string[] = ["site: serve the output folder statically and open index.html"];
  68. if (stats.desktop) {
  69. hints.push('desktop: cd into the desktop folder, then run "npm install && npm start"');
  70. }
  71. if (stats.installer) {
  72. hints.push('installer: run "npm install && npm run dist" in the desktop folder (Linux AppImage; NSIS needs a Windows host)');
  73. }
  74. return hints;
  75. }