Skip to content

Commit 8f5d596

Browse files
committed
feat: add script to rewrite package.json on template creation
1 parent d80873e commit 8f5d596

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed

.github/scripts/rename.sh

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/usr/bin/env bash
2+
3+
# https://gist.github.com/vncsna/64825d5609c146e80de8b1fd623011ca
4+
set -euo pipefail
5+
6+
# Define the input vars
7+
GITHUB_REPOSITORY=${1?Error: Please pass username/repo, e.g. joejordan/vite-frontend-template}
8+
GITHUB_REPOSITORY_OWNER=${2?Error: Please pass username, e.g. joejordan}
9+
GITHUB_REPOSITORY_DESCRIPTION=${3:-""} # If null then replace with empty string
10+
11+
echo "GITHUB_REPOSITORY: $GITHUB_REPOSITORY"
12+
echo "GITHUB_REPOSITORY_OWNER: $GITHUB_REPOSITORY_OWNER"
13+
echo "GITHUB_REPOSITORY_DESCRIPTION: $GITHUB_REPOSITORY_DESCRIPTION"
14+
15+
# jq is like sed for JSON data
16+
JQ_OUTPUT=`jq \
17+
--arg NAME "@$GITHUB_REPOSITORY" \
18+
--arg AUTHOR_NAME "$GITHUB_REPOSITORY_OWNER" \
19+
--arg URL "https://github.com/$GITHUB_REPOSITORY_OWNER" \
20+
--arg DESCRIPTION "$GITHUB_REPOSITORY_DESCRIPTION" \
21+
'.name = $NAME | .description = $DESCRIPTION | .author |= ( .name = $AUTHOR_NAME | .url = $URL )' \
22+
package.json
23+
`
24+
25+
# Overwrite package.json
26+
echo "$JQ_OUTPUT" > package.json

.github/workflows/create.yml

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
name: "Create"
2+
3+
# The workflow will run when the "Use this template" button is used
4+
on:
5+
push:
6+
7+
jobs:
8+
create:
9+
# We only run this action when the repository isn't the template repository. References:
10+
# - https://docs.github.com/en/actions/learn-github-actions/contexts
11+
# - https://docs.github.com/en/actions/learn-github-actions/expressions
12+
if: ${{ !github.event.repository.is_template }}
13+
permissions: "write-all"
14+
runs-on: "ubuntu-latest"
15+
steps:
16+
- name: "Check out the repo"
17+
uses: "actions/checkout@v4"
18+
19+
- name: "Update package.json"
20+
env:
21+
GITHUB_REPOSITORY_DESCRIPTION: ${{ github.event.repository.description }}
22+
run:
23+
./.github/scripts/rename.sh "$GITHUB_REPOSITORY" "$GITHUB_REPOSITORY_OWNER" "$GITHUB_REPOSITORY_DESCRIPTION"
24+
25+
- name: "Add rename summary"
26+
run: |
27+
echo "## Commit result" >> $GITHUB_STEP_SUMMARY
28+
echo "✅ Passed" >> $GITHUB_STEP_SUMMARY
29+
30+
- name: "Remove files not needed in the user's copy of the template"
31+
run: |
32+
rm -rf "./.github/scripts/"
33+
rm -f "./.github/workflows/create.yml"
34+
35+
- name: "Add remove summary"
36+
run: |
37+
echo "## Remove result" >> $GITHUB_STEP_SUMMARY
38+
echo "✅ Passed" >> $GITHUB_STEP_SUMMARY
39+
40+
- name: "Update commit"
41+
uses: "stefanzweifel/git-auto-commit-action@v5"
42+
with:
43+
commit_message: "feat: initial commit"
44+
commit_options: "--amend"
45+
push_options: "--force"
46+
skip_fetch: true
47+
48+
- name: "Add commit summary"
49+
run: |
50+
echo "## Commit result" >> $GITHUB_STEP_SUMMARY
51+
echo "✅ Passed" >> $GITHUB_STEP_SUMMARY

0 commit comments

Comments
 (0)