-
Notifications
You must be signed in to change notification settings - Fork 37
/
new.ts
150 lines (136 loc) · 5.05 KB
/
new.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import { Command, flags } from '@oclif/command';
import { TemplateScaffolding } from '@terra-money/template-scaffolding';
import cli from 'cli-ux';
import * as path from 'path';
import * as fs from 'fs';
import { execSync } from 'child_process';
import { pascal } from 'case';
import dedent from 'dedent';
import axios from 'axios';
import TerrainCLI from '../TerrainCLI';
import { GLOBAL_CONFIG } from '../config';
export default class New extends Command {
static description = 'Create new dapp from template.';
static examples = [
'$ terrain new awesome-dapp',
'$ terrain new awesome-dapp --path path/to/dapp',
'$ terrain new awesome-dapp --path path/to/dapp --authors "ExampleAuthor<example@email.domain>"',
'$ terrain new awesome-dapp --path path/to/dapp --framework vue --authors "ExampleAuthor<example@email.domain>"',
];
static flags = {
path: flags.string({
description: 'Path to create the workspace',
default: '.',
}),
framework: flags.string({
description:
'Choose the frontend framework you want to use. Non-react framework options have better wallet-provider support but less streamlined contract integration.',
options: ['react', 'vue', 'svelte', 'next', 'vite', 'lit'],
default: 'react',
}),
version: flags.string({
default: '1.0',
}),
authors: flags.string({
default: 'Terra Money <core@terra.money>',
}),
};
static args = [{ name: 'name', required: true }];
async run() {
const { args, flags } = this.parse(New);
const appDir = path.join(process.cwd(), flags.path, args.name);
const contractDir = path.join(appDir, 'contracts', args.name);
const frontendDir = path.join(appDir, 'frontend');
if (fs.existsSync(appDir)) {
throw Error(
`Folder '${args.name}' already exists under path '${flags.path}'.\nTip: Use another path or project name`,
);
}
const templateEntries = {
'project-name': args.name,
'client-name': `${pascal(args.name)}Client`,
// Crates cannot have dashes, and Rust will map underscores to dashes for us.
crate_name: args.name.replaceAll('-', '_'),
authors: flags.authors,
' "now" | date: "%Y" ': `${new Date().getFullYear()}`,
};
cli.log(`🚀 Generating app ${args.name}:`);
cli.action.start(' 🛠 Workspace');
await TemplateScaffolding.from({
remoteUrl:
'https://codeload.github.com/terra-money/terrain-core-template/zip/refs/heads/main',
subFolder: 'terrain-core-template-main',
localOptions: {
folderUrl: appDir,
},
replace: {
entries: templateEntries,
},
});
const res = await axios.get('https://station-assets.terra.money/chains.json');
fs.writeFileSync(path.join(appDir, 'config.terrain.json'), JSON.stringify({ ...GLOBAL_CONFIG, ...res.data }, null, 2));
cli.action.stop();
cli.action.start(' 📝 Contract');
await TemplateScaffolding.from({
remoteUrl: `https://codeload.github.com/InterWasm/cw-template/zip/refs/heads/${flags.version}`,
subFolder: `cw-template-${flags.version}`,
localOptions: {
folderUrl: contractDir,
},
replace: {
entries: templateEntries,
},
});
cli.action.stop();
cli.action.start(' 💻 Frontend');
if (flags.framework === 'react') {
await TemplateScaffolding.from({
remoteUrl:
'https://codeload.github.com/terra-money/terrain-frontend-template/zip/refs/heads/main',
subFolder: 'terrain-frontend-template-main',
localOptions: {
folderUrl: frontendDir,
},
replace: {
entries: templateEntries,
},
});
} else {
await TemplateScaffolding.from({
remoteUrl:
'https://codeload.github.com/terra-money/wallet-provider/zip/refs/heads/main',
subFolder: `wallet-provider-main/templates/${flags.framework}`,
localOptions: {
folderUrl: frontendDir,
},
});
}
cli.action.stop();
// Install app dependencies.
process.chdir(appDir);
cli.action.start(' 🏗 Installing app dependencies');
await execSync('npm i --loglevel error', {
stdio: ['ignore', 'ignore', 'inherit'],
});
cli.action.stop();
// Install frontend dependencies.
process.chdir(frontendDir);
cli.action.start(' 🔧 Installing frontend dependencies');
await execSync('npm i --loglevel error', {
stdio: ['ignore', 'ignore', 'inherit'],
});
cli.action.stop();
TerrainCLI.success(
dedent`
Application "${args.name}" was successfully generated.\n
Now, you can change into the contract directory:\n
"cd ${args.name}"\n
And try to deploy it to your preferred Terra network:\n
"terrain deploy ${args.name} --signer <signer-wallet> --network" "<desired-network>"\n
"NOTE:" To deploy your contract to the "LocalTerra" network utilizing the preconfigured test wallet "test1" as the signer, use the following command:\n
"terrain deploy ${args.name}"
`,
'Application Generated',
);
}
}