-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement interactive/non-interactive contexts; add task and utils
- Loading branch information
Showing
10 changed files
with
108 additions
and
6 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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 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 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
export * from './announce'; | ||
export * from './clear'; | ||
export * from './interactive'; | ||
export * from './log'; | ||
export * from './print'; | ||
export * from './select'; |
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,35 @@ | ||
import { Task, Context } from '../../definitions'; | ||
import { isInteractive } from '../../utils/is-interactive'; | ||
import { run } from '../../utils/run'; | ||
import { raises } from '../exception/raises'; | ||
import { log } from './log'; | ||
import { Empty } from 'type-core'; | ||
import { into } from 'pipettes'; | ||
|
||
/** | ||
* Marks a task as interactive. | ||
* Will error on non-interactive environments | ||
* unless an `alternate` task is provided. | ||
* @returns Task | ||
*/ | ||
export function interactive(task: Task, alternate: Task | Empty): Task.Async { | ||
return async (ctx: Context): Promise<void> => { | ||
const interactive = isInteractive(ctx); | ||
|
||
into( | ||
ctx, | ||
log( | ||
'debug', | ||
interactive ? 'Interactive' : 'Non-interactive', | ||
'environment detected' | ||
) | ||
); | ||
|
||
return run( | ||
interactive | ||
? task | ||
: alternate || raises(Error('Non-interactive environment detected')), | ||
ctx | ||
); | ||
}; | ||
} |
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,6 @@ | ||
export * from './fetch'; | ||
export * from './is-cancelled'; | ||
export * from './is-ci'; | ||
export * from './is-interactive'; | ||
export * from './recreate'; | ||
export * from './run'; |
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,32 @@ | ||
import { Context } from '../definitions'; | ||
import { TypeGuard } from 'type-core'; | ||
import vendors from 'ci-info/vendors.json'; | ||
|
||
/** | ||
* Returns `true` when a context's environment | ||
* variables indicate it's running in a CI. | ||
*/ | ||
export function isCI(context: Context): boolean { | ||
if ('CI' in context.env || 'CONTINUOUS_INTEGRATION' in context.env) { | ||
return true; | ||
} | ||
|
||
const arr = vendors.map((vendor) => vendor.env); | ||
|
||
for (const env of arr) { | ||
if (TypeGuard.isString(env)) { | ||
if (env in context.env) return true; | ||
} else if (TypeGuard.isArray(env)) { | ||
const present = env.filter((env) => env in context.env); | ||
if (present.length === env.length) return true; | ||
} else { | ||
const entries = Object.entries(env); | ||
const matches = entries.filter( | ||
([key, value]) => context.env[key] === value | ||
); | ||
if (entries.length === matches.length) return true; | ||
} | ||
} | ||
|
||
return false; | ||
} |
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,20 @@ | ||
import { Context } from '../definitions'; | ||
import { isCI } from './is-ci'; | ||
|
||
/** | ||
* Returns `true` when a context belongs to an interactive | ||
* environment. | ||
* Ensures that the context interactive property is `true`, | ||
* the stdout is a non-dumb *TTY*, and it's not running in a CI. | ||
*/ | ||
export function isInteractive(context: Context): boolean { | ||
if (!context.interactive) return false; | ||
|
||
const stdout = context.stdio[1] as NodeJS.WriteStream; | ||
if (!stdout || !stdout.isTTY) return false; | ||
|
||
if (context.env.TERM === 'dumb') return false; | ||
if (isCI(context)) return false; | ||
|
||
return true; | ||
} |