Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
jdevcs authored Oct 12, 2023
0 parents commit 4896948
Show file tree
Hide file tree
Showing 12 changed files with 4,423 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
extends: "@chainsafe",
}
61 changes: 61 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
name: ci/cd

on:
push:
branches:
- main
pull_request:
branches:
- '**'

jobs:
build:
name: Build
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
cache: yarn
node-version: '18'
- name: Install deps
run: yarn install --frozen-lockfile
- name: Build
run: yarn build
- name: Lint
run: yarn run lint
- name: Tests
run: yarn run test

maybe-release:
name: release
runs-on: ubuntu-latest
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
steps:
- uses: google-github-actions/release-please-action@v3
id: release
with:
release-type: node
package-name: release-please-action
changelog-types: '[{"type":"feat","section":"Features","hidden":false},{"type":"fix","section":"Bug Fixes","hidden":false},{"type":"chore","section":"Miscellaneous","hidden":true}]'

- uses: actions/checkout@v3
if: ${{ steps.release.outputs.release_created }}

- uses: actions/setup-node@v3
with:
cache: 'yarn'
node-version: 18
registry-url: 'https://registry.npmjs.org'
if: ${{ steps.release.outputs.release_created }}

- run: yarn install --frozen-lockfile
if: ${{ steps.release.outputs.release_created }}

- run: yarn build
if: ${{ steps.release.outputs.release_created }}

- run: yarn publish --access public
env:
NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN}}
if: ${{ steps.release.outputs.release_created }}
23 changes: 23 additions & 0 deletions .github/workflows/pr_title.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: "Semantic PR"

on:
pull_request_target:
types:
- opened
- edited
- synchronize

jobs:
main:
name: Validate PR title
runs-on: ubuntu-latest
steps:
- uses: amannn/action-semantic-pull-request@v4
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
types: |
fix
feat
chore
revert
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
lib
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
web3-plugin-template
===========

Contributing
------------

Pull requests are welcome. For major changes, please open an issue first
to discuss what you would like to change.

Please make sure to update tests as appropriate.

License
-------

[MIT](https://choosealicense.com/licenses/mit/)
41 changes: 41 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"name": "web3-plugin-template",
"version": "1.0.0",
"description": "Template plugin to extend web3.js with additional methods",
"main": "lib/index.js",
"types": "lib/index.d.ts",
"homepage": "https://github.com/web3/web3.js-plugin-template#readme",
"bugs": {
"url": "https://github.com/web3/web3.js-plugin-template/issues"
},
"scripts": {
"lint": "eslint '{src,test}/**/*.ts'",
"build": "tsc --project tsconfig.build.json",
"test": "jest --config=./test/jest.config.js"
},
"contributors": [
"ChainSafe <[email protected]>"
],
"license": "MIT",
"repository": {
"type": "git",
"url": "[email protected]:web3/web3.js-plugin-template.git"
},
"dependencies": {
},
"devDependencies": {
"@chainsafe/eslint-config": "^2.0.0",
"@types/jest": "^29.5.2",
"@types/node": "^20.2.6",
"eslint": "8",
"jest": "^29.5.0",
"jest-extended": "^4.0.0",
"ts-jest": "^29.1.0",
"ts-node": "^10.9.1",
"typescript": "^5.1.3",
"web3": "^4.0.3"
},
"peerDependencies": {
"web3": ">= 4.0.3"
}
}
16 changes: 16 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Web3PluginBase } from "web3";

export class TemplatePlugin extends Web3PluginBase {
public pluginNamespace = "template";

public test(param: string): void {
console.log(param);
}
}

// Module Augmentation
declare module "web3" {
interface Web3Context {
template: TemplatePlugin;
}
}
31 changes: 31 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import Web3, { core } from "web3";
import { TemplatePlugin } from "../src";

describe("TemplatePlugin Tests", () => {
it("should register TokensPlugin plugin on Web3Context instance", () => {
const web3Context = new core.Web3Context("http://127.0.0.1:8545");
web3Context.registerPlugin(new TemplatePlugin());
expect(web3Context.template).toBeDefined();
});

describe("TemplatePlugin method tests", () => {
let consoleSpy: jest.SpiedFunction<typeof global.console.log>;

let web3Context: Web3;

beforeAll(() => {
web3Context = new Web3("http://127.0.0.1:8545");
web3Context.registerPlugin(new TemplatePlugin());
consoleSpy = jest.spyOn(global.console, "log").mockImplementation();
});

afterAll(() => {
consoleSpy.mockRestore();
});

it("should call TempltyPlugin test method with expected param", () => {
web3Context.template.test("test-param");
expect(consoleSpy).toHaveBeenCalledWith("test-param");
});
});
});
5 changes: 5 additions & 0 deletions test/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/** @type {import('ts-jest').JestConfigWithTsJest} */
module.exports = {
preset: "ts-jest",
testEnvironment: "node",
};
5 changes: 5 additions & 0 deletions tsconfig.build.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"extends": "./tsconfig.json",
"include": ["src"],
"exclude": ["node_modules", "test"]
}
25 changes: 25 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"compilerOptions": {
"baseUrl": "./",
"resolveJsonModule": true,
"forceConsistentCasingInFileNames": true,
"target": "es2020",
"module": "commonjs",
"declaration": true,
"moduleResolution": "node",
"noFallthroughCasesInSwitch": true,
"noImplicitReturns": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"pretty": true,
"removeComments": true,
"sourceMap": true,
"declarationMap": true,
"strict": true,
"strictNullChecks": true,
"outDir": "lib",
"esModuleInterop": true
},
"include": ["src", "test"],
"exclude": ["node_modules"]
}
Loading

0 comments on commit 4896948

Please sign in to comment.