The generator plugin exports a function by default in the main
file, and the function parameter is context
. All APIs provided by the generator plugin are provided by this context
.
Let's first understand the type definition of context
:
The contents provided by context
can be mainly divided into three categories:
Get the information of the current generator execution environment.
Operate input.
Generator plugin lifecycle function.
The generator plugin APIs will be introduced from these three categories below.
Gets the language of the generator plugin execution environment. @modern-js/create
provides two languages, zh
and en
, which correspond to these two values.
Checks whether a file exists. We often need to confirm whether the target project file exists before defining the operation. This API can be used directly for this purpose.
For example, we need to find out if the package.json
file exists and then do someing:
Gets the file list in a folder. We often need to get the file list under the target project folder before defining the operation. This API can be used directly for this purpose.
For example, we need to get all files under the src
folder and then do someing:
The key
parameter used in input operations can refer to Configuration Parameters and use the key under the solution’s scheme, that is, it does not support solution
and scenes
.
The input parameter in input operations corresponds to the schema type described in Input.
Adds a question before the specified input key
.
For example:
Adds a question after the specified input key
.
For example:
key
for adding a question cannot be the same as the key
of the question provided by Modern.js solution scheme itself.addInputAfter
is higher than that of addInputBefore
. When adding an After question to a key and adding a Before question to the next key, the After question will be before the Before question.key
, this method can be called multiple times, and the order of the questions will be arranged according to the calling sequence.Sets the attributes of the specified input key
.
For example, set the title
attribute of packageName
:
For input options provided by Modern.js solution schemes, only deletion is supported, and adding will cause logical judgment problems in the code.
Sets the default value of the specified input key
.
For example, set the default value of packageManager
:
After setting, the question still needs to be interactive, but the default value configured by the generator plugin will be used.
Sets the default value of the specified input key
.
For example, set the default value of packageManager
:
After setting, the corresponding question will no longer be displayed, which is consistent with the --config
behavior specified by @modern-js/create
. Does not support setting vertical
and projectOrg
.
Sets the initial git commit message. Modern.js defaults to the git initialization commit message as feat: init
, which can be modified by this function.
The lifecycle functions are relatively complex and will be introduced separately in the following two sections, onForged
and afterForged
.