Skip to content

Commit

Permalink
Update scripts to check new contract file structure.
Browse files Browse the repository at this point in the history
  • Loading branch information
nghiant2710 committed May 21, 2018
1 parent 2c7d93e commit ef17200
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 30 deletions.
11 changes: 3 additions & 8 deletions scripts/check-contracts.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,12 @@

'use strict'

const path = require('path')
const utils = require('./utils')

const CONTRACTS_PATH = path.join(__dirname, '..', 'contracts')
let success = true

for (const contract of utils.readContracts()) {
if (contract.source.slug !== contract.name) {
success = false
console.error(contract.path)
console.error(` The contract slug is ${contract.source.slug}, but the file name is ${contract.name}`)
}

for (const contract of utils.readContracts(CONTRACTS_PATH)) {
if (contract.source.type !== contract.type) {
success = false
console.error(contract.path)
Expand Down
41 changes: 19 additions & 22 deletions scripts/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,29 +18,26 @@

const fs = require('fs')
const path = require('path')
const CONTRACTS_PATH = path.join(__dirname, '..', 'contracts')
const TYPES = fs.readdirSync(CONTRACTS_PATH)

exports.readContracts = () => {
return TYPES.reduce((accumulator, type) => {
const typeDirectory = path.join(CONTRACTS_PATH, type)
return accumulator.concat(fs.readdirSync(typeDirectory).map((fileName) => {
const contractPath = path.join(CONTRACTS_PATH, type, fileName)
const getAllFiles = dir =>
fs.readdirSync(dir).reduce((files, file) => {
const name = path.join(dir, file)
const isDirectory = fs.statSync(name).isDirectory()
return isDirectory ? [...files, ...getAllFiles(name)] : [...files, name]
}, [])

let sourceObject = {}
try {
sourceObject = require(contractPath)
} catch (error) {
console.error(`Can't parse ${contractPath}`)
process.exit(1)
}
exports.readContracts = (dir) => {
const allFiles = getAllFiles(dir)
let contracts = []

return {
type,
source: sourceObject,
path: contractPath,
name: path.basename(fileName, '.json')
}
}))
}, [])
allFiles.forEach((file) => {
if (path.extname(file) === '.json') {
contracts.push({
type: path.basename(path.dirname(path.dirname(file))),
source: require(file),
path: file
})
}
})
return contracts
}

0 comments on commit ef17200

Please sign in to comment.