// Copyright (c) Microsoft. All rights reserved. Licensed under the Apache License, Version 2.0. // See LICENSE.txt in the project root for complete license information. /// /* @internal */ namespace ts.JsTyping { interface TypingResolutionHost { directoryExists: (path: string) => boolean; fileExists: (fileName: string) => boolean; readFile: (path: string, encoding?: string) => string; readDirectory: (path: string, extension?: string, exclude?: string[], depth?: number) => string[]; }; interface TsdJson { version: string; repo: string; ref: string; path: string; installed?: Map; }; interface TsdInstalledItem { commit: string; }; interface PackageJson { _requiredBy?: string[]; dependencies?: Map; devDependencies?: Map; name: string; optionalDependencies?: Map; peerDependencies?: Map; typings?: string; }; // A map of loose file names to library names // that we are confident require typings let safeList: Map; /** * @param host is the object providing I/O related operations. * @param fileNames are the file names that belong to the same project. * @param cachePath is the path to the typings cache * @param projectRootPath is the path to the project root directory * @param safeListPath is the path used to retrieve the safe list * @param typingOptions are used for customizing the typing inference process. * @param compilerOptions are used as a source of typing inference. */ export function discoverTypings( host: TypingResolutionHost, fileNames: string[], cachePath: Path, projectRootPath: Path, safeListPath: Path, typingOptions: TypingOptions, compilerOptions: CompilerOptions): { cachedTypingPaths: string[], newTypingNames: string[], filesToWatch: string[] } { // A typing name to typing file path mapping const inferredTypings: Map = {}; if (!typingOptions || !typingOptions.enableAutoDiscovery) { return { cachedTypingPaths: [], newTypingNames: [], filesToWatch: [] }; } // Only infer typings for .js and .jsx files fileNames = filter(map(fileNames, normalizePath), f => scriptKindIs(f, /*LanguageServiceHost*/ undefined, ScriptKind.JS, ScriptKind.JSX)); if (!safeList) { const result = readConfigFile(safeListPath, host.readFile); if (result.config) { safeList = result.config; } } const filesToWatch: string[] = []; // Directories to search for package.json, bower.json and other typing information let searchDirs: string[] = []; let exclude: string[] = []; mergeTypings(typingOptions.include); exclude = typingOptions.exclude || []; if (typingOptions.enableAutoDiscovery) { const possibleSearchDirs = map(fileNames, getDirectoryPath); if (projectRootPath !== undefined) { possibleSearchDirs.push(projectRootPath); } searchDirs = deduplicate(possibleSearchDirs); for (const searchDir of searchDirs) { const packageJsonPath = combinePaths(searchDir, "package.json"); getTypingNamesFromJson(packageJsonPath, filesToWatch); const bowerJsonPath = combinePaths(searchDir, "bower.json"); getTypingNamesFromJson(bowerJsonPath, filesToWatch); const nodeModulesPath = combinePaths(searchDir, "node_modules"); getTypingNamesFromNodeModuleFolder(nodeModulesPath, filesToWatch); } getTypingNamesFromSourceFileNames(fileNames); } const typingsPath = combinePaths(cachePath, "typings"); const tsdJsonPath = combinePaths(cachePath, "tsd.json"); const result = readConfigFile(tsdJsonPath, host.readFile); if (result.config) { const tsdJson: TsdJson = result.config; // The "installed" property in the tsd.json serves as a registry of installed typings. Each item // of this object has a key of the relative file path, and a value that contains the corresponding // commit hash. if (tsdJson.installed) { for (const cachedTypingPath in tsdJson.installed) { // Assuming the cachedTypingPath has the format of "[package name]/[file name]" const cachedTypingName = cachedTypingPath.substr(0, cachedTypingPath.indexOf("/")); // If the inferred[cachedTypingName] is already not null, which means we found a corresponding // d.ts file that coming with the package. That one should take higher priority. if (hasProperty(inferredTypings, cachedTypingName) && !inferredTypings[cachedTypingName]) { inferredTypings[cachedTypingName] = combinePaths(typingsPath, cachedTypingPath); } } } } // Remove typings that the user has added to the exclude list for (const excludeTypingName of exclude) { delete inferredTypings[excludeTypingName]; } const newTypingNames: string[] = []; const cachedTypingPaths: string[] = []; for (const typing in inferredTypings) { if (inferredTypings[typing] !== undefined) { cachedTypingPaths.push(inferredTypings[typing]); } else { newTypingNames.push(typing); } } return { cachedTypingPaths, newTypingNames, filesToWatch }; /** * Merge a given list of typingNames to the inferredTypings map */ function mergeTypings(typingNames: string[]) { if (!typingNames) { return; } for (const typing of typingNames) { if (!hasProperty(inferredTypings, typing)) { inferredTypings[typing] = undefined; } } } /** * Get the typing info from common package manager json files like package.json or bower.json */ function getTypingNamesFromJson(jsonPath: string, filesToWatch: string[]) { const result = readConfigFile(jsonPath, host.readFile); if (result.config) { const jsonConfig: PackageJson = result.config; filesToWatch.push(jsonPath); if (jsonConfig.dependencies) { mergeTypings(getKeys(jsonConfig.dependencies)); } if (jsonConfig.devDependencies) { mergeTypings(getKeys(jsonConfig.devDependencies)); } if (jsonConfig.optionalDependencies) { mergeTypings(getKeys(jsonConfig.optionalDependencies)); } if (jsonConfig.peerDependencies) { mergeTypings(getKeys(jsonConfig.peerDependencies)); } } } /** * Infer typing names from given file names. For example, the file name "jquery-min.2.3.4.js" * should be inferred to the 'jquery' typing name; and "angular-route.1.2.3.js" should be inferred * to the 'angular-route' typing name. * @param fileNames are the names for source files in the project */ function getTypingNamesFromSourceFileNames(fileNames: string[]) { const jsFileNames = filter(fileNames, hasJavaScriptFileExtension); const inferredTypingNames = map(jsFileNames, f => removeFileExtension(getBaseFileName(f.toLowerCase()))); const cleanedTypingNames = map(inferredTypingNames, f => f.replace(/((?:\.|-)min(?=\.|$))|((?:-|\.)\d+)/g, "")); if (safeList === undefined) { mergeTypings(cleanedTypingNames); } else { mergeTypings(filter(cleanedTypingNames, f => hasProperty(safeList, f))); } const hasJsxFile = forEach(fileNames, f => scriptKindIs(f, /*LanguageServiceHost*/ undefined, ScriptKind.JSX)); if (hasJsxFile) { mergeTypings(["react"]); } } /** * Infer typing names from node_module folder * @param nodeModulesPath is the path to the "node_modules" folder */ function getTypingNamesFromNodeModuleFolder(nodeModulesPath: string, filesToWatch: string[]) { // Todo: add support for ModuleResolutionHost too if (!host.directoryExists(nodeModulesPath)) { return; } const typingNames: string[] = []; const fileNames = host.readDirectory(nodeModulesPath, "*.json", /*exclude*/ undefined, /*depth*/ 2); for (const fileName of fileNames) { const normalizedFileName = normalizePath(fileName); if (getBaseFileName(normalizedFileName) !== "package.json") { continue; } const result = readConfigFile(normalizedFileName, host.readFile); if (!result.config) { continue; } const packageJson: PackageJson = result.config; filesToWatch.push(normalizedFileName); // npm 3's package.json contains a "_requiredBy" field // we should include all the top level module names for npm 2, and only module names whose // "_requiredBy" field starts with "#" or equals "/" for npm 3. if (packageJson._requiredBy && filter(packageJson._requiredBy, (r: string) => r[0] === "#" || r === "/").length === 0) { continue; } // If the package has its own d.ts typings, those will take precedence. Otherwise the package name will be used // to download d.ts files from DefinitelyTyped if (!packageJson.name) { continue; } if (packageJson.typings) { const absolutePath = getNormalizedAbsolutePath(packageJson.typings, getDirectoryPath(normalizedFileName)); inferredTypings[packageJson.name] = absolutePath; } else { typingNames.push(packageJson.name); } } mergeTypings(typingNames); } } }