-
Notifications
You must be signed in to change notification settings - Fork 37
/
new.ts
91 lines (80 loc) · 3.02 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
import { Command, flags } from '@oclif/command';
import dedent from 'dedent';
import { cli } from 'cli-ux';
import { TemplateScaffolding } from '@terra-money/template-scaffolding';
import { join } from 'path';
import { existsSync } from 'fs';
import TerrainCLI from '../../TerrainCLI';
import runCommand from '../../lib/runCommand';
export default class CodeNew extends Command {
static description = 'Generate new contract.';
static examples = [
'$ terrain code:new awesome_contract',
'$ terrain code:new awesome_contract --path path/to/dapp',
'$ terrain code:new awesome_contract --path path/to/dapp --authors "ExampleAuthor<example@email.domain>"',
];
static flags = {
path: flags.string({
description: 'path to keep the contracts',
default: 'contracts',
}),
commitHash: flags.string({
default: '9fa5b9b38fca4f99239ac28be48a6e1f0a4d30c8',
}),
authors: flags.string({
default: 'Terra Money <core@terra.money>',
}),
};
static args = [{ name: 'name', required: true }];
async run() {
const { args, flags } = this.parse(CodeNew);
// Command execution path.
const execPath = flags.path;
// Command to be performed.
const command = async () => {
cli.log(`Generating contract ${args.name}:`);
cli.action.start('- contract');
await TemplateScaffolding.from({
remoteUrl: `https://www.github.com/CosmWasm/cw-template/archive/${flags.commitHash}.zip`,
subFolder: `cw-template-${flags.commitHash}`,
localOptions: {
folderUrl: join(process.cwd(), flags.path, args.name),
},
replace: {
entries: {
'project-name': args.name,
crate_name: args.name,
authors: flags.authors,
' "now" | date: "%Y" ': `${new Date().getFullYear()}`,
},
},
});
cli.action.stop();
};
// Error check to be performed upon each backtrack iteration.
const errorCheck = () => {
if (existsSync(join(flags.path, args.name))) {
TerrainCLI.error(
dedent`Contract "${args.name}" is available in the "${flags.path}" directory.\n
"TIP:" Use another contract name or specify a different path.`,
'Contract Already Exists',
);
}
};
// Message to be displayed upon successful command execution.
const successMessage = () => {
TerrainCLI.success(
dedent`
Contract "${args.name}" was successfully generated.\n
Now, you can 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}"
`,
'Contract Generated',
);
};
// Attempt to execute command while backtracking through file tree.
await runCommand(execPath, command, errorCheck, successMessage);
}
}