Runs a user-defined script.
In the scripts object you can configure multiple scripts to run, each of which is in the form of "key": "value". In the following example, scriptName1, scriptName2, and scriptName3 are script aliases, followed by a colon and the corresponding script content, such as "echo hello".
{
"scripts": {
"scriptName1": "echo hello",
"scriptName2": "ohpm run scriptName1",
// After the identifier (--), the parameter key can be specified in the form of -p or --p, and the parameter value can be connected via a space ( ) or an equals sign (=).
"scriptName3": "node test.js -- -paramKey1 paramValue1 -paramKey2=paramValue2 --paramKey3 paramValue3"
}
} ohpm run scriptName3 -- -paramKey1 newValue -paramKey4 paramValue4
The preceding command replaces the paramKey1 parameter in the scriptName3 script with newValue and adds the paramKey4 parameter.
Two types of command connectors are supported: && and ||. (&& and || are not prioritized. Commands are executed from left to right. Parentheses cannot be used to change the priority of each subcommand.) For details, see Example.
Item | Description |
|---|---|
Constraint on scriptKey naming | A valid scriptKey name can contain uppercase and lowercase letters, digits, ASCII colons (:), ASCII underscores (_), and ASCII hyphens (-), and must start with a lowercase letter. |
Constraint on scriptContent | A valid scriptContent cannot reference any ohpm commands except ohpm run. |
Constraint on using ohpm run in scriptContent | 1. Aliases of other scripts on which ohpm run depends must exist in the same oh-package.json5 file. 2. The reference relationship of ohpm run is a DAG and does not support recursive or circular reference. |
You can use --prefix to specify the root directory of the package. The root directory must contain the oh-package.json5 file. The script alias in the dependency package cannot be invoked by running the following command:
ohpm run --prefix <path> <script alias >
In ohpm 6.0.2.636 and later versions, you can append the --log_level <string> parameter to the command to specify the log level (info, debug, warn, or error) for the execution of the current command. If this parameter is not set, the log_level setting in the .ohpmrc file is applied.
In ohpm 6.0.2.636 and later versions, you can append the --debug parameter to the command to set the log level to debug for execution of the current command. This configuration takes effect only on the current command line and does not alter the log level configured in the .ohpmrc file. If this parameter is not set, the log_level setting in the .ohpmrc file is applied.
The scripts configurations in the sample code are all from the oh-package.json5 file:
{
"name": "example",
"version": "1.0.0",
"description": "this is an example for ohpm run.",
"main": "./src/index.ets",
"author": "oh",
"license": "ISC",
"scripts": {
"testLogic": "ohpm run testFail || ohpm run testSuc && ohpm run testSuc",
"testFail": "test1",
"testSuc": "echo hello"
}
} ohpm run script_name -- -arg1=1 --arg2=2 -arg3 3 --arg4 4
Execute the script_name script and set the values of arg1, arg2, arg3, and arg4 to 1, 2, 3, and 4, respectively. In the preceding code, all the four forms are valid and equivalent in effect.
Run the following command to execute the testSuc script:
ohpm run testSuc
The execution result is as follows:
