microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/utils/tsdHelper.ts
56lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | // Licensed under the MIT license. See LICENSE file in the project root for details. |
| 3 | |
| 4 | import * as path from 'path'; |
| 5 | import * as Q from 'q'; |
| 6 | import {FileSystem} from './node/fileSystem'; |
| 7 | |
| 8 | export class TsdHelper { |
| 9 | private static REACT_TYPINGS_FOLDERNAME = "ReactTypings"; |
| 10 | private static REACT_TYPINGS_PATH = path.resolve(__dirname, "..", "..", TsdHelper.REACT_TYPINGS_FOLDERNAME); |
| 11 | |
| 12 | private static installTypeDefinitionFile(src: string, dest: string): Q.Promise<void> { |
| 13 | var fileSystem:FileSystem = new FileSystem(); |
| 14 | if (fileSystem.existsSync(dest)) { |
| 15 | return Q.resolve<void>(void 0); |
| 16 | } |
| 17 | |
| 18 | // Ensure that the parent folder exits; if not, create the hierarchy of directories |
| 19 | let parentFolder = path.resolve(dest, ".."); |
| 20 | if (!fileSystem.existsSync(parentFolder)) { |
| 21 | fileSystem.makeDirectoryRecursiveSync(parentFolder); |
| 22 | } |
| 23 | |
| 24 | return fileSystem.copyFile(src, dest); |
| 25 | } |
| 26 | |
| 27 | /** |
| 28 | * Helper function to get the target path for the type definition files (to be used for intellisense). |
| 29 | * Creates the target path if it does not exist already. |
| 30 | */ |
| 31 | public static getOrCreateTypingsTargetPath(projectRoot: string): string { |
| 32 | let targetPath = path.resolve(projectRoot, ".vscode", "typings"); |
| 33 | let fileSystem:FileSystem = new FileSystem(); |
| 34 | |
| 35 | if (!fileSystem.existsSync(targetPath)) { |
| 36 | fileSystem.makeDirectoryRecursiveSync(targetPath); |
| 37 | } |
| 38 | |
| 39 | return targetPath; |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Helper to install type defintion files for React Native. |
| 44 | * {typingsFolderPath} - the parent folder where the type definitions need to be installed |
| 45 | * {typeDefsPath} - the relative paths of all type definitions that need to be |
| 46 | * installed (relative to <project_root>\.vscode\typings) |
| 47 | */ |
| 48 | public static installTypings(typingsFolderPath: string, typeDefsPath: string[]): Q.Promise<any> { |
| 49 | return Q.all(typeDefsPath.map((relativePath: string): Q.Promise<void> => { |
| 50 | let src = path.resolve(TsdHelper.REACT_TYPINGS_PATH, relativePath); |
| 51 | let dest = path.resolve(typingsFolderPath, relativePath); |
| 52 | |
| 53 | return TsdHelper.installTypeDefinitionFile(src, dest); |
| 54 | })); |
| 55 | } |
| 56 | } |