( opts: IUpdateElectronAppOptions<L> )
| 159 | } |
| 160 | |
| 161 | function validateInput<L extends ILogger = ILogger>( |
| 162 | opts: IUpdateElectronAppOptions<L> |
| 163 | ): IUpdateElectronAppOptions<L> { |
| 164 | const defaults = { |
| 165 | host: 'https://update.electronjs.org', |
| 166 | updateInterval: '10 minutes', |
| 167 | logger: console, |
| 168 | notifyUser: true, |
| 169 | }; |
| 170 | const { host, updateInterval, logger, notifyUser } = Object.assign( |
| 171 | {}, |
| 172 | defaults, |
| 173 | opts |
| 174 | ); |
| 175 | |
| 176 | // allows electron to be mocked in tests |
| 177 | const electron = opts.electron || require('electron'); |
| 178 | |
| 179 | let repo = opts.repo; |
| 180 | if (!repo) { |
| 181 | const pkgBuf = fs.readFileSync( |
| 182 | path.join(electron.app.getAppPath(), 'package.json') |
| 183 | ); |
| 184 | const pkg = JSON.parse(pkgBuf.toString()); |
| 185 | const repoString = (pkg.repository && pkg.repository.url) || pkg.repository; |
| 186 | const repoObject = gh(repoString); |
| 187 | assert( |
| 188 | repoObject, |
| 189 | "repo not found. Add repository string to your app's package.json file" |
| 190 | ); |
| 191 | repo = `${repoObject.user}/${repoObject.repo}`; |
| 192 | } |
| 193 | |
| 194 | assert( |
| 195 | repo && repo.length && repo.includes('/'), |
| 196 | 'repo is required and should be in the format `owner/repo`' |
| 197 | ); |
| 198 | |
| 199 | assert( |
| 200 | isURL(host) && host.startsWith('https'), |
| 201 | 'host must be a valid HTTPS URL' |
| 202 | ); |
| 203 | |
| 204 | assert( |
| 205 | typeof updateInterval === 'string' && updateInterval.match(/^\d+/), |
| 206 | 'updateInterval must be a human-friendly string interval like `20 minutes`' |
| 207 | ); |
| 208 | |
| 209 | assert( |
| 210 | ms(updateInterval) >= 5 * 60 * 1000, |
| 211 | 'updateInterval must be `5 minutes` or more' |
| 212 | ); |
| 213 | |
| 214 | assert(logger && typeof logger.log, 'function'); |
| 215 | |
| 216 | return { host, repo, updateInterval, logger, electron, notifyUser }; |
| 217 | } |
no test coverage detected