File: pkgjs-run

package info (click to toggle)
pkg-js-tools 0.16.0
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,992 kB
  • sloc: perl: 4,975; sh: 840; makefile: 36; javascript: 22
file content (107 lines) | stat: -rwxr-xr-x 2,306 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/bin/bash

set -e

NOFAILONMISSING=no
if test "$1" = "--if-exist"; then
	shift
	NOFAILONMISSING=yes
fi

TARGET=$1
shift

ARGS=""
FOUND=0
while [ "$1" != "" ]; do
        if [ "$1" == "--" ]; then
                FOUND=1
        elif [ $FOUND != 0 ]; then
                ARGS="$ARGS $1"
        fi
        shift
done

function usage() {
	cat <<EOT
Usage: pkgjs-run <target>

Launch script defined in "package.json -> scripts -> <target>" using sh like
a "npm run".

Features:
 - add node_modules/.bin in PATH
 - ignore git, husky, sudo and su commands
 - replace all "npm run" by pkgjs-run
 - replace all "npm install" by pkgjs-install --ignore --no-download
EOT
}

if test "$TARGET" = ""; then
	usage
	exit 1
fi
if test "$TARGET" = "--help" || test "$TARGET" = "-h"; then
	usage
	exit
fi
if test "$TARGET" = "--version" || test "$TARGET" = "-v"; then
    perl -MDebian::PkgJs::Version -le 'print $VERSION'
    exit
fi

COMMAND=`pkgjs-pjson . scripts $TARGET|perl -pe 's/cross-env//g;s/\bp?npm\b/pkgjs/g;s/\byarn\b/pkgjs/g;s/\blerna\s+run\b/pkgjs-lerna run/g'`
if test "$COMMAND" = ""; then
	echo "Target $TARGET is not defined in package.json" >&2
	if test "$NOFAILONMISSING" = "no"; then
		exit 1
	else
		exit 0
	fi
fi

# Fake git / husky
BADCOMMANDS=${BADCOMMANDS:-"git husky su sudo"}
mkdir -p node_modules/.bin || true
for command in $BADCOMMANDS; do
	rm -f node_modules/.bin/$command
	ln -s /usr/bin/true node_modules/.bin/$command
done

# Aliases
ALIASES=${ALIASES:-"babel,/usr/bin/babeljs yarn,/usr/bin/pkg npm,/urn/bin/pkg pnpm,/usr/bin/pkg"}
for pair in $ALIASES; do
	rm -f node_modules/.bin/${pair%,*}
	ln -s ${pair#*,} node_modules/.bin/${pair%,*}
	BADCOMMANDS="$BADCOMMANDS ${pair%,*}"
done

NAME=`pkgjs-pjson . name`
VERSION=`pkgjs-pjson . version`

if test "$VERSION" != ""; then
	NAME="$NAME@$VERSION"
fi

# Main: launch wanted command
export PATH="node_modules/.bin:$PATH"
CODE=0

# Same output than "npm run"
echo
if test "$NAME" != ""; then
	echo "> $NAME $TARGET"
fi
echo "> $COMMAND $ARGS"
echo
sh -c "KEEP_COMMAND_LINKS=1 $COMMAND $ARGS" || CODE=$?

# Clean our stuff
if test "$KEEP_COMMAND_LINKS" = ""; then
	for command in $BADCOMMANDS; do
		rm -f node_modules/.bin/$command
	done
	rmdir node_modules/.bin 2>/dev/null || true
	rmdir node_modules 2>/dev/null || true
fi
exit $CODE