-
Notifications
You must be signed in to change notification settings - Fork 81
/
create_module.sh
executable file
·160 lines (129 loc) · 4.05 KB
/
create_module.sh
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
#!/bin/bash
#
# This script creates a module in base MagicMirror-Module template
# You can get a copy from https://github.com/roramirez/MagicMirror-Module-Template
#
#
# Manager Module Template
#
# By Rodrigo Ramìrez Norambuena https://rodrigoramirez.com
# MIT Licensed.
#
REPOSITORY_URL=https://github.com/roramirez/MagicMirror-Module-Template
YEAR=$(date +"%Y")
if ! [ -x "$(command -v git)" ]; then
echo "Please install git"
exit 1
fi
# check if a directory is the MM directory (checks for correct content of package.json)
is_mm_directory() {
TEST_STRING="\"url\": \"git+https://github.com/MichMich/MagicMirror.git\""
if grep -sq "$TEST_STRING" "$1/package.json"; then
# it is correct
return 0
fi
# wrong one
return 1
}
MM_HOME=""
# check default directory
if is_mm_directory "$HOME/MagicMirror"; then
MM_HOME="$HOME/MagicMirror"
fi
# check working directory
if is_mm_directory "."; then
MM_HOME=$(cd "." && pwd)
fi
# check directory above working directory (i.e. in modules directory)
if is_mm_directory ".."; then
MM_HOME=$(cd ".." && pwd)
fi
if [ -d "$MM_HOME" ] ; then
echo "MagicMirror installation found in: $MM_HOME"
else
echo "MagicMirror installation not found."
read -p "Please input its path now (or restart script in its directory): " MM_HOME
fi
read -p "Insert your module name: " MODULE_NAME
if [ "$MODULE_NAME" = "" ]; then
echo "No module name entered."
exit 1
fi
DIRECTORY_DST="$MM_HOME/modules/$MODULE_NAME"
read -p "Do you want create in $DIRECTORY_DST (y/N) " choice
if [[ ! "$choice" =~ ^[Yy]$ ]]; then
read -p "Insert destination module path: " DIRECTORY_DST
fi
if [ -d "$DIRECTORY_DST" ]; then
echo "Warning!. The destination $DIRECTORY_DST exists"
echo "To prevent override please rename destination directory"
echo "or run again with another name module or destination path"
exit 1
fi
# Author & Licenses
AUTHOR_NAME=$(git config user.name)
if [ -z "$AUTHOR_NAME" ]; then
read -p "Insert your full name: " $AUTHOR_NAME
fi
read -p "Pickup a license
1. MIT (Default)
2. ISC
Choice: " LICENSE
case $LICENSE in
[1] | [MIT] )
LICENSE="MIT"
;;
[2] | [ISC] )
LICENSE="ISC"
;;
* )
LICENSE="MIT"
;;
esac
echo "Type a short description of what your module does (leave empty to insert a Todo)"
read -p ": " $DESCRIPTION
if [ "$DESCRIPTION" = "" ]; then
DESCRIPTION="Todo: Insert description here!"
fi
# Create temporal directory
TMPDIR=$(mktemp -d)
# Clone repository here
git clone $REPOSITORY_URL $TMPDIR
# Here add templates stuff
mkdir -p $DIRECTORY_DST
# copy the needed stuff
cp -a $TMPDIR/* $DIRECTORY_DST
# Copy dot files issue #4
cp -a $TMPDIR/.eslintrc.json $DIRECTORY_DST
cp -a $TMPDIR/.stylelintrc $DIRECTORY_DST
mv $DIRECTORY_DST/MagicMirror-Module-Template.js $DIRECTORY_DST/$MODULE_NAME.js
mv $DIRECTORY_DST/MagicMirror-Module-Template.css $DIRECTORY_DST/$MODULE_NAME.css
mv $DIRECTORY_DST/templates/licenses/$LICENSE $DIRECTORY_DST/LICENSE.txt
mv $DIRECTORY_DST/templates/CHANGELOG.md $DIRECTORY_DST/
mv $DIRECTORY_DST/templates/README.md $DIRECTORY_DST/
mv $DIRECTORY_DST/templates/package.json $DIRECTORY_DST/
rm -frv $DIRECTORY_DST/templates > /dev/null
# remove unneeded files
rm $DIRECTORY_DST/LICENSE # Module-Template-License
rm $DIRECTORY_DST/create_module.sh # module creation script
# Based on https://stackoverflow.com/a/51060063
# Default case for Linux sed, just use "-i"
sedi=(-i)
case "$(uname)" in
# For macOS, use two parameters
Darwin*) sedi=(-i "")
esac
# Expand the parameters in the actual call to "sed"
sed "${sedi[@]}" -e s/\{\{MODULE_NAME\}\}/$MODULE_NAME/g $DIRECTORY_DST/*.*
sed "${sedi[@]}" -e s/\{\{AUTHOR_NAME\}\}/"$AUTHOR_NAME"/g $DIRECTORY_DST/*.*
sed "${sedi[@]}" -e s/\{\{LICENSE\}\}/$LICENSE/g $DIRECTORY_DST/*.*
sed "${sedi[@]}" -e s/\{\{YEAR\}\}/$YEAR/g $DIRECTORY_DST/*.*
sed "${sedi[@]}" -e s/\{\{DESCRIPTION\}\}/"$DESCRIPTION"/g $DIRECTORY_DST/*.*
cd $DIRECTORY_DST
git init
# Delete temporal directory
rm -frv $TMPDIR 2 > /dev/null
echo "Happy coding! Have fun you are an awesome developer :)"
echo "here your development directory $DIRECTORY_DST"
cd $DIRECTORY_DST
ls $DIRECTORY_DST