-
Notifications
You must be signed in to change notification settings - Fork 0
/
scaffold-js-practice
106 lines (96 loc) · 2.65 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
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'
}
while getopts ':hn' flag; do
case "${flag}" in
h)
print_usage
return 0;;
n)
isFlagged='true'
useNodemon='false';;
# :)
# 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 [[ ${#} -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
}
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"
}' >> 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
npm 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 0