Skip to content

Commit aa74e00

Browse files
committed
feat: load new extension after update
1 parent d64a674 commit aa74e00

File tree

3 files changed

+39
-17
lines changed

3 files changed

+39
-17
lines changed

packages/electron-chrome-web-store/src/browser/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,6 @@ export async function installChromeWebStore(opts: ElectronChromeWebStoreOptions
9898
}
9999

100100
if (autoUpdate) {
101-
await initUpdater(webStoreState)
101+
void initUpdater(webStoreState)
102102
}
103103
}

packages/electron-chrome-web-store/src/browser/loader.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ export async function loadAllExtensions(
135135
d('skipping loading existing extension %s', ext.id)
136136
continue
137137
}
138-
d('loading extension %s', ext.id)
138+
d('loading extension %s', `${ext.id}@${ext.manifest.version}`)
139139
await session.loadExtension(ext.path)
140140
} else if (options.allowUnpacked) {
141141
d('loading unpacked extension %s', ext.path)

packages/electron-chrome-web-store/src/browser/updater.ts

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ const getSessionId = (() => {
7676
return () => sessionId || (sessionId = crypto.randomUUID())
7777
})()
7878

79-
const getOmahaPlatform = () => {
79+
const getOmahaPlatform = (): string => {
8080
switch (process.platform) {
8181
case 'win32':
8282
return 'win'
@@ -87,14 +87,14 @@ const getOmahaPlatform = () => {
8787
}
8888
}
8989

90-
const getOmahaArch = () => {
90+
const getOmahaArch = (): string => {
9191
switch (process.arch) {
9292
case 'ia32':
9393
return 'x86'
9494
case 'x64':
9595
return 'x64'
9696
default:
97-
process.arch
97+
return process.arch
9898
}
9999
}
100100

@@ -107,7 +107,6 @@ async function requestExtensionUpdates(extensions: Electron.Extension[]) {
107107
}),
108108
{},
109109
)
110-
d('checking extensions for updates', extensionIds)
111110

112111
const chromeVersion = getChromeVersion()
113112
const url = 'https://update.googleapis.com/service/update2/json'
@@ -197,26 +196,49 @@ async function requestExtensionUpdates(extensions: Electron.Extension[]) {
197196
return updates
198197
}
199198

200-
async function updateExtension(update: ExtensionUpdate) {
201-
d('updating %s', update.id)
202-
const updateDir = path.join(update.extension.path, '..', `${update.version}_0`)
199+
async function updateExtension(session: Electron.Session, update: ExtensionUpdate) {
200+
const oldExtension = update.extension
201+
d('updating %s %s -> %s', update.id, oldExtension.version, update.version)
202+
203+
// Updates must be installed in adjacent directories. Ensure the old install
204+
// was contained in a versioned directory structure.
205+
const oldVersionDirectoryName = path.basename(oldExtension.path)
206+
if (!oldVersionDirectoryName.startsWith(oldExtension.version)) {
207+
console.error(
208+
`updateExtension: extension ${update.id} must conform to versioned directory names`,
209+
{
210+
oldPath: oldExtension.path,
211+
},
212+
)
213+
d('skipping %s update due to invalid install path %s', update.id, oldExtension.path)
214+
return
215+
}
216+
217+
// Download update
218+
const updateDir = path.join(oldExtension.path, '..', `${update.version}_0`)
203219
await downloadCrx(update.url, updateDir)
204-
d('updated %s', update.id)
205-
// TODO: load new extension version
220+
d('downloaded update %s@%s', update.id, update.version)
221+
222+
// Replace extension
223+
session.removeExtension(update.id)
224+
await session.loadExtension(updateDir)
225+
d('loaded update %s@%s', update.id, update.version)
226+
227+
// TODO: remove old extension
206228
}
207229

208-
async function checkForUpdates(extensions: Electron.Extension[]) {
209-
d('checking for updates', extensions)
230+
async function checkForUpdates(session: Electron.Session, extensions: Electron.Extension[]) {
231+
d('checking for updates: %s', extensions.map((ext) => `${ext.id}@${ext.version}`).join(','))
210232

211233
const updates = await requestExtensionUpdates(extensions)
212-
if (!updates) {
234+
if (!updates || updates.length === 0) {
213235
d('no updates found')
214236
return
215237
}
216238

217-
d('updating %d extensions', updates.length)
239+
d('updating %d extension(s)', updates.length)
218240
for (const update of updates) {
219-
await updateExtension(update)
241+
await updateExtension(session, update)
220242
}
221243
}
222244

@@ -246,7 +268,7 @@ async function maybeCheckForUpdates(session: Electron.Session) {
246268
return
247269
}
248270

249-
await checkForUpdates(extensions)
271+
await checkForUpdates(session, extensions)
250272
}
251273

252274
export async function initUpdater(state: WebStoreState) {

0 commit comments

Comments
 (0)