Skip to content

Commit

Permalink
feat: add ability to export libraries (#56)
Browse files Browse the repository at this point in the history
  • Loading branch information
0xShaito authored Aug 8, 2024
1 parent f4db6ae commit 3df3ab4
Show file tree
Hide file tree
Showing 10 changed files with 47 additions and 27 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ Solidity Exporter Action automates the process of extracting TypeScript interfac
| out | path to the `out` folder containing the compiled contracts | out | |
| interfaces | path to the interfaces directory | src/interfaces | |
| contracts | path to the contracts directory | src/contracts | |
| export_type | `interface` for exporting only the interfaces and their ABIs, `contracts` for exporting the contracts, interfaces and their ABIs as well | interfaces | interfaces, contracts |
| libraries | path to the libraries directory | src/libraries | |
| export_type | `interface` for exporting only the interfaces and their ABIs, `all` for exporting the contracts, libraries, interfaces and their ABIs as well | interfaces | interfaces, all |

## Action Outputs

Expand All @@ -39,7 +40,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
export_type: ['interfaces', 'contracts']
export_type: ['interfaces', 'all']

steps:
- uses: actions/checkout@v3
Expand Down Expand Up @@ -71,6 +72,7 @@ jobs:
out: 'out'
interfaces: 'solidity/interfaces'
contracts: 'solidity/contracts'
libraries: 'solidity/libraries'
export_type: '${{ matrix.export_type }}'

- name: Publish
Expand Down
10 changes: 7 additions & 3 deletions action.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: 'Solidity exporter action'
description: 'An action to generate npm packages based on Solidity interfaces and contracts'
description: 'An action to generate npm packages based on Solidity interfaces, contracts and libraries'
author: 'Wonderland'

inputs:
Expand All @@ -18,14 +18,18 @@ inputs:
description: 'path to the contracts directory'
required: false
default: 'solidity/contracts'
libraries:
description: 'path to the libraries directory'
required: false
default: 'solidity/libraries'
export_type:
description: '`interface` for exporting only the interfaces and their ABIs, `contracts` for exporting the contracts, interfaces and their ABIs as well'
description: '`interface` for exporting only the interfaces and their ABIs, `contracts` for exporting the contracts, libraries, interfaces and their ABIs as well'
required: false
default: 'interfaces'

outputs:
exported:
description: 'boolean describing package was exported successfully or not'
runs:
using: 'node18'
using: 'node20'
main: 'dist/index.js'
27 changes: 17 additions & 10 deletions dist/index.js

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

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "solidity-exporter-action",
"version": "2.0.0",
"version": "2.1.0",
"description": "An action to generate npm packages based on Solidity contracts and interfaces",
"main": "lib/main.js",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export enum ExportType {
INTERFACES = 'interfaces',
CONTRACTS = 'contracts',
ALL = 'all',
}
14 changes: 10 additions & 4 deletions src/createPackage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@ export const createPackage = (
outDir: string,
interfacesDir: string,
contractsDir: string,
librariesDir: string,
packageName: string,
exportType: ExportType,
) => {
const exportName = exportType === ExportType.INTERFACES ? '-interfaces' : '';
// Empty export destination directory
const destinationDir = `export/${packageName}-${exportType}`;
const packageFullName = `${packageName}${exportName}`;
const destinationDir = `export/${packageFullName}`;
fse.emptyDirSync(destinationDir);

console.log('Installing dependencies');
Expand All @@ -24,7 +27,7 @@ export const createPackage = (
if (!inputPackageJson) throw new Error('package.json not found');
// Create custom package.json in the export directory
const packageJson: PackageJson = {
name: packageName,
name: packageFullName,
version: inputPackageJson.version,
dependencies: {
...inputPackageJson.dependencies,
Expand All @@ -35,8 +38,11 @@ export const createPackage = (
// Copy the interfaces and their ABIs
copySolidityFiles(outDir, interfacesDir, destinationDir);

// Copy the contracts only if the export type is contracts
if (exportType === ExportType.CONTRACTS) copySolidityFiles(outDir, contractsDir, destinationDir);
// Copy the contracts and libraries only if the export type is all
if (exportType === ExportType.ALL) {
if (contractsDir != '') copySolidityFiles(outDir, contractsDir, destinationDir);
if (librariesDir != '') copySolidityFiles(outDir, librariesDir, destinationDir);
}

createReadmeAndLicense(packageJson.name, exportType, destinationDir);
console.log(`Created README and LICENSE`);
Expand Down
5 changes: 3 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,16 @@ async function run(): Promise<void> {
const packageName = core.getInput('package_name');
const outDir = core.getInput('out');
const interfacesDir = core.getInput('interfaces');
const contractsDir = core.getInput('contracts') || '';
const contractsDir = core.getInput('contracts');
const librariesDir = core.getInput('libraries');
const exportType = core.getInput('export_type') as ExportType;

if (!Object.values(ExportType).includes(exportType)) {
throw new Error(`Invalid input for export_type. Valid inputs are: ${Object.values(ExportType).join(', ')}`);
}

core.debug(`Creating package`);
createPackage(outDir, interfacesDir, contractsDir, packageName, exportType);
createPackage(outDir, interfacesDir, contractsDir, librariesDir, packageName, exportType);
core.setOutput('passed', true);
} catch (e) {
const error = e as Error;
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ export interface PackageJson {

export const publicTypeLabels: Record<ExportType, string> = {
[ExportType.INTERFACES]: 'ABIs and interfaces',
[ExportType.CONTRACTS]: 'ABIs, interfaces and contracts',
[ExportType.ALL]: 'ABIs, interfaces, contracts and libraries',
};
4 changes: 2 additions & 2 deletions test/unit/createReadmeAndLicense.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ describe('createReadmeAndLicense', () => {

it('should create web3 README.MD file with correct package name', () => {
const packageName = 'test-package';
createReadmeAndLicense(packageName, ExportType.CONTRACTS, exportDir);
createReadmeAndLicense(packageName, ExportType.ALL, exportDir);

const readmePath = `${exportDir}/README.MD`;
expect(fse.existsSync(readmePath)).to.be.true;
Expand All @@ -49,7 +49,7 @@ describe('createReadmeAndLicense', () => {

it('should create LICENSE file if it exists', () => {
const packageName = 'test-package';
createReadmeAndLicense(packageName, ExportType.CONTRACTS, exportDir);
createReadmeAndLicense(packageName, ExportType.ALL, exportDir);

const licenseFilePath = `${exportDir}/LICENSE`;
expect(fse.existsSync(licenseFilePath)).to.be.true;
Expand Down

0 comments on commit 3df3ab4

Please sign in to comment.