@lvce-editor/update-worker 1.0.0 → 1.2.0

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.
@@ -742,7 +742,7 @@ const send = (transport, method, ...params) => {
742
742
  const message = create$4(method, params);
743
743
  transport.send(message);
744
744
  };
745
- const invoke = (ipc, method, ...params) => {
745
+ const invoke$1 = (ipc, method, ...params) => {
746
746
  return invokeHelper(ipc, method, params, false);
747
747
  };
748
748
  const invokeAndTransfer = (ipc, method, ...params) => {
@@ -781,7 +781,7 @@ const createRpc = ipc => {
781
781
  send(ipc, method, ...params);
782
782
  },
783
783
  invoke(method, ...params) {
784
- return invoke(ipc, method, ...params);
784
+ return invoke$1(ipc, method, ...params);
785
785
  },
786
786
  invokeAndTransfer(method, ...params) {
787
787
  return invokeAndTransfer(ipc, method, ...params);
@@ -872,9 +872,192 @@ const create = rpcId => {
872
872
  };
873
873
 
874
874
  const {
875
+ invoke,
875
876
  set} = create(RendererWorker);
876
877
 
877
- const commandMap = {};
878
+ const emptyObject = {};
879
+ const RE_PLACEHOLDER = /\{(PH\d+)\}/g;
880
+ const i18nString = (key, placeholders = emptyObject) => {
881
+ if (placeholders === emptyObject) {
882
+ return key;
883
+ }
884
+ const replacer = (match, rest) => {
885
+ return placeholders[rest];
886
+ };
887
+ return key.replaceAll(RE_PLACEHOLDER, replacer);
888
+ };
889
+
890
+ /**
891
+ * @enum {string}
892
+ */
893
+ const UiStrings = {
894
+ DoYouWantToUpdate: 'Do you want to update to version {PH1}?',
895
+ DoYouWantToRestart: 'The Update has been downloaded. Do you want to restart now?'
896
+ };
897
+ const promptMessage = version => {
898
+ return i18nString(UiStrings.DoYouWantToUpdate, {
899
+ PH1: version
900
+ });
901
+ };
902
+ const promptRestart = () => {
903
+ return i18nString(UiStrings.DoYouWantToRestart);
904
+ };
905
+
906
+ // TODO avoid js prompt in elctron, since it crashes electron
907
+
908
+ const confirmPrompt = async message => {
909
+ // TODO
910
+ return true;
911
+ };
912
+
913
+ const downloadUpdate = async updateUrl => {
914
+ const response = await fetch(updateUrl, {
915
+ priority: 'low',
916
+ redirect: 'follow'
917
+ });
918
+ if (!response.ok) {
919
+ throw new Error(response.statusText);
920
+ }
921
+ return response;
922
+ };
923
+
924
+ const getUpdateUrl = (repository, version) => {
925
+ const fileName = `Lvce-Setup-v${version}-x64.exe`;
926
+ const url = `https://github.com/${repository}/releases/download/v${version}/${fileName}`;
927
+ return url;
928
+ };
929
+
930
+ const putInCache = async (url, response) => {
931
+ const cache = await caches.open('electron-updates');
932
+ await cache.put(url, response);
933
+ };
934
+
935
+ const downloadUpdateToCache = async (repository, version) => {
936
+ const downloadUrl = getUpdateUrl(repository, version);
937
+ const response = await downloadUpdate(downloadUrl);
938
+ await putInCache(downloadUrl, response);
939
+ };
940
+
941
+ const parseVersionFromUrl = (url, repository) => {
942
+ if (!url.includes('releases/tag')) {
943
+ if (url.endsWith('/releases')) {
944
+ throw new Error(`no releases found for ${repository}`);
945
+ }
946
+ throw new Error(`cannot parse release version from url ${url}`);
947
+ }
948
+ const slashIndex = url.lastIndexOf('/');
949
+ const version = url.slice(slashIndex + 1);
950
+ if (version.startsWith('v')) {
951
+ return version.slice(1);
952
+ }
953
+ return version;
954
+ };
955
+ const getLatestReleaseVersion = async repository => {
956
+ try {
957
+ const url = `https://github.com/${repository}/releases/latest`;
958
+ const finalUrlResponse = await fetch(url, {
959
+ method: 'HEAD'
960
+ });
961
+ const finalUrl = finalUrlResponse.url;
962
+ const version = parseVersionFromUrl(finalUrl, repository);
963
+ return {
964
+ version
965
+ };
966
+ } catch (error) {
967
+ throw new VError(error, `Failed to get latest release for ${repository}`);
968
+ }
969
+ };
970
+
971
+ const exec = async (path, args, options) => {
972
+ // TODO
973
+ return {
974
+ type: 0,
975
+ event: {}
976
+ };
977
+ };
978
+
979
+ const Error$1 = 2;
980
+
981
+ const getNsisUpdateArgs = () => {
982
+ const args = ['--updated', '/S', '--force-run'];
983
+ return args;
984
+ };
985
+
986
+ const installAndRestart = async downloadPath => {
987
+ try {
988
+ const args = getNsisUpdateArgs();
989
+ const {
990
+ type,
991
+ event
992
+ } = await exec(downloadPath, args, {
993
+ stdio: 'inherit',
994
+ detached: true
995
+ });
996
+ if (type === Error$1) {
997
+ throw new Error(`Child process error: ${event}`);
998
+ }
999
+ } catch (error) {
1000
+ throw new VError(error, `Failed to install nsis update`);
1001
+ }
1002
+ };
1003
+
1004
+ const shouldUpdate = async (updateSetting, version) => {
1005
+ if (updateSetting && updateSetting !== 'none') {
1006
+ return true;
1007
+ }
1008
+ promptMessage(version);
1009
+ const shouldUpdate = await confirmPrompt();
1010
+ return shouldUpdate;
1011
+ };
1012
+
1013
+ const checkForUpdates = async (updateSetting, repository) => {
1014
+ try {
1015
+ const info = await getLatestReleaseVersion(repository);
1016
+ if (!info || !info.version) {
1017
+ return {
1018
+ updated: false,
1019
+ error: undefined
1020
+ };
1021
+ }
1022
+ if (!(await shouldUpdate(updateSetting, info.version))) {
1023
+ return {
1024
+ updated: false,
1025
+ error: undefined
1026
+ };
1027
+ }
1028
+ // @ts-ignore
1029
+ await invoke('Layout.setUpdateState', {
1030
+ state: 'downloading',
1031
+ progress: 0
1032
+ });
1033
+ await downloadUpdateToCache(repository, info.version);
1034
+ const messageRestart = promptRestart();
1035
+ const shouldRestart = await confirmPrompt(messageRestart);
1036
+ if (!shouldRestart) {
1037
+ return {
1038
+ updated: false,
1039
+ error: undefined
1040
+ };
1041
+ }
1042
+ const downloadPath = ''; // TODO get it from cache to disk somehow
1043
+ await installAndRestart(downloadPath);
1044
+ return {
1045
+ updated: true,
1046
+ error: undefined
1047
+ };
1048
+ } catch (error) {
1049
+ console.error(error);
1050
+ return {
1051
+ updated: false,
1052
+ error: undefined
1053
+ };
1054
+ }
1055
+ };
1056
+
1057
+ const commandMap = {
1058
+ 'Update.downloadToCache': downloadUpdateToCache,
1059
+ 'Update.checkForUpdates': checkForUpdates
1060
+ };
878
1061
 
879
1062
  const listen = async () => {
880
1063
  const rpc = await WebWorkerRpcClient.create({
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lvce-editor/update-worker",
3
- "version": "1.0.0",
3
+ "version": "1.2.0",
4
4
  "description": "Update Worker",
5
5
  "repository": {
6
6
  "type": "git",