Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/az/integrate-prettier-with-eslin…
Browse files Browse the repository at this point in the history
…t' into az/integrate-prettier-with-eslint
  • Loading branch information
Zewasik committed Oct 21, 2024
2 parents 9021427 + db1a580 commit 2e9d802
Show file tree
Hide file tree
Showing 13 changed files with 254 additions and 160 deletions.
28 changes: 14 additions & 14 deletions .github/workflows/ci-js.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: "CI-CD sails-js"
name: 'CI-CD sails-js'

on:
pull_request:
Expand Down Expand Up @@ -29,38 +29,38 @@ jobs:
- name: Checkout
uses: actions/checkout@v4

- name: "Install: NodeJS 20.x"
- name: 'Install: NodeJS 20.x'
uses: actions/setup-node@v4
with:
node-version: 20.x

- name: "Install: dependencies"
- name: 'Install: dependencies'
run: yarn install

- name: "Install: binaryen"
- name: 'Install: binaryen'
working-directory: /usr
run: |
sudo wget -c https://github.com/WebAssembly/binaryen/releases/download/$BINARYEN_VERSION/binaryen-$BINARYEN_VERSION-x86_64-linux.tar.gz -O - | sudo tar -xz -C .
sudo cp binaryen-$BINARYEN_VERSION/bin/wasm-opt /usr/bin/
- name: "Prepare: build all"
- name: 'Prepare: build all'
run: yarn build

- name: "Prepare: fmt"
run: yarn fmt
- name: 'Prepare: fmt'
run: yarn lint

- name: "Prepare: download Gear node"
- name: 'Prepare: download Gear node'
run: |
wget -O ./gear https://github.com/gear-tech/gear/releases/download/v1.5.0-1/gear
chmod +x gear
- name: "Prepare: run Gear node"
- name: 'Prepare: run Gear node'
run: nohup ./gear --dev --execution=wasm --tmp --unsafe-rpc-external --rpc-methods Unsafe --rpc-cors all &

- name: "Prepare: sleep 3 min"
- name: 'Prepare: sleep 3 min'
run: sleep 180

- name: "Test: run"
- name: 'Test: run'
run: yarn test

publish-to-npm:
Expand All @@ -76,16 +76,16 @@ jobs:
with:
node-version: 20.x

- name: "Prepare: install dependencies"
- name: 'Prepare: install dependencies'
run: yarn install

- name: "Install: binaryen"
- name: 'Install: binaryen'
working-directory: /usr
run: |
sudo wget -c https://github.com/WebAssembly/binaryen/releases/download/$BINARYEN_VERSION/binaryen-$BINARYEN_VERSION-x86_64-linux.tar.gz -O - | sudo tar -xz -C .
sudo cp binaryen-$BINARYEN_VERSION/bin/wasm-opt /usr/bin/
- name: "Prepare: build all"
- name: 'Prepare: build all'
run: yarn build

- name: Publish
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: CI (Rust)

on:
push:
branches: [ master ]
branches: [master]
pull_request:

env:
Expand Down
File renamed without changes.
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ precommit: fmt clippy test
precommit-js:
@yarn install
@yarn build
@yarn fmt
@yarn format
@yarn lint

fmt:
@__GEAR_WASM_BUILDER_NO_FEATURES_TRACKING=1 cargo fmt --all -- --check
Expand Down
10 changes: 5 additions & 5 deletions js/cli/src/generate/format.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
export function formatDocs(docs: string): string[] {
if (!docs) return []
const result = new Array<string>()
if (!docs) return [];
const result = new Array<string>();

const lines = docs.split("\n")
const lines = docs.split('\n');
for (const line of lines) {
result.push(` * ${line.trim()}`)
result.push(` * ${line.trim()}`);
}

return ["/**", ...result, "*/"]
return ['/**', ...result, '*/'];
}
5 changes: 4 additions & 1 deletion js/cli/src/generate/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ export class ProjectBuilder {
private isProject: boolean = true;
private isAutomaricOverride: boolean = false;

constructor(private sails: Sails, private name: string = 'Program') {}
constructor(
private sails: Sails,
private name: string = 'Program',
) {}

private async canCreateFile(filePath: string): Promise<boolean> {
if (this.isAutomaricOverride || !existsSync(filePath)) {
Expand Down
176 changes: 88 additions & 88 deletions js/cli/src/generate/service-gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export class ServiceGenerator extends BaseGenerator {
for (const { name, params, docs } of this._program.ctor.funcs) {
const args = this.getArgs(params);

const ctorDocs = formatDocs(docs)
const ctorDocs = formatDocs(docs);

this._out
.lines(ctorDocs, false)
Expand Down Expand Up @@ -167,56 +167,56 @@ export class ServiceGenerator extends BaseGenerator {
const returnType = this.getType(def, decodeMethod);

this._out
.line()
.lines(formatDocs(docs), false)
.block(this.getFuncSignature(name, params, returnType, isQuery), () => {
if (isQuery) {
this._out
.line(createPayload(service.name, name, params))
.import('@gear-js/api', 'decodeAddress')
.line(`const reply = await this._program.api.message.calculateReply({`, false)
.increaseIndent()
.line(`destination: this._program.programId,`, false)
.line(`origin: originAddress ? decodeAddress(originAddress) : ZERO_ADDRESS,`, false)
.line(`payload,`, false)
.line(`value: value || 0,`, false)
.line(`gasLimit: this._program.api.blockGasLimit.toBigInt(),`, false)
.line(`at: atBlock || null,`, false)
.reduceIndent()
.line(`})`)
.line(
"if (!reply.code.isSuccess) throw new Error(this._program.registry.createType('String', reply.payload).toString())",
)
.line(
`const result = this._program.registry.createType('(String, String, ${returnScaleType})', reply.payload)`,
)
.line(`return result[2].${decodeMethod}() as unknown as ${returnType}`);
} else {
this._out
.line(`if (!this._program.programId) throw new Error('Program ID is not set')`)
.line(`return new TransactionBuilder<${returnType}>(`, false)
.increaseIndent()
.line(`this._program.api,`, false)
.line(`this._program.registry,`, false)
.line(`'send_message',`, false)
.line(
params.length === 0
? `['${service.name}', '${name}'],`
: `['${service.name}', '${name}', ${params.map(({ name }) => name).join(', ')}],`,
false,
)
.line(
params.length === 0
? `'(String, String)',`
: `'(String, String, ${params.map(({ def }) => getScaleCodecDef(def)).join(', ')})',`,
false,
)
.line(`'${returnScaleType}',`, false)
.line(`this._program.programId`, false)
.reduceIndent()
.line(`)`);
}
});
.line()
.lines(formatDocs(docs), false)
.block(this.getFuncSignature(name, params, returnType, isQuery), () => {
if (isQuery) {
this._out
.line(createPayload(service.name, name, params))
.import('@gear-js/api', 'decodeAddress')
.line(`const reply = await this._program.api.message.calculateReply({`, false)
.increaseIndent()
.line(`destination: this._program.programId,`, false)
.line(`origin: originAddress ? decodeAddress(originAddress) : ZERO_ADDRESS,`, false)
.line(`payload,`, false)
.line(`value: value || 0,`, false)
.line(`gasLimit: this._program.api.blockGasLimit.toBigInt(),`, false)
.line(`at: atBlock || null,`, false)
.reduceIndent()
.line(`})`)
.line(
"if (!reply.code.isSuccess) throw new Error(this._program.registry.createType('String', reply.payload).toString())",
)
.line(
`const result = this._program.registry.createType('(String, String, ${returnScaleType})', reply.payload)`,
)
.line(`return result[2].${decodeMethod}() as unknown as ${returnType}`);
} else {
this._out
.line(`if (!this._program.programId) throw new Error('Program ID is not set')`)
.line(`return new TransactionBuilder<${returnType}>(`, false)
.increaseIndent()
.line(`this._program.api,`, false)
.line(`this._program.registry,`, false)
.line(`'send_message',`, false)
.line(
params.length === 0
? `['${service.name}', '${name}'],`
: `['${service.name}', '${name}', ${params.map(({ name }) => name).join(', ')}],`,
false,
)
.line(
params.length === 0
? `'(String, String)',`
: `'(String, String, ${params.map(({ def }) => getScaleCodecDef(def)).join(', ')})',`,
false,
)
.line(`'${returnScaleType}',`, false)
.line(`this._program.programId`, false)
.reduceIndent()
.line(`)`);
}
});
}
}

Expand All @@ -233,43 +233,43 @@ export class ServiceGenerator extends BaseGenerator {
const jsType = event.def ? this.getType(event.def, decodeMethod) : 'null';

this._out
.line()
.lines(formatDocs(event.docs), false)
.block(
`public subscribeTo${event.name}Event(callback: (data: ${jsType}) => void | Promise<void>): Promise<() => void>`,
() => {
this._out
.line(
`return this._program.api.gearEvents.subscribeToGearEvent('UserMessageSent', ({ data: { message } }) => {`,
)
.increaseIndent()
.block(
`if (!message.source.eq(this._program.programId) || !message.destination.eq(ZERO_ADDRESS))`,
() => {
this._out.line(`return`);
},
)
.line()
.line(`const payload = message.payload.toHex()`)
.block(
`if (getServiceNamePrefix(payload) === '${service.name}' && getFnNamePrefix(payload) === '${event.name}')`,
() => {
if (jsType === 'null') {
this._out.line(`callback(null)`);
} else {
this._out.line(
`callback(this._program.registry.createType('(String, String, ${getScaleCodecDef(
event.def,
true,
)})', message.payload)[2].${decodeMethod}() as unknown as ${jsType})`,
);
}
},
)
.reduceIndent()
.line(`})`);
},
);
.line()
.lines(formatDocs(event.docs), false)
.block(
`public subscribeTo${event.name}Event(callback: (data: ${jsType}) => void | Promise<void>): Promise<() => void>`,
() => {
this._out
.line(
`return this._program.api.gearEvents.subscribeToGearEvent('UserMessageSent', ({ data: { message } }) => {`,
)
.increaseIndent()
.block(
`if (!message.source.eq(this._program.programId) || !message.destination.eq(ZERO_ADDRESS))`,
() => {
this._out.line(`return`);
},
)
.line()
.line(`const payload = message.payload.toHex()`)
.block(
`if (getServiceNamePrefix(payload) === '${service.name}' && getFnNamePrefix(payload) === '${event.name}')`,
() => {
if (jsType === 'null') {
this._out.line(`callback(null)`);
} else {
this._out.line(
`callback(this._program.registry.createType('(String, String, ${getScaleCodecDef(
event.def,
true,
)})', message.payload)[2].${decodeMethod}() as unknown as ${jsType})`,
);
}
},
)
.reduceIndent()
.line(`})`);
},
);
}
}

Expand Down
10 changes: 4 additions & 6 deletions js/cli/src/generate/types-gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export class TypesGenerator extends BaseGenerator {
public generate() {
this._out.line('declare global {').increaseIndent();
for (const { name, def, docs } of this._program.types) {
this._out.lines(formatDocs(docs), false)
this._out.lines(formatDocs(docs), false);

if (def.isStruct) {
this.generateStruct(name, def);
Expand All @@ -39,9 +39,7 @@ export class TypesGenerator extends BaseGenerator {
return this._out
.block(`export interface ${name}`, () => {
for (const field of def.asStruct.fields) {
this._out
.lines(formatDocs(field.docs), false)
.line(`${field.name}: ${this.getType(field.def)}`);
this._out.lines(formatDocs(field.docs), false).line(`${field.name}: ${this.getType(field.def)}`);
}
})
.line();
Expand All @@ -52,8 +50,8 @@ export class TypesGenerator extends BaseGenerator {
this._out.line(`export type ${typeName} = `, false).increaseIndent();
for (const [i, variant] of def.variants.entries()) {
this._out
.lines(formatDocs(variant.docs), false)
.line(`| ${this.getEnumFieldString(variant)}`, i === def.variants.length - 1);
.lines(formatDocs(variant.docs), false)
.line(`| ${this.getEnumFieldString(variant)}`, i === def.variants.length - 1);
}
this._out.reduceIndent().line();
} else {
Expand Down
Loading

0 comments on commit 2e9d802

Please sign in to comment.