Skip to content

Commit 5864264

Browse files
committed
fix: conditionally reload updated extensions
fix: updateExtensions should have optional arg
1 parent 33c9f0b commit 5864264

File tree

1 file changed

+15
-12
lines changed
  • packages/electron-chrome-web-store/src/browser

1 file changed

+15
-12
lines changed

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

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -208,38 +208,41 @@ async function fetchAvailableUpdates(extensions: Electron.Extension[]): Promise<
208208
}
209209

210210
async function updateExtension(session: Electron.Session, update: ExtensionUpdate) {
211+
const extensionId = update.id
211212
const oldExtension = update.extension
212-
d('updating %s %s -> %s', update.id, oldExtension.version, update.version)
213+
d('updating %s %s -> %s', extensionId, oldExtension.version, update.version)
213214

214215
// Updates must be installed in adjacent directories. Ensure the old install
215216
// was contained in a versioned directory structure.
216217
const oldVersionDirectoryName = path.basename(oldExtension.path)
217218
if (!oldVersionDirectoryName.startsWith(oldExtension.version)) {
218219
console.error(
219-
`updateExtension: extension ${update.id} must conform to versioned directory names`,
220+
`updateExtension: extension ${extensionId} must conform to versioned directory names`,
220221
{
221222
oldPath: oldExtension.path,
222223
},
223224
)
224-
d('skipping %s update due to invalid install path %s', update.id, oldExtension.path)
225+
d('skipping %s update due to invalid install path %s', extensionId, oldExtension.path)
225226
return
226227
}
227228

228229
// Download update
229230
const extensionsPath = path.join(oldExtension.path, '..', '..')
230-
const updatePath = await downloadExtensionFromURL(update.url, extensionsPath, update.id)
231-
d('downloaded update %s@%s', update.id, update.version)
232-
233-
// Replace extension
234-
session.removeExtension(update.id)
235-
await session.loadExtension(updatePath)
236-
d('loaded update %s@%s', update.id, update.version)
231+
const updatePath = await downloadExtensionFromURL(update.url, extensionsPath, extensionId)
232+
d('downloaded update %s@%s', extensionId, update.version)
233+
234+
// Reload extension if already loaded
235+
if (session.getExtension(extensionId)) {
236+
session.removeExtension(extensionId)
237+
await session.loadExtension(updatePath)
238+
d('loaded update %s@%s', extensionId, update.version)
239+
}
237240

238241
// Remove old version
239242
await fs.promises.rm(oldExtension.path, { recursive: true, force: true })
240243
}
241244

242-
async function checkForUpdates(session: Electron.Session = electronSession.defaultSession) {
245+
async function checkForUpdates(session: Electron.Session) {
243246
// Only check for extensions from the store
244247
const extensions = session.getAllExtensions().filter(filterWebStoreExtension)
245248
d('checking for updates: %s', extensions.map((ext) => `${ext.id}@${ext.version}`).join(','))
@@ -268,7 +271,7 @@ async function installUpdates(session: Electron.Session, updates: ExtensionUpdat
268271
/**
269272
* Check session's loaded extensions for updates and install any if available.
270273
*/
271-
export async function updateExtensions(session: Electron.Session): Promise<void> {
274+
export async function updateExtensions(session: Electron.Session = electronSession.defaultSession): Promise<void> {
272275
const updates = await checkForUpdates(session)
273276
if (updates.length > 0) {
274277
await installUpdates(session, updates)

0 commit comments

Comments
 (0)