Skip to content

Commit

Permalink
refactor!: use sdk provided filesystem util, remove aeproject util im…
Browse files Browse the repository at this point in the history
…plementation
  • Loading branch information
thepiwo committed Nov 19, 2024
1 parent 6494ded commit 93cf8c5
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 62 deletions.
5 changes: 3 additions & 2 deletions docs/cli/test.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Helper and utilities for AEproject use, e.g. prefunded wallets, network definiti

```js
const { networks, utils, wallets } = require("@aeternity/aeproject");
const { getFileSystem } = require("@aeternity/aepp-sdk");
```

Read [AEproject Library](../lib.md) for a more detailed explanation about the usage of these imports.
Expand All @@ -45,7 +46,7 @@ aeSdk = utils.getSdk();
Get the filesystem definition for (custom) `includes` of the given contract:

```js
const filesystem = utils.getFilesystem(EXAMPLE_CONTRACT_SOURCE);
const fileSystem = await getFileSystem(EXAMPLE_CONTRACT_SOURCE);
```

Read the contract source from the filesystem:
Expand All @@ -57,7 +58,7 @@ const sourceCode = utils.getContractContent(EXAMPLE_CONTRACT_SOURCE);
Initialize the contract instance:

```js
contract = await aeSdk.initializeContract({ sourceCode, filesystem });
contract = await aeSdk.initializeContract({ sourceCode, fileSystem });
```

Deploy the contract:
Expand Down
6 changes: 0 additions & 6 deletions docs/lib.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,6 @@ utils.getContractContent(contractPath);

Read the contract source from given path, just a wrapper for `fs.readFileSync` using `utf-8` encoding.

```javascript
utils.getFilesystem(contractPath);
```

Add the required filesystem imports for contract from given path, excluding the Sophia provided library imports.

```javascript
utils.get(url);
```
Expand Down
3 changes: 2 additions & 1 deletion src/init/update-artifacts/test/exampleTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const { assert } = require("chai");
const { utils } = require("@aeternity/aeproject");
const chaiAsPromised = require("chai-as-promised");
const chai = require("chai");
const { getFileSystem } = require("@aeternity/aepp-sdk");

chai.use(chaiAsPromised);

Expand All @@ -15,7 +16,7 @@ describe("ExampleContract", () => {
aeSdk = utils.getSdk();

// a filesystem object must be passed to the compiler if the contract uses custom includes
const fileSystem = utils.getFilesystem(EXAMPLE_CONTRACT_SOURCE);
const fileSystem = await getFileSystem(EXAMPLE_CONTRACT_SOURCE);

// get content of contract
const sourceCode = utils.getContractContent(EXAMPLE_CONTRACT_SOURCE);
Expand Down
53 changes: 0 additions & 53 deletions src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,59 +10,6 @@ export function getContractContent(contractPath: string): string {
return fs.readFileSync(contractPath, "utf8");
}

export function getFilesystem(contractPath: string): { [key: string]: string } {
const defaultIncludes = [
"List.aes",
"Option.aes",
"String.aes",
"Func.aes",
"Pair.aes",
"Triple.aes",
"BLS12_381.aes",
"Frac.aes",
"Set.aes",
"Bitwise.aes",
];

const rgx = /^include\s+"([\w/.-]+)"/gim;
const rgxIncludePath = /"([\w/.-]+)"/i;
const rgxMainPath = /.*\//g;

const contractContent = getContractContent(contractPath);
const filesystem = {};

const rootIncludes = contractContent.match(rgx);
if (!rootIncludes) return filesystem;
const contractPathMatch = rgxMainPath.exec(contractPath);

// eslint-disable-next-line no-restricted-syntax
for (const rootInclude of rootIncludes) {
const includeRelativePath = rgxIncludePath.exec(rootInclude);

// eslint-disable-next-line no-continue
if (defaultIncludes.includes(includeRelativePath[1])) continue;

// eslint-disable-next-line no-console
console.log(`==> Adding include to filesystem: ${includeRelativePath[1]}`);
const includePath = path.resolve(
`${contractPathMatch[0]}/${includeRelativePath[1]}`,
);

try {
filesystem[includeRelativePath[1]] = fs.readFileSync(
includePath,
"utf-8",
);
} catch (error) {
throw Error(`File to include '${includeRelativePath[1]}' not found.`);
}

Object.assign(filesystem, getFilesystem(includePath));
}

return filesystem;
}

export function getDefaultAccounts(): MemoryAccount[] {
return wallets.map((keypair) => new MemoryAccount(keypair.secretKey));
}
Expand Down

0 comments on commit 93cf8c5

Please sign in to comment.