Skip to content

Commit

Permalink
fix(pactjs-cli): use import in index.d.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
javadkh2 committed Oct 13, 2023
1 parent 977ed69 commit 99ce19b
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 22 deletions.
7 changes: 7 additions & 0 deletions .changeset/empty-bears-develop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@kadena/pactjs-generator': patch
'@kadena/pactjs-cli': patch
---

Fix index.d.ts by using import instead of export since we use interface merging
for types
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ interface ICapability_Coin_GAS {
}
${capsInterfaces ? `${EOL}${capsInterfaces}${EOL}` : ''}
declare module '@kadena/client' {
export interface IPactModules {
interface IPactModules {
${module.doc ? `/**${EOL}${indent(module.doc, 2)}${indent(EOL, 2)}*/` : ''}
"${getModuleFullName(module)}": {
${indent(functions.map(getFunctionType).join(`,${EOL}${EOL}`), 2)}
Expand Down
46 changes: 28 additions & 18 deletions packages/tools/pactjs-cli/src/contract-generate/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,38 +154,48 @@ export const generate: IGenerate = (program, version) => async (args) => {
mkdirp.sync(targetDirectory);
}

const indexPath: string = join(targetDirectory, 'index.d.ts');

moduleDtss.forEach((dts, moduleName) => {
const targetFilePath: string = join(
dirname(targetPackageJson),
'node_modules',
TARGET_PACKAGE,
`${moduleName}.d.ts`,
);

// write dts to index.d.ts to file
const indexPath: string = join(targetDirectory, 'index.d.ts');
const exportStatement: string = `export * from './${moduleName}';`;

// always overwrite existing file
console.log(`Writing to new file ${targetFilePath}`);
writeFileSync(targetFilePath, dts);
});

const doNotEdit =
'/** THIS FILE IS GENERATED BY pactjs-cli. DO NOT EDIT IT */';

// if indexPath exists, append export to existing file
if (existsSync(indexPath)) {
console.log(`Appending to existing file ${indexPath}`);
const indexDts: string = readFileSync(indexPath, 'utf8');
// Append the export to the file if it's not already there.
if (!indexDts.includes(exportStatement)) {
const separator = indexDts.endsWith('\n') ? '' : '\n';
const newIndexDts = [indexDts, exportStatement].join(separator);
writeFileSync(indexPath, newIndexDts);
}
} else {
console.log(`Writing to new file ${indexPath}`);
writeFileSync(indexPath, exportStatement);
// if indexPath exists, append export to existing file otherwise create new file
let indexDts: string = existsSync(indexPath)
? readFileSync(indexPath, 'utf8').replace(doNotEdit, '')
: '';

// add doNotEdit comment to index.d.ts
indexDts = `${doNotEdit}\n${indexDts}`;

moduleDtss.forEach((_, moduleName) => {
const importStatement: string = `import './${moduleName}';`;
// We have used "export * ..." previously, which wasn't necessary and caused some bugs.
const exportStatement: string = `export * from './${moduleName}';`;

// Remove the export statement if it's there;
indexDts = indexDts.replace(exportStatement, '');

// Append the import to the file if it's not already there.
if (!indexDts.includes(importStatement)) {
indexDts = `${indexDts}\n${importStatement}`;
}
});

// remove redundant line breaks and write index.d.ts
writeFileSync(indexPath, `${indexDts.replace(/\n+/g, '\n').trim()}\n`);

// write npm init to package.json
const defaultPackageJsonPath: string = join(targetDirectory, 'package.json');

Expand Down
3 changes: 0 additions & 3 deletions packages/tools/pactjs-cli/src/contract-generate/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,6 @@ const Options = z
if (file === undefined && contract === undefined) {
return false;
}
if (file !== undefined && contract !== undefined) {
return false;
}
return true;
}, 'Error: either file or contract must be specified')
.refine(({ contract, api: host }) => {
Expand Down

0 comments on commit 99ce19b

Please sign in to comment.