Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
polymath-eric authored Oct 22, 2024
0 parents commit 968a6eb
Show file tree
Hide file tree
Showing 21 changed files with 8,530 additions and 0 deletions.
28 changes: 28 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"parser": "@typescript-eslint/parser",
"env": {
"browser": true,
"jest": true,
"node": true,
"es6": true
},
"rules": {
"@typescript-eslint/indent": "off",
"quotes": ["error", "single", { "avoidEscape": true }],
"simple-import-sort/imports": "error",
"sort-imports": "off",
"import/order": "off",
"no-dupe-class-members": "off",
"no-shadow": "error",
"arrow-parens": "off",
"no-useless-constructor": "off",
"@typescript-eslint/no-useless-constructor": "error"
},
"plugins": ["@typescript-eslint", "simple-import-sort", "node", "import", "prettier", "promise"],
"parserOptions": {
"sourceType": "module",
"project": "./tsconfig.json"
},
"extends": ["standard", "semistandard", "plugin:@typescript-eslint/recommended", "prettier"],
"ignorePatterns": ["src/polkadot/", "src/middleware/", "docs/*"]
}
1 change: 1 addition & 0 deletions .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* @PolymeshAssociation/middleware
48 changes: 48 additions & 0 deletions .github/workflows/authenticate-commits.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
name: Authenticate Commits
on:
pull_request:
types: [opened, reopened, synchronize]
jobs:
validate:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v3
with:
fetch-depth: 0

- name: Import allowed SSH keys
env:
ALLOWED_SIGNERS: ${{ vars.MIDDLEWARE_ALLOWED_SIGNERS }}
run: |
mkdir -p ~/.ssh
echo "$ALLOWED_SIGNERS" > ~/.ssh/allowed_signers
git config --global gpg.ssh.allowedSignersFile "~/.ssh/allowed_signers"
- name: Validate commit signatures
env:
HEAD_SHA: ${{ github.event.pull_request.head.sha }}
BASE_SHA: ${{ github.event.pull_request.base.sha }}
run: |
# Function to verify a commit
verify_commit() {
local commit=$1
local status=$(git show --pretty="format:%G?" $commit | head -n 1)
if [ "$status" != "G" ]; then
local committer=$(git log -1 --pretty=format:'%cn (%ce)' $commit)
echo "Commit $commit from $committer has an invalid signature or is not signed by an allowed key."
exit 1
fi
}
# Get all commits in the PR
commits=$(git rev-list $BASE_SHA..$HEAD_SHA)
# Iterate over all commits in the PR and verify each one
for COMMIT in $commits; do
verify_commit $COMMIT
done
echo "All commits are signed with allowed keys."
27 changes: 27 additions & 0 deletions .github/workflows/fast-forward.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: fast-forward
on:
issue_comment:
types: [created, edited]
jobs:
fast-forward:
# Only run if the comment contains the /fast-forward command.
if: ${{ contains(github.event.comment.body, '/fast-forward')
&& github.event.issue.pull_request }}
runs-on: ubuntu-latest

permissions:
contents: write
pull-requests: write
issues: write

steps:
- name: Fast forwarding
uses: sequoia-pgp/fast-forward@v1
with:
merge: true
# To reduce the workflow's verbosity, use 'on-error'
# to only post a comment when an error occurs, or 'never' to
# never post a comment. (In all cases the information is
# still available in the step's summary.)
comment: on-error
GITHUB_TOKEN: ${{ secrets.RELEASE_TOKEN }} ## This allows to trigger push action from within this workflow. Read more - https://docs.github.com/en/actions/using-workflows/triggering-a-workflow#triggering-a-workflow-from-a-workflow
66 changes: 66 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
name: CI

on:
push:
branches: [master, beta]
pull_request:
types: [assigned, opened, synchronize, reopened]

jobs:
lint:
name: Linting
runs-on: ubuntu-latest
env:
CI: true
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: '18.x'
cache: 'yarn'
- name: install dependencies
run: yarn --frozen-lockfile
- name: lint
run: yarn lint

test:
name: Testing
runs-on: ubuntu-latest
env:
CI: true
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: '18.x'
cache: 'yarn'
- name: install dependencies
run: yarn --frozen-lockfile
- name: test
run: yarn test

release:
name: Building and releasing project
runs-on: ubuntu-latest
needs: [lint, test]
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: '22.x'
cache: 'yarn'
- name: install dependencies
run: yarn --frozen-lockfile
- name: build
run: |
yarn build:ts
sed 's/dist\//.\//' package.json > dist/package.json
cp README.md dist/README.md
- name: release
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
NPM_TOKEN: ${{ secrets.ASSOCIATION_NPM_TOKEN }}
run: |
cd dist
yarn --frozen-lockfile
yarn semantic-release
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
.DS_STORE
node_modules/
dist/
yarn-error.log
npm-package/
.idea/
coverage/
test.ts
docs/
4 changes: 4 additions & 0 deletions .husky/commit-msg
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

yarn commitlint --edit $1
4 changes: 4 additions & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

yarn lint-staged
5 changes: 5 additions & 0 deletions .lintstagedrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"*.{ts,tsx,js,jsx}": [
"prettier-eslint --write"
]
}
6 changes: 6 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"singleQuote": true,
"trailingComma": "es5",
"tabWidth": 2,
"printWidth": 100
}
10 changes: 10 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"typescript.preferences.importModuleSpecifier": "non-relative",
"typescript.tsdk": "node_modules/typescript/lib",
"editor.tabSize": 2,
"editor.formatOnSave": true,
"cSpell.words": [
"commitlint",
"lintstagedrc"
]
}
50 changes: 50 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
[![js-semistandard-style](https://img.shields.io/badge/code%20style-semistandard-brightgreen.svg?style=flat-square)](https://github.com/standard/semistandard)
[![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg)](https://github.com/semantic-release/semantic-release)

# Polymesh Typescript Template Repo

This is a template repository for typescript projects. It includes some initial typescript config and tooling to make our lives easier

**NOTE**: This repo uses `yarn` instead of `npm` for dependencies

Things included in the repo:

- Typescript (duh)
- Absolute imports (allows you to `import { foo } from ~/bar;` instead of `import { foo } from ../../../../bar;`. The default character is `~` but it can be changed in `tsconfig.json`)
- Eslint to enforce code style rules (extending standard JS with enforced semicolons and typescript-eslint)
- Prettier to format code on save
- Semantic release for automatic versioning
- Commitizen
- Husky to enforce conventional commits and format the code using prettier before committing
- Github actions for CI (runs linter, tests, build and semantic-release)

## Scripts

- `yarn build:ts` compiles typescript files into javascript and type declarations. Outputs to `dist/` directory
- `yarn build:docs` builds a documentation page from TSDoc comments in the code. Outputs to `docs/` directory
- `yarn test` runs tests and outputs the coverage report
- `yarn commit` runs the commit formatting tool (should replace normal commits)
- `yarn semantic-release` runs semantic release to calculate version numbers based on the nature of changes since the last version (used in CI pipelines)
- `yarn lint` runs the linter on all .ts(x) and .js(x) files and outputs all errors
- `yarn format` runs prettier on all .ts(x) and .js(x) files and formats them according to the project standards

## Notes

- All tools are configured via their respective config files instead of adding the config in `package.json`. There is also some vscode project config in `.vscode/settings.json`
- eslint: `.eslintrc`
- lint-staged: `.lintstagedrc`
- prettier: `.prettierrc`
- commitlint: `commitlint.config.js`
- husky: `.husky`
- jest: `jest.config.js`
- semantic-release: `release.config.js`
- typedoc: `typedoc.json`
- github actions: `.github/main.yml`
- The CI config assumes a `master` branch for stable releases and a `beta` branch for beta releases. Every time something gets pushed to either of those branches (or any time a pull request is opened to any branch), github actions will run. Semantic-release config makes it so that actual releases are only made on pushes to `master` or `beta`
- The CI config also adds an extra couple of steps to flatten the file structure that actually gets published. This means that your published package will have the built files at the root level instead of inside a `dist` folder. Those steps are:
- copy `package.json` into the `dist` folder after building
- `cd` into the `dist` folder
- install deps into the `dist` folder
- run `semantic-release` from there
- In order for automated NPM releases to actually work, you need to add an NPM auth token as a secret in your repo. To do that, go to your repo's `settings -> secrets -> add a new secret` input `NPM_TOKEN` as the secret name and the token you generated on your NPM account in the text area
- If you don't need automated NPM releases, you might want to uninstall `semantic-release` and tweak the github actions yaml file to skip the release step
6 changes: 6 additions & 0 deletions commitlint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
extends: ['@commitlint/config-conventional'],
ignores: [
(message) => /^chore\(release\): \d+\.\d+\.\d+(-alpha(\.\d+)*)? \[skip ci\]/.test(message),
],
};
10 changes: 10 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
testMatch: ['**/__tests__/**/*.(ts|tsx)'],
testPathIgnorePatterns: ['dist'],
moduleNameMapper: {
'~/(.*)': '<rootDir>/src/$1',
},
};
61 changes: 61 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
{
"name": "typescript-repo-template",
"version": "0.0.0",
"description": "Template repository for typescript projects",
"types": "dist/index.d.ts",
"author": "Polymesh Association",
"license": "ISC",
"scripts": {
"test": "jest --coverage",
"build:ts": "ttsc -b",
"build:docs": "typedoc src",
"commit": "npx git-cz",
"semantic-release": "semantic-release",
"lint": "eslint src --ext .js,.jsx,.ts,.tsx",
"format": "cross-env prettier-eslint $PWD\"/src/**/*.{ts,tsx,js,jsx,json,css,md}\" --write"
},
"devDependencies": {
"@commitlint/cli": "17.5.1",
"@commitlint/config-conventional": "17.4.4",
"@golevelup/ts-jest": "^0.5.6",
"@types/jest-when": "^3.5.5",
"@semantic-release/changelog": "^6.0.3",
"@types/jest": "27.4.0",
"@types/node": "18.15.11",
"@typescript-eslint/eslint-plugin": "5.57.0",
"@typescript-eslint/parser": "5.57.0",
"@zerollup/ts-transform-paths": "1.7.18",
"cross-env": "7.0.3",
"eslint": "8.37.0",
"eslint-config-prettier": "8.8.0",
"eslint-config-semistandard": "17.0.0",
"eslint-config-standard": "17.0.0",
"eslint-plugin-import": "2.27.5",
"eslint-plugin-n": "^15.7.0",
"eslint-plugin-node": "11.1.0",
"eslint-plugin-prettier": "4.2.1",
"eslint-plugin-promise": "6.1.1",
"eslint-plugin-simple-import-sort": "10.0.0",
"eslint-plugin-standard": "5.0.0",
"husky": "8.0.3",
"jest": "29.5.0",
"jest-when": "^3.6.0",
"lint-staged": "13.2.0",
"prettier": "2.8.7",
"prettier-eslint": "15.0.1",
"prettier-eslint-cli": "7.1.0",
"semantic-release": "21.0.0",
"ts-jest": "^29.1.0",
"ttypescript": "1.5.15",
"typedoc": "0.23.18",
"typescript": "4.6.2"
},
"config": {
"commitizen": {
"path": "./node_modules/cz-conventional-changelog"
}
},
"publishConfig": {
"access": "public"
}
}
37 changes: 37 additions & 0 deletions release.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
module.exports = {
repositoryUrl: 'https://github.com/PolymeshAssociation/typescript-boilerplate.git',
branches: [
'master',
{
name: 'beta',
prerelease: true,
},
],
/*
* In this order the **prepare** step of @semantic-release/npm will run first
* followed by @semantic-release/git:
* - Update the package.json version and create the npm package tarball
* - Push a release commit and tag, including configurable files
*
* See:
* - https://github.com/semantic-release/semantic-release/blob/beta/docs/usage/plugins.md#plugin-ordering
* - https://github.com/semantic-release/semantic-release/blob/beta/docs/extending/plugins-list.md
*/
plugins: [
'@semantic-release/commit-analyzer',
'@semantic-release/release-notes-generator',
'@semantic-release/changelog',
[
'@semantic-release/npm',
{
tarballDir: 'npm-package/',
},
],
[
'@semantic-release/github',
{
assets: ['CHANGELOG.md'],
},
],
],
};
5 changes: 5 additions & 0 deletions src/__tests__/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
describe('dummy test', () => {
test('should pass', () => {
expect(true).toBe(true);
});
});
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Happy Coding!
console.log('Polymesh Rocks!');
Loading

0 comments on commit 968a6eb

Please sign in to comment.