From 3ad36aace25d20e06179a7dbe9982d84cbe3c236 Mon Sep 17 00:00:00 2001 From: Nick Gheorghita Date: Mon, 19 Oct 2020 11:40:14 +0200 Subject: [PATCH] More pr feedback --- packages/ethpm-v3/lib/utils.js | 13 +++--- packages/resolver/lib/resolver.ts | 7 +-- packages/resolver/lib/sources/ethpm-v3.ts | 54 +++++++++++------------ packages/resolver/lib/sources/index.ts | 2 +- 4 files changed, 37 insertions(+), 39 deletions(-) diff --git a/packages/ethpm-v3/lib/utils.js b/packages/ethpm-v3/lib/utils.js index ba407df562c..90f602bb00b 100644 --- a/packages/ethpm-v3/lib/utils.js +++ b/packages/ethpm-v3/lib/utils.js @@ -146,8 +146,8 @@ async function resolveEthpmUri(options, provider) { // update provider if it doesn't match chain id in ethpm uri if ( - connectedChainId != ethpmUri.chainId && - options.ethpm.registry.network != "development" + connectedChainId !== ethpmUri.chainId && + options.ethpm.registry.network !== "development" ) { const targetNetwork = SUPPORTED_CHAIN_IDS[ethpmUri.chainId]; if (typeof targetNetwork === "undefined") { @@ -196,11 +196,10 @@ function fetchInstalledBuildDependencies(workingDirectory) { } catch (err) { return {}; } - const installedBuildDependencies = {}; - Object.keys(ethpmLock).forEach(key => { - installedBuildDependencies[key] = ethpmLock[key].resolved_uri; - }); - return installedBuildDependencies; + return Object.keys(ethpmLock).reduce((accumulator, key) => { + accumulator[key] = ethpmLock[key].resolved_uri; + return accumulator; + }, {}); } function convertContractTypeToContractSchema( diff --git a/packages/resolver/lib/resolver.ts b/packages/resolver/lib/resolver.ts index 9e161d7b1bb..28cc7a1080d 100644 --- a/packages/resolver/lib/resolver.ts +++ b/packages/resolver/lib/resolver.ts @@ -3,7 +3,7 @@ const expect = require("@truffle/expect"); const provision = require("@truffle/provisioner"); import { ResolverSource } from "./source"; -import { EthPMv1, NPM, GlobalNPM, FS, Truffle } from "./sources"; +import { EthPMv3, NPM, GlobalNPM, FS, Truffle } from "./sources"; export class Resolver { options: any; @@ -14,7 +14,8 @@ export class Resolver { this.options = options; this.sources = [ - new EthPMv1(options.working_directory), + //new EthPMv1(options.working_directory), + new EthPMv3(options.working_directory), new NPM(options.working_directory), new GlobalNPM(), new FS(options.working_directory, options.contracts_build_directory) @@ -70,7 +71,7 @@ export class Resolver { return { body, filePath, - source, + source }; } } diff --git a/packages/resolver/lib/sources/ethpm-v3.ts b/packages/resolver/lib/sources/ethpm-v3.ts index 3073e0ade96..a7357c66f1e 100644 --- a/packages/resolver/lib/sources/ethpm-v3.ts +++ b/packages/resolver/lib/sources/ethpm-v3.ts @@ -17,7 +17,7 @@ export class EthPMv3 implements ResolverSource { } require(importPath: string) { - if (importPath.indexOf(".") === 0 || importPath.indexOf("/") === 0) { + if (importPath.startsWith(".") || importPath.startsWith("/")) { return null; } @@ -26,12 +26,12 @@ export class EthPMv3 implements ResolverSource { // We haven't compiled our own version. Assemble from data in the manifest. var separator = importPath.indexOf("/"); - var package_name = importPath.substring(0, separator); + var packageName = importPath.substring(0, separator); - var install_directory = path.join(this.workingDirectory, "_ethpm_packages"); + var installDirectory = path.join(this.workingDirectory, "_ethpm_packages"); var manifest: any = path.join( - install_directory, - package_name, + installDirectory, + packageName, "manifest.json" ); @@ -80,8 +80,8 @@ export class EthPMv3 implements ResolverSource { async resolve(importPath: string) { var separator = importPath.indexOf("/"); - var package_name = importPath.substring(0, separator); - var internal_path = importPath.substring(separator + 1); + var packageName = importPath.substring(0, separator); + var internalPath = importPath.substring(separator + 1); var installDir = this.workingDirectory; // If nothing's found, body returns `undefined` @@ -90,42 +90,40 @@ export class EthPMv3 implements ResolverSource { while (true) { // check for root level ethpm sources - var file_path = path.join( + var filePath = path.join( installDir, "_ethpm_packages", - package_name, + packageName, "_src", - internal_path + internalPath ); try { - body = fs.readFileSync(file_path, { encoding: "utf8" }); + body = fs.readFileSync(filePath, { encoding: "utf8" }); break; } catch (err) {} - file_path = path.join( + filePath = path.join( installDir, "installed_contracts", - package_name, + packageName, "contracts", - internal_path + internalPath ); try { - body = fs.readFileSync(file_path, { encoding: "utf8" }); + body = fs.readFileSync(filePath, { encoding: "utf8" }); break; } catch (err) {} - if (matches.length > 0) { - if (matches.length === 1) { - try { - body = fs.readFileSync( - path.join(installDir, "_ethpm_packages", matches[0]), - { encoding: "utf8" } - ); - break; - } catch (err) {} - } + if (matches.length === 1) { + try { + body = fs.readFileSync( + path.join(installDir, "_ethpm_packages", matches[0]), + { encoding: "utf8" } + ); + break; + } catch (err) {} } // Recurse outwards until impossible @@ -145,14 +143,14 @@ export class EthPMv3 implements ResolverSource { // that when this path is evaluated this source is used again. resolveDependencyPath(importPath: string, dependencyPath: string) { var dirname = path.dirname(importPath); - var resolved_dependency_path = path.join(dirname, dependencyPath); + var resolvedDependencyPath = path.join(dirname, dependencyPath); // Note: We use `path.join()` here to take care of path idiosyncrasies // like joining "something/" and "./something_else.sol". However, this makes // paths OS dependent, and on Windows, makes the separator "\". Solidity // needs the separator to be a forward slash. Let's massage that here. - resolved_dependency_path = resolved_dependency_path.replace(/\\/g, "/"); + resolvedDependencyPath = resolvedDependencyPath.replace(/\\/g, "/"); - return resolved_dependency_path; + return resolvedDependencyPath; } } diff --git a/packages/resolver/lib/sources/index.ts b/packages/resolver/lib/sources/index.ts index 43463ed55e6..5ef8c2acfae 100644 --- a/packages/resolver/lib/sources/index.ts +++ b/packages/resolver/lib/sources/index.ts @@ -1,4 +1,4 @@ -export { EthPMv1 } from "./ethpm-v1"; // todo: remove? +export { EthPMv1 } from "./ethpm-v1"; export { EthPMv3 } from "./ethpm-v3"; export { NPM } from "./npm"; export { GlobalNPM } from "./globalnpm";