Skip to content
This repository was archived by the owner on Jun 24, 2025. It is now read-only.

refactor(bin/electron-forge): make desktop.ejs easier to read #1099

Merged
merged 1 commit into from
Feb 4, 2025

Conversation

pano9000
Copy link
Member

@pano9000 pano9000 commented Feb 2, 2025

Hi,

This PR aims to make the desktop.ejs template that is used for the Linux packages a lot easier to read.
We just define the lines as an Array tuple, and then filter out any values, which are falsy (or in this case undefined), before joining them with a "\n" new line.

electron-forge correctly packages the file and its output is identical to the previous template
grafik

I admit, this a tiny bit of a useless refactor, but it still irked me to see that file in its previous state :-D

Comment on lines 2 to 17
[
["Name", productName],
["Comment", description],
["GenericName", genericName],
["Exec", name ? `${name} %U` : undefined],
["Icon", name],
["Type", "Application"],
["StartupNotify", "true"],
["StartupWMClass", productName],
["Categories", categories?.length ? `${categories.join(";")};` : undefined],
["MimeType", mimeType?.length ? `${mimeType.join(";")};` : undefined]
].map(line => line[1] ? line.join("=") : undefined)
.filter(line => !!line)
.join("\n")%>
Copy link
Contributor

@eliandoran eliandoran Feb 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't an object navigated with Object.entries make it even more readable?

Seems that the order is stable for ES2015+, https://stackoverflow.com/a/23202095/4898894

And either way, I don't think the order matters that much inside desktop entries.

And yes, it was absolutely hideous. I think it's how the guys in Electron Forge do it, might be worth contributing the change upstream.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't an object navigated with Object.entries make it even more readable?

Yes, using Object.entries works as well and would look like the one below

[Desktop Entry]
<%=
Object.entries({
    "Name": productName,
    "Comment": description,
    "GenericName": genericName,
    "Exec": name ? `${name} %U` : undefined,
    "Icon": name,
    "Type": "Application",
    "StartupNotify": "true",
    "StartupWMClass": productName,
    "Categories": categories?.length ? `${categories.join(";")};` : undefined,
    "MimeType": mimeType?.length ? `${mimeType.join(";")};` : undefined
})
.map(line => line[1] ? line.join("=") : undefined)
.filter(line => !!line)
.join("\n")%>

not too much of a huge difference IMHO, but I don't mind changing it either – anything is better than the previous one :-D
will commit the change in a couple minutes.

I think it's how the guys in Electron Forge do it, might be worth contributing the change upstream.

yes, our original file was based on the node_modules/electron-installer-debian/resources/desktop.ejs file.
– I'll might give it a try to bring it to upstream as well, thanks

in theory we could use variables in ejs, but unfortunately electron uses lodash template to create the file, which itself DOES NOT like any let/const/var assignments – wasted a bit of time finding this out, before coming up with this solution
@pano9000 pano9000 force-pushed the refactor_simplify-electron-desktop-ejs branch from 4b24d76 to 61e5602 Compare February 2, 2025 21:28
@eliandoran eliandoran merged commit 0c35714 into develop Feb 4, 2025
5 checks passed
@eliandoran eliandoran deleted the refactor_simplify-electron-desktop-ejs branch February 4, 2025 19:24
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants