@lvce-editor/shared-process 0.14.7 → 0.14.9

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lvce-editor/shared-process",
3
- "version": "0.14.7",
3
+ "version": "0.14.9",
4
4
  "description": "Utility package for @lvce-editor/server",
5
5
  "main": "index.js",
6
6
  "type": "module",
@@ -17,9 +17,9 @@
17
17
  },
18
18
  "dependencies": {
19
19
  "@babel/code-frame": "^7.21.4",
20
- "@lvce-editor/extension-host": "0.14.7",
21
- "@lvce-editor/extension-host-helper-process": "0.14.7",
22
- "@lvce-editor/pty-host": "0.14.7",
20
+ "@lvce-editor/extension-host": "0.14.9",
21
+ "@lvce-editor/extension-host-helper-process": "0.14.9",
22
+ "@lvce-editor/pty-host": "0.14.9",
23
23
  "debug": "^4.3.4",
24
24
  "execa": "^7.1.1",
25
25
  "exit-hook": "^3.2.0",
@@ -0,0 +1,9 @@
1
+ import * as AutoUpdater from './AutoUpdater.js'
2
+
3
+ export const name = 'AutoUpdater'
4
+
5
+ export const Commands = {
6
+ checkForUpdatesAndNotify: AutoUpdater.checkForUpdatesAndNotify,
7
+ downloadUpdate: AutoUpdater.downloadUpdate,
8
+ installAndRestart: AutoUpdater.installAndRestart,
9
+ }
@@ -0,0 +1,29 @@
1
+ import * as AutoUpdaterAppImage from '../AutoUpdaterAppImage/AutoUpdaterAppImage.js'
2
+ import { VError } from '../VError/VError.js'
3
+
4
+ export const checkForUpdatesAndNotify = async () => {
5
+ try {
6
+ return await AutoUpdaterAppImage.checkForUpdatesAndNotify()
7
+ } catch (error) {
8
+ // @ts-ignore
9
+ throw new VError(error, `Failed to check for updates`)
10
+ }
11
+ }
12
+
13
+ export const downloadUpdate = async (version) => {
14
+ try {
15
+ return await AutoUpdaterAppImage.downloadUpdate(version)
16
+ } catch (error) {
17
+ // @ts-ignore
18
+ throw new VError(error, `Failed to download update`)
19
+ }
20
+ }
21
+
22
+ export const installAndRestart = async (downloadPath) => {
23
+ try {
24
+ return await AutoUpdaterAppImage.installAndRestart(downloadPath)
25
+ } catch (error) {
26
+ // @ts-ignore
27
+ throw new VError(error, `Failed to install and restart`)
28
+ }
29
+ }
@@ -0,0 +1,84 @@
1
+ import { spawn } from 'node:child_process'
2
+ import { rename } from 'node:fs/promises'
3
+ import { tmpdir } from 'node:os'
4
+ import { join } from 'node:path'
5
+ import * as Assert from '../Assert/Assert.js'
6
+ import * as CompareVersion from '../CompareVersion/CompareVersion.js'
7
+ import * as Download from '../Download/Download.js'
8
+ import * as GetLatestReleaseVersion from '../GetLatestReleaseVersion/GetLatestReleaseVersion.js'
9
+ import * as MakeExecutable from '../MakeExecutable/MakeExecutable.js'
10
+ import * as Platform from '../Platform/Platform.js'
11
+ import { VError } from '../VError/VError.js'
12
+
13
+ const getDownloadUrl = (repository, version, appImageName) => {
14
+ Assert.string(version)
15
+ return `https://github.com/${repository}/releases/download/v${version}/${appImageName}-v${version}.AppImage`
16
+ }
17
+
18
+ const getOutfilePath = (version) => {
19
+ Assert.string(version)
20
+ const outFile = join(tmpdir(), `appimage-${version}`)
21
+ return outFile
22
+ }
23
+
24
+ export const downloadUpdate = async (version) => {
25
+ try {
26
+ Assert.string(version)
27
+ const repository = Platform.getRepository()
28
+ const appImageName = Platform.getAppImageName()
29
+ const downLoadUrl = getDownloadUrl(repository, version, appImageName)
30
+ const outFile = getOutfilePath(version)
31
+ await Download.download(downLoadUrl, outFile)
32
+ return outFile
33
+ } catch (error) {
34
+ // @ts-ignore
35
+ throw new VError(error, `Failed to download new version ${version}`)
36
+ }
37
+ }
38
+
39
+ export const checkForUpdatesAndNotify = async () => {
40
+ const repository = Platform.getRepository()
41
+ const version = await GetLatestReleaseVersion.getLatestReleaseVersion(repository)
42
+ const currentVersion = Platform.version
43
+ if (CompareVersion.isGreater(version, currentVersion)) {
44
+ return {
45
+ version,
46
+ }
47
+ } else {
48
+ console.log('not update is available')
49
+ }
50
+ }
51
+
52
+ const getAppImagePath = () => {
53
+ return process.env.APPIMAGE
54
+ }
55
+
56
+ const installNewAppImage = async (appImageFile, downloadPath) => {
57
+ try {
58
+ await rename(downloadPath, appImageFile)
59
+ } catch (error) {
60
+ // @ts-ignore
61
+ throw new VError(error, `Failed to rename AppImage file`)
62
+ }
63
+ }
64
+
65
+ const restart = (downloadPath) => {
66
+ // TODO handle errors
67
+ spawn(downloadPath, { stdio: 'inherit' })
68
+ }
69
+
70
+ export const installAndRestart = async (downloadPath) => {
71
+ try {
72
+ Assert.string(downloadPath)
73
+ const appImageFile = getAppImagePath()
74
+ if (!appImageFile) {
75
+ throw new Error(`AppImage path not found`)
76
+ }
77
+ await MakeExecutable.makeExecutable(downloadPath)
78
+ await installNewAppImage(appImageFile, downloadPath)
79
+ await restart(downloadPath)
80
+ } catch (error) {
81
+ // @ts-ignore
82
+ throw new VError(error, `Failed to install AppImage update`)
83
+ }
84
+ }
@@ -0,0 +1,3 @@
1
+ export const isGreater = (version, otherVersion) => {
2
+ return true
3
+ }
@@ -0,0 +1,60 @@
1
+ import got, { HTTPError } from 'got'
2
+ import { VError } from '../VError/VError.js'
3
+
4
+ /**
5
+ *
6
+ * @param {HTTPError} error
7
+ */
8
+ const getHttpErrorMessage = (error) => {
9
+ try {
10
+ const body = error.response.body
11
+ if (error.response.url.includes('api.github.com') && typeof body === 'string') {
12
+ const json = JSON.parse(body)
13
+ if (json.message) {
14
+ const message = json.message
15
+ if (message.includes('rate limit exceeded')) {
16
+ const reset = error.response.headers['x-ratelimit-reset']
17
+ const limit = error.response.headers['x-ratelimit-limit']
18
+ if (reset && typeof reset === 'string' && typeof limit === 'string') {
19
+ const resetDate = new Date(parseInt(reset) * 1000)
20
+ const limitAmount = parseInt(limit)
21
+ return `GitHub rate limit of ${limitAmount} requests per hour execeeded, resets at ${resetDate}`
22
+ }
23
+ }
24
+ return json.message
25
+ }
26
+ }
27
+ } catch {}
28
+ return `${error.message}`
29
+ }
30
+
31
+ const parseVersionFromUrl = (url, repository) => {
32
+ if (!url.includes('releases/tag')) {
33
+ if (url.endsWith('/releases')) {
34
+ throw new Error(`no releases found for ${repository}`)
35
+ }
36
+ throw new Error(`cannot parse release version from url ${url}`)
37
+ }
38
+ const slashIndex = url.lastIndexOf('/')
39
+ const version = url.slice(slashIndex + 1)
40
+ if (version.startsWith('v')) {
41
+ return version.slice(1)
42
+ }
43
+ return version
44
+ }
45
+
46
+ export const getLatestReleaseVersion = async (repository) => {
47
+ try {
48
+ const json = await got.head(`https://github.com/${repository}/releases/latest`)
49
+ const finalUrl = json.url
50
+ const version = parseVersionFromUrl(finalUrl, repository)
51
+ return version
52
+ } catch (error) {
53
+ if (error instanceof HTTPError) {
54
+ const httpErrorMessage = getHttpErrorMessage(error)
55
+ throw new VError(`Failed to get latest release for ${repository}: ${httpErrorMessage}`)
56
+ }
57
+ // @ts-ignore
58
+ throw new VError(error, `Failed to get latest release for ${repository}`)
59
+ }
60
+ }
@@ -0,0 +1,7 @@
1
+ import * as IsAutoUpdateSupported from './IsAutoUpdateSupported.js'
2
+
3
+ export const name = 'IsAutoUpdateSupported'
4
+
5
+ export const Commands = {
6
+ isAutoUpdateSupported: IsAutoUpdateSupported.isAutoUpdateSupported,
7
+ }
@@ -0,0 +1,10 @@
1
+ import * as Platform from '../Platform/Platform.js'
2
+
3
+ export const isAutoUpdateSupported = () => {
4
+ return true
5
+ // return Platform.isWindows || Platform.isMacOs
6
+ }
7
+
8
+ export const useElectronBuilderAutoUpdate = () => {
9
+ return Platform.isWindows
10
+ }
@@ -0,0 +1,10 @@
1
+ import { chmod } from 'node:fs/promises'
2
+ import { VError } from '../VError/VError.js'
3
+
4
+ export const makeExecutable = async (file) => {
5
+ try {
6
+ await chmod(file, 0o755)
7
+ } catch (error) {
8
+ throw new VError(error, `Failed to make file executable`)
9
+ }
10
+ }
@@ -44,6 +44,10 @@ export const load = (moduleId) => {
44
44
  return import('../Workspace/Workspace.ipc.js')
45
45
  case ModuleId.InstallExtension:
46
46
  return import('../InstallExtension/InstallExtension.ipc.js')
47
+ case ModuleId.AutoUpdater:
48
+ return import('../AutoUpdater/AutoUpdater.ipc.js')
49
+ case ModuleId.IsAutoUpdateSupported:
50
+ return import('../IsAutoUpdateSupported/IsAutoUpdateSupported.ipc.js')
47
51
  default:
48
52
  throw new Error(`module ${moduleId} not found`)
49
53
  }
@@ -19,3 +19,5 @@ export const Download = 18
19
19
  export const GitLsFiles = 19
20
20
  export const BulkReplacement = 20
21
21
  export const InstallExtension = 21
22
+ export const AutoUpdater = 22
23
+ export const IsAutoUpdateSupported = 23
@@ -3,6 +3,10 @@ import * as ModuleId from '../ModuleId/ModuleId.js'
3
3
 
4
4
  export const getModuleId = (commandId) => {
5
5
  switch (commandId) {
6
+ case 'AutoUpdater.checkForUpdatesAndNotify':
7
+ case 'AutoUpdater.downloadUpdate':
8
+ case 'AutoUpdater.installAndRestart':
9
+ return ModuleId.AutoUpdater
6
10
  case 'BulkReplacement.applyBulkReplacement':
7
11
  return ModuleId.BulkReplacement
8
12
  case 'ChromeExtension.install':
@@ -99,6 +103,8 @@ export const getModuleId = (commandId) => {
99
103
  case 'GitLsFiles.gitLsFilesHash':
100
104
  case 'GitLsFiles.resolveGit':
101
105
  return ModuleId.GitLsFiles
106
+ case 'IsAutoUpdateSupported.isAutoUpdateSupported':
107
+ return ModuleId.IsAutoUpdateSupported
102
108
  case 'Native.openFolder':
103
109
  return ModuleId.Native
104
110
  case 'OutputChannel.close':
@@ -147,3 +147,13 @@ export const getDownloadDir = () => {
147
147
  const { XDG_DOWNLOAD_DIR } = env
148
148
  return XDG_DOWNLOAD_DIR || join(homeDir, 'Downloads')
149
149
  }
150
+
151
+ export const getRepository = () => {
152
+ return `lvce-editor/lvce-editor`
153
+ }
154
+
155
+ export const getAppImageName = () => {
156
+ return 'Lvce'
157
+ }
158
+
159
+ export const version = '0.0.0-dev'