1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | var path = require("path");
|
4 | var node_modules_helpers_1 = require("./node-modules-helpers");
|
5 | function getModuleInfo(fileName, criteria) {
|
6 | return getModuleInfoImpl(fileName, fileName, criteria);
|
7 | }
|
8 | exports.getModuleInfo = getModuleInfo;
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 | function getModuleInfoImpl(currentFilePath, originalFileName, criteria) {
|
15 | var npmLibraryName = node_modules_helpers_1.getLibraryName(currentFilePath);
|
16 | if (npmLibraryName === null) {
|
17 | if (criteria.typeRoots !== undefined) {
|
18 | for (var _i = 0, _a = criteria.typeRoots; _i < _a.length; _i++) {
|
19 | var root = _a[_i];
|
20 | var relativePath = path.relative(root, originalFileName).replace(/\\/g, '/');
|
21 | if (!relativePath.startsWith('../')) {
|
22 |
|
23 |
|
24 | return getModuleInfoImpl(remapToTypesFromNodeModules(relativePath), originalFileName, criteria);
|
25 | }
|
26 | }
|
27 | }
|
28 | return { type: 1 , fileName: originalFileName, isExternal: false };
|
29 | }
|
30 | if (node_modules_helpers_1.isTypescriptLibFile(currentFilePath)) {
|
31 | return { type: 0 };
|
32 | }
|
33 | var typesLibraryName = node_modules_helpers_1.getTypesLibraryName(currentFilePath);
|
34 | if (shouldLibraryBeInlined(npmLibraryName, typesLibraryName, criteria.inlinedLibraries)) {
|
35 | return { type: 1 , fileName: originalFileName, isExternal: true };
|
36 | }
|
37 | if (shouldLibraryBeImported(npmLibraryName, typesLibraryName, criteria.importedLibraries)) {
|
38 | return { type: 2 , fileName: originalFileName, libraryName: typesLibraryName || npmLibraryName, isExternal: true };
|
39 | }
|
40 | if (typesLibraryName !== null && isLibraryAllowed(typesLibraryName, criteria.allowedTypesLibraries)) {
|
41 | return { type: 3 , fileName: originalFileName, typesLibraryName: typesLibraryName, isExternal: true };
|
42 | }
|
43 | return { type: 4 , fileName: originalFileName, isExternal: true };
|
44 | }
|
45 | function shouldLibraryBeInlined(npmLibraryName, typesLibraryName, inlinedLibraries) {
|
46 | return isLibraryAllowed(npmLibraryName, inlinedLibraries) || typesLibraryName !== null && isLibraryAllowed(typesLibraryName, inlinedLibraries);
|
47 | }
|
48 | function shouldLibraryBeImported(npmLibraryName, typesLibraryName, importedLibraries) {
|
49 |
|
50 | var shouldNpmLibraryBeImported = typesLibraryName === null && isLibraryAllowed(npmLibraryName, importedLibraries);
|
51 |
|
52 | var shouldTypesLibraryBeImported = importedLibraries !== undefined && typesLibraryName !== null && isLibraryAllowed(typesLibraryName, importedLibraries);
|
53 | return shouldNpmLibraryBeImported || shouldTypesLibraryBeImported;
|
54 | }
|
55 | function isLibraryAllowed(libraryName, allowedArray) {
|
56 | return allowedArray === undefined || allowedArray.indexOf(libraryName) !== -1;
|
57 | }
|
58 | function remapToTypesFromNodeModules(pathRelativeToTypesRoot) {
|
59 | return "node_modules/@types/" + pathRelativeToTypesRoot;
|
60 | }
|