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