|
| 1 | +/// <reference path="./types.ts"/> |
| 2 | + |
| 3 | +namespace ts.server { |
| 4 | + function isGenerated(path: string) { |
| 5 | + const GEN_EXT = ['ngsummary', 'ngstyle', 'ngfactory']; |
| 6 | + const TS_EXT = ['ts', 'tsx', 'd.ts']; |
| 7 | + |
| 8 | + for (const gen of GEN_EXT) { |
| 9 | + for (const ext of TS_EXT) { |
| 10 | + if (fileExtensionIs(path, gen + '.' + ext)) { |
| 11 | + return true; |
| 12 | + } |
| 13 | + } |
| 14 | + } |
| 15 | + return false; |
| 16 | + } |
| 17 | + |
| 18 | + export function getG3ServerHostProxy( |
| 19 | + tsconfigPath: string, |
| 20 | + host: ServerHost, |
| 21 | + logger: Logger): ServerHost { |
| 22 | + |
| 23 | + let proxyHost:ServerHost = (<any>Object).assign({}, host); |
| 24 | + |
| 25 | + const {config, error} = readConfigFile(tsconfigPath, proxyHost.readFile); |
| 26 | + if (!error) { |
| 27 | + const projectDir = getDirectoryPath(tsconfigPath); |
| 28 | + |
| 29 | + // Get the files list from the tsconfig. |
| 30 | + let {options, errors, fileNames} = |
| 31 | + parseJsonConfigFileContent(config, proxyHost, projectDir); |
| 32 | + if (!errors || errors.length === 0) { |
| 33 | + // Get the list of files into a map. |
| 34 | + let fileMap: {[k: string]: boolean} = {}; |
| 35 | + |
| 36 | + // Just put the directory of the files in the known directories list. |
| 37 | + // We don't rely on the behavior of walking up the directories to find |
| 38 | + // the node_modules. (This part may not work for opensource) |
| 39 | + let directoryMap: {[k: string]: boolean} = {}; |
| 40 | + let rootDirs = options.rootDirs; |
| 41 | + |
| 42 | + // Add all the rootDirs to the known directories list. |
| 43 | + options.rootDirs.forEach(d => { |
| 44 | + logger.info('Adding rootdir: ' + d); |
| 45 | + directoryMap[d] = true; |
| 46 | + }); |
| 47 | + |
| 48 | + // Add the tsconfig.json as a valid project file. |
| 49 | + fileMap[tsconfigPath] = true; |
| 50 | + |
| 51 | + // For each file add to the filesMap and add their directory |
| 52 | + // (and few directories above them) to the directoryMap. |
| 53 | + fileNames.forEach(f => { |
| 54 | + f = proxyHost.resolvePath(f); |
| 55 | + logger.info('Adding file: ' + f); |
| 56 | + fileMap[f] = true; |
| 57 | + // TODO(viks): How deep should we go? Is 2 enough? |
| 58 | + for (let i = 0; i < 2; i++) { |
| 59 | + f = getDirectoryPath(f); |
| 60 | + if (f) { |
| 61 | + logger.info('Adding dir: ' + getDirectoryPath(f)); |
| 62 | + directoryMap[f] = true; |
| 63 | + } else { |
| 64 | + break; |
| 65 | + } |
| 66 | + } |
| 67 | + }); |
| 68 | + |
| 69 | + // Override the fileExists in the ServerHost to reply using the fileMap |
| 70 | + // instead of hitting the (network) file system. |
| 71 | + proxyHost.fileExists = (path: string) => { |
| 72 | + path = proxyHost.resolvePath(path); |
| 73 | + if (path in fileMap) { |
| 74 | + // File found in map! |
| 75 | + logger.info('Found: ' + path); |
| 76 | + return true; |
| 77 | + } else { |
| 78 | + // Only ever allow looking in the filesystem for files inside |
| 79 | + // the project dir. Allows for discovery of new source files |
| 80 | + // in the project without having to rebuild tsconfig. |
| 81 | + // Skip generated files since the tsconfig would have to generated anyways |
| 82 | + // while regenerating these. |
| 83 | + if (!isGenerated(path)) { |
| 84 | + for (const rootDir of rootDirs) { |
| 85 | + if (path.indexOf(rootDir) === 0) { |
| 86 | + logger.info('Search: ' + path); |
| 87 | + return host.fileExists(path); |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + // File not in map. Just return false without hitting file system. |
| 93 | + logger.info('Did not find: ' + path); |
| 94 | + return false; |
| 95 | + } |
| 96 | + |
| 97 | + // Override the directoryExists in the ServerHost to reply using the |
| 98 | + // directoryMap without hitting the file system. |
| 99 | + proxyHost.directoryExists = (path: string) => { |
| 100 | + path = proxyHost.resolvePath(path); |
| 101 | + if (path in directoryMap) { |
| 102 | + logger.info('Dir Found: ' + path); |
| 103 | + return true; |
| 104 | + } |
| 105 | + logger.info('Dir NOT Found: ' + path); |
| 106 | + return false; |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | + return proxyHost; |
| 111 | + } |
| 112 | +} |
0 commit comments