Skip to content

Commit

Permalink
Add --template arg to create-dapp cli (MystenLabs#14231)
Browse files Browse the repository at this point in the history
## Description 

Describe the changes or additions included in this PR.

## Test Plan 

How did you test the new or updated feature?

---
If your changes are not user-facing and not a breaking change, you can
skip the following section. Otherwise, please indicate what changed, and
then add to the Release Notes section as highlighted during the release
process.

### Type of Change (Check all that apply)

- [ ] protocol change
- [ ] user-visible impact
- [ ] breaking change for a client SDKs
- [ ] breaking change for FNs (FN binary must upgrade)
- [ ] breaking change for validators or node operators (must upgrade
binaries)
- [ ] breaking change for on-chain data layout
- [ ] necessitate either a data wipe or data migration

### Release notes

---------

Co-authored-by: William Robertson <[email protected]>
  • Loading branch information
hayes-mysten and williamrobertson13 authored Oct 12, 2023
1 parent 6896fbc commit 2470213
Showing 1 changed file with 39 additions and 25 deletions.
64 changes: 39 additions & 25 deletions sdk/create-dapp/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,43 +6,57 @@
import { existsSync, statSync } from 'fs';
import { mkdir, readdir, readFile, writeFile } from 'fs/promises';
import { relative, resolve } from 'path';
import { parseArgs } from 'util';
import { prompt } from 'enquirer';

const { values: args } = parseArgs({
options: {
template: {
type: 'string',
default: '',
short: 't',
},
},
});

async function main() {
const results = await prompt<{
template: string;
dAppName: string;
}>([
{
type: 'select',
name: 'template',
message: 'Which started template would you like to use?',
choices: [
{
name: 'react-client-dapp',
hint: 'React Client dApp that reads data from wallet and the blockchain',
},
{
name: 'react-e2e-counter',
hint: 'React dApp with a move smart contract that implements a distributed counter',
},
],
},
{
type: 'input',
name: 'dAppName',
message: 'What is the name of your dApp? (this will be used as the directory name)',
initial: 'my-first-sui-dapp',
},
]);
}>(
[
{
type: 'select',
name: 'template',
message: 'Which starter template would you like to use?',
choices: [
{
name: 'react-client-dapp',
hint: 'React Client dApp that reads data from wallet and the blockchain',
},
{
name: 'react-e2e-counter',
hint: 'React dApp with a move smart contract that implements a distributed counter',
},
],
},
{
type: 'input',

name: 'dAppName',
message: 'What is the name of your dApp? (this will be used as the directory name)',
initial: 'my-first-sui-dapp',
},
].filter((question) => !args[question.name as 'template']),
);

const outDir = resolve(process.cwd(), results.dAppName);

if (existsSync(outDir)) {
throw new Error(`Directory ${outDir} already exists`);
}

const files = await collectFiles(results.template, results.dAppName);
const files = await collectFiles(results.template ?? args.template, results.dAppName);
await writeFiles(files, outDir);
}

Expand All @@ -57,7 +71,7 @@ async function collectFiles(template: string, dAppName: string) {
}>();

if (!statSync(templateDir).isDirectory()) {
throw new Error(`Template directory ${templateDir} could not be found`);
throw new Error(`Template ${templateDir} could not be found`);
}

await addDir(templateDir);
Expand Down

0 comments on commit 2470213

Please sign in to comment.