From c8eaa63cce99e6666d6d863e8a47007e4cbcbbbb Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Wed, 19 Dec 2018 14:06:58 +0100 Subject: Add to type in sol-resolver --- packages/sol-resolver/CHANGELOG.json | 13 +++++++++++++ packages/sol-resolver/src/resolvers/fs_resolver.ts | 5 +---- packages/sol-resolver/src/resolvers/name_resolver.ts | 10 ++-------- packages/sol-resolver/src/resolvers/npm_resolver.ts | 5 +---- packages/sol-resolver/src/resolvers/relative_fs_resolver.ts | 7 ++----- packages/sol-resolver/src/resolvers/url_resolver.ts | 5 +---- packages/sol-resolver/src/types.ts | 1 + 7 files changed, 21 insertions(+), 25 deletions(-) (limited to 'packages/sol-resolver') diff --git a/packages/sol-resolver/CHANGELOG.json b/packages/sol-resolver/CHANGELOG.json index 85398e624..b898a422f 100644 --- a/packages/sol-resolver/CHANGELOG.json +++ b/packages/sol-resolver/CHANGELOG.json @@ -1,4 +1,17 @@ [ + { + "version": "1.2.1", + "changes": [ + { + "note": "Add `absolutePath` to `ContractSource` type", + "pr": "TODO" + }, + { + "note": "Add `SpyResolver` that records all resolved contracts data", + "pr": "TODO" + } + ] + }, { "version": "1.1.1", "changes": [ diff --git a/packages/sol-resolver/src/resolvers/fs_resolver.ts b/packages/sol-resolver/src/resolvers/fs_resolver.ts index 63fc3448e..86128023d 100644 --- a/packages/sol-resolver/src/resolvers/fs_resolver.ts +++ b/packages/sol-resolver/src/resolvers/fs_resolver.ts @@ -9,10 +9,7 @@ export class FSResolver extends Resolver { public resolveIfExists(importPath: string): ContractSource | undefined { if (fs.existsSync(importPath) && fs.lstatSync(importPath).isFile()) { const fileContent = fs.readFileSync(importPath).toString(); - return { - source: fileContent, - path: importPath, - }; + return { source: fileContent, path: importPath, absolutePath: importPath }; } return undefined; } diff --git a/packages/sol-resolver/src/resolvers/name_resolver.ts b/packages/sol-resolver/src/resolvers/name_resolver.ts index d6ac6a499..aee326fb7 100644 --- a/packages/sol-resolver/src/resolvers/name_resolver.ts +++ b/packages/sol-resolver/src/resolvers/name_resolver.ts @@ -20,10 +20,7 @@ export class NameResolver extends EnumerableResolver { if (contractName === lookupContractName) { const absoluteContractPath = path.join(this._contractsDir, filePath); const source = fs.readFileSync(absoluteContractPath).toString(); - contractSource = { - source, - path: filePath, - }; + contractSource = { source, path: filePath, absolutePath: absoluteContractPath }; return true; } return undefined; @@ -36,10 +33,7 @@ export class NameResolver extends EnumerableResolver { const onFile = (filePath: string) => { const absoluteContractPath = path.join(this._contractsDir, filePath); const source = fs.readFileSync(absoluteContractPath).toString(); - const contractSource = { - source, - path: filePath, - }; + const contractSource = { source, path: filePath, absolutePath: absoluteContractPath }; contractSources.push(contractSource); }; this._traverseContractsDir(this._contractsDir, onFile); diff --git a/packages/sol-resolver/src/resolvers/npm_resolver.ts b/packages/sol-resolver/src/resolvers/npm_resolver.ts index eeb2b5493..3c1d09557 100644 --- a/packages/sol-resolver/src/resolvers/npm_resolver.ts +++ b/packages/sol-resolver/src/resolvers/npm_resolver.ts @@ -32,10 +32,7 @@ export class NPMResolver extends Resolver { const lookupPath = path.join(currentPath, 'node_modules', packagePath, pathWithinPackage); if (fs.existsSync(lookupPath) && fs.lstatSync(lookupPath).isFile()) { const fileContent = fs.readFileSync(lookupPath).toString(); - return { - source: fileContent, - path: lookupPath, - }; + return { source: fileContent, path: importPath, absolutePath: lookupPath }; } currentPath = path.dirname(currentPath); } diff --git a/packages/sol-resolver/src/resolvers/relative_fs_resolver.ts b/packages/sol-resolver/src/resolvers/relative_fs_resolver.ts index ed96040d3..cfff145f9 100644 --- a/packages/sol-resolver/src/resolvers/relative_fs_resolver.ts +++ b/packages/sol-resolver/src/resolvers/relative_fs_resolver.ts @@ -13,13 +13,10 @@ export class RelativeFSResolver extends Resolver { } // tslint:disable-next-line:prefer-function-over-method public resolveIfExists(importPath: string): ContractSource | undefined { - const filePath = path.join(this._contractsDir, importPath); + const filePath = path.resolve(path.join(this._contractsDir, importPath)); if (fs.existsSync(filePath) && !fs.lstatSync(filePath).isDirectory()) { const fileContent = fs.readFileSync(filePath).toString(); - return { - source: fileContent, - path: importPath, - }; + return { source: fileContent, path: importPath, absolutePath: filePath }; } return undefined; } diff --git a/packages/sol-resolver/src/resolvers/url_resolver.ts b/packages/sol-resolver/src/resolvers/url_resolver.ts index 180b0c9f6..ef300e6db 100644 --- a/packages/sol-resolver/src/resolvers/url_resolver.ts +++ b/packages/sol-resolver/src/resolvers/url_resolver.ts @@ -11,10 +11,7 @@ export class URLResolver extends Resolver { if (importPath.startsWith(FILE_URL_PREXIF)) { const filePath = importPath.substr(FILE_URL_PREXIF.length); const fileContent = fs.readFileSync(filePath).toString(); - return { - source: fileContent, - path: importPath, - }; + return { source: fileContent, path: importPath, absolutePath: filePath }; } return undefined; } diff --git a/packages/sol-resolver/src/types.ts b/packages/sol-resolver/src/types.ts index 41492622d..b4ba164c8 100644 --- a/packages/sol-resolver/src/types.ts +++ b/packages/sol-resolver/src/types.ts @@ -1,6 +1,7 @@ export interface ContractSource { source: string; path: string; + absolutePath: string; } export interface ContractSources { -- cgit From 8ddf925a8feb41e240871fe0d57e0f40b9bb4896 Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Wed, 19 Dec 2018 14:07:20 +0100 Subject: Add SpyResolver --- packages/sol-resolver/src/index.ts | 1 + .../sol-resolver/src/resolvers/spy_resolver.ts | 23 ++++++++++++++++++++++ 2 files changed, 24 insertions(+) create mode 100644 packages/sol-resolver/src/resolvers/spy_resolver.ts (limited to 'packages/sol-resolver') diff --git a/packages/sol-resolver/src/index.ts b/packages/sol-resolver/src/index.ts index a86053259..f55aca070 100644 --- a/packages/sol-resolver/src/index.ts +++ b/packages/sol-resolver/src/index.ts @@ -5,5 +5,6 @@ export { NPMResolver } from './resolvers/npm_resolver'; export { FSResolver } from './resolvers/fs_resolver'; export { RelativeFSResolver } from './resolvers/relative_fs_resolver'; export { NameResolver } from './resolvers/name_resolver'; +export { SpyResolver } from './resolvers/spy_resolver'; export { EnumerableResolver } from './resolvers/enumerable_resolver'; export { Resolver } from './resolvers/resolver'; diff --git a/packages/sol-resolver/src/resolvers/spy_resolver.ts b/packages/sol-resolver/src/resolvers/spy_resolver.ts new file mode 100644 index 000000000..df56c8f9e --- /dev/null +++ b/packages/sol-resolver/src/resolvers/spy_resolver.ts @@ -0,0 +1,23 @@ +import * as fs from 'fs'; +import * as _ from 'lodash'; +import * as path from 'path'; + +import { ContractSource } from '../types'; + +import { Resolver } from './resolver'; + +export class SpyResolver extends Resolver { + public resolvedContractSources: ContractSource[] = []; + private _resolver: Resolver; + constructor(resolver: Resolver) { + super(); + this._resolver = resolver; + } + public resolveIfExists(importPath: string): ContractSource | undefined { + const contractSourceIfExists = this._resolver.resolveIfExists(importPath); + if (!_.isUndefined(contractSourceIfExists)) { + this.resolvedContractSources.push(contractSourceIfExists); + } + return contractSourceIfExists; + } +} -- cgit From 56d48758d308f9450aaac2e986dd09efd8d479c0 Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Wed, 19 Dec 2018 14:19:35 +0100 Subject: Add PR numbers --- packages/sol-resolver/CHANGELOG.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'packages/sol-resolver') diff --git a/packages/sol-resolver/CHANGELOG.json b/packages/sol-resolver/CHANGELOG.json index b898a422f..74c4d39c5 100644 --- a/packages/sol-resolver/CHANGELOG.json +++ b/packages/sol-resolver/CHANGELOG.json @@ -4,11 +4,11 @@ "changes": [ { "note": "Add `absolutePath` to `ContractSource` type", - "pr": "TODO" + "pr": 1461 }, { "note": "Add `SpyResolver` that records all resolved contracts data", - "pr": "TODO" + "pr": 1461 } ] }, -- cgit From e2535027e92206356d9dde80978edf64f00a50d4 Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Wed, 19 Dec 2018 14:44:05 +0100 Subject: Remove unused variables --- packages/sol-resolver/src/resolvers/spy_resolver.ts | 2 -- 1 file changed, 2 deletions(-) (limited to 'packages/sol-resolver') diff --git a/packages/sol-resolver/src/resolvers/spy_resolver.ts b/packages/sol-resolver/src/resolvers/spy_resolver.ts index df56c8f9e..3fd62985b 100644 --- a/packages/sol-resolver/src/resolvers/spy_resolver.ts +++ b/packages/sol-resolver/src/resolvers/spy_resolver.ts @@ -1,6 +1,4 @@ -import * as fs from 'fs'; import * as _ from 'lodash'; -import * as path from 'path'; import { ContractSource } from '../types'; -- cgit From 85a8c6160f046ad5b5e33d30fc7a71abea3ecf33 Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Wed, 19 Dec 2018 14:44:40 +0100 Subject: Mark the field as readonly --- packages/sol-resolver/src/resolvers/spy_resolver.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'packages/sol-resolver') diff --git a/packages/sol-resolver/src/resolvers/spy_resolver.ts b/packages/sol-resolver/src/resolvers/spy_resolver.ts index 3fd62985b..b31ecfe1d 100644 --- a/packages/sol-resolver/src/resolvers/spy_resolver.ts +++ b/packages/sol-resolver/src/resolvers/spy_resolver.ts @@ -6,7 +6,7 @@ import { Resolver } from './resolver'; export class SpyResolver extends Resolver { public resolvedContractSources: ContractSource[] = []; - private _resolver: Resolver; + private readonly _resolver: Resolver; constructor(resolver: Resolver) { super(); this._resolver = resolver; -- cgit From 0abf1c71f64e90aba3e3bac55c8c24dc984bf80d Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Wed, 19 Dec 2018 15:33:25 +0100 Subject: Add a comment for SpyResolver --- packages/sol-resolver/src/resolvers/spy_resolver.ts | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'packages/sol-resolver') diff --git a/packages/sol-resolver/src/resolvers/spy_resolver.ts b/packages/sol-resolver/src/resolvers/spy_resolver.ts index b31ecfe1d..5582d771a 100644 --- a/packages/sol-resolver/src/resolvers/spy_resolver.ts +++ b/packages/sol-resolver/src/resolvers/spy_resolver.ts @@ -4,6 +4,10 @@ import { ContractSource } from '../types'; import { Resolver } from './resolver'; +/** + * This resolver is a passthrough proxy to any resolver that records all the resolved contracts sources. + * You can access them later using the `resolvedContractSources` public field. + */ export class SpyResolver extends Resolver { public resolvedContractSources: ContractSource[] = []; private readonly _resolver: Resolver; -- cgit