Skip to content

Instantly share code, notes, and snippets.

@wevertoum
Created April 19, 2023 16:02
Show Gist options
  • Save wevertoum/eca99c9a1a2412a8ad1fc8d9b9eed89b to your computer and use it in GitHub Desktop.
Save wevertoum/eca99c9a1a2412a8ad1fc8d9b9eed89b to your computer and use it in GitHub Desktop.
#!/bin/bash
function createComponent() {
COMPONENT_NAME="$1"
FILE_TYPE="$2"
if [ -z "$COMPONENT_NAME" ]; then
echo "Component name missing. Please provide a name."
exit 1
fi
if [ ! -d "src/components" ]; then
echo "The directory src/components does not exist. Please create it first."
exit 1
fi
mkdir "src/components/$COMPONENT_NAME"
touch "src/components/$COMPONENT_NAME/$COMPONENT_NAME.tsx"
touch "src/components/$COMPONENT_NAME/$COMPONENT_NAME.$FILE_TYPE"
touch "src/components/$COMPONENT_NAME/index.tsx"
if [ "$FILE_TYPE" == "less" ]; then
echo "import React from 'react';
require('./$COMPONENT_NAME.less');
interface Props {}
const $COMPONENT_NAME: React.FC<Props> = () => {
return <>$COMPONENT_NAME is ready</>;
};
export default $COMPONENT_NAME;" >"src/components/$COMPONENT_NAME/$COMPONENT_NAME.tsx"
echo "export { default } from './$COMPONENT_NAME';" >"src/components/$COMPONENT_NAME/index.tsx"
elif [ "$FILE_TYPE" == "scss" ]; then
echo "import React from 'react';
import './$COMPONENT_NAME.scss';
interface Props {}
const $COMPONENT_NAME: React.FC<Props> = () => {
return <>$COMPONENT_NAME is ready</>;
};
export default $COMPONENT_NAME;" >"src/components/$COMPONENT_NAME/$COMPONENT_NAME.tsx"
echo "export { default } from './$COMPONENT_NAME';" >"src/components/$COMPONENT_NAME/index.tsx"
else
echo "Invalid file type. Please use 'less' or 'scss'."
exit 1
fi
echo "Component $COMPONENT_NAME created!"
}
createComponent "$1" "$2"
@wevertoum
Copy link
Author

to create React/Next js component in src/components/ in root directory, type

./create-component.sh ExampleComponent less

or

./create-component.sh ExampleComponent scss

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment