aboutsummaryrefslogtreecommitdiffstats
path: root/packages/sol-resolver/src/resolvers/relative_fs_resolver.ts
blob: cfff145f991691bb4e1777f0c76223aa77551b36 (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
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.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, absolutePath: filePath };
        }
        return undefined;
    }
}