如何从shell脚本调用webpack并获取错误
我已经用它制作了一个玩具应用程序,我想用一个shell脚本简化部署过程 package.json的一个片段如下所示如何从shell脚本调用webpack并获取错误,shell,npm,webpack,yeoman,Shell,Npm,Webpack,Yeoman,我已经用它制作了一个玩具应用程序,我想用一个shell脚本简化部署过程 package.json的一个片段如下所示 "scripts": { "clean": "rimraf dist/*", "copy": "copyfiles -f ./src/index.html ./src/favicon.ico ./dist", "dist": "npm run copy & webpack --env=dist", #!/bin/sh if [[ -z $(git statu
"scripts": {
"clean": "rimraf dist/*",
"copy": "copyfiles -f ./src/index.html ./src/favicon.ico ./dist",
"dist": "npm run copy & webpack --env=dist",
#!/bin/sh
if [[ -z $(git status -s) ]]
then
npm run clean
npm run dist
#
# My problem is here
#
# want, if no errors in my code, commit and push subtree
else
echo "must commit changes before running"
exit
fi
Hash: e4e23433b4c1b1ab7a97
Version: webpack 1.15.0
Time: 6140ms
Asset Size Chunks Chunk Names
app.js 188 kB 0 [emitted] main
app.js.map 1.95 MB 0 [emitted] main
+ 231 hidden modules
WARNING in ./src/components/MyComponent.js
/Users/path_to_my_project/MyComponent.js
19:13 warning 'j' is defined but never used no-unused-vars
✖ 1 problem (0 errors, 1 warning)
WARNING in app.js from UglifyJs
Condition always false [./~/fbjs/lib/invariant.js:26,0]
Dropping unreachable code [./~/fbjs/lib/invariant.js:27,0]
...a gazzillion more warnings like these, unrelated to my code
有了这个,我当前的过程是提交更改(使用git),然后npm运行clean
,然后npm运行dist
,然后再次提交发布#提交消息,真正使用git子树推送…
我想用一个脚本来自动化这个。以下是我目前掌握的情况:
首先,我在包json中添加了对脚本的调用
...
"deploy": "./deploy2server.sh",
...
在“部署到服务器”中,我仅在有未提交的更改时才继续,并启动手动过程,如下所示
"scripts": {
"clean": "rimraf dist/*",
"copy": "copyfiles -f ./src/index.html ./src/favicon.ico ./dist",
"dist": "npm run copy & webpack --env=dist",
#!/bin/sh
if [[ -z $(git status -s) ]]
then
npm run clean
npm run dist
#
# My problem is here
#
# want, if no errors in my code, commit and push subtree
else
echo "must commit changes before running"
exit
fi
Hash: e4e23433b4c1b1ab7a97
Version: webpack 1.15.0
Time: 6140ms
Asset Size Chunks Chunk Names
app.js 188 kB 0 [emitted] main
app.js.map 1.95 MB 0 [emitted] main
+ 231 hidden modules
WARNING in ./src/components/MyComponent.js
/Users/path_to_my_project/MyComponent.js
19:13 warning 'j' is defined but never used no-unused-vars
✖ 1 problem (0 errors, 1 warning)
WARNING in app.js from UglifyJs
Condition always false [./~/fbjs/lib/invariant.js:26,0]
Dropping unreachable code [./~/fbjs/lib/invariant.js:27,0]
...a gazzillion more warnings like these, unrelated to my code
我的问题是,我需要从网页中检测有关我的代码的错误和警告。网页包控制台输出如下所示
"scripts": {
"clean": "rimraf dist/*",
"copy": "copyfiles -f ./src/index.html ./src/favicon.ico ./dist",
"dist": "npm run copy & webpack --env=dist",
#!/bin/sh
if [[ -z $(git status -s) ]]
then
npm run clean
npm run dist
#
# My problem is here
#
# want, if no errors in my code, commit and push subtree
else
echo "must commit changes before running"
exit
fi
Hash: e4e23433b4c1b1ab7a97
Version: webpack 1.15.0
Time: 6140ms
Asset Size Chunks Chunk Names
app.js 188 kB 0 [emitted] main
app.js.map 1.95 MB 0 [emitted] main
+ 231 hidden modules
WARNING in ./src/components/MyComponent.js
/Users/path_to_my_project/MyComponent.js
19:13 warning 'j' is defined but never used no-unused-vars
✖ 1 problem (0 errors, 1 warning)
WARNING in app.js from UglifyJs
Condition always false [./~/fbjs/lib/invariant.js:26,0]
Dropping unreachable code [./~/fbjs/lib/invariant.js:27,0]
...a gazzillion more warnings like these, unrelated to my code
我的shell脚本如何才能发现代码中是否只有错误或警告,如果没有错误或警告,如何继续执行?
我唯一的想法是将所有输出通过管道传输到一个tmp文件,然后解析它,查找我关心的错误/警告,但我真的不知道如何编写这样的解析(在grep中?)并返回结果
如果有什么帮助的话,我找到了如何直接从脚本调用webpack的方法,使用
node./node\u modules/webpack/bin/webpack--env=dist
,但我仍然不知道如何获得输出或理解它。?有没有最不受欢迎的问题的徽章
我通过学习set-e
解决了这个问题,它告诉脚本在随后的命令中出现任何错误时退出。现在,我的部署脚本如下所示:
#!/bin/sh
if [[ -z $(git status -s) ]]
then
read -p "Deployment commit message: " message
set -e
npm run clean
npm run dist
git add -A
git commit -m "$message"
git subtree push --prefix dist origin gh-pages
else
echo "must commit changes before running"
exit
fi
我将使用部署提交消息来指示版本,或在日志中容易搜索的内容