microsoft/vscode-react-native
Publicmirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable
src/common/ios/xcodeproj.ts
52lines · 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 | |
| 7 | import {FileSystem} from "../../common/node/fileSystem"; |
| 8 | |
| 9 | export interface IXcodeProjFile { |
| 10 | fileName: string; |
| 11 | fileType: string; |
| 12 | projectName: string; |
| 13 | } |
| 14 | |
| 15 | export class Xcodeproj { |
| 16 | private nodeFileSystem: FileSystem; |
| 17 | |
| 18 | constructor({ |
| 19 | nodeFileSystem = new FileSystem(), |
| 20 | } = {}) { |
| 21 | this.nodeFileSystem = nodeFileSystem; |
| 22 | } |
| 23 | |
| 24 | public findXcodeprojFile(projectRoot: string): Q.Promise<IXcodeProjFile> { |
| 25 | return this.nodeFileSystem |
| 26 | .readDir(projectRoot) |
| 27 | .then((files: string[]): IXcodeProjFile => { |
| 28 | const extensions = [".xcworkspace", ".xcodeproj"]; |
| 29 | const sorted = files.sort(); |
| 30 | const candidates = sorted.filter((file: string) => |
| 31 | extensions.indexOf(path.extname(file)) !== -1 |
| 32 | ).sort((a, b) => |
| 33 | extensions.indexOf(path.extname(a)) - extensions.indexOf(path.extname(b)) |
| 34 | ); |
| 35 | |
| 36 | if (candidates.length === 0) { |
| 37 | throw new Error("Unable to find any xcodeproj or xcworkspace files."); |
| 38 | } |
| 39 | |
| 40 | const bestCandidate = candidates[0]; |
| 41 | |
| 42 | const fileName = path.join(projectRoot, bestCandidate); |
| 43 | const fileType = path.extname(bestCandidate); |
| 44 | const projectName = path.basename(bestCandidate, fileType); |
| 45 | return { |
| 46 | fileName, |
| 47 | fileType, |
| 48 | projectName, |
| 49 | }; |
| 50 | }); |
| 51 | } |
| 52 | } |