|
1 | | -const cp = require('child_process'); |
2 | | -const marked = require('marked'); |
3 | | -const { sanitize } = require('isomorphic-dompurify'); |
4 | | -const { githubRepoInfo, repoUrl } = require('../common/utils'); |
5 | | - |
6 | | -/** |
7 | | - * Enum of update states. |
8 | | - */ |
9 | | -const UpdaterStatus = { |
10 | | - CheckingForUpdates: 'CheckingForUpdates', |
11 | | - UpdateAvailable: 'UpdateAvailable', |
12 | | - UpdateNotAvailable: 'UpdateNotAvailable', |
13 | | - UpdateDownloaded: 'UpdateDownloaded', |
14 | | - DownloadProgress: 'DownloadProgress', |
15 | | - InstallingUpdate: 'InstallingUpdate', |
16 | | - UpdateApplied: 'UpdateApplied', |
17 | | - Error: 'Error', |
18 | | -}; |
19 | | - |
20 | | -const releasesApiEndpoint = `https://api.github.com/repos/${githubRepoInfo.owner}/${githubRepoInfo.repo}/releases`; |
21 | | -const releasesUrl = `${repoUrl}/releases`; |
22 | | - |
23 | | -/** |
24 | | - * Checks if a given command exists in the current system |
25 | | - * |
26 | | - * @param {string} cmdName |
27 | | - * Name of the command whose existance has to be checked |
28 | | - * |
29 | | - * @returns {Promise<boolean>} |
30 | | - * Boolean value based on command availibility |
31 | | - */ |
32 | | -async function isCommandAvailable(cmdName) { |
33 | | - return new Promise((resolve, _) => { |
34 | | - cp.exec(`which ${cmdName}`, (err, stdout, stderr) => { |
35 | | - if (err || stderr) { |
36 | | - resolve(false); |
37 | | - } |
38 | | - |
39 | | - resolve(true); |
40 | | - }); |
41 | | - }); |
42 | | -} |
43 | | - |
44 | | -/** |
45 | | - * Checks the preferred package format (deb or rpm) for |
46 | | - * current linux system. If the host system is not linux, |
47 | | - * `null` is returned. |
48 | | - */ |
49 | | -async function getSupportedLinuxPackageFormat() { |
50 | | - if (process.platform !== 'linux') return null; |
51 | | - |
52 | | - const hasDpkg = await isCommandAvailable('dpkg'); |
53 | | - const hasRpm = await isCommandAvailable('rpm'); |
54 | | - |
55 | | - if (hasDpkg) { |
56 | | - return 'deb'; |
57 | | - } |
58 | | - |
59 | | - if (hasRpm) { |
60 | | - return 'rpm'; |
61 | | - } |
62 | | - |
63 | | - return null; |
64 | | -} |
65 | | - |
66 | | -/** |
67 | | - * Returns link for GitHub Release page for a given |
68 | | - * version tag |
69 | | - * |
70 | | - * @param {string} version |
71 | | - * Tag name (i.e., version). The version should be |
72 | | - * prefixed with a `v` |
73 | | - */ |
74 | | -function getTagReleaseLink(version) { |
75 | | - return `${releasesUrl}/tag/${version}`; |
76 | | -} |
77 | | - |
78 | | -/** |
79 | | - * Converts a string of Markdown to a string |
80 | | - * of HTML. |
81 | | - * |
82 | | - * @param {string} markdownString |
83 | | - * String containing Markdown |
84 | | - */ |
85 | | -function markdownToHtml(markdownString) { |
86 | | - // Compile Markdown to HTML |
87 | | - let htmlString = marked(markdownString); |
88 | | - |
89 | | - // Sanitize HTML String |
90 | | - htmlString = sanitize(htmlString); |
91 | | - |
92 | | - // Replace anchor tags' href attribute with custom onclick listener |
93 | | - htmlString = htmlString.replace(/<a(.*)href=['"](.*)['"](.*)>(.*)<\/a>/g, '<a$1onclick="openLink(\'$2\')"$3>$4</a>'); |
94 | | - |
95 | | - return htmlString; |
96 | | -} |
97 | | - |
98 | | -module.exports = { |
99 | | - UpdaterStatus, |
100 | | - releasesApiEndpoint, |
101 | | - releasesUrl, |
102 | | - getSupportedLinuxPackageFormat, |
103 | | - getTagReleaseLink, |
104 | | - markdownToHtml, |
105 | | -}; |
| 1 | +const cp = require('child_process'); |
| 2 | +const marked = require('marked'); |
| 3 | +const { sanitize } = require('isomorphic-dompurify'); |
| 4 | +const { githubRepoInfo, repoUrl } = require('../common/utils'); |
| 5 | + |
| 6 | +/** |
| 7 | + * Enum of update states. |
| 8 | + */ |
| 9 | +const UpdaterStatus = { |
| 10 | + CheckingForUpdates: 'CheckingForUpdates', |
| 11 | + UpdateAvailable: 'UpdateAvailable', |
| 12 | + UpdateNotAvailable: 'UpdateNotAvailable', |
| 13 | + UpdateDownloaded: 'UpdateDownloaded', |
| 14 | + DownloadProgress: 'DownloadProgress', |
| 15 | + InstallingUpdate: 'InstallingUpdate', |
| 16 | + UpdateApplied: 'UpdateApplied', |
| 17 | + Error: 'Error', |
| 18 | +}; |
| 19 | + |
| 20 | +const releasesApiEndpoint = `https://api.github.com/repos/${githubRepoInfo.owner}/${githubRepoInfo.repo}/releases`; |
| 21 | +const releasesUrl = `${repoUrl}/releases`; |
| 22 | + |
| 23 | +/** |
| 24 | + * Checks if a given command exists in the current system |
| 25 | + * |
| 26 | + * @param {string} cmdName |
| 27 | + * Name of the command whose existance has to be checked |
| 28 | + * |
| 29 | + * @returns {Promise<boolean>} |
| 30 | + * Boolean value based on command availibility |
| 31 | + */ |
| 32 | +async function isCommandAvailable(cmdName) { |
| 33 | + return new Promise((resolve, _) => { |
| 34 | + cp.exec(`which ${cmdName}`, (err, stdout, stderr) => { |
| 35 | + if (err || stderr) { |
| 36 | + resolve(false); |
| 37 | + } |
| 38 | + |
| 39 | + resolve(true); |
| 40 | + }); |
| 41 | + }); |
| 42 | +} |
| 43 | + |
| 44 | +/** |
| 45 | + * Checks the preferred package format (deb or rpm) for |
| 46 | + * current linux system. If the host system is not linux, |
| 47 | + * `null` is returned. |
| 48 | + */ |
| 49 | +async function getSupportedLinuxPackageFormat() { |
| 50 | + if (process.platform !== 'linux') return null; |
| 51 | + |
| 52 | + const hasDpkg = await isCommandAvailable('dpkg'); |
| 53 | + const hasRpm = await isCommandAvailable('rpm'); |
| 54 | + |
| 55 | + if (hasDpkg) { |
| 56 | + return 'deb'; |
| 57 | + } |
| 58 | + |
| 59 | + if (hasRpm) { |
| 60 | + return 'rpm'; |
| 61 | + } |
| 62 | + |
| 63 | + return null; |
| 64 | +} |
| 65 | + |
| 66 | +/** |
| 67 | + * Returns link for GitHub Release page for a given |
| 68 | + * version tag |
| 69 | + * |
| 70 | + * @param {string} version |
| 71 | + * Tag name (i.e., version). The version should be |
| 72 | + * prefixed with a `v` |
| 73 | + */ |
| 74 | +function getTagReleaseLink(version) { |
| 75 | + return `${releasesUrl}/tag/${version}`; |
| 76 | +} |
| 77 | + |
| 78 | +/** |
| 79 | + * Converts a string of Markdown to a string |
| 80 | + * of HTML. |
| 81 | + * |
| 82 | + * @param {string} markdownString |
| 83 | + * String containing Markdown |
| 84 | + */ |
| 85 | +function markdownToHtml(markdownString) { |
| 86 | + // Compile Markdown to HTML |
| 87 | + let htmlString = marked.marked(markdownString); |
| 88 | + |
| 89 | + // Sanitize HTML String |
| 90 | + htmlString = sanitize(htmlString); |
| 91 | + |
| 92 | + // Replace anchor tags' href attribute with custom onclick listener |
| 93 | + htmlString = htmlString.replace(/<a(.*)href=['"](.*)['"](.*)>(.*)<\/a>/g, '<a$1onclick="openLink(\'$2\')"$3>$4</a>'); |
| 94 | + |
| 95 | + return htmlString; |
| 96 | +} |
| 97 | + |
| 98 | +module.exports = { |
| 99 | + UpdaterStatus, |
| 100 | + releasesApiEndpoint, |
| 101 | + releasesUrl, |
| 102 | + getSupportedLinuxPackageFormat, |
| 103 | + getTagReleaseLink, |
| 104 | + markdownToHtml, |
| 105 | +}; |
0 commit comments