Skip to content

Instantly share code, notes, and snippets.

@OpenGG
Last active December 31, 2021 12:28
Show Gist options
  • Save OpenGG/258b0eb9a68a59054cdb7bbed3bee448 to your computer and use it in GitHub Desktop.
Save OpenGG/258b0eb9a68a59054cdb7bbed3bee448 to your computer and use it in GitHub Desktop.
codesandbox-vite-template
#!/usr/bin/env bash
set -e
# SECTION_HEAD: definitions
## dir basename
PWD=`pwd`
BASENAME=`basename "$PWD"`
## define prefix
PREFIX="codesandbox-vite-"
## define template name
TEMPLATE=${BASENAME/$PREFIX/""}
## define getFiles(): get files with exclude
getFiles() {
local dir="$1"
local exclude="$2"
local files=`ls -1 -a $dir`
for f in $exclude
do
files=`echo "$files"|grep -v "^$f$" || true`
done
return=$files
}
# SECTION_ACTION: processing template
## 0 cleaning
### whitelist
WHITELIST='
.
..
.git
.github
.update-template.sh
'
### files to remove
getFiles . "$WHITELIST"
filesRemove=$return
if [ ! -z "$filesRemove" ]
then
echo "=== 0. Cleaning folder, removing files:"
echo $filesRemove
rm -rf $filesRemove
fi
## 1 init template
echo "=== 1. initiating template: $TEMPLATE"
yarn create vite $BASENAME --template $TEMPLATE
## 2 move files
echo "=== 2. moving files: "
### cd
cd $BASENAME
### files to move
getFiles . '. ..'
filesMove=$return
echo $filesMove
### moving
for f in $filesMove
do
mv $f ..
done
### remove temp folder
echo "moving finish:"
cd -
rm -r $BASENAME
echo "=== 3. modify vite.config.js"
## adapt codesandbox port
viteConfig=`ls vite.config.*`
echo "add server.hmr.port=443 to $viteConfig"
sed -e 's|defineConfig({|defineConfig({\n server: process.cwd() === "/sandbox" \&\& {\n hmr: {\n port: 443\n }\n },|' $viteConfig > $viteConfig.out
mv $viteConfig.out $viteConfig
echo "=== 4. modify readme.md"
readme=`ls | grep -m 1 -i readme || true`
if [ -z "$readme" ]
then
readme="README.md"
fi
echo "add githubbox link to $readme"
touch $readme
echo -e "# [$BASENAME on codesandbox](https://githubbox.com/OpenGG/$BASENAME)\n\n$(cat $readme)" > $readme
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment