Skip to content

Commit 0947402

Browse files
authored
update templates/react-counter & Refactor create-rooch (rooch-network#1743)
* Refactor create-rooch & update templates * rm test val
1 parent bec054f commit 0947402

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+1124
-14054
lines changed

pnpm-lock.yaml

+675-1,597
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pnpm-workspace.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ packages:
22
- 'infra/dashboard/**'
33
- 'infra/rooch-portal/**'
44
- 'grow-rooch/**'
5-
# TODO: sdk/** There is a compilation conflict with dashboard
65
- 'sdk/typescript/build-scripts'
76
- 'sdk/typescript/rooch-sdk'
87
- 'sdk/typescript/rooch-sdk-kit'
98
- 'sdk/typescript/templates/**'
9+
- 'sdk/typescript/rooch-create'
1010
- 'docs/**'
+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// Copyright (c) RoochNetwork
2+
// SPDX-License-Identifier: Apache-2.0
3+
require('../dist/index.js');

sdk/typescript/rooch-create/dist/index.js

-2
This file was deleted.

sdk/typescript/rooch-create/dist/templates/react/package.json

-28
This file was deleted.
+21-15
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,39 @@
11
{
22
"name": "@roochnetwork/create-rooch",
3-
"version": "0.0.6",
3+
"version": "0.1.1",
44
"description": "Create a new Rooch project",
55
"license": "Apache-2.0",
66
"author": "Rooch.network <[email protected]>",
77
"bin": {
8-
"rooch": "dist/index.js"
8+
"rooch": "./bin/index.js"
99
},
10-
"files": ["dist"],
10+
"files": ["dist", "bin"],
11+
"type": "commonjs",
12+
"main": "./dist/cjs/index.js",
13+
"module": "./dist/esm/index.js",
14+
"types": "./dist/cjs/index.d.ts",
1115
"scripts": {
1216
"build": "pnpm run build:js",
13-
"build:js": "tsup && ./scripts/copy-templates.sh",
14-
"clean": "pnpm run clean:js",
15-
"clean:js": "rimraf dist",
16-
"dev": "tsup --watch",
17-
"prepublishOnly": "npm run clean && npm run build",
18-
"test": "pnpm run test:react",
19-
"test:ci": "pnpm run test",
20-
"test:react": "dist/cli.js test-project --template react && rimraf test-project"
17+
"build:js": "tsc --build && ./scripts/copy-templates.sh",
18+
"clean": "rm -rf tsconfig.tsbuildinfo ./dist",
19+
"prepublishOnly": "pnpm build",
20+
"prettier:check": "prettier -c --ignore-unknown .",
21+
"prettier:fix": "prettier -w --ignore-unknown .",
22+
"eslint:check": "eslint --max-warnings=0 .",
23+
"eslint:fix": "pnpm run eslint:check --fix",
24+
"lint": "pnpm run eslint:check && pnpm run prettier:check",
25+
"lint:fix": "pnpm run eslint:fix && pnpm run prettier:fix"
2126
},
2227
"dependencies": {
23-
"create-create-app": "git+https://github.com/holic/create-create-app#74376c59b48a04aabbe94d9cacfe9cb1cecccd63"
28+
"typescript": "^5.1.6",
29+
"enquirer": "^2.4.1"
2430
},
2531
"devDependencies": {
26-
"@types/node": "^18.15.13",
27-
"tsup": "^6.7.0"
32+
"@types/node": "^18.15.13"
2833
},
2934
"publishConfig": {
3035
"access": "public",
3136
"registry": "https://registry.npmjs.org"
32-
}
37+
},
38+
"sideEffects": false
3339
}

sdk/typescript/rooch-create/scripts/copy-templates.sh

+35-7
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,48 @@
22
# Copyright (c) RoochNetwork
33
# SPDX-License-Identifier: Apache-2.0
44

5-
#!/usr/bin/env bash
6-
# Copyright (c) RoochNetwork
7-
# SPDX-License-Identifier: Apache-2.0
8-
95
# Find git-aware template files (ignores things like node_modules, etc.)
106
# and copy them to dist/templates
117
git ls-files ../templates/ | rsync --files-from=- ../ dist
128

13-
# Replace all Rooch package links with mustache placeholder used by create-create-app
14-
# that will be replaced with the latest Rooch version number when the template is used
9+
# Function to get the latest version from NPM
10+
get_latest_version() {
11+
local package_name=$1
12+
npm show "$package_name" version
13+
}
14+
15+
# Find all package.json files
1516
find ./dist/templates/* -name "package.json" -type f | while read -r file; do
1617
echo "Before replacement in $file:"
1718
cat "$file"
18-
perl -pi -e 's|"(?=@roochnetwork)([^"]+)":\s*"link:[^"]+"|"\1": "{{rooch-version}}"|g' "$file"
19+
20+
# Read the package.json content
21+
content=$(cat "$file")
22+
23+
# Extract all @roochnetwork packages with link: or workspace: versions
24+
packages=$(echo "$content" | jq -r '
25+
.dependencies // {} | to_entries |
26+
map(select(.key | test("^@roochnetwork/")) | select(.value | test("^link:|^workspace:"))) |
27+
.[].key
28+
')
29+
30+
if [ -z "$packages" ]; then
31+
echo "No @roochnetwork packages to update."
32+
continue
33+
fi
34+
35+
updated_content=$content
36+
37+
# Iterate over each package to update its version
38+
for package in $packages; do
39+
latest_version=$(get_latest_version "$package")
40+
updated_content=$(echo "$updated_content" | jq --arg package "$package" --arg version "$latest_version" '
41+
.dependencies[$package] = $version
42+
')
43+
done
44+
45+
echo "$updated_content" > "$file"
46+
1947
echo "After replacement in $file:"
2048
cat "$file"
2149
echo

sdk/typescript/rooch-create/src/index.ts

+107-27
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,112 @@
22
// Copyright (c) RoochNetwork
33
// SPDX-License-Identifier: Apache-2.0
44

5-
import { create } from 'create-create-app'
6-
import { resolve } from 'path'
7-
import packageJson from '../package.json'
8-
9-
const templateRoot = resolve(__dirname, '..', 'dist', 'templates')
10-
11-
// See https://github.com/uetchy/create-create-app/blob/master/README.md for other options.
12-
13-
create('create-rooch', {
14-
templateRoot,
15-
defaultTemplate: 'react',
16-
// templates use pnpm workspaces, so default to that for now
17-
// not sure if it's worth trying to support multiple kinds of package managers for monorepos, given the tooling is so different
18-
defaultPackageManager: 'pnpm',
19-
promptForDescription: false,
20-
promptForAuthor: false,
21-
promptForEmail: false,
22-
promptForLicense: false,
23-
promptForTemplate: true,
24-
caveat: ({ answers, packageManager }) =>
25-
`Done! Play in the rooch with \`cd ${answers.name}\` and \`${packageManager} run dev\``,
26-
extra: {
27-
'rooch-version': {
28-
type: 'input',
29-
describe: 'The version of Rooch packages to use, defaults to latest',
30-
default: packageJson.version,
5+
import { existsSync, statSync } from 'fs';
6+
import { mkdir, readdir, readFile, writeFile } from 'fs/promises';
7+
import { relative, resolve } from 'path';
8+
import { parseArgs } from 'util';
9+
import { prompt } from 'enquirer';
10+
11+
const { values: args } = parseArgs({
12+
options: {
13+
template: {
14+
type: 'string',
15+
default: '',
16+
short: 't',
3117
},
3218
},
33-
})
19+
});
20+
21+
async function main() {
22+
const results = await prompt<{
23+
template: string;
24+
dAppName: string;
25+
}>(
26+
[
27+
{
28+
type: 'select',
29+
name: 'template',
30+
message: 'Which starter template would you like to use?',
31+
choices: [
32+
{
33+
name: 'react-counter',
34+
hint: 'React dApp with a move smart contract that implements a distributed counter',
35+
},
36+
],
37+
},
38+
{
39+
type: 'input',
40+
name: 'dAppName',
41+
message: 'What is the name of your dApp? (this will be used as the directory name)',
42+
initial: 'my-first-dapp',
43+
},
44+
].filter((question) => !args[question.name as 'template']),
45+
);
46+
47+
const outDir = resolve(process.cwd(), results.dAppName);
48+
49+
if (existsSync(outDir)) {
50+
throw new Error(`Directory ${outDir} already exists`);
51+
}
52+
53+
const files = await collectFiles(results.template ?? args.template, results.dAppName);
54+
await writeFiles(files, outDir);
55+
}
56+
57+
main();
58+
59+
async function collectFiles(template: string, dAppName: string) {
60+
// const dependencies = await getDependencyVersions();
61+
const templateDir = resolve(__dirname, '../dist/templates', template);
62+
const files = new Array<{
63+
path: string;
64+
content: Buffer;
65+
}>();
66+
67+
if (!statSync(templateDir).isDirectory()) {
68+
throw new Error(`Template ${templateDir} could not be found`);
69+
}
70+
71+
await addDir(templateDir);
72+
73+
return files;
74+
75+
async function addDir(dir: string) {
76+
const entries = await readdir(dir);
77+
78+
for (const entry of entries) {
79+
if (entry === 'node_modules') {
80+
continue;
81+
}
82+
const entryPath = resolve(dir, entry);
83+
const stat = statSync(entryPath);
84+
85+
if (stat.isDirectory()) {
86+
await addDir(entryPath);
87+
} else {
88+
let content = await readFile(entryPath);
89+
90+
if (entry === 'package.json') {
91+
const json = JSON.parse(content.toString());
92+
json.name = dAppName;
93+
94+
content = Buffer.from(JSON.stringify(json, null, 2));
95+
}
96+
97+
files.push({ path: relative(templateDir, entryPath), content });
98+
}
99+
}
100+
}
101+
}
102+
103+
async function writeFiles(files: Array<{ path: string; content: Buffer }>, outDir: string) {
104+
for (const file of files) {
105+
const filePath = resolve(outDir, file.path);
106+
const dirPath = resolve(filePath, '..');
107+
if (!existsSync(dirPath)) {
108+
await mkdir(dirPath, { recursive: true });
109+
}
110+
111+
await writeFile(filePath, file.content);
112+
}
113+
}
+11-10
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
{
2-
"exclude": ["dist"],
2+
"extends": "../build-scripts/tsconfig.shared.json",
3+
"include": ["src"],
34
"compilerOptions": {
4-
"target": "es2020",
5-
"module": "commonjs",
6-
"declaration": true,
7-
"inlineSourceMap": true,
8-
"outDir": "./dist",
9-
"strict": true,
10-
"esModuleInterop": true,
11-
"resolveJsonModule": true
12-
}
5+
"target": "ES2020",
6+
"module": "CommonJS",
7+
"moduleResolution": "Node",
8+
"outDir": "dist",
9+
"isolatedModules": true,
10+
"rootDir": "src",
11+
"emitDeclarationOnly": false
12+
},
13+
"references": [{ "path": "../rooch-sdk" }, { "path": "../rooch-sdk-kit" }]
1314
}

sdk/typescript/rooch-create/tsconfig.tsbuildinfo

+1
Large diffs are not rendered by default.

sdk/typescript/rooch-create/tsup.config.ts

-10
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+
dist
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Rooch dApp Starter Template
2+
3+
This dApp was created using `@roochnetwork/create-rooch` that sets up a simple React
4+
Client dApp using the following tools:
5+
6+
- [React](https://react.dev/) as the UI framework
7+
- [TypeScript](https://www.typescriptlang.org/) for type checking
8+
- [Vite](https://vitejs.dev/) for build tooling
9+
- [Radix UI](https://www.radix-ui.com/) for pre-built UI components
10+
- [ESLint](https://eslint.org/) for linting
11+
- [@roochnetwork/rooch-sdk](https://www.npmjs.com/package/@roochnetwork/rooch-sdk)
12+
- [@roochnetwork/rooch-sdk-kit](https://www.npmjs.com/package/@roochnetwork/rooch-sdk-kit) for connecting to
13+
wallets and loading data
14+
- [pnpm](https://pnpm.io/) for package management
15+
16+
We can ensure we have some Gss in our new wallet by requesting Gas from the [faucet doc](https://rooch.network/learn/portal) Receive Gas chapter
17+
18+
## Install [rooch cli doc](https://rooch.network/zh-CN/build/getting-started/installation)
19+
## Publish [counter package doc](https://github.com/rooch-network/rooch/tree/main/examples/counter)
20+
21+
## Starting your dApp
22+
23+
To install dependencies you can run
24+
25+
```bash
26+
pnpm install
27+
```
28+
29+
To start your dApp in development mode run
30+
31+
```bash
32+
pnpm dev
33+
```
34+
35+
## Building
36+
37+
To build your app for deployment you can run
38+
39+
```bash
40+
pnpm build
41+
```

0 commit comments

Comments
 (0)