import { autoUpdater } from 'electron-updater';
autoUpdater의 주요 메소드 목록
1. autoUpdater.checkForUpdates()
- 업데이트 서버에 연결하여 새 버전이 있는지 확인
- 새로운 업데이트가 있는 경우 이벤트(
update-available)를 트리거
2. autoUpdater.checkForUpdatesAndNotify()
- 새 버전을 확인하고 알림을 띄워 줌
- 업데이트가 있을 때 사용자에게 자동 알림을 보여줌
3. autoUpdater.downloadUpdate()
- 업데이트 파일을 다운로드
- 다운로드가 완료되면
update-downloaded이벤트가 발생
4. autoUpdater.quitAndInstall(isSilent?: boolean, isForceRunAfter?: boolean)
- 업데이트를 적용하고 앱을 종료한 뒤 재시작
isSilent: 사용자에게 재시작 알림을 표시하지 않고 조용히 설치isForceRunAfter: 업데이트 후 앱을 자동으로 재실행
5. autoUpdater.getFeedURL()
- 업데이트 서버의 URL을 반환
6. autoUpdater.setFeedURL(options)
- 업데이트 서버를 동적으로 설정
- 옵션 예시:
autoUpdater.setFeedURL({ url: '<https://example.com/updates>', headers: { Authorization: 'Bearer <token>' }, });
7. autoUpdater.autoDownload (프로퍼티)
- 기본값:
true
false로 설정하면 사용자가 직접 다운로드를 시작해야 함
8. autoUpdater.autoInstallOnAppQuit (프로퍼티)
- 기본값:
true
- 앱이 종료될 때 자동으로 업데이트를 설치할지 여부를 결정
9. on(event, listener)
- 특정 이벤트에 대한 리스너를 등록.
- 예를 들어, update-downloaded, update-available 등의 이벤트를 수신할 수 있음
removeListener(event, listener): 특정 이벤트에 등록된 리스너를 제거
autoUpdater의 주요 이벤트 목록
1. error
- 업데이트 도중 오류가 발생했을 때 호출됨
autoUpdater.on('error', (error) => { console.error('업데이트 오류:', error); });
2. update-available
- 새로운 버전이 존재할 때 발생
autoUpdater.on('update-available', () => { console.log('업데이트가 있습니다.'); });
3. update-not-available
- 사용 중인 버전이 최신일 때 호출됨
autoUpdater.on('update-not-available', () => { console.log('현재 최신 버전입니다.'); });
4. download-progress
- 다운로드 진행 상황을 나타냄
- 진행률과 속도 정보를 포함한 객체를 전달
autoUpdater.on('download-progress', (progress) => { console.log(`진행률: ${progress.percent}%`); });
5. update-downloaded
- 다운로드가 완료된 후 발생하며, 업데이트를 설치할 준비가 되었음을 의미
autoUpdater.on('update-downloaded', () => { console.log('업데이트 다운로드 완료.'); });
코드 예시: autoUpdater 메소드와 이벤트 활용
const { autoUpdater, dialog } = require('electron'); // 새로운 업데이트가 있는지 확인 autoUpdater.checkForUpdates(); // 업데이트가 가능할 때 autoUpdater.on('update-available', () => { dialog.showMessageBox({ type: 'info', title: '업데이트 알림', message: '새로운 버전이 있습니다. 다운로드 하시겠습니까?', buttons: ['다운로드', '나중에'], }).then((result) => { if (result.response === 0) { autoUpdater.downloadUpdate(); // 다운로드 시작 } }); }); // 다운로드 진행 상황 표시 autoUpdater.on('download-progress', (progress) => { console.log(`다운로드 진행률: ${progress.percent}%`); }); // 다운로드 완료 후 설치 여부 확인 autoUpdater.on('update-downloaded', () => { dialog.showMessageBox({ type: 'info', title: '업데이트 준비 완료', message: '지금 설치하시겠습니까?', buttons: ['설치', '나중에'], }).then((result) => { if (result.response === 0) { autoUpdater.quitAndInstall(); // 설치 및 재시작 } }); }); // 오류 처리 autoUpdater.on('error', (error) => { console.error('업데이트 오류:', error); });