-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: improve error handling with update check (#76)
- Loading branch information
Showing
15 changed files
with
100,892 additions
and
90,514 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
import * as core from '@actions/core'; | ||
import { run } from './run'; | ||
import { handleError } from './tools'; | ||
|
||
run().catch(core.setFailed); | ||
run().catch(handleError); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
import * as core from '@actions/core'; | ||
import * as cli from '@actions/exec'; | ||
import semver from 'semver'; | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-var-requires | ||
const registry = require('libnpm'); | ||
|
||
type AuthenticateOptions = { | ||
token?: string; | ||
username?: string; | ||
password?: string; | ||
}; | ||
|
||
/** | ||
* Resolve the provided semver to exact version of `expo-cli`. | ||
* This uses the npm registry and accepts latest, dist-tags or version ranges. | ||
* It's used to determine the cached version of `expo-cli`. | ||
*/ | ||
export async function resolveVersion(version: string): Promise<string> { | ||
return (await registry.manifest(`expo-cli@${version}`)).version; | ||
} | ||
|
||
/** | ||
* Authenticate with Expo using either the token or username/password method. | ||
* If both of them are set, token has priority. | ||
*/ | ||
export async function maybeAuthenticate(options: AuthenticateOptions = {}): Promise<void> { | ||
// github actions toolkit will handle commands with `.cmd` on windows, we need that | ||
const bin = process.platform === 'win32' ? 'expo.cmd' : 'expo'; | ||
|
||
if (options.token) { | ||
await cli.exec(bin, ['whoami'], { | ||
env: { ...process.env, EXPO_TOKEN: options.token }, | ||
}); | ||
return core.exportVariable('EXPO_TOKEN', options.token); | ||
} | ||
|
||
if (options.username || options.password) { | ||
if (!options.username || !options.password) { | ||
return core.info('Skipping authentication: `expo-username` and/or `expo-password` not set...'); | ||
} | ||
await cli.exec(bin, ['login', `--username=${options.username}`], { | ||
env: { ...process.env, EXPO_CLI_PASSWORD: options.password }, | ||
}); | ||
return; | ||
} | ||
|
||
core.info('Skipping authentication: `expo-token`, `expo-username`, and/or `expo-password` not set...'); | ||
} | ||
|
||
/** | ||
* Try to patch the default watcher/inotify limit. | ||
* This is a limitation from GitHub Actions and might be an issue in some Expo projects. | ||
* It sets the system's `fs.inotify` limits to a more sensible setting. | ||
* | ||
* @see https://github.com/expo/expo-github-action/issues/20 | ||
*/ | ||
export async function maybePatchWatchers(): Promise<void> { | ||
if (process.platform !== 'linux') { | ||
return core.info('Skipping patch for watchers, not running on Linux...'); | ||
} | ||
|
||
core.info('Patching system watchers for the `ENOSPC` error...'); | ||
|
||
try { | ||
// see https://github.com/expo/expo-cli/issues/277#issuecomment-452685177 | ||
await cli.exec('sudo sysctl fs.inotify.max_user_instances=524288'); | ||
await cli.exec('sudo sysctl fs.inotify.max_user_watches=524288'); | ||
await cli.exec('sudo sysctl fs.inotify.max_queued_events=524288'); | ||
await cli.exec('sudo sysctl -p'); | ||
} catch { | ||
core.warning("Looks like we can't patch watchers/inotify limits, you might encouter the `ENOSPC` error."); | ||
core.warning('For more info, https://github.com/expo/expo-github-action/issues/20'); | ||
} | ||
} | ||
|
||
/** | ||
* Check if there is a new major version available. | ||
* If there is, create a warning for people to upgrade their workflow. | ||
* Because this introduces additional requests, it should only be executed when necessary. | ||
*/ | ||
export async function maybeWarnForUpdate(): Promise<void> { | ||
const latest = await resolveVersion('latest'); | ||
const current = await resolveVersion(core.getInput('expo-version') || 'latest'); | ||
|
||
if (semver.diff(latest, current) === 'major') { | ||
core.warning(`There is a new major version available of the Expo CLI (${latest})`); | ||
core.warning(`If you run into issues, try upgrading your workflow to "expo-version: ${semver.major(latest)}.x"`); | ||
} | ||
} | ||
|
||
/** | ||
* Handle errors when this action fails, providing useful next-steps for developers. | ||
* This mostly checks if the installed version is the latest version. | ||
*/ | ||
export async function handleError(error: Error) { | ||
try { | ||
await maybeWarnForUpdate(); | ||
} catch { | ||
// If this fails, ignore it | ||
} | ||
|
||
core.setFailed(error); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.