Skip to content

Commit

Permalink
Update readme and solve relative path issue.
Browse files Browse the repository at this point in the history
  • Loading branch information
prathamesh0 committed Sep 17, 2021
1 parent 7337077 commit 75aa647
Show file tree
Hide file tree
Showing 8 changed files with 736 additions and 731 deletions.
86 changes: 63 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,55 +8,95 @@

### Installation from npm

`npm i @poanet/solidity-flattener`
```bash
npm install https://github.com/vulcanize/solidity-flattener.git
```

### Usage
#### Usage

`./node_modules/.bin/poa-solidity-flattener ./contracts/example.sol`
* CLI:

It will save flattened source of Solidity smart-contract into `./out` directory
```bash
npm run solidity-flattener [input-file-path] [output-file-path]
```

* `input-file-path`: Input contract file path.
* `output-file-path`: Flattened contract output file path (default: appends `-flat` to input filename).

### Installation from source
Arguments are taken from `config.json` if not provided.

Example:

```bash
npm run solidity-flattener ./contracts/ERC20/ERC20.sol ./out/ERC20-flat.sol
```

* As a package:

Example:

```
const { flatten } = require('@poanet/solidity-flattener')
const inputFile = './contracts/ERC20/ERC20.sol'
const flatContract = flatten(inputFile)
console.log(flatContract)
```

### Installation from source

```
git clone https://github.com/poanetwork/solidity-flattener
git clone https://github.com/vulcanize/solidity-flattener
cd solidity-flattener
npm install
```

You can start script either
#### Usage

You can start the script with:

```
npm start "path_to_not_flat_contract_definition_file.sol"
npm start [input-file-path] [output-file-path]
```

or without paramaters (path to input file will be extracted from `./config.json`)
* `input-file-path`: Input contract file path.
* `output-file-path`: Flattened contract output file path (default: appends `-flat` to input filename).

Arguments are taken from `config.json` if not provided.

Examples:

```
npm start
# Saves the flattened contract at the provided output-file-path.
npm start ./contracts/ERC20/ERC20.sol ./out/ERC20-flat.sol
```

```
# Saves the flattened contract at the provided output-file-path in config.json.
npm start
```


Expected result:
If there is no `output-file-path` in `config.json`:

```
Success! Flat file ORIGINAL_FILE_NAME_flat.sol is generated to ./out directory
# Saves the flattened contract at `./Oracles-flat.sol`
npm start ./demo/src/Oracles.sol
```

`./flatContract.sol` - flat .sol file is created in output directory (`./out/` by default)

**Note:** *utility doesn't support aliases at import statements*
**Note:** *Utility doesn't support aliases at import statements.*

## Config

path `./config.json`
* Path: `./config.json`
* Fields:
* `input-file-path`: Input contract file path.
* `output-file-path`: Flattened contract output file path.

```
{
"inputFilePath": "./demo/src/Oracles.sol",
"outputDir": "./out"
}
```
* Example:

```
{
"inputFilePath": "./demo/src/Oracles.sol",
"outputFilePath": "./demo/out/Oracles-flat.sol"
}
```
Empty file modified bin/solidity-flattener
100644 → 100755
Empty file.
3 changes: 2 additions & 1 deletion helpers/find-file.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const fs = require('fs')
const glob = require('glob-promise')
const path = require('path')
const { processVariables } = require('./variables')
const constants = require('./constants')
const changeRelativePathToAbsolute = require('./change-relative-path-to-absolute')

Expand Down Expand Up @@ -59,7 +60,7 @@ async function byNameAndReplaceInnerRecursivelyInner(importStatement, updatedFil

let isAbsolutePath = !dependencyPath.startsWith(constants.DOT)
const filePath = srcFiles[j]
const importedSrcFiles = {}
const { importedSrcFiles } = processVariables(process.argv.slice(2))
if (isAbsolutePath && filePath.includes(dependencyPath)) {
let flattenFileContent = constants.EMPTY
if (!importedSrcFiles.hasOwnProperty(path.basename(filePath)) || fs.existsSync(dependencyPath)) {
Expand Down
9 changes: 7 additions & 2 deletions helpers/logger.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
let bunyan = require('bunyan')
let log = bunyan.createLogger({name: 'solidity-flattener'})

module.exports = log
const level = process.env.DEBUG_LEVEL || 'error'
let log = bunyan.createLogger({
name: 'solidity-flattener',
level
})

module.exports = log
3 changes: 2 additions & 1 deletion helpers/replace-all-imports-in-current-layer.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const fs = require('fs')
const path = require('path')
const { processVariables } = require('./variables')
const constants = require('./constants')
const findFile = require('./find-file')
const updateImportObjectLocationInTarget = require('./update-import-object-location-in-target')
Expand All @@ -19,7 +20,7 @@ async function replaceAllImportsInCurrentLayerInner(i, importObjs, updatedFileCo
}

let importObj = importObjs[i]
const importedSrcFiles = {}
const { importedSrcFiles } = processVariables(process.argv.slice(2))
let _updatedFileContent

//replace contracts aliases
Expand Down
11 changes: 7 additions & 4 deletions helpers/variables.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,23 @@ function processVariables(args) {
config = JSON.parse(fs.readFileSync(configPath, constants.UTF8))
}

// Get file paths.
// Get file paths. Resolution order: Command line args, config, defaults.
const inputFilePath = args.length > 0 ? args[0] : config ? config.inputFilePath : constants.EMPTY
const outputFilePath = args.length > 1 ? args[1] : config ? config.outputFilePath : constants.EMPTY

// Output directory to store flat combined solidity file
let outputDir = path.dirname(outputFilePath)
// Output directory to store flat combined solidity file.
const outputDir = path.dirname(outputFilePath)

// Extracting filename for output file if outputFilePath not given.
let flatContractPrefix = path.basename(inputFilePath, '.sol')
const flatContractPrefix = path.basename(inputFilePath, '.sol')

const importedSrcFiles = {}

const variables = {
inputFilePath,
outputFilePath,
outputDir,
importedSrcFiles,
flatContractPrefix
}

Expand Down
9 changes: 3 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ const fs = require('fs')
const { processVariables } = require('./helpers/variables')
const log = require('./helpers/logger')
const constants = require('./helpers/constants')
const cleanPath = require('./helpers/clean-path')
const replaceAllImportsRecursively = require('./helpers/replace-all-imports-recursively')
const {
deduplicateSolidityVersoins,
Expand All @@ -21,6 +20,7 @@ async function main(args) {
// Flatten the contract.
const outputFileContent = await flatten(variables.inputFilePath)

// Store the output file at the required destination.
if (!fs.existsSync(variables.outputDir)) fs.mkdirSync(variables.outputDir, { recursive: true })
const fileName = variables.outputFilePath ? path.basename(variables.outputFilePath) : `${variables.flatContractPrefix}-flat.sol`
const filePath = `${variables.outputDir}/${fileName}`
Expand All @@ -29,14 +29,11 @@ async function main(args) {
}

async function flatten(inputFilePath) {
inputFilePath = path.resolve(inputFilePath)

const inputFileContent = fs.readFileSync(inputFilePath, 'utf8')

let dir = path.dirname(inputFilePath) + constants.SLASH
const isAbsolutePath = !dir.startsWith(constants.DOT)
if (!isAbsolutePath) {
dir = __dirname + constants.SLASH + dir
}
dir = cleanPath(dir)

return await replaceImports(inputFileContent, dir)
}
Expand Down
Loading

0 comments on commit 75aa647

Please sign in to comment.