I've got a long command in deploy script.
It seems that it checks the repository for changes, and if there are any, it runs 'npm run build' but something is not working right.
(git diff -s --exit-code web/builder/ && test -d web/dist/js -a -d web/dist/css) || npm --prefix=web/builder run build_${SITE}
CodePudding user response:
The command consists of multiple parts:
git diff -s --exit-code web/builder/
This command exits with 1
if there web/builder/
changed and with 0
if not. Enter man git-diff
in your terminal for details.
test -d web/dist/js -a -d web/dist/css
This checks if web/dist/js
and web/dist/css
are both valid directories. Enter man test
in your terminal for details.
(git diff -s --exit-code web/builder/ && test -d web/dist/js -a -d web/dist/css)
This checks if web/builder
has not changed and web/dist/js
and web/dist/css
are both valid directories.
npm --prefix=web/builder run build_${SITE}
This is just an npm command.
(git diff -s --exit-code web/builder/ && test -d web/dist/js -a -d web/dist/css) || npm --prefix=web/builder run build_${SITE}
This runs the npm command if the previous condition is false
. This is the case if web/builder
changed or web/dist/js
is not a valid directory or web/dist/css
is not a valid directory.
CodePudding user response:
So, the command looks like
(a && b) || c
Which means that if a is false, then b will not be evaluated, c will be evaluated instead due to the optimizations on logical operands. If a is true and b is false, then c will be evaluated (executed). If a and b are true, then c will not be evaluated.
So, let's see the parts:
a: git diff -s --exit-code web/builder/
Checks whether there are differences. If so, the result will be 1 (true). Otherwise the result will be 0 (false). See
b: test -d web/dist/js -a -d web/dist/css
This checks whether the paths are valid directories.
c: npm --prefix=web/builder run build_${SITE}
This runs an npm
command.
Summary
If there are no differences on web/builder or web/dist/js and web/dist/css are not both valid directories, then an npm
command will be executed.