Skip to content

Commit

Permalink
Merge pull request #323 from Concordium/ccd-js-gen-ts-nocheck
Browse files Browse the repository at this point in the history
ccd-js-gen: Flag for generating @ts-nocheck
  • Loading branch information
limemloh authored Jan 9, 2024
2 parents aa7734e + 9137dec commit c2a9f4c
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 11 deletions.
3 changes: 2 additions & 1 deletion packages/ccd-js-gen/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

## Unreleased

- Add output-type flag to cli.
- Add optional flag `--ts-nocheck`, enabling it will add `// @ts-nocheck` in each generated file.
- Add `--output-type <type>` flag to CLI allowing to specify whether to generate TypeScript or JavaScript directly.
- Fix the executable version of the package on Windows.

## 1.0.1
Expand Down
8 changes: 8 additions & 0 deletions packages/ccd-js-gen/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ type Options = {
outDir: string;
/** The output file types for the generated code */
outputType: lib.OutputOptions;
/** Generate `// @ts-nocheck` annotations in each file. */
tsNocheck: boolean;
};

// Main function, which is called in the executable script in `bin`.
Expand Down Expand Up @@ -50,13 +52,19 @@ export async function main(): Promise<void> {
},
'Everything'
)
.option(
'-n, --ts-nocheck',
'Generate `@ts-nocheck` annotations at the top of each typescript file.',
false
)
.parse(process.argv);
const options = program.opts<Options>();
console.log('Generating smart contract clients...');

const startTime = Date.now();
await lib.generateContractClientsFromFile(options.module, options.outDir, {
output: options.outputType,
tsNocheck: options.tsNocheck,
onProgress(update) {
if (update.type === 'Progress') {
console.log(
Expand Down
37 changes: 27 additions & 10 deletions packages/ccd-js-gen/src/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ export type OutputOptions =
export type GenerateContractClientsOptions = {
/** Options for the output. */
output?: OutputOptions;
/** Generate `// @ts-nocheck` annotations in each file. Defaults to `false`. */
tsNocheck?: boolean;
/** Callback for getting notified on progress. */
onProgress?: NotifyProgress;
};
Expand Down Expand Up @@ -97,6 +99,7 @@ export async function generateContractClients(
outName,
outDirPath,
moduleSource,
options.tsNocheck ?? false,
options.onProgress
);

Expand All @@ -118,13 +121,15 @@ export async function generateContractClients(
* @param {string} outModuleName The name for outputting the module client file.
* @param {string} outDirPath The directory to use for outputting files.
* @param {SDK.VersionedModuleSource} moduleSource The source of the smart contract module.
* @param {boolean} tsNocheck When `true` generate `// @ts-nocheck` annotations in each file.
* @param {NotifyProgress} [notifyProgress] Callback to report progress.
*/
async function generateCode(
project: tsm.Project,
outModuleName: string,
outDirPath: string,
moduleSource: SDK.VersionedModuleSource,
tsNocheck: boolean,
notifyProgress?: NotifyProgress
) {
const [moduleInterface, moduleRef, rawModuleSchema] = await Promise.all([
Expand Down Expand Up @@ -162,7 +167,8 @@ async function generateCode(
moduleRef,
moduleClientId,
moduleClientType,
internalModuleClientId
internalModuleClientId,
tsNocheck
);

for (const contract of moduleInterface.values()) {
Expand Down Expand Up @@ -205,6 +211,7 @@ async function generateCode(
contractClientId,
contractClientType,
moduleRef,
tsNocheck,
contractSchema
);

Expand Down Expand Up @@ -245,14 +252,19 @@ function generateModuleBaseCode(
moduleRef: SDK.ModuleReference.Type,
moduleClientId: string,
moduleClientType: string,
internalModuleClientId: string
internalModuleClientId: string,
tsNocheck: boolean
) {
const moduleRefId = 'moduleReference';

moduleSourceFile.addImportDeclaration({
namespaceImport: 'SDK',
moduleSpecifier: '@concordium/web-sdk',
});
moduleSourceFile.addStatements([
tsNocheck ? '// @ts-nocheck' : '',
{
kind: tsm.StructureKind.ImportDeclaration,
namespaceImport: 'SDK',
moduleSpecifier: '@concordium/web-sdk',
},
]);

moduleSourceFile.addVariableStatement({
isExported: true,
Expand Down Expand Up @@ -565,6 +577,7 @@ function generateContractBaseCode(
contractClientId: string,
contractClientType: string,
moduleRef: SDK.ModuleReference.Type,
tsNocheck: boolean,
contractSchema?: SDK.SchemaContractV3
) {
const moduleRefId = 'moduleReference';
Expand All @@ -574,10 +587,14 @@ function generateContractBaseCode(
const contractAddressId = 'contractAddress';
const blockHashId = 'blockHash';

contractSourceFile.addImportDeclaration({
namespaceImport: 'SDK',
moduleSpecifier: '@concordium/web-sdk',
});
contractSourceFile.addStatements([
tsNocheck ? '// @ts-nocheck' : '',
{
kind: tsm.StructureKind.ImportDeclaration,
namespaceImport: 'SDK',
moduleSpecifier: '@concordium/web-sdk',
},
]);

contractSourceFile.addVariableStatement({
docs: [
Expand Down

0 comments on commit c2a9f4c

Please sign in to comment.