feat(aqua-compiler)!: JS-client aqua wrapper [fixes DXJ-461] (#347)

* Implement wrapper compiler

* Small formatting fixes

* Add simple test

* Package rename

* Add new package to release-please

* Misc fixes

* Fix object type

* Update package name

* Revert "Fix object type"

This reverts commit 572f1e10e2.

* Set string type

* Make generator func async

* Cleanup

* Code refactoring

* Fix PR comments

* Fix file path

* Refactor generate module

* Change header text

* Rename package

* Move some deps to devDeps

* Add a comment

* New index file

* Move aqua-api to devDeps

* Add desc

* Fix header

* Change return type

* Fix all tests

* Fix func

* Fix index file

* Add file entry

* Change package name (again)

* Snapshot testing

* Add func name

* Fix return type

* Fix nox images

* Update aurora image

* Conditional services

* Use function instead classes

* Refactor header

* Import same type

* Make named export

* Type generator array
This commit is contained in:
Akim
2023-09-21 15:32:27 +07:00
committed by GitHub
parent 580aff0042
commit 7fff3b1c03
22 changed files with 2600 additions and 38 deletions

View File

@ -0,0 +1,70 @@
/*
* Copyright 2023 Fluence Labs Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { ArrowWithoutCallbacks, NonArrowType, ProductType } from '@fluencelabs/interfaces';
import { readFile } from 'fs/promises';
import path from 'path';
export interface PackageJson {
name: string;
version: string;
devDependencies: {
['@fluencelabs/aqua-api']: string
}
}
export async function getPackageJsonContent(): Promise<PackageJson> {
const content = await readFile(new URL(path.join('..', 'package.json'), import.meta.url), 'utf-8');
return JSON.parse(content);
}
export function getFuncArgs(domain: ProductType<NonArrowType | ArrowWithoutCallbacks>): [string, NonArrowType | ArrowWithoutCallbacks][] {
if (domain.tag === 'labeledProduct') {
return Object.entries(domain.fields).map(([label, type]) => [label, type]);
} else if (domain.tag === 'unlabeledProduct') {
return domain.items.map((type, index) => ['arg' + index, type]);
} else {
return [];
}
}
export function recursiveRenameLaquaProps(obj: unknown): unknown {
if (typeof obj !== 'object' || obj === null) return obj;
if (Array.isArray(obj)) {
return obj.map(item => recursiveRenameLaquaProps(item));
}
return Object.getOwnPropertyNames(obj).reduce((acc, prop) => {
let accessProp = prop;
if (prop.includes('Laqua_js')) {
// Last part of the property separated by "_" is a correct name
const refinedProperty = prop.split('_').pop()!;
if (refinedProperty in obj) {
accessProp = refinedProperty;
}
}
return {
...acc,
[accessProp]: recursiveRenameLaquaProps(obj[accessProp as keyof typeof obj])
};
}, {});
}
export function capitalize(str: string) {
return str.slice(0, 1).toUpperCase() + str.slice(1);
}