Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from

Conversation

pano9000
Copy link
Contributor

@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
Contributor 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants