From ef1720077a9e7297a87d17d615398f06989d530f Mon Sep 17 00:00:00 2001 From: Trong Nghia Nguyen Date: Mon, 14 May 2018 16:31:43 +0700 Subject: [PATCH] Update scripts to check new contract file structure. --- scripts/check-contracts.js | 11 +++------- scripts/utils.js | 41 ++++++++++++++++++-------------------- 2 files changed, 22 insertions(+), 30 deletions(-) diff --git a/scripts/check-contracts.js b/scripts/check-contracts.js index c411309f..7a61d752 100644 --- a/scripts/check-contracts.js +++ b/scripts/check-contracts.js @@ -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) diff --git a/scripts/utils.js b/scripts/utils.js index 413ba019..62b0ba46 100644 --- a/scripts/utils.js +++ b/scripts/utils.js @@ -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 }