-
Notifications
You must be signed in to change notification settings - Fork 30
/
new.ts
122 lines (105 loc) · 3.8 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
import { Args, Flags } from "@oclif/core";
import path from "node:path";
import { ensureDir, pathExists, pathExistsSync } from "fs-extra/esm";
import {
checkCliDependencies,
copyContractTemplateFiles,
processTemplates,
getTemplates,
prepareTestFiles,
getSwankyConfig,
configName,
} from "../../lib/index.js";
import { email, name, pickTemplate } from "../../lib/prompts.js";
import { kebabCase, pascalCase, snakeCase } from "change-case";
import { execaCommandSync } from "execa";
import inquirer from "inquirer";
import { SwankyCommand } from "../../lib/swankyCommand.js";
import { InputError } from "../../lib/errors.js";
import { ConfigBuilder } from "../../lib/config-builder.js";
export class NewContract extends SwankyCommand<typeof NewContract> {
static description = "Generate a new smart contract template inside a project";
static flags = {
template: Flags.string({
options: getTemplates().contractTemplatesList,
}),
verbose: Flags.boolean({ char: "v" }),
};
static args = {
contractName: Args.string({
name: "contractName",
required: true,
description: "Name of the new contract",
}),
};
async run(): Promise<void> {
const projectPath = process.cwd();
const { args, flags } = await this.parse(NewContract);
if (await pathExists(path.resolve(projectPath, "contracts", args.contractName))) {
throw new InputError(`Contract folder '${args.contractName}' already exists`);
}
if (this.swankyConfig.contracts[args.contractName]) {
throw new InputError(
`Contract with a name '${args.contractName}' already exists in ${configName()}`
);
}
const templates = getTemplates();
const { contractTemplate } = flags.template
? { contractTemplate: flags.template }
: await inquirer.prompt([pickTemplate(templates.contractTemplatesList)]);
const questions = [
name(
"author",
() => execaCommandSync("git config --get user.name").stdout,
"What is your name?"
),
email(),
];
const answers = await inquirer.prompt(questions);
await this.spinner.runCommand(
() => checkCliDependencies(this.spinner),
"Checking dependencies"
);
await this.spinner.runCommand(
() =>
copyContractTemplateFiles(
path.resolve(templates.contractTemplatesPath, contractTemplate),
args.contractName,
projectPath
),
"Copying contract template files"
);
if (contractTemplate === "psp22") {
const e2eTestHelpersPath = path.resolve(projectPath, "tests", "test_helpers");
if (!pathExistsSync(e2eTestHelpersPath)) {
await this.spinner.runCommand(
() => prepareTestFiles("e2e", path.resolve(templates.templatesPath), projectPath),
"Copying e2e test helpers"
);
} else {
console.log("e2e test helpers already exist. No files were copied.");
}
}
await this.spinner.runCommand(
() =>
processTemplates(projectPath, {
project_name: kebabCase(this.config.pjson.name),
author_name: answers.authorName,
author_email: answers.email,
swanky_version: this.config.pjson.version,
contract_name: args.contractName,
contract_name_snake: snakeCase(args.contractName),
contract_name_pascal: pascalCase(args.contractName),
}),
"Processing contract templates"
);
await ensureDir(path.resolve(projectPath, "artifacts", args.contractName));
await this.spinner.runCommand(async () => {
const newLocalConfig = new ConfigBuilder(getSwankyConfig("local"))
.addContract(args.contractName)
.build();
await this.storeConfig(newLocalConfig, "local");
}, "Writing config");
this.log("😎 New contract successfully generated! 😎");
}
}