Skip to content

Commit 286efc2

Browse files
chore: Reformat code (#274)
Reformat code in cli and cli utils
1 parent 04a0b6d commit 286efc2

File tree

5 files changed

+84
-49
lines changed

5 files changed

+84
-49
lines changed

Diff for: lib/cli/cli.js

+3-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: lib/cli/utils.d.ts

+4-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: lib/cli/utils.js

+23-15
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: src/cli/cli.ts

+6-5
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,9 @@ export async function buildCom(
6767
await executeCommand(`mkdir -p ${TARGET_DIR}`, verbose);
6868

6969
signal.await(`Validatig ${source} contract...`);
70-
await validateContract(source);
70+
if (!await validateContract(source, verbose)) {
71+
process.exit(1);
72+
}
7173

7274
signale.await(`Creating ${source} file with Rollup...`);
7375
await createJsFileWithRullup(source, ROLLUP_TARGET, verbose);
@@ -146,8 +148,8 @@ async function createMethodsHeaderFile(rollupTarget: string, verbose = false) {
146148
const buildPath = path.dirname(rollupTarget);
147149

148150
if (verbose) {
149-
new Signale({scope: "method-header"}).info(
150-
rollupTarget
151+
new Signale({ scope: "method-header" }).info(
152+
rollupTarget
151153
)
152154
}
153155

@@ -189,8 +191,7 @@ async function createWasmContract(
189191
await executeCommand(`mv ${qjscTarget} build/code.h`, verbose);
190192

191193
await executeCommand(
192-
`${CC} --target=wasm32-wasi -nostartfiles -Oz -flto ${DEFS} ${INCLUDES} ${SOURCES} ${LIBS} -Wl,--no-entry -Wl,--allow-undefined -Wl,-z,stack-size=${
193-
256 * 1024
194+
`${CC} --target=wasm32-wasi -nostartfiles -Oz -flto ${DEFS} ${INCLUDES} ${SOURCES} ${LIBS} -Wl,--no-entry -Wl,--allow-undefined -Wl,-z,stack-size=${256 * 1024
194195
} -Wl,--lto-O3 -o ${contractTarget}`,
195196
verbose
196197
);

Diff for: src/cli/utils.ts

+48-26
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
11
import childProcess from "child_process";
22
import { promisify } from "util";
33
import signal from "signale"
4-
import { ClassDeclaration, ClassDeclarationStructure, ConstructorDeclaration, OptionalKind, Project, PropertyDeclarationStructure, SourceFile } from "ts-morph";
4+
import { Project } from "ts-morph";
55
import chalk from "chalk";
6-
import signale from "signale";
76

8-
const {Signale} = signal;
7+
const { Signale } = signal;
98

109
const exec = promisify(childProcess.exec);
1110

1211
export async function executeCommand(
1312
command: string,
1413
verbose = false
1514
): Promise<string> {
16-
const signale = new Signale({scope: "exec", interactive: !verbose})
15+
const signale = new Signale({ scope: "exec", interactive: !verbose })
1716

1817
if (verbose) {
1918
signale.info(`Running command: ${command}`);
@@ -45,43 +44,66 @@ const UNINITIALIZED_PARAMETERS_ERROR = "All parameters must be initialized in th
4544

4645
/**
4746
* 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.
4950
**/
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();
5255
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+
5560
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+
6167
if (hasNearBindgen) {
62-
const constructors: ConstructorDeclaration[] = classDeclaration.getConstructors();
68+
if (verbose) {
69+
signale.info(`Validating ${name} class...`)
70+
}
71+
72+
const constructors = classDeclaration.getConstructors();
6373
const hasConstructor = constructors.length > 0;
64-
const propertiesToBeInited: OptionalKind<PropertyDeclarationStructure>[] = properties.filter((p) => !p.initializer);
74+
const propertiesToBeInited = properties.filter(({ initializer }) => !initializer);
75+
6576
if (!hasConstructor && propertiesToBeInited.length === 0) {
6677
return true;
6778
}
79+
6880
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;
7183
}
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...");
7990
}
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+
80101
if (nonInitedProperties.length > 0) {
81102
signale.error(chalk.redBright(`${UNINITIALIZED_PARAMETERS_ERROR} ${nonInitedProperties.join(", ")}`));
82-
process.exit(1);
103+
return false;
83104
}
84105
}
85106
}
107+
86108
return true;
87109
}

0 commit comments

Comments
 (0)