Skip to content

Commit

Permalink
Add function to flatten single file (#527)
Browse files Browse the repository at this point in the history
* Add function to flatten single file

* Add changeset
  • Loading branch information
vanruch committed Jun 17, 2021
1 parent 414e1a9 commit 80d215b
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 9 deletions.
14 changes: 14 additions & 0 deletions .changeset/tough-crabs-drive.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
"@ethereum-waffle/compiler": minor
"@ethereum-waffle/chai": minor
"ethereum-waffle": minor
"@ethereum-waffle/e2e": minor
"@ethereum-waffle/ens": minor
"@ethereum-waffle/jest": minor
"@ethereum-waffle/mock-contract": minor
"@ethereum-waffle/provider": minor
---

- Fix vulnerabilities shown by `yarn audit`
- Fix typings in `closeTo` matcher
- Add `flattenSingleFile` function to compiler
33 changes: 26 additions & 7 deletions waffle-compiler/src/flattener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,20 @@ export async function flattenAndSave(input: InputConfig) {
await saveToFile(output, config);
}

const getFileName = (rootContract: GatheredContractInterface) => path.parse(rootContract.url).base;

const getFilePath = (fileName: string, outputDirectory: string) => path.join(outputDirectory, fileName);

export async function flattenSingleFile(input: InputConfig, name: string) {
const config = inputToConfig(input);
const output = await getContractDependency(config);
const contract = output.find((contracts) => getFileName(contracts[contracts.length - 1]) === name);
if (!contract) {
return null;
}
return getFlattenedSource(contract, '').sourceWithNormalizedLicences;
}

async function getContractDependency(config: Config): Promise<GatheredContractInterface[][]> {
const resolver = ImportsFsEngine().addResolver(
resolvers.BacktrackFsResolver(config.nodeModulesDirectory)
Expand All @@ -44,6 +58,17 @@ const fsOps = {

const unique = <T>(arr: T[]) => [...new Set(arr)];

function getFlattenedSource(contract: Array<GatheredContractInterface>, outputDirectory: string) {
const rootContract = contract[contract.length - 1];
const fileName = getFileName(rootContract);
const filePath = getFilePath(fileName, outputDirectory);

const contractsWithCommentedDirectives = contract.map(replaceDirectivesWithComments(rootContract));
const source = ''.concat(...unique(contractsWithCommentedDirectives));
const sourceWithNormalizedLicences = normalizeSpdxLicenceIdentifiers(source, fileName);
return {filePath, sourceWithNormalizedLicences};
}

function saveToFile(
output: GatheredContractInterface[][],
config: Config,
Expand All @@ -54,13 +79,7 @@ function saveToFile(
fileSystem.createDirectory(outputDirectory);

output.map((contract: Array<GatheredContractInterface>) => {
const rootContract = contract[contract.length - 1];
const fileName = path.parse(rootContract.url).base;
const filePath = path.join(outputDirectory, fileName);

const contractsWithCommentedDirectives = contract.map(replaceDirectivesWithComments(rootContract));
const source = ''.concat(...unique(contractsWithCommentedDirectives));
const sourceWithNormalizedLicences = normalizeSpdxLicenceIdentifiers(source, fileName);
const {filePath, sourceWithNormalizedLicences} = getFlattenedSource(contract, outputDirectory);

fileSystem.writeFile(filePath, sourceWithNormalizedLicences);
});
Expand Down
8 changes: 6 additions & 2 deletions waffle-compiler/test/flattener/flattener.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {expect} from 'chai';
import fs from 'fs-extra';
import {inputToConfig} from '../../src/config';
import {flattenAndSave, normalizeSpdxLicenceIdentifiers} from '../../src';
import {inputToConfig, flattenAndSave, flattenSingleFile, normalizeSpdxLicenceIdentifiers} from '../../src';

const sourceDirectory = './test/flattener/testSource';
const flattenOutputDirectory = './test/flattener/flattenFiles';
Expand Down Expand Up @@ -60,4 +59,9 @@ describe('flattening', () => {
test3
`);
});

it('single file flattening', async () => {
const expectedFile = fs.readFileSync('./test/flattener/expectedFlattenChild.sol', 'utf8');
expect(await flattenSingleFile(config, 'child.sol')).to.equal(expectedFile);
});
});

0 comments on commit 80d215b

Please sign in to comment.