Skip to content

Commit

Permalink
chore: Reformat code (#274)
Browse files Browse the repository at this point in the history
Reformat code in cli and cli utils
  • Loading branch information
petarvujovic98 authored Oct 27, 2022
1 parent 04a0b6d commit 286efc2
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 49 deletions.
4 changes: 3 additions & 1 deletion lib/cli/cli.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions lib/cli/utils.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 23 additions & 15 deletions lib/cli/utils.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 6 additions & 5 deletions src/cli/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@ export async function buildCom(
await executeCommand(`mkdir -p ${TARGET_DIR}`, verbose);

signal.await(`Validatig ${source} contract...`);
await validateContract(source);
if (!await validateContract(source, verbose)) {
process.exit(1);
}

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

if (verbose) {
new Signale({scope: "method-header"}).info(
rollupTarget
new Signale({ scope: "method-header" }).info(
rollupTarget
)
}

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

await executeCommand(
`${CC} --target=wasm32-wasi -nostartfiles -Oz -flto ${DEFS} ${INCLUDES} ${SOURCES} ${LIBS} -Wl,--no-entry -Wl,--allow-undefined -Wl,-z,stack-size=${
256 * 1024
`${CC} --target=wasm32-wasi -nostartfiles -Oz -flto ${DEFS} ${INCLUDES} ${SOURCES} ${LIBS} -Wl,--no-entry -Wl,--allow-undefined -Wl,-z,stack-size=${256 * 1024
} -Wl,--lto-O3 -o ${contractTarget}`,
verbose
);
Expand Down
74 changes: 48 additions & 26 deletions src/cli/utils.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
import childProcess from "child_process";
import { promisify } from "util";
import signal from "signale"
import { ClassDeclaration, ClassDeclarationStructure, ConstructorDeclaration, OptionalKind, Project, PropertyDeclarationStructure, SourceFile } from "ts-morph";
import { Project } from "ts-morph";
import chalk from "chalk";
import signale from "signale";

const {Signale} = signal;
const { Signale } = signal;

const exec = promisify(childProcess.exec);

export async function executeCommand(
command: string,
verbose = false
): Promise<string> {
const signale = new Signale({scope: "exec", interactive: !verbose})
const signale = new Signale({ scope: "exec", interactive: !verbose })

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

/**
* Validates the contract by checking that all parameters are initialized in the constructor. Works only for contracts written in TypeScript.
* @param contractPath Path to the contract.
*
* @param contractPath - Path to the contract.
* @param verbose - Whether to print verbose output.
**/
export async function validateContract(contractPath: string): Promise<boolean> {
const project: Project = new Project();
export async function validateContract(contractPath: string, verbose = false): Promise<boolean> {
const signale = new Signale({ scope: "validate-contract" });

const project = new Project();
project.addSourceFilesAtPaths(contractPath);
const sourceFile: SourceFile = project.getSourceFile(contractPath);
const classDeclarations: ClassDeclaration[] = sourceFile.getClasses();

const sourceFile = project.getSourceFile(contractPath);
const classDeclarations = sourceFile.getClasses();

for (const classDeclaration of classDeclarations) {
const classStructure: ClassDeclarationStructure = classDeclaration.getStructure();
const { decorators, properties } = classStructure;
const hasNearBindgen: boolean = decorators.find(
(decorator) => decorator.name === "NearBindgen"
) ? true : false;
const classStructure = classDeclaration.getStructure();
const { decorators, properties, name } = classStructure;
const hasNearBindgen = decorators.some(
({ name }) => name === "NearBindgen"
);

if (hasNearBindgen) {
const constructors: ConstructorDeclaration[] = classDeclaration.getConstructors();
if (verbose) {
signale.info(`Validating ${name} class...`)
}

const constructors = classDeclaration.getConstructors();
const hasConstructor = constructors.length > 0;
const propertiesToBeInited: OptionalKind<PropertyDeclarationStructure>[] = properties.filter((p) => !p.initializer);
const propertiesToBeInited = properties.filter(({ initializer }) => !initializer);

if (!hasConstructor && propertiesToBeInited.length === 0) {
return true;
}

if (!hasConstructor && propertiesToBeInited.length > 0) {
signale.error(chalk.redBright(`${UNINITIALIZED_PARAMETERS_ERROR} ${propertiesToBeInited.map((p) => p.name)}`));
process.exit(1);
signale.error(chalk.redBright(`${UNINITIALIZED_PARAMETERS_ERROR} ${propertiesToBeInited.map(({ name }) => name).join(", ")}`));
return false;
}
const constructor: ConstructorDeclaration = constructors[0];
const constructorContent: string = constructor.getText();
const nonInitedProperties: string[] = [];
for (const property of propertiesToBeInited) {
if (!constructorContent.includes(`this.${property.name}`)) {
nonInitedProperties.push(property.name);
}

const [constructor] = constructors;
const constructorContent = constructor.getText();

if (verbose) {
signale.info("Checking for non initialized properties...");
}

const nonInitedProperties = propertiesToBeInited.reduce((properties, { name }) => {
if (constructorContent.includes(`this.${name}`)) {
return properties;
}

return [...properties, name]
}, [] as string[]);


if (nonInitedProperties.length > 0) {
signale.error(chalk.redBright(`${UNINITIALIZED_PARAMETERS_ERROR} ${nonInitedProperties.join(", ")}`));
process.exit(1);
return false;
}
}
}

return true;
}

0 comments on commit 286efc2

Please sign in to comment.