|
1 | 1 | import childProcess from "child_process";
|
2 | 2 | import { promisify } from "util";
|
3 | 3 | import signal from "signale"
|
4 |
| -import { ClassDeclaration, ClassDeclarationStructure, ConstructorDeclaration, OptionalKind, Project, PropertyDeclarationStructure, SourceFile } from "ts-morph"; |
| 4 | +import { Project } from "ts-morph"; |
5 | 5 | import chalk from "chalk";
|
6 |
| -import signale from "signale"; |
7 | 6 |
|
8 |
| -const {Signale} = signal; |
| 7 | +const { Signale } = signal; |
9 | 8 |
|
10 | 9 | const exec = promisify(childProcess.exec);
|
11 | 10 |
|
12 | 11 | export async function executeCommand(
|
13 | 12 | command: string,
|
14 | 13 | verbose = false
|
15 | 14 | ): Promise<string> {
|
16 |
| - const signale = new Signale({scope: "exec", interactive: !verbose}) |
| 15 | + const signale = new Signale({ scope: "exec", interactive: !verbose }) |
17 | 16 |
|
18 | 17 | if (verbose) {
|
19 | 18 | signale.info(`Running command: ${command}`);
|
@@ -45,43 +44,66 @@ const UNINITIALIZED_PARAMETERS_ERROR = "All parameters must be initialized in th
|
45 | 44 |
|
46 | 45 | /**
|
47 | 46 | * Validates the contract by checking that all parameters are initialized in the constructor. Works only for contracts written in TypeScript.
|
48 |
| - * @param contractPath Path to the contract. |
| 47 | + * |
| 48 | + * @param contractPath - Path to the contract. |
| 49 | + * @param verbose - Whether to print verbose output. |
49 | 50 | **/
|
50 |
| -export async function validateContract(contractPath: string): Promise<boolean> { |
51 |
| - const project: Project = new Project(); |
| 51 | +export async function validateContract(contractPath: string, verbose = false): Promise<boolean> { |
| 52 | + const signale = new Signale({ scope: "validate-contract" }); |
| 53 | + |
| 54 | + const project = new Project(); |
52 | 55 | project.addSourceFilesAtPaths(contractPath);
|
53 |
| - const sourceFile: SourceFile = project.getSourceFile(contractPath); |
54 |
| - const classDeclarations: ClassDeclaration[] = sourceFile.getClasses(); |
| 56 | + |
| 57 | + const sourceFile = project.getSourceFile(contractPath); |
| 58 | + const classDeclarations = sourceFile.getClasses(); |
| 59 | + |
55 | 60 | for (const classDeclaration of classDeclarations) {
|
56 |
| - const classStructure: ClassDeclarationStructure = classDeclaration.getStructure(); |
57 |
| - const { decorators, properties } = classStructure; |
58 |
| - const hasNearBindgen: boolean = decorators.find( |
59 |
| - (decorator) => decorator.name === "NearBindgen" |
60 |
| - ) ? true : false; |
| 61 | + const classStructure = classDeclaration.getStructure(); |
| 62 | + const { decorators, properties, name } = classStructure; |
| 63 | + const hasNearBindgen = decorators.some( |
| 64 | + ({ name }) => name === "NearBindgen" |
| 65 | + ); |
| 66 | + |
61 | 67 | if (hasNearBindgen) {
|
62 |
| - const constructors: ConstructorDeclaration[] = classDeclaration.getConstructors(); |
| 68 | + if (verbose) { |
| 69 | + signale.info(`Validating ${name} class...`) |
| 70 | + } |
| 71 | + |
| 72 | + const constructors = classDeclaration.getConstructors(); |
63 | 73 | const hasConstructor = constructors.length > 0;
|
64 |
| - const propertiesToBeInited: OptionalKind<PropertyDeclarationStructure>[] = properties.filter((p) => !p.initializer); |
| 74 | + const propertiesToBeInited = properties.filter(({ initializer }) => !initializer); |
| 75 | + |
65 | 76 | if (!hasConstructor && propertiesToBeInited.length === 0) {
|
66 | 77 | return true;
|
67 | 78 | }
|
| 79 | + |
68 | 80 | if (!hasConstructor && propertiesToBeInited.length > 0) {
|
69 |
| - signale.error(chalk.redBright(`${UNINITIALIZED_PARAMETERS_ERROR} ${propertiesToBeInited.map((p) => p.name)}`)); |
70 |
| - process.exit(1); |
| 81 | + signale.error(chalk.redBright(`${UNINITIALIZED_PARAMETERS_ERROR} ${propertiesToBeInited.map(({ name }) => name).join(", ")}`)); |
| 82 | + return false; |
71 | 83 | }
|
72 |
| - const constructor: ConstructorDeclaration = constructors[0]; |
73 |
| - const constructorContent: string = constructor.getText(); |
74 |
| - const nonInitedProperties: string[] = []; |
75 |
| - for (const property of propertiesToBeInited) { |
76 |
| - if (!constructorContent.includes(`this.${property.name}`)) { |
77 |
| - nonInitedProperties.push(property.name); |
78 |
| - } |
| 84 | + |
| 85 | + const [constructor] = constructors; |
| 86 | + const constructorContent = constructor.getText(); |
| 87 | + |
| 88 | + if (verbose) { |
| 89 | + signale.info("Checking for non initialized properties..."); |
79 | 90 | }
|
| 91 | + |
| 92 | + const nonInitedProperties = propertiesToBeInited.reduce((properties, { name }) => { |
| 93 | + if (constructorContent.includes(`this.${name}`)) { |
| 94 | + return properties; |
| 95 | + } |
| 96 | + |
| 97 | + return [...properties, name] |
| 98 | + }, [] as string[]); |
| 99 | + |
| 100 | + |
80 | 101 | if (nonInitedProperties.length > 0) {
|
81 | 102 | signale.error(chalk.redBright(`${UNINITIALIZED_PARAMETERS_ERROR} ${nonInitedProperties.join(", ")}`));
|
82 |
| - process.exit(1); |
| 103 | + return false; |
83 | 104 | }
|
84 | 105 | }
|
85 | 106 | }
|
| 107 | + |
86 | 108 | return true;
|
87 | 109 | }
|
0 commit comments