-
-
Notifications
You must be signed in to change notification settings - Fork 6.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4d03124
commit 5d9bfd9
Showing
18 changed files
with
156 additions
and
162 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import colors from 'picocolors' | ||
import type { Logger } from './logger' | ||
import type { ResolvedConfig, ResolvedEnvironmentOptions } from './config' | ||
import type { EnvironmentPlugin } from './plugin' | ||
|
||
export class BaseEnvironment { | ||
name: string | ||
config: ResolvedConfig | ||
options: ResolvedEnvironmentOptions | ||
logger: Logger | ||
get plugins(): EnvironmentPlugin[] { | ||
if (!this._plugins) | ||
throw new Error( | ||
`${this.name} environment.plugins called before initialized`, | ||
) | ||
return this._plugins | ||
} | ||
|
||
/** | ||
* @internal | ||
*/ | ||
_plugins: EnvironmentPlugin[] | undefined | ||
/** | ||
* @internal | ||
*/ | ||
_initiated: boolean = false | ||
|
||
constructor( | ||
name: string, | ||
config: ResolvedConfig, | ||
options: ResolvedEnvironmentOptions = config.environments[name], | ||
) { | ||
this.name = name | ||
this.config = config | ||
this.options = options | ||
const environment = colors.dim(`(${this.name})`) | ||
const colorIndex = | ||
[...environment].reduce((acc, c) => acc + c.charCodeAt(0), 0) % | ||
environmentColors.length | ||
const infoColor = environmentColors[colorIndex || 0] | ||
this.logger = { | ||
get hasWarned() { | ||
return config.logger.hasWarned | ||
}, | ||
info(msg, opts) { | ||
return config.logger.info(msg, { | ||
...opts, | ||
environment: infoColor(environment), | ||
}) | ||
}, | ||
warn(msg, opts) { | ||
return config.logger.warn(msg, { | ||
...opts, | ||
environment: colors.yellow(environment), | ||
}) | ||
}, | ||
warnOnce(msg, opts) { | ||
return config.logger.warnOnce(msg, { | ||
...opts, | ||
environment: colors.yellow(environment), | ||
}) | ||
}, | ||
error(msg, opts) { | ||
return config.logger.error(msg, { | ||
...opts, | ||
environment: colors.red(environment), | ||
}) | ||
}, | ||
clearScreen(type) { | ||
return config.logger.clearScreen(type) | ||
}, | ||
hasErrorLogged(error) { | ||
return config.logger.hasErrorLogged(error) | ||
}, | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* This is used both to avoid users to hardcode conditions like | ||
* !scan && !build => dev | ||
* and for us to use a light environment in unit test | ||
*/ | ||
export class FutureCompatEnvironment extends BaseEnvironment { | ||
mode = 'futureCompat' as const | ||
} | ||
|
||
const environmentColors = [ | ||
colors.blue, | ||
colors.magenta, | ||
colors.green, | ||
colors.gray, | ||
] |
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
Oops, something went wrong.