-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscaffold-js-practice
executable file
·137 lines (125 loc) · 3.39 KB
/
scaffold-js-practice
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
#!/bin/zsh
print_usage() {
echo "Usage: scaffold-practice [-flags] <dirName> <functionName> <variables>\n" 2>&1
echo ' <dirName> - required, specify the name of a new directory to be created (name of the problem) inside ~/Dev_Projects/Toptal_test_prep'
echo ' <functionName> - required, specify the name of the function'
echo ' <variables> - required, comma separated, no whte spaces characters used in the problem for input variables'
echo '\n [-flags]:'
echo ' -h prints out this message'
echo ' -n prevents Nodemon from launhing automatically'
}
dirName=
functionName=
variables=
isFlagged='false'
debuggingMode='OFF'
while getopts ':hnd' flag; do
case "${flag}" in
h)
print_usage
return 0
;;
n)
isFlagged='true'
useNodemon='false'
;;
d)
isFlagged='true'
debuggingMode='ON'
;;
# :)
# echo "$0:Must supply <dirName> <functionName> <variables>\n" >&2
# print_usage
# return 1;;
?)
printf "Invalid option: -${OPTARG}.\n\n" >&2
print_usage
return 1
;;
esac
done
if [[ $isFlagged == 'true' ]]; then
shift
fi
dirName=${1}
functionName=${2}
variables=${3}
if [[ $debuggingMode == 'ON' ]]; then
echo '\n\n######################### Debugging mode ON #########################\n\n'
echo "flags ${OPTARG}"
echo "is flagged: ${isFlagged}"
echo "dirName is: \"${dirName}\" vs the \${1}: \"${1}\""
echo "functionName is: \"${functionName}\" vs the \${2}: \"${2}\""
echo "variables are: \"${variables}\" vs the \${3}: \"${3}\""
echo '\n\n######################### end of debugging messages #########################\n\n'
return
fi
if [[ ${#} -lt 3 ]]; then
echo 'Must specify <dirName>, <functionName> and <variables>\n\n'
print_usage
return 1
fi
isUniqueName() {
DIRECTORYEXISTS=$(basename $(find ~/Dev_Projects/Toptal_test_prep -type d -name ${dirName})) >/dev/null 2>&1
if [[ $DIRECTORYEXISTS = '' ]]; then
return 0
else
return 1
fi
}
# TODO: #8 incorrect exit on existing dirName
while true; do
isUniqueName
if [[ ${?} -ne 0 ]]; then
echo "Folder '${dirName}' already exists. Folder names must be unique."
dirName=
read dirName'?Please enter new folder name: '
isUniqueName
if [[ ${?} -eq 0 ]]; then
break
fi
else
break
fi
done
mkdir ~/Dev_Projects/coding_practice/"$dirName"
cd ~/Dev_Projects/coding_practice/"$dirName"
echo 'line one
line two
line3 with more elements' >>input.txt
echo '{
"main": "index.js",
"type": "module",
"scripts": {
"watch": "nodemon -e js,txt -w"
},
"dependencies": {
"nodemon": "^2.0.16"
}
}' >>package.json
echo 'import { readFileSync } from "fs";
let input = readFileSync("input.txt", "utf8", (err, data) => {
if (!!err) {
console.error(err);
}
return data;
}).toString()
.replace(/\B\s+/gm, "") // replace preceding empty spaces and lines with ""
.split("\\n") // split into `line` arrays
.map((line) => line.split(" ")); // on each `line` split based on spaces
let ['$variables'] = input;
function '$functionName'('$variables') {
return {'$variables'}
}
console.time("'$functionName'");
console.log('$functionName'('$variables'));
console.timeEnd("'$functionName'");
' >>index.js
pnpm install
code -g index.js:16:3 .
if [[ $useNodemon == 'false' ]]; then
return 0
fi
# TODO: #2 find a way to push this into the integrated terminal in VScode
nodemon -e "js, txt" -w
return