aboutsummaryrefslogtreecommitdiffstats
path: root/packages/sol-resolver/src/resolvers/relative_fs_resolver.ts
blob: 77103b8c1c5b5c9b2589a0cdedab0501d747a166 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import * as fs from 'fs';
import * as path from 'path';

import { ContractSource } from '../types';

import { Resolver } from './resolver';

export class RelativeFSResolver extends Resolver {
    private readonly _contractsDir: string;
    constructor(contractsDir: string) {
        super();
        this._contractsDir = contractsDir;
    }
    // tslint:disable-next-line:prefer-function-over-method
    public resolveIfExists(importPath: string): ContractSource | undefined {
        const filePath = path.join(this._contractsDir, importPath);
        if (fs.existsSync(filePath)) {
            const fileContent = fs.readFileSync(filePath).toString();
            return {
                source: fileContent,
                path: importPath,
            };
        }
        return undefined;
    }
}