Skip to content

Commit 51771f9

Browse files
authored
feat(create): add create dapp only (#232)
* feat(create): add create dapp only * ask for inject config after create dapp only project * fix: inject config with file path target * chore: update readme for create dapp-only * fix(readme): section link
1 parent 6383102 commit 51771f9

4 files changed

Lines changed: 51 additions & 6 deletions

File tree

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ There are BREAKING CHANGES between v0.2.x and v0.3.x, make sure to read the [mig
3030
- [Tweak Devnet Config](#tweak-devnet-config)
3131
- [Create a full-stack Project](#create-a-full-stack-project)
3232
- [Create a script-only Project](#create-a-script-only-project)
33+
- [Create a dApp-only Project](#create-a-dapp-only-project)
3334
- [Build and Deploy a script](#build-and-deploy-a-script)
3435
- [Start the frontend project](#start-the-frontend-project)
3536
- [Debug a transaction](#debug-a-transaction)
@@ -208,6 +209,14 @@ offckb create <your-project-name> --script
208209

209210
Note: you need to have rust/cargo/cargo-generate/clang 16+ installed in your environment to use this command. offckb doesn't do anything really, it just call [ckb-script-template](https://github.com/cryptape/ckb-script-templates) to do all the magic.
210211

212+
### Create a dApp-only Project
213+
214+
You can create a new dApp project without the script(CKB Smart Contract). This is useful when you don't need to create your own on-chain script logic.
215+
216+
```sh
217+
offckb create <your-project-name> --dapp # or -d
218+
```
219+
211220
### Build and Deploy a script
212221

213222
The fullstack boilerplate project is a monorepo, which contains a script project and a frontend project.

src/cli.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { DeployOptions, deploy } from './cmd/deploy';
1111
import { syncScripts } from './cmd/sync-scripts';
1212
import { TransferOptions, transfer } from './cmd/transfer';
1313
import { BalanceOption, balanceOf } from './cmd/balance';
14-
import { create, selectBareTemplate, CreateOption, createScriptProject } from './cmd/create';
14+
import { create, selectBareTemplate, CreateOption, createScriptProject, createDappProject } from './cmd/create';
1515
import { printMyScripts, DeployedScriptOption } from './cmd/my-scripts';
1616
import { Config, ConfigItem } from './cmd/config';
1717
import { debugSingleScript, debugTransaction, parseSingleScriptOption } from './cmd/debug';
@@ -35,12 +35,17 @@ program
3535
.command('create [your-project-name]')
3636
.description('Create a new dApp from bare templates')
3737
.option('-s, --script', 'Only create the script project')
38+
.option('-d, --dapp', 'Only create the ccc dapp project')
3839
.action(async (projectName: string, option: CreateOption) => {
3940
const name = projectName ?? 'my-first-ckb-project';
4041
if (option.script) {
4142
return await createScriptProject(name);
4243
}
4344

45+
if (option.dapp) {
46+
return await createDappProject(name);
47+
}
48+
4449
const template = await selectBareTemplate();
4550
return create(name, template);
4651
});

src/cmd/create.ts

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,26 @@
11
import path from 'path';
22
import { findFileInFolder } from '../util/fs';
33
import { gitCloneAndDownloadFolderSync } from '../util/git';
4-
import { select } from '@inquirer/prompts';
4+
import { injectConfig } from './inject-config';
5+
import { select, confirm } from '@inquirer/prompts';
56
import { execSync } from 'child_process';
67
import { genMyScriptsJsonFile, genSystemScriptsJsonFile } from '../scripts/gen';
78
import { readSettings } from '../cfg/setting';
89
import { BareTemplateOption, loadBareTemplateOpts } from '../template/option';
910
import { OffCKBConfigFile } from '../template/offckb-config';
1011
const version = require('../../package.json').version;
1112

12-
export interface CreateOption {
13-
script: boolean;
14-
}
13+
export type ScriptOnly = {
14+
script: true;
15+
dapp?: false;
16+
};
17+
18+
export type DappOnly = {
19+
dapp: true;
20+
script?: false;
21+
};
22+
23+
export type CreateOption = ScriptOnly | DappOnly;
1524

1625
export function createScriptProject(name: string) {
1726
const cmd = `cargo generate gh:cryptape/ckb-script-templates workspace --name ${name}`;
@@ -22,6 +31,17 @@ export function createScriptProject(name: string) {
2231
}
2332
}
2433

34+
export async function createDappProject(name: string) {
35+
const cmd = `npx create-ccc-app@latest ${name} --ts --}`;
36+
try {
37+
execSync(cmd, { encoding: 'utf-8', stdio: 'inherit' });
38+
const dappFolderPath = path.resolve(process.cwd(), name);
39+
await askForInjectOffckbConfig(dappFolderPath);
40+
} catch (error: unknown) {
41+
console.error('create ccc-appp project failed, ', (error as Error).message);
42+
}
43+
}
44+
2545
export async function create(name: string, template: BareTemplateOption) {
2646
const targetPath = path.resolve(process.cwd(), name);
2747
const settings = readSettings();
@@ -69,3 +89,13 @@ export async function selectBareTemplate() {
6989

7090
return opts.find((opt) => opt.value === answer)!;
7191
}
92+
93+
export async function askForInjectOffckbConfig(dappFolderPath: string) {
94+
const answer = await confirm({
95+
message: 'Do you want to inject offckb configs in your project so that it can work with local blockchain info?',
96+
});
97+
if (answer) {
98+
const target = path.resolve(dappFolderPath, 'offckb.config.ts');
99+
injectConfig({ target });
100+
}
101+
}

src/cmd/inject-config.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ export function injectConfig({ target }: InjectConfigProp) {
4040
const myScriptCodeHash = offCKB.myScripts['script-name'].codeHash;
4141
const omnilockScriptCodeHash = offCKB.systemScripts['omnilock'].codeHash;
4242
43-
Check example at https://github.com/nervosnetwork/docs.nervos.org/tree/develop/examples/simple-transfer
43+
Check more on how to integrate offckb config to develop your app at https://docs.nervos.org/docs/getting-started/quick-start#different-frontend-framework
44+
A Full example can also be found at https://github.com/nervosnetwork/docs.nervos.org/tree/develop/examples/simple-transfer
4445
`);
4546
}

0 commit comments

Comments
 (0)