-
-
Notifications
You must be signed in to change notification settings - Fork 6.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: rename to BaseEnvironment (#16797)
- Loading branch information
1 parent
4d03124
commit d86553a
Showing
19 changed files
with
204 additions
and
193 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,103 @@ | ||
import colors from 'picocolors' | ||
import type { Logger } from './logger' | ||
import type { ResolvedConfig, ResolvedEnvironmentOptions } from './config' | ||
import type { EnvironmentPlugin } from './plugin' | ||
|
||
export class PartialEnvironment { | ||
name: string | ||
config: ResolvedConfig | ||
options: ResolvedEnvironmentOptions | ||
logger: Logger | ||
|
||
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) | ||
}, | ||
} | ||
} | ||
} | ||
|
||
export class BaseEnvironment extends PartialEnvironment { | ||
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], | ||
) { | ||
super(name, config, options) | ||
} | ||
} | ||
|
||
/** | ||
* This is used both to avoid users to hardcode conditions like | ||
* !scan && !build => dev | ||
*/ | ||
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
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.