-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathts_yarn_init
executable file
·138 lines (100 loc) · 2.57 KB
/
ts_yarn_init
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
#!/bin/bash -eu
# ****************************************************************************
# DESCRIPTON
# Creates a yarn based typescript project
#
# Installation steps friendly copied from:
#
# https://losikov.medium.com/
# part-1-project-initial-setup-typescript-node-js-31ba3aa7fbf1
# ****************************************************************************
# bugs and hints: lrsklemstein@gmail.com
# --- constants
readonly PROG=${0##*/}
# --- functions
msg() {
echo "[$PROG] $*" >&2
}
abort() {
msg "$*"
exit 1
}
# --- main
if [ $# -ne 1 ]
then
echo "usage: $PROG DIR"
exit 2
fi
project_dir="$(readlink -f $1)"
src_dir=$project_dir/src
out_dir=$project_dir/dist
tst_dir=$project_dir/test
app_ts=$src_dir/app.ts
app_js=$out_dir/app.js
jest_cfg=$project_dir/jest.config.js
# using an existing dir is only allowed with $PWD and without package.json
if [ -d "$project_dir" ]
then
project_dir_norm=$(readlink -e "$project_dir")
pwd_norm=$(readlink -e "$PWD")
if [ "$project_dir_norm" != "$pwd_norm" ]
then
abort "project dir \"$project_dir\" already exist."
fi
if [ -f "$project_dir/package.json" ]
then
abort "project dir \"$project_dir\" contains a package.json"
fi
fi
mkdir -p "$project_dir"
cd "$project_dir"
msg 'init...'
yarn init -y
msg "Install dev dependencies..."
yarn add --dev typescript jest ts-jest @types/jest @types/node nodemon ts-node
msg 'Create tsconfig.json, required for tsc and ts-node...'
/bin/cat >$project_dir/tsconfig.json <<EOF
{
"compilerOptions": {
"target": "es2016",
"lib": ["es2019"],
"module": "commonjs",
rootDir": "${src_dir}",
"outDir": "${out_dir}",
"removeComments": true,
"esModuleInterop": true,
"strict": true,
"noImplicitAny": true,
"skipLibCheck": true
},
"include": ["src/**/*"],
}
EOF
mkdir $src_dir $out_dir $tst_dir
msg "Create initial ${app_ts}..."
/bin/cat >$app_ts << EOF
// your code here...
console.log('Hello from app.ts...');
EOF
msg "Create jest ${jest_cfg}..."
/bin/cat >$jest_cfg <<EOF
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
transform: {
'^.+\\.ts?$': 'babel-jest'
},
testRegex: '(/__tests__/.*|(\\.|/)(test|spec))\\.tsx?$',
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
};
EOF
msg "Add yarn scripts to package.json..."
# '.scripts={compile:"npx tsc", run:"node src/app.js"}'
#
jqc='.scripts={'
jqc+='compile:"npx tsc", execute:"node '$app_js'"'
jqc+='}'
tmpf=$(mktemp)
jq "$jqc" < package.json >$tmpf
mv $tmpf package.json
msg "Done!"