我正在创建内部包含3个不同项目的内部版本。对于其中2个,我需要进行npm安装,对于第三个,我需要在打包和创建构建之前进行Bower安装。因此,我在这里寻找优化机会并创建了此脚本:
install_dependency.sh
# $1 = project_1 $2=project_2 $3=project_3
npm install --global bower ng-cli && <!-- This runs 1st -->
echo "Global Installation complete" && <!-- This runs 2nd -->
node --version && npm --version && <!-- This runs 3rd -->
parallel --halt 2 ::: \ <!-- From here till END runs in parallel -->
"cd $1; npm install" \
"cd $1; npm install --only=dev" \
"cd $2; npm install" \
"cd $2; npm install --only=dev" \ <!-- "END" Till here it runs parallel-->
"cd $3; bower --allow-root install" && echo All is OK && <!-- It runs next -->
cd $1; npm run build_stage && echo build created && <!-- It runs next -->
cd $2; npm run build && echo build created <!-- It runs next -->
但是似乎我无法并行运行npm install,因为它会导致一些冲突并失败,并出现如下随机错误:
> node-sass@4.9.4 install /data/project/uiv2/node_modules/node-sass
> node scripts/install.js
npm WARN deprecated typings@1.5.0: Typings is deprecated in favor of NPM @types -- see README for more information
npm WARN deprecated browserslist@2.11.3: Browserslist 2 could fail on reading Browserslist >3.0 config used in other tools.
npm WARN deprecated gulp-util@3.0.7: gulp-util is deprecated - replace it, following the guidelines at https://medium.com/gulpjs/gulp-util-ca3b1f9f9ac5
npm WARN deprecated hoek@2.16.3: The major version is no longer supported. Please update to 4.x or newer
module.js:550
throw err;
^
Error: Cannot find module 'inherits'
at Function.Module._resolveFilename (module.js:548:15)
如果可能,请帮助我解决此问题。如果不是,那么什么是解决此问题的最佳方法。
答案 0 :(得分:1)
并行构建的问题可能可以通过docker化。例如,一个用于构建$ 1的命令:
docker run -it --rm -v $(pwd)/$1:/srv -w=/srv node:8.11.3 "npm install && npm install --only-dev && npm run build_stage && echo build created"
然后您没有事件更改目录。这将是:
docker run
-it
--rm
/srv
--v $(pwd)/$1:/srv
-w=/srv
node:8.11.3
npm install --global bower ng-cli && npm install && npm install --only-dev && npm run build_stage && echo build created
完成后,$1
下的项目将被构建,就像将其构建在主机上一样。
然后,您将拥有三个这样的命令(根据您的脚本),并且可以并行运行它们。为了说明脚本:
parallel --halt 2 ::: \
"docker run -it --rm -v $(pwd)/$1:/srv -w=/srv \
node:8.11.3 sh -c 'npm install --global bower ng-cli && npm install && npm install --only-dev && npm run build_stage && echo $1 build created'"
"docker run -it --rm -v $(pwd)/$2:/srv -w=/srv \
node:8.11.3 sh -c 'npm install --global bower ng-cli && npm install && npm install --only-dev && npm run build && echo $2 build created'"
"docker run -it --rm -v $(pwd)/$3:/srv -w=/srv \
node:8.11.3 sh -c 'npm install --global bower ng-cli && bower --allow-root install'"
请注意,此示例中的命令不是用于说明目的的准确猴子输入,并且未经测试