-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
.node-functions
178 lines (151 loc) · 4.68 KB
/
.node-functions
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
###########################
# Node Helper Functions
# Let's make working with node easier.
###########################
# https://github.com/microsoft/vscode/issues/94679
vscode-fix-typescript-truncation() {
local file="/Applications/Visual Studio Code.app/Contents/Resources/app/extensions/node_modules/typescript/lib/tsserver.js"
sed -i '' 's/defaultMaximumTruncationLength = 160;/defaultMaximumTruncationLength = 800;/g' "$file"
}
# TODO need to refactor for tsx, I don't think tsconfig-paths are required anymore
# ts-node execution
tsn() {
if [ -f pnpm-lock.yaml ]; then
if ! pnpm list tsconfig-paths &>/dev/null; then
echo "adding tsconfig-paths"
pnpm add -D tsconfig-paths
fi
else
if ! npm list tsconfig-paths &>/dev/null; then
echo "adding tsconfig-paths"
npmi tsconfig-paths --save-dev
fi
fi
tsx "$@"
}
# node debugger
ndb() {
# `NODE_INSPECT_RESUME_ON_START` runs until first breakpoint when in `node inspect`
if [[ "$1" == *.ts ]]; then
echo "Using ts-node for typescript..."
NODE_INSPECT_RESUME_ON_START=1 node inspect \
--debug-port=0 \
--loader ts-node/esm \
\
-r tsconfig-paths/register \
"$@" \
else # -r ts-node/esm/transpile-only \
# if not, assume basic JS
NODE_INSPECT_RESUME_ON_START=1 node inspect \
--debug-port=0 \
"$@"
fi
}
jest-debug() {
local TEST_TIMEOUT=""
if [ -n "$JEST_TEST_TIMEOUT" ]; then
TEST_TIMEOUT="--testTimeout=$JEST_TEST_TIMEOUT"
fi
NODE_INSPECT_RESUME_ON_START=1 node inspect --port=0 \
node_modules/.bin/jest --runInBand --no-cache $TEST_TIMEOUT "$@"
}
npm-upgrade-packages() {
# check if npm-check-updates is installed globally
if (! npm list npm-check-updates &>/dev/null) && (! npm list -g npm-check-updates &>/dev/null); then
echo "adding npm-check-updates"
_npx npm-check-updates
fi
_npx ncu --interactive --format group
}
npm-package-tree() {
if [ -f pnpm-lock.yaml ]; then
pnpm list --prod --depth 2
else
npm list --omit dev --omit optional --depth 2
fi
}
npm-reset() {
rm -rf node_modules
if [ -f pnpm-lock.yaml ]; then
pnpm install
else
npm install
fi
}
npm-remove-node-modules() {
fd -td node_modules
}
# TODO any `file:./` references, automatically remove and reinstall packages
# update package.json engine configuration based on asdf + npm config
node-package-version-sync() {
# Get NodeJS version from asdf
local NODE_VERSION
NODE_VERSION=$(mise list --current --json | jq -r ".node[0].version // empty")
# Get NPM/PNPM version from the command line
local NPM_VERSION
if [ -f pnpm-lock.yaml ]; then
NPM_VERSION=$(pnpm -v)
else
NPM_VERSION=$(npm -v)
fi
# Define the part of the package.json file before the "engines" section
local JSON_HEAD
JSON_HEAD=$(cat package.json | jq 'del(.engines)')
# Form the "engines" section with the latest NodeJS and package manager versions
local ENGINES
if [ -f pnpm-lock.yaml ]; then
ENGINES=$(jq -n \
--arg NODE_VERSION "$NODE_VERSION" \
--arg PNPM_VERSION "$NPM_VERSION" \
'{
"node": $NODE_VERSION,
"pnpm": $PNPM_VERSION
}')
else
ENGINES=$(jq -n \
--arg NODE_VERSION "$NODE_VERSION" \
--arg NPM_VERSION "$NPM_VERSION" \
'{
"node": $NODE_VERSION,
"npm": $NPM_VERSION
}')
fi
# Combine the two parts and write them back to package.json
echo $JSON_HEAD | jq --argjson engines "$ENGINES" '. + {engines: $engines}' >package.json
}
###########################
# Package Management Help
###########################
# jump into an existing package folder
npm-show() {
if [ -f pnpm-lock.yaml ]; then
cd "$(pnpm ls "$@" --long | rg --no-filename --no-column --no-line-number --color=never node_modules | uniq)" || return
else
cd "$(npm ls "$@" --long | grep node_modules | uniq)" || return
fi
}
# helper function to execute a command in npm or pnpm
_npx() {
if [ -f pnpm-lock.yaml ]; then
pnpm "$@"
else
npx "$@"
fi
}
# install a package and run typesync, pnpm aware
npmi() {
# if not a typescript project, just run normally
if [ ! -f tsconfig.json ]; then
_install_node_package "$@"
return $?
fi
# if typesync is not installed in the local package.json, add it to the dev deps
if ! grep -q typesync package.json; then
_install_node_package typesync --save-dev
fi
_install_node_package "$@"
_npx typesync
# now, after typesync has added it's packages, reinstall silently
_install_node_package --silent
}
# fd '.*(d.ts|js)$' --type f --no-ignore --exclude=node_modules --exclude=cdk.out --exclude=jest.config.js --exec rm {}