Skip to content
This repository has been archived by the owner on Feb 26, 2024. It is now read-only.

Commit

Permalink
Remove node-dir dependency from debug-utils
Browse files Browse the repository at this point in the history
  • Loading branch information
haltman-at committed Aug 27, 2020
1 parent 143510a commit 0d263c2
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 46 deletions.
40 changes: 30 additions & 10 deletions packages/core/lib/debug/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ const debugModule = require("debug");
const debug = debugModule("lib:debug:cli");

const ora = require("ora");
const fs = require("fs-extra");
const path = require("path");

const Debugger = require("@truffle/debugger");
const DebugUtils = require("@truffle/debug-utils");
Expand Down Expand Up @@ -64,16 +66,7 @@ class CLIDebugger {

async getCompilations() {
let artifacts;
try {
artifacts = await DebugUtils.gatherArtifacts(this.config);
} catch (error) {
//leave artifacts undefined if build directory doesn't exist
//(HACK, sorry for doing it this way!)
if (!error.message.startsWith("ENOENT")) {
//avoid swallowing other errors
throw error;
}
}
artifacts = await this.gatherArtifacts();
if (artifacts) {
let shimmedCompilations = Codec.Compilations.Utils.shimArtifacts(
artifacts
Expand Down Expand Up @@ -156,6 +149,33 @@ class CLIDebugger {
async buildInterpreter(session) {
return new DebugInterpreter(this.config, session, this.txHash);
}

async gatherArtifacts() {
// Gather all available contract artifacts
// if build directory doesn't exist, return undefined to signal that
// a recompile is necessary
if (!fs.existsSync(this.config.contracts_build_directory)) {
return undefined;
}
const files = fs.readdirSync(this.config.contracts_build_directory);

let contracts = files
.filter(filePath => {
return path.extname(filePath) === ".json";
})
.map(filePath => {
return path.basename(filePath, ".json");
})
.map(contractName => {
return this.config.resolver.require(contractName);
});

await Promise.all(
contracts.map(abstraction => abstraction.detectNetwork())
);

return contracts;
}
}

module.exports = {
Expand Down
24 changes: 0 additions & 24 deletions packages/debug-utils/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
var OS = require("os");
var dir = require("node-dir");
var path = require("path");
var debug = require("debug")("debug-utils");
var BN = require("bn.js");
var util = require("util");
Expand Down Expand Up @@ -78,28 +76,6 @@ const DEFAULT_TAB_WIDTH = 8;
var DebugUtils = {
truffleColors, //make these externally available

gatherArtifacts: async function (config) {
// Gather all available contract artifacts
let files = await dir.promiseFiles(config.contracts_build_directory);

var contracts = files
.filter(file_path => {
return path.extname(file_path) === ".json";
})
.map(file_path => {
return path.basename(file_path, ".json");
})
.map(contract_name => {
return config.resolver.require(contract_name);
});

await Promise.all(
contracts.map(abstraction => abstraction.detectNetwork())
);

return contracts;
},

//attempts to test whether a given compilation is a real compilation,
//i.e., was compiled all at once.
//if it is real, it will definitely pass this test, barring a Solidity bug.
Expand Down
3 changes: 1 addition & 2 deletions packages/debug-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@
"chalk": "^2.4.2",
"debug": "^4.1.0",
"highlight.js": "^9.15.8",
"highlightjs-solidity": "^1.0.18",
"node-dir": "0.1.17"
"highlightjs-solidity": "^1.0.18"
},
"devDependencies": {
"chai": "^4.2.0",
Expand Down
36 changes: 27 additions & 9 deletions packages/debugger/test/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ const debug = debugModule("test:helpers");
import path from "path";
import fs from "fs-extra";
import Contracts from "@truffle/workflow-compile";
import Debug from "@truffle/debug-utils";
import Artifactor from "@truffle/artifactor";
import Web3 from "web3";
import Migrate from "@truffle/migrate";
Expand Down Expand Up @@ -46,7 +45,10 @@ export async function prepareContracts(provider, sources = {}, migrations) {
await migrate(config);

let artifacts = await gatherArtifacts(config);
debug("artifacts: %o", artifacts.map(a => a.contractName));
debug(
"artifacts: %o",
artifacts.map(a => a.contractName)
);

let abstractions = {};
for (let name of contractNames) {
Expand All @@ -65,8 +67,8 @@ export async function prepareContracts(provider, sources = {}, migrations) {

export function getAccounts(provider) {
let web3 = new Web3(provider);
return new Promise(function(accept, reject) {
web3.eth.getAccounts(function(err, accounts) {
return new Promise(function (accept, reject) {
web3.eth.getAccounts(function (err, accounts) {
if (err) return reject(err);
accept(accounts);
});
Expand Down Expand Up @@ -139,13 +141,13 @@ export async function defaultMigrations(contractNames) {
}

export async function compile(config) {
return new Promise(function(accept, reject) {
return new Promise(function (accept, reject) {
Contracts.compile(
config.with({
all: true,
quiet: true
}),
function(err, result) {
function (err, result) {
if (err) return reject(err);
const { contracts, outputs } = result;
debug("result %O", result);
Expand All @@ -156,12 +158,12 @@ export async function compile(config) {
}

export async function migrate(config) {
return new Promise(function(accept, reject) {
return new Promise(function (accept, reject) {
Migrate.run(
config.with({
quiet: true
}),
function(err, contracts) {
function (err, contracts) {
if (err) return reject(err);
accept(contracts);
}
Expand All @@ -170,7 +172,23 @@ export async function migrate(config) {
}

export async function gatherArtifacts(config) {
return Debug.gatherArtifacts(config);
// Gather all available contract artifacts
const files = fs.readdirSync(config.contracts_build_directory);

let contracts = files
.filter(filePath => {
return path.extname(filePath) === ".json";
})
.map(filePath => {
return path.basename(filePath, ".json");
})
.map(contractName => {
return config.resolver.require(contractName);
});

await Promise.all(contracts.map(abstraction => abstraction.detectNetwork()));

return contracts;
}

export function lineOf(searchString, source) {
Expand Down
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -11369,7 +11369,7 @@ node-addon-api@^2.0.0:
resolved "https://registry.yarnpkg.com/node-addon-api/-/node-addon-api-2.0.2.tgz#432cfa82962ce494b132e9d72a15b29f71ff5d32"
integrity sha512-Ntyt4AIXyaLIuMHF6IOoTakB3K+RWxwtsHNRxllEoA6vPwP9o4866g6YWDLUdnucilZhmkxiHwHr11gAENw+QA==

node-dir@0.1.17, node-dir@^0.1.16:
node-dir@^0.1.16:
version "0.1.17"
resolved "https://registry.yarnpkg.com/node-dir/-/node-dir-0.1.17.tgz#5f5665d93351335caabef8f1c554516cf5f1e4e5"
integrity sha1-X1Zl2TNRM1yqvvjxxVRRbPXx5OU=
Expand Down

0 comments on commit 0d263c2

Please sign in to comment.