aboutsummaryrefslogtreecommitdiffstats
path: root/packages/resolver/src/resolvers/npm_resolver.ts
diff options
context:
space:
mode:
authorLeonid Logvinov <logvinov.leon@gmail.com>2018-04-10 04:23:25 +0800
committerLeonid Logvinov <logvinov.leon@gmail.com>2018-04-12 18:52:48 +0800
commiteb89926cee2c50ef657b3c033b5637f527d73c6a (patch)
tree6a4ece7902c860bfa48ac53033baf86ba25683d0 /packages/resolver/src/resolvers/npm_resolver.ts
parent7923ff4ac6d03adf3787a2a3e6f7deb6aa38fc73 (diff)
downloaddexon-0x-contracts-eb89926cee2c50ef657b3c033b5637f527d73c6a.tar
dexon-0x-contracts-eb89926cee2c50ef657b3c033b5637f527d73c6a.tar.gz
dexon-0x-contracts-eb89926cee2c50ef657b3c033b5637f527d73c6a.tar.bz2
dexon-0x-contracts-eb89926cee2c50ef657b3c033b5637f527d73c6a.tar.lz
dexon-0x-contracts-eb89926cee2c50ef657b3c033b5637f527d73c6a.tar.xz
dexon-0x-contracts-eb89926cee2c50ef657b3c033b5637f527d73c6a.tar.zst
dexon-0x-contracts-eb89926cee2c50ef657b3c033b5637f527d73c6a.zip
Implement the resolver
Diffstat (limited to 'packages/resolver/src/resolvers/npm_resolver.ts')
-rw-r--r--packages/resolver/src/resolvers/npm_resolver.ts34
1 files changed, 34 insertions, 0 deletions
diff --git a/packages/resolver/src/resolvers/npm_resolver.ts b/packages/resolver/src/resolvers/npm_resolver.ts
new file mode 100644
index 000000000..2c0a44aad
--- /dev/null
+++ b/packages/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;
+ }
+}