-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpwagen.sh
executable file
·162 lines (137 loc) · 4.42 KB
/
pwagen.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
161
162
#!/bin/bash
# Default variables
command=""
component_name=""
custom_path="lib/components"
install_tool="yarn" # Defaulting to Yarn
template="@devgfnl/pwa-extension-template@latest" # Default template
parent_component="" # Optional parent component name
# Colors
RED="\033[1;31m"
YELLOW="\033[1;33m"
GREEN="\033[1;32m"
RESET="\033[0m"
function show_error_message {
echo -e "${RED}[✘] ${RESET}$1" >&2
}
function show_warning_message {
echo -e "${YELLOW}[!] ${RESET}$1" >&2
}
function show_success_message {
echo -e "${GREEN}[✔] ${RESET}$1"
}
# Function to display helper message
function show_helper_message {
echo -e "${GREEN}Usage:${RESET}"
echo -e "${YELLOW} $0 <command> [--args]...${RESET}"
echo
echo -e "${GREEN}Options:${RESET}"
echo -e "${YELLOW} create-component, component, cc, c${RESET} Create a new React component."
echo -e "${YELLOW} create-extension, extension, ce, e${RESET} Create a new extension using a template."
echo
echo -e "${GREEN}Arguments:${RESET}"
echo -e "${YELLOW} ComponentName${RESET} Name of the component to be created."
echo -e "${YELLOW} -d, --path CustomPath${RESET} Custom path to create the component. (default: lib/components)"
echo -e "${YELLOW} -p, --parent ParentComponentName${RESET} Name of the parent component (optional)."
echo -e "${YELLOW} -u, --use yarn || npm${RESET} Installation tool (default: yarn)."
echo -e "${YELLOW} -t, --template TemplateName${RESET} Name of the template to create the extension. (default: @devgfnl/pwa-extension-template)"
exit 1
}
# Process arguments
while [ "$#" -gt 0 ]; do
case "$1" in
create-component | component | cc | c)
command="create-component"
;;
create-extension | extension | ce | e)
command="create-extension"
;;
--use | -u)
shift
install_tool="$1"
;;
--path | -d)
shift
custom_path="$1"
;;
--template | -t)
shift
template="$1"
;;
--parent | -p)
shift
parent_component="$1"
;;
*)
# If not a known argument, assume it is the ComponentName
component_name="$1"
;;
esac
shift
done
# Check if the command and necessary arguments are provided
if [ -z "$command" ]; then
show_helper_message
fi
# Check if it's the create-extension command and --use is present
if [ "$command" == "create-extension" ] && [ -z "$install_tool" ]; then
show_helper_message
fi
if [ "$command" == "create-component" ] && [ -z "$component_name" ]; then
show_helper_message
fi
if [ "$command" == "create-extension" ]; then
if [ "$install_tool" == "yarn" ]; then
yarn create @larsroettig/pwa-extension --template $template
elif [ "$install_tool" == "npm" ]; then
# npm create @larsroettig/pwa-extension --template $template
show_warning_message "npm is not supported yet!"
else
show_error_message "Incorrect option for --use. Please use 'yarn' or 'npm'."
show_helper_message
fi
exit 0
fi
# Convert the first letter to lowercase
first_letter_lowercase="$(echo -n $component_name | head -c 1 | tr '[:upper:]' '[:lower:]')"
file_name="$first_letter_lowercase$(echo -n $component_name | tail -c +2)"
# Determine the destination directory
if [ -n "$parent_component" ]; then
destination_directory="$custom_path/$parent_component"
else
destination_directory="$custom_path/$component_name"
fi
# Create the directory if it doesn't exist
mkdir -p "$destination_directory"
# Content of the component file
component_content="\
import React from 'react';
import { mergeClasses } from '@magento/venia-ui/lib/classify';
import { shape, string } from 'prop-types';
import defaultClasses from './$file_name.module.css';
const $component_name = props => {
const classes = mergeClasses(defaultClasses, props.classes);
return <div className={classes.root} />;
};
$component_name.propTypes = {
classes: shape({ root: string })
};
$component_name.defaultProps = {};
export default $component_name;
"
index_content="\
export { default } from './$file_name';
"
css_content="\
.root {
}
"
# Create the file with content
echo "$component_content" > "$destination_directory/$file_name.js"
if [ ! -n "$parent_component" ]; then
echo "$index_content" > "$destination_directory/index.js"
fi
echo "$css_content" > "$destination_directory/$file_name.module.css"
if [ "$command" == "create-component" ]; then
show_success_message "React component '$component_name' created successfully in '$destination_directory'"
fi