Last active
February 17, 2023 07:56
-
-
Save roerohan/2edfe3782d9e9afb1e9e10111373febe to your computer and use it in GitHub Desktop.
A script that sets up a proper release process for Javascript/Typescript projects, with `semantic-release`, `commitizen`, `commitlint`, `husky`, etc.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
RELEASE_RC_FILE=".releaserc.json" | |
GITHUB_WORKFLOWS_DIR=".github/workflows" | |
NPM_RELEASE_ACTION_FILE="$GITHUB_WORKFLOWS_DIR/release.yml" | |
LINT_BUILD_ACTION_FILE="$GITHUB_WORKFLOWS_DIR/lint.yml" | |
TEST_ACTION_FILE="$GITHUB_WORKFLOWS_DIR/test.yml" | |
if ! command -v npm &> /dev/null | |
then | |
echo "npm could not be found" | |
exit 1 | |
fi | |
if ! command -v jq &> /dev/null | |
then | |
echo "jq could not be found" | |
exit 1 | |
fi | |
if [ ! -d ".git" ] | |
then | |
echo "Not a git repository." | |
exit 1 | |
fi | |
# Input URL of repository | |
echo "Enter the remote URL for this repository:" | |
read repository | |
# Install and set up husky` | |
echo "Installing and setting up husky" | |
npx husky-init && npm install | |
rm -rf .husky/{pre-commit,prepare-commit-msg,commit-msg} | |
npm i -D is-ci | |
# Install commitlint | |
echo "Installing and setting up commitlint..." | |
npm install --save-dev @commitlint/{config-conventional,cli} | |
jq '. + { "commitlint": { | |
"extends": [ | |
"@commitlint/config-conventional" | |
] | |
} }' <<< $(cat package.json) > package.json | |
# Install commitizen | |
echo "Installing and setting up commitizen..." | |
npm install --save-dev commitizen | |
npx commitizen init cz-conventional-changelog --save-dev --save-exact | |
# Add commitizen to husky | |
echo "Adding commitizen config to husky..." | |
npx husky add .husky/prepare-commit-msg 'exec < /dev/tty && npx git-cz --hook || true' | |
# Add commitlint to husky | |
echo "Adding commitlint to husky..." | |
npx husky add .husky/commit-msg 'npx --no-install commitlint --edit "$1"' | |
# Add pre-commit hook to husky | |
npx husky add .husky/pre-commit 'npm run lint:fix' | |
# This assumes you have ESLint set up. | |
echo "The script assumes ESLint is set up on the repository." | |
jq '.scripts=(.scripts + {"lint": "eslint . --ext .ts --ext .js", "lint:fix": "eslint . --ext .ts --ext .js --fix", "test": "jest", "prepare": "is-ci || husky install"})' <<< $(cat package.json) > package.json | |
# Set up semantic release | |
echo "Installing semantic release and other required dependencies..." | |
npm i -D semantic-release@latest @semantic-release/{git@latest,commit-analyzer@latest,release-notes-generator@latest,npm@latest,changelog@latest} | |
# Create semantic-release config file | |
echo "Generating semantic-release config for $repository." | |
cat > $RELEASE_RC_FILE <<- EOM | |
{ | |
"branches": [ | |
"main", | |
{ "name": "beta", "prerelease": true } | |
], | |
"plugins": [ | |
"@semantic-release/commit-analyzer", | |
"@semantic-release/release-notes-generator", | |
"@semantic-release/changelog", | |
[ | |
"@semantic-release/npm", | |
{ | |
"npmPublish": true, | |
"tarballDir": "dist" | |
} | |
], | |
[ | |
"@semantic-release/git", | |
{ | |
"assets": [ | |
"package.json", | |
"package-lock.json", | |
"CHANGELOG.md" | |
], | |
"message": "chore(release): \${nextRelease.version} [skip ci]\n\n\${nextRelease.notes}\n\n\nskip-checks: true" | |
} | |
], | |
[ | |
"@semantic-release/github", | |
{ | |
"assets": "dist/*.tgz" | |
} | |
] | |
], | |
"repositoryUrl": "$repository" | |
} | |
EOM | |
# Make .github/workflows directory if it does not exist | |
echo "Populating workflow files." | |
mkdir -p $GITHUB_WORKFLOWS_DIR | |
echo "Creating action to release package..." | |
cat > $NPM_RELEASE_ACTION_FILE <<- EOM | |
name: Release to NPM Registry | |
on: | |
push: | |
branches: | |
- main | |
- beta | |
jobs: | |
release: | |
name: Release | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout | |
uses: actions/checkout@v2 | |
with: | |
submodules: recursive | |
- uses: actions/setup-node@v2 | |
with: | |
node-version: 16 | |
registry-url: https://registry.npmjs.org/ | |
- name: Install dependencies | |
run: npm install | |
- name: Build package | |
run: npm run build | |
- name: Release | |
env: | |
GITHUB_TOKEN: \${{ secrets.GITHUB_TOKEN }} | |
NPM_TOKEN: \${{ secrets.NPM_PUBLISH_TOKEN }} | |
NODE_AUTH_TOKEN: \${{ secrets.NPM_PUBLISH_TOKEN }} | |
run: npx semantic-release | |
EOM | |
echo "Creating action to lint and build..." | |
cat > $LINT_BUILD_ACTION_FILE <<- EOM | |
name: Check for lint/build errors | |
on: | |
pull_request: | |
branches: | |
- "**" | |
jobs: | |
lint: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout | |
uses: actions/checkout@v2 | |
with: | |
submodules: recursive | |
- uses: actions/setup-node@v2 | |
with: | |
node-version: 16 | |
registry-url: https://registry.npmjs.org/ | |
- name: npm install, lint | |
run: | | |
npm install | |
npm run lint | |
build: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout | |
uses: actions/checkout@v2 | |
with: | |
submodules: recursive | |
- uses: actions/setup-node@v2 | |
with: | |
node-version: 16 | |
registry-url: https://registry.npmjs.org/ | |
- name: npm install, build | |
run: | | |
npm install | |
npm run build | |
EOM | |
echo "Creating action to run npm tests..." | |
cat > $TEST_ACTION_FILE <<- EOM | |
name: Run automated tests | |
on: | |
pull_request: | |
branches: | |
- main | |
- beta | |
jobs: | |
lint: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout | |
uses: actions/checkout@v2 | |
with: | |
submodules: recursive | |
- uses: actions/setup-node@v2 | |
with: | |
node-version: 16 | |
registry-url: https://registry.npmjs.org/ | |
- name: npm install, test | |
run: | | |
npm install | |
npm test | |
EOM | |
echo "Release setup complete." | |
echo "Please verify all the changes in github actions, package.json and .husky/**/* before proceeding." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment