UNPKG

hardhat-log-remover

Version:

Remove Hardhat console.log imports and calls from Solidity source files

40 lines (39 loc) 1.74 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const package_json_1 = require("../../package.json"); const regexp_1 = __importDefault(require("../lib/regexp")); const fs_1 = __importDefault(require("fs")); const task_names_1 = require("hardhat/builtin-tasks/task-names"); const config_1 = require("hardhat/config"); const plugins_1 = require("hardhat/plugins"); (0, config_1.task)('remove-logs', 'Removes console.log calls and imports from local source files', async function (args, hre) { try { await hre.run(task_names_1.TASK_COMPILE); } catch (e) { throw new plugins_1.HardhatPluginError(package_json_1.name, 'failed to compile contracts before removing logs'); } const sourcePaths = await hre.run(task_names_1.TASK_COMPILE_SOLIDITY_GET_SOURCE_PATHS); const sourceNames = await hre.run(task_names_1.TASK_COMPILE_SOLIDITY_GET_SOURCE_NAMES, { sourcePaths, }); let graph = await hre.run(task_names_1.TASK_COMPILE_SOLIDITY_GET_DEPENDENCY_GRAPH, { sourceNames, }); let count = 0; graph.getResolvedFiles().forEach(function ({ absolutePath, content, }) { const { rawContent } = content; if (rawContent.includes('console.log') || rawContent.includes('console.sol')) { let output = rawContent .replace(regexp_1.default.imports, '') .replace(regexp_1.default.calls, ''); fs_1.default.writeFileSync(absolutePath, output); count++; } }); console.log(`Removed logs from ${count} sources.`); });