如何用 pre script 组织 npm script 子任务

问题描述 投票:0回答:0

我的第一个 npm 脚本

package.json(我在 github 中创建了一个 showcase-repo

{
  "script": {
    "pre": "echo \">>>>>>  running pre ...\" && npm-run-all --serial pre:preA pre:preB",
    "pre:preA": "echo \">>>>>>  running pre:preA ...\" && exit 0",
    "pre:preB": "echo \">>>>>>  running pre:preB ...\" && exit 0",
    "build": "echo \">>>>>>  running build ...\" && npm-run-all --serial pre build:*",
    "prebuild:modA": "npm run pre:preA",
    "build:modA": "echo \">>>>>>  running build:modA ...\" && exit 0",
    "prebuild:modB": "npm run pre:preB",
    "build:modB": "echo \">>>>>>  running build:modB ...\" && exit 0"
  }
}

当我运行 sub mod build 时,一切顺利

 npm run build:modA

> [email protected] prebuild:modA
> npm run pre:preA


> [email protected] pre:preA
> echo ">>>>>>  running pre:preA ..." && exit 0


> [email protected] build:modA
> echo ">>>>>>  running build:modA ..." && exit 0

">>>>>>  running build:modA ..."
npm run build:modB

> [email protected] prebuild:modB
> npm run pre:preB


> [email protected] pre:preB
> echo ">>>>>>  running pre:preB ..." && exit 0

">>>>>>  running pre:preB ..."

> [email protected] build:modB
> echo ">>>>>>  running build:modB ..." && exit 0

">>>>>>  running build:modB ..."

但是如果我运行构建,pre subtask 运行 2 次(如果有更多的 subtask,它会运行更多次)

npm run build

> [email protected] build
> echo ">>>>>>  running build ..." && npm-run-all --serial pre build:*

">>>>>>  running build ..." 

> [email protected] pre
> echo ">>>>>>  running pre ..." && npm-run-all --serial pre:preA pre:preB

">>>>>>  running pre ..." 

> [email protected] pre:preA
> echo ">>>>>>  running pre:preA ..." && exit 0

">>>>>>  running pre:preA ..." 

> [email protected] pre:preB
> echo ">>>>>>  running pre:preB ..." && exit 0

">>>>>>  running pre:preB ..." 

> [email protected] prebuild:modA
> npm run pre:preA


> [email protected] pre:preA
> echo ">>>>>>  running pre:preA ..." && exit 0

">>>>>>  running pre:preA ..."

> [email protected] build:modA
> echo ">>>>>>  running build:modA ..." && exit 0

">>>>>>  running build:modA ..."

> [email protected] prebuild:modB
> npm run pre:preB


> [email protected] pre:preB
> echo ">>>>>>  running pre:preB ..." && exit 0

">>>>>>  running pre:preB ..."

> [email protected] build:modB
> echo ">>>>>>  running build:modB ..." && exit 0

">>>>>>  running build:modB ..."

问题

如何组织 npm 脚本:

  • 运行构建时:modA
# run buid:modA's pre script
run pre:preA

# then run build:modA it self
run build:modA
  • 运行构建时,
# run build's pre script
run pre:A
run pre:B

# then run build's sub task

# below here is important part! (also be the question point): 

# before run build:modA
# how to detect that the pre:preA already run in this workflow instance, so we can skip it ?
run build:modA

# before run build:modB
# also how to skip pre:preB
run build:modB

如果不使用 pre script 手动处理每个子任务的 prepare 和 build ,就可以了。

但我不想写大量的硬编码任务安排脚本

所以,有没有办法以优雅的方式组织我的子任务?

谢谢!

npm-scripts
© www.soinside.com 2019 - 2024. All rights reserved.