Skip to content

Commit

Permalink
Add Demonstration React Template (#9)
Browse files Browse the repository at this point in the history
* Add Demonstration React Template

* Rework Use of Numbers and BigInts

* Display Network ID

* Remove Unused App.css Files

* Remove More Unused CSS Files
  • Loading branch information
danforbes authored Oct 7, 2024
1 parent 75dda5b commit 87fd705
Show file tree
Hide file tree
Showing 37 changed files with 20,089 additions and 27 deletions.
22 changes: 22 additions & 0 deletions .github/workflows/web3js-react-dapp-demo.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: web3js-react-dapp-demo Build & Test
on:
push:
branches: [ main ]
pull_request:
branches: [ main ]
types: [ opened, reopened, synchronize ]
jobs:
build:
name: Build & Test
runs-on: ubuntu-latest
defaults:
run:
working-directory: templates/demo/web3js-react-dapp-demo
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
- run: npm i
- run: npm run build
- run: npm test
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Usage: npx create-web3js-dapp [options]

Options:
-f, --framework <name> front-end framework (choices: "angular", "react")
-t, --template <type> template type (choices: "minimal")
-t, --template <type> template type (choices: "demonstration", "minimal")
-h, --help display help for command
```

Expand All @@ -30,5 +30,5 @@ can be used to build dApps.

This utility supports the following front-end frameworks:

- [Angular](https://angular.dev/)
- [Angular](https://angular.dev/) (only available as a minimal template)
- [React](https://react.dev/)
35 changes: 24 additions & 11 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { Option, type OptionValues, program } from "commander";
import { select } from "@inquirer/prompts";

import { getSelectedOption } from "./lib";
import type { Options } from "./types";
import { Framework, TemplateType, type Options } from "./types";

program
.addOption(
Expand All @@ -19,7 +19,10 @@ program
]),
)
.addOption(
new Option("-t, --template <type>", "template type").choices(["minimal"]),
new Option("-t, --template <type>", "template type").choices([
"demonstration",
"minimal",
]),
);

program.parse();
Expand Down Expand Up @@ -62,26 +65,36 @@ async function inquire(cliOpts: OptionValues): Promise<Options> {
choices: [
{
name: "Angular",
value: "angular",
value: Framework.Angular,
},
{
name: "React",
value: "react",
value: Framework.React,
},
],
});
}

if (!options.template) {
const choices = [
{
name: "Minimal",
value: TemplateType.Minimal,
description: "Starter project with minimal boilerplate",
},
];

if (options.framework === Framework.React) {
choices.unshift({
name: "Demonstration",
value: TemplateType.Demonstration,
description: "Demonstration dApp",
});
}

options.template = await select({
message: "Select a template type",
choices: [
{
name: "Minimal",
value: "minimal",
description: "Starter project with minimal boilerplate",
},
],
choices,
});
}

Expand Down
63 changes: 60 additions & 3 deletions src/lib.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,18 @@ import { test } from "node:test";
import { join } from "path";

import { getSelectedOption } from "./lib";
import type { Options, SelectedOption } from "./types";
import {
Framework,
TemplateType,
type Options,
type SelectedOption,
} from "./types";

test("getSelectedOption(angular, minimal)", () => {
const opts: Options = { framework: "angular", template: "minimal" };
const opts: Options = {
framework: Framework.Angular,
template: TemplateType.Minimal,
};
const result: SelectedOption = getSelectedOption(opts);

const expectedName: string = "web3js-angular-dapp-min";
Expand All @@ -30,8 +38,29 @@ test("getSelectedOption(angular, minimal)", () => {
);
});

test("getSelectedOption(angular, demonstration)", () => {
const opts: Options = {
framework: Framework.Angular,
template: TemplateType.Demonstration,
};
try {
const result: SelectedOption = getSelectedOption(opts);
} catch (e) {
assert.strictEqual(
e.toString(),
"Error: Angular demonstration dApp has not yet been implemented.",
);
return;
}

assert.strictEqual(true, false);
});

test("getSelectedOption(react, minimal)", () => {
const opts: Options = { framework: "react", template: "minimal" };
const opts: Options = {
framework: Framework.React,
template: TemplateType.Minimal,
};
const result: SelectedOption = getSelectedOption(opts);

const expectedName: string = "web3js-react-dapp-min";
Expand All @@ -54,3 +83,31 @@ test("getSelectedOption(react, minimal)", () => {
"unexpected project location",
);
});

test("getSelectedOption(react, demonstration)", () => {
const opts: Options = {
framework: Framework.React,
template: TemplateType.Demonstration,
};
const result: SelectedOption = getSelectedOption(opts);

const expectedName: string = "web3js-react-dapp-demo";
assert.strictEqual(
result.projectName,
expectedName,
"unexpected project name",
);

const expectedLocation: string = join(
__dirname,
"..",
"templates",
"demo",
expectedName,
);
assert.strictEqual(
result.projectLocation,
expectedLocation,
"unexpected project location",
);
});
24 changes: 19 additions & 5 deletions src/lib.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
import { join } from "path";

import type { Options, SelectedOption } from "./types";
import {
Framework,
TemplateType,
type Options,
type SelectedOption,
} from "./types";

export function getSelectedOption(opts: Options): SelectedOption {
switch (opts.framework) {
case "angular": {
case Framework.Angular: {
if (opts.template === TemplateType.Demonstration) {
throw new Error(
"Angular demonstration dApp has not yet been implemented.",
);
}

const projectName: string = "web3js-angular-dapp-min";
const projectLocation: string = join(
__dirname,
Expand All @@ -15,13 +26,16 @@ export function getSelectedOption(opts: Options): SelectedOption {
);
return { projectName, projectLocation };
}
case "react": {
const projectName: string = "web3js-react-dapp-min";
case Framework.React: {
const isDemo: boolean = opts.template === TemplateType.Demonstration;
const projectName: string = isDemo
? "web3js-react-dapp-demo"
: "web3js-react-dapp-min";
const projectLocation: string = join(
__dirname,
"..",
"templates",
"min",
isDemo ? "demo" : "min",
projectName,
);
return { projectName, projectLocation };
Expand Down
11 changes: 9 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
export type Framework = "angular" | "react";
export type TemplateType = "minimal";
export enum Framework {
Angular = "angular",
React = "react",
}

export enum TemplateType {
Demonstration = "demonstration",
Minimal = "minimal",
}

export interface Options {
framework: Framework;
Expand Down
23 changes: 23 additions & 0 deletions templates/demo/web3js-react-dapp-demo/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.js

# testing
/coverage

# production
/build

# misc
.DS_Store
.env.local
.env.development.local
.env.test.local
.env.production.local

npm-debug.log*
yarn-debug.log*
yarn-error.log*
21 changes: 21 additions & 0 deletions templates/demo/web3js-react-dapp-demo/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 web3js-react-dapp-demo

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Loading

0 comments on commit 87fd705

Please sign in to comment.