-
Notifications
You must be signed in to change notification settings - Fork 0
/
refresh.ts
95 lines (69 loc) · 2.86 KB
/
refresh.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
import { CliUx, Command } from '@oclif/core'
import { Templates } from "../create/template";
import { ITemplateData, TemplateProcessor } from "../create/templateProcessor";
import * as fs from "fs-extra";
import { join } from "path";
import ABIToScript from '../create/abiToScript';
export interface ContractLocator {
chainId: number;
address: string;
name: string;
}
export default class Refresh extends Command {
static description = 'Refresh a HardHat project';
private dir: string = ".";
static SRC_XML_FILE = "tokenscript.xml";
static hardHatTemplate: string = "hardhat";
async catch(error: Error) {
// do something or
// re-throw to be handled globally
throw error;
}
async run(): Promise<void> {
this.dir = process.cwd();
if (fs.existsSync(this.dir)) {
let numFiles = fs.readdirSync(this.dir).length;
if (numFiles && fs.existsSync(join(this.dir, "tstemplate.json"))) {
await this.handleExistingProject(Refresh.hardHatTemplate);
return;
} else {
CliUx.ux.error("Cannot refresh HardHat project; use tokenscript create -h <HardHat Project Dir>");
}
}
}
getContract(values: any[], templateDef: ITemplateData): ContractLocator {
const addressIndex = templateDef.templateFields.findIndex((element) => element.token === 'CONTRACT_ADDRESS');
const chainIndex = templateDef.templateFields.findIndex((element) => element.token === 'CONTRACT_CHAIN');
const nameIndex = templateDef.templateFields.findIndex((element) => element.token === 'TOKENSCRIPT_NAME');
const addressVal = values[addressIndex];
const chainIdVal = values[chainIndex];
const nameVal = values[nameIndex];
const tokenAddress: ContractLocator = { chainId: chainIdVal, address: addressVal, name: nameVal };
return tokenAddress;
}
private async handleExistingProject(hardHat: string): Promise<boolean> {
let templateDef = fs.readJsonSync(this.dir + "/tstemplate.json");
if (!templateDef)
throw new Error("Template file is invalid or not readable");
if (hardHat) {
//TODO: Keep tokenscript file and work out how to make the diff
// For first draft, rebuild tokenscript xml base
if (fs.existsSync(join(this.dir, "tokenscript.xml"))) {
fs.rmSync(join(this.dir, "tokenscript.xml"),);
}
//add rebuild files in case we need them
Templates.copyHHInit(this.dir);
let templateProcessor = new TemplateProcessor(templateDef, this.dir);
let values = templateProcessor.getValuesFromTSTemplate();
let tokenAddress = this.getContract(values, templateDef);
//pull HardHat directory from tstemplate
hardHat = templateProcessor.getHardHatDirectory();
await templateProcessor.processTemplateUpdate(values);
let ABIEncoder = new ABIToScript(this.dir, tokenAddress);
await ABIEncoder.initFromHardHat(hardHat, templateDef);
Templates.cleanBuildFiles(this.dir);
return true;
}
return false;
}
}