1 | #!/bin/sh
|
2 |
|
3 | TEMP_DIR_PATH=""
|
4 | FONTAWESOME_PRO_DIR_NAME=""
|
5 | DEST_DIR_PATH=${1:-"assets/fonts"}
|
6 | PROJECT_NAME="react-native-vector-icons"
|
7 | FONT_NAME="Font Awesome Pro"
|
8 |
|
9 | setup_npm_config()
|
10 | {
|
11 |
|
12 | if [ "$(npm config get @fortawesome:registry)" = "undefined" ]; then
|
13 | npm config set "@fortawesome:registry" https://npm.fontawesome.com/
|
14 | fi
|
15 |
|
16 | local npm_token=""
|
17 | echo "Please enter your $FONT_NAME npm token:";
|
18 | read npm_token
|
19 | npm config set "//npm.fontawesome.com/:_authToken" "${npm_token}"
|
20 | }
|
21 |
|
22 | create_tmp_directory()
|
23 | {
|
24 | local tmp_dir="$(mktemp -d -t 'rnvi.XXXXXX')"
|
25 | retval=$?
|
26 | if [ "$retval" != 0 ]; then
|
27 | echo "[FAIL] Can't create temporary directory";
|
28 | return 1;
|
29 | fi
|
30 |
|
31 | if [ -z "$tmp_dir" ]; then
|
32 | echo "[FAIL] Generated temporary directory name is empty";
|
33 | return 1;
|
34 | fi
|
35 |
|
36 | TEMP_DIR_PATH="$tmp_dir"
|
37 | }
|
38 |
|
39 | download_and_unpack_fontawesome_pro()
|
40 | {
|
41 | local archive_file_name="$(npm pack @fortawesome/fontawesome-pro --silent)"
|
42 | retval=$?
|
43 | if [ "$retval" != 0 ]; then
|
44 | echo "[FAIL] Can't download [$archive_file_name] archive";
|
45 | return 1;
|
46 | fi
|
47 |
|
48 | tar -xzf "$archive_file_name"
|
49 | retval=$?
|
50 | if [ "$retval" != 0 ]; then
|
51 | echo "[FAIL] Can't unpack [$archive_file_name] archive";
|
52 | return 1;
|
53 | fi
|
54 |
|
55 | local font_dir_name="package"
|
56 | if [ ! -d "$font_dir_name" ]; then
|
57 | echo "[FAIL] Archive doesn't contain [$font_dir_name] required directory";
|
58 | return 1;
|
59 | fi
|
60 |
|
61 | FONTAWESOME_PRO_DIR_NAME="$font_dir_name"
|
62 | }
|
63 |
|
64 | copy_ttf_fonts_to_dest_dir()
|
65 | {
|
66 | mkdir -p "$DEST_DIR_PATH"
|
67 | retval=$?
|
68 | if [ "$retval" != 0 ]; then
|
69 | echo "[FAIL] Can't create [$DEST_DIR_PATH] directory";
|
70 | return 1;
|
71 | fi
|
72 |
|
73 | local font_dir_path="$TEMP_DIR_PATH/$FONTAWESOME_PRO_DIR_NAME/webfonts"
|
74 |
|
75 | cp "$font_dir_path/fa-brands-400.ttf" "$DEST_DIR_PATH/FontAwesome5_Pro_Brands.ttf" &&
|
76 | cp "$font_dir_path/fa-light-300.ttf" "$DEST_DIR_PATH/FontAwesome5_Pro_Light.ttf" &&
|
77 | cp "$font_dir_path/fa-regular-400.ttf" "$DEST_DIR_PATH/FontAwesome5_Pro_Regular.ttf" &&
|
78 | cp "$font_dir_path/fa-solid-900.ttf" "$DEST_DIR_PATH/FontAwesome5_Pro_Solid.ttf"
|
79 |
|
80 | retval=$?
|
81 | if [ "$retval" != 0 ]; then
|
82 | echo "[FAIL] Can't copy ttf fonts to [$DEST_DIR_PATH] directory";
|
83 | return 1;
|
84 | fi
|
85 | }
|
86 |
|
87 | create_rn_config()
|
88 | {
|
89 | if [ -f "./react-native.config.js" ]; then
|
90 | echo "You already have a react-native-config.js file, make sure you have the new fonts added to the dependencies!";
|
91 | return 1;
|
92 | else
|
93 | echo "module.exports = { assets: [ '${DEST_DIR_PATH}' ] };" > react-native.config.js;
|
94 | fi
|
95 | }
|
96 |
|
97 | react_native_link_project()
|
98 | {
|
99 | react-native link
|
100 | }
|
101 |
|
102 | if setup_npm_config; then
|
103 | echo "[SUCCESS] Set up npm config";
|
104 | else
|
105 | exit 1;
|
106 | fi
|
107 |
|
108 | if create_tmp_directory; then
|
109 | echo "[SUCCESS] Temporary directory [$TEMP_DIR_PATH] was created";
|
110 | else
|
111 | exit 1;
|
112 | fi
|
113 |
|
114 | cd "$TEMP_DIR_PATH"
|
115 | if download_and_unpack_fontawesome_pro; then
|
116 | echo "[SUCCESS] $FONT_NAME was unpacked to [$TEMP_DIR_PATH/$FONTAWESOME_PRO_DIR_NAME] directory";
|
117 | else
|
118 | exit 1;
|
119 | fi
|
120 | cd - > /dev/null
|
121 |
|
122 | if copy_ttf_fonts_to_dest_dir; then
|
123 | echo "[SUCCESS] Copied $FONT_NAME to [$DEST_DIR_PATH] directory";
|
124 | else
|
125 | exit 1;
|
126 | fi
|
127 |
|
128 | if create_rn_config; then
|
129 | echo "[SUCCESS] Created react-native.config.js";
|
130 | else
|
131 | echo "[INFO] Didn't create react-native.config.js, it already exists. Make sure '${DEST_DIR_PATH}' is part of the 'assets' array!";
|
132 | fi
|
133 |
|
134 | if react_native_link_project; then
|
135 | echo "[SUCCESS] Linked $PROJECT_NAME to React Native";
|
136 | else
|
137 | exit 1;
|
138 | fi
|
139 |
|
140 | echo "[SUCCESS] $FONT_NAME was successfully upgraded"
|
141 | echo "Note: [$TEMP_DIR_PATH] was created. Delete it manually or it will be deleted automatically on next reboot"
|