aboutsummaryrefslogtreecommitdiffstats
path: root/packages/sol-resolver/src/resolvers/npm_resolver.ts
diff options
context:
space:
mode:
authorJacob Evans <dekz@dekz.net>2018-04-17 11:47:57 +0800
committerGitHub <noreply@github.com>2018-04-17 11:47:57 +0800
commit79d01fe2d7582266dafedefee2873c9d488f4a20 (patch)
tree5a6dbbf7fcb1dced1ace80c03343ca789dc49b54 /packages/sol-resolver/src/resolvers/npm_resolver.ts
parent0d02037a129a97b6afb724a5316d54730c93c0d8 (diff)
parentd263f7783fabe89cc9714b596068eccdc5babc1c (diff)
downloaddexon-sol-tools-79d01fe2d7582266dafedefee2873c9d488f4a20.tar
dexon-sol-tools-79d01fe2d7582266dafedefee2873c9d488f4a20.tar.gz
dexon-sol-tools-79d01fe2d7582266dafedefee2873c9d488f4a20.tar.bz2
dexon-sol-tools-79d01fe2d7582266dafedefee2873c9d488f4a20.tar.lz
dexon-sol-tools-79d01fe2d7582266dafedefee2873c9d488f4a20.tar.xz
dexon-sol-tools-79d01fe2d7582266dafedefee2873c9d488f4a20.tar.zst
dexon-sol-tools-79d01fe2d7582266dafedefee2873c9d488f4a20.zip
Merge branch 'development' into feature/0x.js/remove-hd-wallet-from-test
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;
+ }
+}