Add support for scoped packages resolving to NPMResoler

This commit is contained in:
Leonid Logvinov
2018-11-23 14:05:40 +01:00
parent 0faa8b3231
commit 5b0746a8ef
2 changed files with 20 additions and 2 deletions

View File

@@ -1,4 +1,13 @@
[
{
"version": "1.1.0",
"changes": [
{
"note": "NPMResolver now supports scoped packages",
"pr": 1311
}
]
},
{
"timestamp": 1542821676,
"version": "1.0.17",

View File

@@ -1,4 +1,5 @@
import * as fs from 'fs';
import * as _ from 'lodash';
import * as path from 'path';
import { ContractSource } from '../types';
@@ -13,12 +14,20 @@ export class NPMResolver extends Resolver {
}
public resolveIfExists(importPath: string): ContractSource | undefined {
if (!importPath.startsWith('/')) {
const [packageName, ...other] = importPath.split('/');
let packageName;
let packageScope;
let other;
if (importPath.startsWith('@')) {
[packageScope, packageName, ...other] = importPath.split('/');
} else {
[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);
const packagePath = _.isUndefined(packageScope) ? packageName : path.join(packageScope, packageName);
const lookupPath = path.join(currentPath, 'node_modules', packagePath, pathWithinPackage);
if (fs.existsSync(lookupPath) && fs.lstatSync(lookupPath).isFile()) {
const fileContent = fs.readFileSync(lookupPath).toString();
return {