microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/common/ios/xcodeproj.ts
52lines · modeblame
afc46a73Jimmy Thomson10 years ago | 1 | // Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | // Licensed under the MIT license. See LICENSE file in the project root for details. | |
| 3 | | |
c4a42a32Jimmy Thomson9 years ago | 4 | import * as path from "path"; |
afc46a73Jimmy Thomson10 years ago | 5 | import * as Q from "q"; |
| 6 | | |
db80cd4eJimmy Thomson10 years ago | 7 | import {FileSystem} from "../../common/node/fileSystem"; |
afc46a73Jimmy Thomson10 years ago | 8 | |
c4a42a32Jimmy Thomson9 years ago | 9 | export interface IXcodeProjFile { |
81d6e05bJimmy Thomson9 years ago | 10 | fileName: string; |
| 11 | fileType: string; | |
eec73ab5Jimmy Thomson9 years ago | 12 | projectName: string; |
c4a42a32Jimmy Thomson9 years ago | 13 | } |
| 14 | | |
afc46a73Jimmy Thomson10 years ago | 15 | export class Xcodeproj { |
81226dceJimmy Thomson10 years ago | 16 | private nodeFileSystem: FileSystem; |
| 17 | | |
| 18 | constructor({ | |
cdf34447digeff10 years ago | 19 | nodeFileSystem = new FileSystem(), |
81226dceJimmy Thomson10 years ago | 20 | } = {}) { |
| 21 | this.nodeFileSystem = nodeFileSystem; | |
| 22 | } | |
| 23 | | |
c4a42a32Jimmy Thomson9 years ago | 24 | public findXcodeprojFile(projectRoot: string): Q.Promise<IXcodeProjFile> { |
81226dceJimmy Thomson10 years ago | 25 | return this.nodeFileSystem |
c4a42a32Jimmy Thomson9 years ago | 26 | .readDir(projectRoot) |
| 27 | .then((files: string[]): IXcodeProjFile => { | |
67b4477aNathan Piper9 years ago | 28 | const extensions = [".xcworkspace", ".xcodeproj"]; |
c4a42a32Jimmy Thomson9 years ago | 29 | const sorted = files.sort(); |
67b4477aNathan Piper9 years ago | 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)) | |
c4a42a32Jimmy Thomson9 years ago | 34 | ); |
67b4477aNathan Piper9 years ago | 35 | |
| 36 | if (candidates.length === 0) { | |
c4a42a32Jimmy Thomson9 years ago | 37 | throw new Error("Unable to find any xcodeproj or xcworkspace files."); |
| 38 | } | |
| 39 | | |
67b4477aNathan Piper9 years ago | 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); | |
c4a42a32Jimmy Thomson9 years ago | 45 | return { |
81d6e05bJimmy Thomson9 years ago | 46 | fileName, |
| 47 | fileType, | |
eec73ab5Jimmy Thomson9 years ago | 48 | projectName, |
| 49 | }; | |
afc46a73Jimmy Thomson10 years ago | 50 | }); |
| 51 | } | |
| 52 | } |