File: nodepath

package info (click to toggle)
pkg-js-tools 0.17.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,036 kB
  • sloc: perl: 5,006; sh: 840; makefile: 36; javascript: 22
file content (147 lines) | stat: -rwxr-xr-x 2,661 bytes parent folder | download | duplicates (2)
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/bin/bash

set -e

function usage()
{
cat <<EOF
Usage: nodepath <option> module

nodepath shows the file or the package that corresponds to the node given module.

Options:
 -p: print package
 -o: print only package (implies -p)
 -r: search also in related paths (**/node_modules)
 -B: ignore builtin modules
 -t: search for typescript declarations

Copyright (C) Yadd <yadd@debian.org>

Licensed under GPL-2+ (see /usr/share/common-licenses/GPL-2)
EOF
}

if test "$1" = "--version"; then
	echo `perl -MDebian::PkgJs::Version -e 'print $VERSION'`
	exit
fi
PACKAGE=0
PACKAGEONLY=0
RELATED=0
NOBUILTIN=0
TS=0
while getopts 'hporBt' opt; do
	case $opt in
		h)
			usage
			exit
			;;
		p)
			PACKAGE=1
			;;
		o)
			PACKAGE=1
			PACKAGEONLY=1
			;;
		r)
			RELATED=1
			;;
		B)
			NOBUILTIN=1
			;;
		t)
			TS=1
			;;
		*)
			echo "Unknown option $opt" >&2
			exit 1
			;;
	esac
done
shift $((OPTIND-1))
MODULE=$1

if test "$MODULE" = ""; then
	usage
	exit 1
fi

if test "$NOBUILTIN" = "0"; then
if node -e 'process.exit(require("module").builtinModules.includes(process.argv[1]) ? 0 : 1)' $MODULE; then
	if test "$TS" = "1"; then
		MODULE="/usr/share/nodejs/@types/node"
	fi
	if test "$PACKAGE" = "1"; then
		if test $PACKAGEONLY = 1; then
			echo nodejs
		else
			echo "nodejs: $MODULE"
		fi
	else
		echo $MODULE
	fi
	exit
fi
fi

function resolve()
{
	MOD=$1
	P=`(node -p "require.resolve('$MOD'+'/package.json')" 2>/dev/null || true) | perl -pe 's/\/package\.json$/$1/'`
	if [ "$P" == "" ]; then
		LOG=`mktemp`
		P=`(node -p "require.resolve('$MOD')" 2>$LOG|perl -pe 's/(nodejs\/(?:\@[^\/]*\/)?[^\@][^\/]*)(\/.*)?$/$1/') || true`
		if [ "$P" == "" ]; then
			if grep 'ERR_PACKAGE_PATH_NOT_EXPORTED' $LOG >/dev/null; then
				echo `perl -ne 'if(m#(/usr/\S+/nodejs/\S+)/package\.json#){print $1;exit}' $LOG`
			fi
		fi
		rm -f $LOG
	fi
	echo $P
}

if test $RELATED = "1"; then
	FILE=`resolve $MODULE`
else
	FILE=`(cd /;resolve $MODULE)`
fi
if [ "$FILE" == "" ]; then
	echo "Not found" >&2
	rm -f $LOG
	exit 1
fi

if test "$TS" = "1"; then
	TYPES=`pkgjs-pjson $FILE types`
	if test "$TYPES" = ""; then
		if test ! -e $FILE/index.d.ts; then
			TSMODULE=@types/`echo $MODULE|perl -pe 's#^\@(.*?)/#$1__#'`
			FILE=`nodepath $TSMODULE`
		fi
	fi
fi
if test "$PACKAGE" = "1"; then
	if test "$FILE" = "${FILE#/}"; then
		echo "No package for $FILE" >&2
		exit 1
	fi
	LOG=`mktemp`
	PKG=`dpkg -S $FILE 2>$LOG || true`
	if test $RELATED = "1" -a "$PKG" = ""; then
		PKG=": $FILE"
	fi
	if test "$PKG" != ""; then
		if test $PACKAGEONLY = 1; then
			echo $PKG|sed -e 's/:.*//'
		else
			echo $PKG
		fi
	else
		echo "No package for $FILE" >&2
		exit 1
	fi
else
	echo $FILE
fi