@lvce-editor/update-worker 1.0.0 → 1.1.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.
- package/dist/updateWorkerMain.js +164 -3
- package/package.json +1 -1
package/dist/updateWorkerMain.js
CHANGED
|
@@ -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,170 @@ const create = rpcId => {
|
|
|
872
872
|
};
|
|
873
873
|
|
|
874
874
|
const {
|
|
875
|
+
invoke,
|
|
875
876
|
set} = create(RendererWorker);
|
|
876
877
|
|
|
877
|
-
const
|
|
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
|
+
const downloadUpdate = async updateUrl => {
|
|
907
|
+
const response = await fetch(updateUrl, {
|
|
908
|
+
priority: 'low',
|
|
909
|
+
redirect: 'follow'
|
|
910
|
+
});
|
|
911
|
+
if (!response.ok) {
|
|
912
|
+
throw new Error(response.statusText);
|
|
913
|
+
}
|
|
914
|
+
return response;
|
|
915
|
+
};
|
|
916
|
+
|
|
917
|
+
const getUpdateUrl = (repository, version) => {
|
|
918
|
+
const fileName = `Lvce-Setup-v${version}-x64.exe`;
|
|
919
|
+
const url = `https://github.com/${repository}/releases/download/v${version}/${fileName}`;
|
|
920
|
+
return url;
|
|
921
|
+
};
|
|
922
|
+
|
|
923
|
+
const putInCache = async (url, response) => {
|
|
924
|
+
const cache = await caches.open('electron-updates');
|
|
925
|
+
await cache.put(url, response);
|
|
926
|
+
};
|
|
927
|
+
|
|
928
|
+
const downloadUpdateToCache = async (repository, version) => {
|
|
929
|
+
const downloadUrl = getUpdateUrl(repository, version);
|
|
930
|
+
const response = await downloadUpdate(downloadUrl);
|
|
931
|
+
await putInCache(downloadUrl, response);
|
|
932
|
+
};
|
|
933
|
+
|
|
934
|
+
const parseVersionFromUrl = (url, repository) => {
|
|
935
|
+
if (!url.includes('releases/tag')) {
|
|
936
|
+
if (url.endsWith('/releases')) {
|
|
937
|
+
throw new Error(`no releases found for ${repository}`);
|
|
938
|
+
}
|
|
939
|
+
throw new Error(`cannot parse release version from url ${url}`);
|
|
940
|
+
}
|
|
941
|
+
const slashIndex = url.lastIndexOf('/');
|
|
942
|
+
const version = url.slice(slashIndex + 1);
|
|
943
|
+
if (version.startsWith('v')) {
|
|
944
|
+
return version.slice(1);
|
|
945
|
+
}
|
|
946
|
+
return version;
|
|
947
|
+
};
|
|
948
|
+
const getLatestReleaseVersion = async repository => {
|
|
949
|
+
try {
|
|
950
|
+
const url = `https://github.com/${repository}/releases/latest`;
|
|
951
|
+
const finalUrlResponse = await fetch(url, {
|
|
952
|
+
method: 'HEAD'
|
|
953
|
+
});
|
|
954
|
+
const finalUrl = await finalUrlResponse.text();
|
|
955
|
+
const version = parseVersionFromUrl(finalUrl, repository);
|
|
956
|
+
return {
|
|
957
|
+
version
|
|
958
|
+
};
|
|
959
|
+
} catch (error) {
|
|
960
|
+
throw new VError(error, `Failed to get latest release for ${repository}`);
|
|
961
|
+
}
|
|
962
|
+
};
|
|
963
|
+
|
|
964
|
+
const exec = async (path, args, options) => {
|
|
965
|
+
// TODO
|
|
966
|
+
return {
|
|
967
|
+
type: 0,
|
|
968
|
+
event: {}
|
|
969
|
+
};
|
|
970
|
+
};
|
|
971
|
+
|
|
972
|
+
const Error$1 = 2;
|
|
973
|
+
|
|
974
|
+
const getNsisUpdateArgs = () => {
|
|
975
|
+
const args = ['--updated', '/S', '--force-run'];
|
|
976
|
+
return args;
|
|
977
|
+
};
|
|
978
|
+
|
|
979
|
+
const installAndRestart = async downloadPath => {
|
|
980
|
+
try {
|
|
981
|
+
const args = getNsisUpdateArgs();
|
|
982
|
+
const {
|
|
983
|
+
type,
|
|
984
|
+
event
|
|
985
|
+
} = await exec(downloadPath, args, {
|
|
986
|
+
stdio: 'inherit',
|
|
987
|
+
detached: true
|
|
988
|
+
});
|
|
989
|
+
if (type === Error$1) {
|
|
990
|
+
throw new Error(`Child process error: ${event}`);
|
|
991
|
+
}
|
|
992
|
+
} catch (error) {
|
|
993
|
+
throw new VError(error, `Failed to install nsis update`);
|
|
994
|
+
}
|
|
995
|
+
};
|
|
996
|
+
|
|
997
|
+
const shouldUpdate = async (updateSetting, version) => {
|
|
998
|
+
if (updateSetting && updateSetting !== 'none') {
|
|
999
|
+
return true;
|
|
1000
|
+
}
|
|
1001
|
+
const message = promptMessage(version);
|
|
1002
|
+
const shouldUpdate = await invoke('ConfirmPrompt.prompt', message, {
|
|
1003
|
+
confirmMessage: '',
|
|
1004
|
+
title: ''
|
|
1005
|
+
});
|
|
1006
|
+
return shouldUpdate;
|
|
1007
|
+
};
|
|
1008
|
+
|
|
1009
|
+
const checkForUpdates = async (updateSetting, repository) => {
|
|
1010
|
+
const info = await getLatestReleaseVersion(repository);
|
|
1011
|
+
if (!info || !info.version) {
|
|
1012
|
+
return;
|
|
1013
|
+
}
|
|
1014
|
+
if (!(await shouldUpdate(updateSetting, info.version))) {
|
|
1015
|
+
return;
|
|
1016
|
+
}
|
|
1017
|
+
// @ts-ignore
|
|
1018
|
+
await invoke('Layout.setUpdateState', {
|
|
1019
|
+
state: 'downloading',
|
|
1020
|
+
progress: 0
|
|
1021
|
+
});
|
|
1022
|
+
await downloadUpdateToCache(repository, info.version);
|
|
1023
|
+
const downloadPath = ''; // TODO get it from cache to disk somehow
|
|
1024
|
+
const messageRestart = promptRestart();
|
|
1025
|
+
const shouldRestart = await invoke('ConfirmPrompt.prompt', messageRestart, {
|
|
1026
|
+
confirmMessage: '',
|
|
1027
|
+
title: ''
|
|
1028
|
+
});
|
|
1029
|
+
if (!shouldRestart) {
|
|
1030
|
+
return;
|
|
1031
|
+
}
|
|
1032
|
+
await installAndRestart(downloadPath);
|
|
1033
|
+
};
|
|
1034
|
+
|
|
1035
|
+
const commandMap = {
|
|
1036
|
+
'Update.downloadToCache': downloadUpdateToCache,
|
|
1037
|
+
'Update.checkForUpdates': checkForUpdates
|
|
1038
|
+
};
|
|
878
1039
|
|
|
879
1040
|
const listen = async () => {
|
|
880
1041
|
const rpc = await WebWorkerRpcClient.create({
|