Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add frontend templates #197

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 11 additions & 2 deletions src/commands/contract/new.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import {
checkCliDependencies,
copyContractTemplateFiles,
processTemplates,
getTemplates,
getTemplates, copyFrontendTemplateFiles,
} from "../../lib/index.js";
import { email, name, pickTemplate } from "../../lib/prompts.js";
import { choice, email, name, pickTemplate } from "../../lib/prompts.js";
import { paramCase, pascalCase, snakeCase } from "change-case";
import { execaCommandSync } from "execa";
import inquirer from "inquirer";
Expand Down Expand Up @@ -59,6 +59,7 @@ export class NewContract extends SwankyCommand<typeof NewContract> {
"What is your name?"
),
email(),
choice("useFrontendTemplate", "Do you want to use a frontend template?"),
];

const answers = await inquirer.prompt(questions);
Expand All @@ -78,6 +79,14 @@ export class NewContract extends SwankyCommand<typeof NewContract> {
"Copying contract template files"
);

if(answers.useFrontendTemplate) {
await copyFrontendTemplateFiles(
templates.frontendTemplatesPath,
args.contractName,
projectPath,
)
}

await this.spinner.runCommand(
() =>
processTemplates(projectPath, {
Expand Down
22 changes: 19 additions & 3 deletions src/commands/init/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
processTemplates,
swankyNode,
getTemplates,
copyFrontendTemplateFiles
} from "../../lib/index.js";
import {
DEFAULT_ASTAR_NETWORK_URL,
Expand Down Expand Up @@ -268,6 +269,21 @@ export class Init extends SwankyCommand<typeof Init> {
],
runningMessage: "Copying contract template files",
});
const {useFrontendTemplate} = await inquirer.prompt([
choice("useFrontendTemplate", "Do you want to use a frontend template?"),
]);
if(useFrontendTemplate)
{
this.taskQueue.push({
task: copyFrontendTemplateFiles,
args: [
templates.frontendTemplatesPath,
contractName,
this.projectPath,
],
runningMessage: "Copying frontend template files",
});
}

this.taskQueue.push({
task: processTemplates,
Expand Down Expand Up @@ -402,19 +418,19 @@ export class Init extends SwankyCommand<typeof Init> {
runningMessage: "Copying workspace files",
});

const existingPJsonPath = path.resolve(pathToExistingProject, "package.json");
const existingPJsonPath = path.resolve(pathToExistingProject, "package.json.hbs");
if (await pathExists(existingPJsonPath)) {
this.taskQueue.push({
task: async (pJsonPath, projectPath) => {
const existingPJson = await readJSON(pJsonPath);
const templatePJsonPath = path.resolve(projectPath, "package.json");
const templatePJsonPath = path.resolve(projectPath, "package.json.hbs");
const templatePJson = await readJSON(templatePJsonPath);
const mergedJson = merge(templatePJson, existingPJson);
await remove(templatePJsonPath);
await writeJSON(templatePJsonPath, mergedJson, { spaces: 2 });
},
args: [existingPJsonPath, this.projectPath],
runningMessage: "Merging package.json",
runningMessage: "Merging package.json.hbs",
});
}
}
Expand Down
26 changes: 26 additions & 0 deletions src/lib/tasks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import decompress from "decompress";
import { Spinner } from "./spinner.js";
import { SupportedPlatforms, SupportedArch } from "../types/index.js";
import { ConfigError, NetworkError } from "./errors.js";
import { existsSync } from "node:fs";

export async function checkCliDependencies(spinner: Spinner) {
const dependencyList = [
Expand Down Expand Up @@ -59,6 +60,31 @@ export async function copyContractTemplateFiles(
);
}

export async function copyFrontendTemplateFiles(
frontendTemplatePath: string,
contractName: string,
projectPath: string,
) {
if(!existsSync(path.resolve(projectPath, "frontends"))) {
await copy(
path.resolve(frontendTemplatePath, "ui"),
path.resolve(projectPath, "frontends", "ui")
)
await copy(
path.resolve(frontendTemplatePath, "package.json.hbs"),
path.resolve(projectPath, "frontends", "package.json.hbs")
)
await copy(
path.resolve(frontendTemplatePath, "pnpm-workspace.yaml.hbs"),
path.resolve(projectPath, "frontends", "pnpm-workspace.yaml.hbs")
)
}
await copy(
path.resolve(frontendTemplatePath, contractName),
path.resolve(projectPath, "frontends", contractName)
)
}

export async function processTemplates(projectPath: string, templateData: Record<string, string>) {
const templateFiles = await globby(projectPath, {
expandDirectories: { extensions: ["hbs"] },
Expand Down
2 changes: 2 additions & 0 deletions src/lib/templates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const __dirname = path.dirname(__filename);
export function getTemplates() {
const templatesPath = path.resolve(__dirname, "..", "templates");
const contractTemplatesPath = path.resolve(templatesPath, "contracts");
const frontendTemplatesPath = path.resolve(templatesPath, "frontends");
const fileList = readdirSync(contractTemplatesPath, {
withFileTypes: true,
});
Expand All @@ -19,5 +20,6 @@ export function getTemplates() {
templatesPath,
contractTemplatesPath,
contractTemplatesList,
frontendTemplatesPath,
};
}
4 changes: 4 additions & 0 deletions src/templates/frontends/blank/README.md.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Have Questions?

For any questions about building front end applications with [useink](https://use.ink/frontend/overview/), join the [Element chat](https://matrix.to/#/%23useink:parity.io).

5 changes: 5 additions & 0 deletions src/templates/frontends/blank/hbs.sh.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
files=$(find . -type f | tr " " "\n")
for f in $files; do
mv $f "$f.hbs"
done
echo "done"
13 changes: 13 additions & 0 deletions src/templates/frontends/blank/index.html.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/logo.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>ink! Examples</title>
</head>
<body>
<div id="root"></div>
<script type="module" src="/src/main.tsx"></script>
</body>
</html>
26 changes: 26 additions & 0 deletions src/templates/frontends/blank/package.json.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"name": "blank",
"private": true,
"version": "0.1.0",
"type": "module",
"scripts": {
"dev": "vite",
"build": "tsc && vite build",
"lint": "eslint src --ext ts,tsx --report-unused-disable-directives --max-warnings 0",
"preview": "vite preview"
},
"dependencies": {
"ui": "workspace:ui@*"
},
"devDependencies": {
"@types/react": "^18.0.37",
"@types/react-dom": "^18.0.11",
"@vitejs/plugin-react": "^4.0.0",
"autoprefixer": "^10.4.14",
"eslint": "^8.38.0",
"postcss": "^8.4.24",
"tailwindcss": "^3.3.2",
"typescript": "^5.0.2",
"vite": "^4.3.9"
}
}
6 changes: 6 additions & 0 deletions src/templates/frontends/blank/postcss.config.js.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export default {
plugins: {
tailwindcss: {},
autoprefixer: {},
},
};
1 change: 1 addition & 0 deletions src/templates/frontends/blank/public/logo.svg.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<svg clip-rule="evenodd" fill-rule="evenodd" stroke-linecap="round" stroke-linejoin="round" stroke-miterlimit="1.5" viewBox="0 0 585 1776" xmlns="http://www.w3.org/2000/svg"><g fill="#bc83fb" stroke="#000"><path d="m61.59 492.6-17.04-77.34c-15.38-69.8 16.47-141.5 78.58-176.8 40.37-23.04 83.92-59.21 115.6-115.4M544.1 492.6s4.848-21.72 11.01-49.35c12.99-58.2-14.73-117.9-67.58-145.5-53-27.82-109.8-79.72-114.7-174.7l-.854 319.6 172.2 50z" stroke-width="38.83"/><path d="M271.4 61.1c.001-.002 8.026-36.94 49.42-37.2 22.92-.142 43.07 15.17 49.42 37.19.001.001 18.47 136.3 28.24 208.5a128.539 128.539 0 0 0 34.03 71.12c65 68.64 209.5 254.3 67.78 427.9 0 0-11.16-146.8-188.3-146.8h-6.074c-177.2 0-200.4 146.8-200.4 146.8-178.1-218.1 111.5-582.7 165.9-707.5z" stroke-width="47.8"/></g><path d="M485.2 826.2c59.88 25.86 80.38 99.62 64.18 161a.783.783 0 0 0-.015.066c-76.12 392-273 737.6-353.1 764.5 0 0 14.57-96.57 21.81-144.6a158.037 158.037 0 0 0-3.296-63.26c-21.19-80.79-71.19-148.9-91.26-226.7-30.17-116.9-38.33-243.9-77.66-386.6-11.65-41.96 7.464-86.39 45.95-106.8l-.039-.076c54.69-10.51 8.172-71.69 5.561-116.8 42.4-127.5 76.69-171.2 189.1-172.7 121.9-11.06 190.5 83.67 222.1 170.5-10.38 109.7-29.44 100.2-23.34 121.3z" fill="#bc83fb"/><g fill="#5a007e" stroke="#000" stroke-width="47.8"><path d="M505.5 1167c-111.8 446.2-51.52 489.3-51.52 489.3l-80.53-75.67M114.6 1280c7.085 193 44.58 303.8-22.91 337.9 0 0 71.03-2.088 107.3-60.75"/></g><path d="m455.4 845.6 53.24 32.45c15.55 9.478 23.27 27.85 19.16 45.59-23.43 100.1-101.5 424-152.8 516.7l-1.498 5.541-5.115 19.17c-4.31 20.48-9.004 45.19-8.722 32.68l8.722-32.68c2.245-10.96 4.41-20.72 5.615-22.84l.998-1.869c67.53-250.1 123.4-436.8-7.457-539 45.51-7.88 74.76-25.83 87.9-55.73z" fill="#d8b6ff"/><path d="M298.4 80.96c.859-19.67-33.44 70.94-35.6 77.78-60.31 191.6-222.9 305.5-155.7 529.9.599 2.001 7.039-11.2 19.88-34 36.46-64.71 135.2-82.88 135.2-82.88-188-146.4 29.66-342.3 36.15-490.8z" fill="#5a007e"/><path d="M334.5 92.91c4.751-19.1 13.02 40.86 11.7 47.91-34.98 185.6 247.2 345.9 149.7 559.7-.425.933-7.418-32.56-26.6-57.79-19.91-26.18-52.03-44.15-52.03-44.15 104.5-117.2-144.5-257.3-82.77-505.7z" fill="#d8b6ff"/><path d="M213.8 895.1c-125.9 104.1 17.82 544.2 32.35 724.2.159 1.97-.512 3.869-.819 5.72-3.224 19.42-3.006-33.56-3.849-40.68-7.219-60.98-39.37-151.2-74.87-226.2-36.85-77.85-42.67-199-51.18-282.2-13.83-135.2-60.48-169.7 27.88-237.6.248-.191 2.731.897 6.684 2.883 9.77 26.17 30.99 43.78 63.81 53.93z" fill="#5a007e"/><path d="m221.2 897.3 3.017.727c-27.17 39.28-62.22 178 1.925 558-15.66-92.77-101-443.9-6.891-559.3l1.949.562z" fill="#d8b6ff"/><path d="M229.5 899.3c19.85 4.357 43.19 6.542 70.03 6.689-73.29 35.32-111.4 86.45-42.92 437.1 19.98 102.3-11.21 202.3-10.52 276.2.019 1.977-.512 3.869-.819 5.72-3.224 19.42-3.006-33.56-3.849-40.68-23.44-278.1-61.4-522.9-11.92-685z" fill="#5a007e"/><path d="M485.2 826.2c59.88 25.86 80.38 99.62 64.18 161a.783.783 0 0 0-.015.066c-76.12 392-273 737.6-353.1 764.5 0 0 14.57-96.57 21.81-144.6a158.037 158.037 0 0 0-3.296-63.26c-21.19-80.79-71.19-148.9-91.26-226.7-28.86-111.9-37.58-232.9-72.72-368.1-13.61-52.31 12.67-106.8 62.08-128.7l-.039-.089" fill="none" stroke="#000" stroke-width="47.8"/><g fill="#d8b6ff" stroke="#000" stroke-width="47.8"><path d="M330.9 1488c-7.247 28.95 4.705 57.4 26.67 63.48s45.69-12.48 52.94-41.43-4.706-57.4-26.68-63.48c-21.97-6.085-45.69 12.48-52.94 41.43zM375.1 1349c-9.986 36.51 4.39 72.92 32.08 81.24 27.69 8.325 58.28-14.56 68.27-51.07s-4.39-72.92-32.08-81.24c-27.69-8.326-58.28 14.56-68.27 51.07zM294.9 1587c-13.53 22.43-10.45 48.87 6.875 59 17.33 10.13 42.38.142 55.91-22.29 13.53-22.43 10.45-48.87-6.875-59s-42.38-.142-55.91 22.29zM237 1669c-17.26 15.76-21.14 38.38-8.646 50.48s36.65 9.12 53.91-6.644c17.26-15.76 21.14-38.38 8.646-50.48s-36.65-9.12-53.91 6.644z"/></g><g fill="none" stroke="#000"><path d="m289.7 785.5.001.004a22.038 22.038 0 0 0 41.405-.205" stroke-width="17.41"/><path d="m369.6 730.2 5.108-3.539c20.62-14.29 48.78-10.2 64.48 9.374l2.116 2.638M253.4 731.7l-14.19-7.343c-18.5-9.576-41.08-6.06-55.79 8.691l-7.824 7.845" stroke-width="29.87"/></g></svg>
12 changes: 12 additions & 0 deletions src/templates/frontends/blank/src/App.tsx.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { InkLayout } from 'ui';
function App() {
return (
<InkLayout
className='md:py-12 md:p-6 p-4 h-screen flex items-center justify-center'
animationSrc='https://raw.githubusercontent.com/paritytech/ink-workshop/d819d10a35b2ac3d2bff4f77a96701a527b3ad3a/frontend/public/dark-sea-creatures.json'
>
</InkLayout>
);
}

export default App;
7 changes: 7 additions & 0 deletions src/templates/frontends/blank/src/Global.css.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
@tailwind base;
@tailwind components;
@tailwind utilities;

body {
background-color: #1A1452;
}
26 changes: 26 additions & 0 deletions src/templates/frontends/blank/src/main.tsx.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import React from 'react';
import ReactDOM from 'react-dom/client';
import 'ui/style.css';
import { UseInkProvider } from 'useink';
import { RococoContractsTestnet } from 'useink/chains';
import { NotificationsProvider } from 'useink/notifications';
import App from './App.tsx';
import './Global.css';

ReactDOM.createRoot(document.getElementById('root') as HTMLElement).render(
<React.StrictMode>
<UseInkProvider
config=\{{
dappName: "blank",
chains: [RococoContractsTestnet],
caller: {
default: '5EyR7vEk7DtvEWeefGcXXMV6hKwB8Ex5uvjHufm466mbjJkR',
},
}}
>
<NotificationsProvider>
<App />
</NotificationsProvider>
</UseInkProvider>
</React.StrictMode>,
);
1 change: 1 addition & 0 deletions src/templates/frontends/blank/src/vite-env.d.ts.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/// <reference types="vite/client" />
2 changes: 2 additions & 0 deletions src/templates/frontends/blank/tailwind.config.js.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import config from '../ui/tailwind.config';
export default config;
25 changes: 25 additions & 0 deletions src/templates/frontends/blank/tsconfig.json.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"compilerOptions": {
"target": "ES2020",
"useDefineForClassFields": true,
"lib": ["ES2020", "DOM", "DOM.Iterable"],
"module": "ESNext",
"skipLibCheck": true,

/* Bundler mode */
"moduleResolution": "bundler",
"allowImportingTsExtensions": true,
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "react-jsx",

/* Linting */
"strict": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noFallthroughCasesInSwitch": true
},
"include": ["src", "../../ui/src/contexts/DeployerContextTsx"],
"references": [{ "path": "./tsconfig.node.json" }]
}
10 changes: 10 additions & 0 deletions src/templates/frontends/blank/tsconfig.node.json.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"compilerOptions": {
"composite": true,
"skipLibCheck": true,
"module": "ESNext",
"moduleResolution": "bundler",
"allowSyntheticDefaultImports": true
},
"include": ["vite.config.ts"]
}
7 changes: 7 additions & 0 deletions src/templates/frontends/blank/vite.config.ts.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import react from '@vitejs/plugin-react';
import { defineConfig } from 'vite';

// https://vitejs.dev/config/
export default defineConfig({
plugins: [react()],
});
21 changes: 21 additions & 0 deletions src/templates/frontends/flipper/.gitignore.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Logs
logs
*.log
pnpm-debug.log*
lerna-debug.log*

node_modules
dist
dist-ssr
*.local

# Editor directories and files
.vscode/*
!.vscode/extensions.json
.idea
.DS_Store
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?
4 changes: 4 additions & 0 deletions src/templates/frontends/flipper/README.md.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Have Questions?

For any questions about building front end applications with [useink](https://use.ink/frontend/overview/), join the [Element chat](https://matrix.to/#/%23useink:parity.io).

5 changes: 5 additions & 0 deletions src/templates/frontends/flipper/hbs.sh.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
files=$(find . -type f | tr " " "\n")
for f in $files; do
mv $f "$f.hbs"
done
echo "done"
13 changes: 13 additions & 0 deletions src/templates/frontends/flipper/index.html.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/logo.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>ink! Examples</title>
</head>
<body>
<div id="root"></div>
<script type="module" src="/src/main.tsx"></script>
</body>
</html>
31 changes: 31 additions & 0 deletions src/templates/frontends/flipper/package.json.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"name": "flipper",
"private": true,
"version": "0.1.0",
"type": "module",
"scripts": {
"dev": "vite",
"build": "tsc && vite build",
"lint": "eslint src --ext ts,tsx --report-unused-disable-directives --max-warnings 0",
"preview": "vite preview"
},
"dependencies": {
"ui": "workspace:ui@*",
"useink": "^1.14.1"
},
"devDependencies": {
"@types/react": "^18.0.37",
"@types/react-dom": "^18.0.11",
"@typescript-eslint/eslint-plugin": "^5.59.0",
"@typescript-eslint/parser": "^5.59.0",
"@vitejs/plugin-react": "^4.0.0",
"autoprefixer": "^10.4.14",
"eslint": "^8.38.0",
"eslint-plugin-react-hooks": "^4.6.0",
"eslint-plugin-react-refresh": "^0.3.4",
"postcss": "^8.4.24",
"tailwindcss": "^3.3.2",
"typescript": "^5.0.2",
"vite": "^4.3.9"
}
}
6 changes: 6 additions & 0 deletions src/templates/frontends/flipper/postcss.config.js.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export default {
plugins: {
tailwindcss: {},
autoprefixer: {},
},
};
Loading