aboutsummaryrefslogtreecommitdiffstats
path: root/packages/sol-resolver/src/resolvers/npm_resolver.ts
diff options
context:
space:
mode:
authorLeonid Logvinov <logvinov.leon@gmail.com>2018-04-16 22:38:55 +0800
committerLeonid Logvinov <logvinov.leon@gmail.com>2018-04-16 23:43:59 +0800
commitd8ef76fd5efe63ec8f6205a73494ce388e64391f (patch)
tree22a5740584029830e37961136e229938b8f735f4 /packages/sol-resolver/src/resolvers/npm_resolver.ts
parente5cf41b3134d6cd5fdda995aa81ec23e51bb5145 (diff)
downloaddexon-sol-tools-d8ef76fd5efe63ec8f6205a73494ce388e64391f.tar
dexon-sol-tools-d8ef76fd5efe63ec8f6205a73494ce388e64391f.tar.gz
dexon-sol-tools-d8ef76fd5efe63ec8f6205a73494ce388e64391f.tar.bz2
dexon-sol-tools-d8ef76fd5efe63ec8f6205a73494ce388e64391f.tar.lz
dexon-sol-tools-d8ef76fd5efe63ec8f6205a73494ce388e64391f.tar.xz
dexon-sol-tools-d8ef76fd5efe63ec8f6205a73494ce388e64391f.tar.zst
dexon-sol-tools-d8ef76fd5efe63ec8f6205a73494ce388e64391f.zip
Rename resolver to sol-resolver
Diffstat (limited to 'packages/sol-resolver/src/resolvers/npm_resolver.ts')
-rw-r--r--packages/sol-resolver/src/resolvers/npm_resolver.ts34
1 files changed, 34 insertions, 0 deletions
diff --git a/packages/sol-resolver/src/resolvers/npm_resolver.ts b/packages/sol-resolver/src/resolvers/npm_resolver.ts
new file mode 100644
index 000000000..2c0a44aad
--- /dev/null
+++ b/packages/sol-resolver/src/resolvers/npm_resolver.ts
@@ -0,0 +1,34 @@
+import * as fs from 'fs';
+import * as path from 'path';
+
+import { ContractSource } from '../types';
+
+import { Resolver } from './resolver';
+
+export class NPMResolver extends Resolver {
+ private _packagePath: string;
+ constructor(packagePath: string) {
+ super();
+ this._packagePath = packagePath;
+ }
+ public resolveIfExists(importPath: string): ContractSource | undefined {
+ if (!importPath.startsWith('/')) {
+ const [packageName, ...other] = importPath.split('/');
+ const pathWithinPackage = path.join(...other);
+ let currentPath = this._packagePath;
+ const ROOT_PATH = '/';
+ while (currentPath !== ROOT_PATH) {
+ const lookupPath = path.join(currentPath, 'node_modules', packageName, pathWithinPackage);
+ if (fs.existsSync(lookupPath)) {
+ const fileContent = fs.readFileSync(lookupPath).toString();
+ return {
+ source: fileContent,
+ path: lookupPath,
+ };
+ }
+ currentPath = path.dirname(currentPath);
+ }
+ }
+ return undefined;
+ }
+}