1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | "use strict";
|
9 | var __extends = (this && this.__extends) || function (d, b) {
|
10 | for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
|
11 | function __() { this.constructor = d; }
|
12 | d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
13 | };
|
14 | var path = require('path');
|
15 | var ts = require('typescript');
|
16 | var reflector_host_1 = require('./reflector_host');
|
17 | var EXT = /(\.ts|\.d\.ts|\.js|\.jsx|\.tsx)$/;
|
18 | var DTS = /\.d\.ts$/;
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 | var PathMappedReflectorHost = (function (_super) {
|
27 | __extends(PathMappedReflectorHost, _super);
|
28 | function PathMappedReflectorHost(program, compilerHost, options, context) {
|
29 | _super.call(this, program, compilerHost, options, context);
|
30 | }
|
31 | PathMappedReflectorHost.prototype.getCanonicalFileName = function (fileName) {
|
32 | if (!fileName)
|
33 | return fileName;
|
34 |
|
35 | for (var _i = 0, _a = this.options.rootDirs || []; _i < _a.length; _i++) {
|
36 | var dir = _a[_i];
|
37 | if (fileName.indexOf(dir) === 0) {
|
38 | fileName = fileName.substring(dir.length);
|
39 | }
|
40 | }
|
41 | return fileName;
|
42 | };
|
43 | PathMappedReflectorHost.prototype.resolve = function (m, containingFile) {
|
44 | for (var _i = 0, _a = this.options.rootDirs || ['']; _i < _a.length; _i++) {
|
45 | var root = _a[_i];
|
46 | var rootedContainingFile = path.join(root, containingFile);
|
47 | var resolved = ts.resolveModuleName(m, rootedContainingFile, this.options, this.context).resolvedModule;
|
48 | if (resolved) {
|
49 | if (this.options.traceResolution) {
|
50 | console.error('resolve', m, containingFile, '=>', resolved.resolvedFileName);
|
51 | }
|
52 | return resolved.resolvedFileName;
|
53 | }
|
54 | }
|
55 | };
|
56 | |
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 | PathMappedReflectorHost.prototype.getImportPath = function (containingFile, importedFile) {
|
63 | var _this = this;
|
64 | importedFile = this.resolveAssetUrl(importedFile, containingFile);
|
65 | containingFile = this.resolveAssetUrl(containingFile, '');
|
66 | if (this.options.traceResolution) {
|
67 | console.error('getImportPath from containingFile', containingFile, 'to importedFile', importedFile);
|
68 | }
|
69 |
|
70 |
|
71 | if (!this.context.fileExists(importedFile)) {
|
72 | if (this.options.rootDirs && this.options.rootDirs.length > 0) {
|
73 | this.context.assumeFileExists(path.join(this.options.rootDirs[0], importedFile));
|
74 | }
|
75 | else {
|
76 | this.context.assumeFileExists(importedFile);
|
77 | }
|
78 | }
|
79 | var resolvable = function (candidate) {
|
80 | var resolved = _this.getCanonicalFileName(_this.resolve(candidate, importedFile));
|
81 | return resolved && resolved.replace(EXT, '') === importedFile.replace(EXT, '');
|
82 | };
|
83 | var importModuleName = importedFile.replace(EXT, '');
|
84 | var parts = importModuleName.split(path.sep).filter(function (p) { return !!p; });
|
85 | var foundRelativeImport;
|
86 | for (var index = parts.length - 1; index >= 0; index--) {
|
87 | var candidate_1 = parts.slice(index, parts.length).join(path.sep);
|
88 | if (resolvable(candidate_1)) {
|
89 | return candidate_1;
|
90 | }
|
91 | candidate_1 = '.' + path.sep + candidate_1;
|
92 | if (resolvable(candidate_1)) {
|
93 | foundRelativeImport = candidate_1;
|
94 | }
|
95 | }
|
96 | if (foundRelativeImport)
|
97 | return foundRelativeImport;
|
98 |
|
99 | var candidate = path.relative(path.dirname(containingFile), importModuleName);
|
100 | if (resolvable(candidate)) {
|
101 | return candidate;
|
102 | }
|
103 | throw new Error("Unable to find any resolvable import for " + importedFile + " relative to " + containingFile);
|
104 | };
|
105 | PathMappedReflectorHost.prototype.getMetadataFor = function (filePath) {
|
106 | for (var _i = 0, _a = this.options.rootDirs || []; _i < _a.length; _i++) {
|
107 | var root = _a[_i];
|
108 | var rootedPath = path.join(root, filePath);
|
109 | if (!this.compilerHost.fileExists(rootedPath)) {
|
110 |
|
111 |
|
112 |
|
113 | continue;
|
114 | }
|
115 | if (DTS.test(rootedPath)) {
|
116 | var metadataPath = rootedPath.replace(DTS, '.metadata.json');
|
117 | if (this.context.fileExists(metadataPath)) {
|
118 | var metadata = this.readMetadata(metadataPath);
|
119 | return (Array.isArray(metadata) && metadata.length == 0) ? undefined : metadata;
|
120 | }
|
121 | }
|
122 | else {
|
123 | var sf = this.program.getSourceFile(rootedPath);
|
124 | if (!sf) {
|
125 | throw new Error("Source file " + rootedPath + " not present in program.");
|
126 | }
|
127 | sf.fileName = this.getCanonicalFileName(sf.fileName);
|
128 | return this.metadataCollector.getMetadata(sf);
|
129 | }
|
130 | }
|
131 | };
|
132 | return PathMappedReflectorHost;
|
133 | }(reflector_host_1.ReflectorHost));
|
134 | exports.PathMappedReflectorHost = PathMappedReflectorHost;
|
135 |
|
\ | No newline at end of file |