UNPKG

2.47 kBJavaScriptView Raw
1/**
2 Licensed to the Apache Software Foundation (ASF) under one
3 or more contributor license agreements. See the NOTICE file
4 distributed with this work for additional information
5 regarding copyright ownership. The ASF licenses this file
6 to you under the Apache License, Version 2.0 (the
7 "License"); you may not use this file except in compliance
8 with the License. You may obtain a copy of the License at
9
10 http://www.apache.org/licenses/LICENSE-2.0
11
12 Unless required by applicable law or agreed to in writing,
13 software distributed under the License is distributed on an
14 "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 KIND, either express or implied. See the License for the
16 specific language governing permissions and limitations
17 under the License.
18*/
19
20var fs = require('fs');
21var path = require('path');
22
23function isRootDir (dir) {
24 if (fs.existsSync(path.join(dir, 'www'))) {
25 if (fs.existsSync(path.join(dir, 'config.xml'))) {
26 // For sure is.
27 if (fs.existsSync(path.join(dir, 'platforms'))) {
28 return 2;
29 } else {
30 return 1;
31 }
32 }
33 // Might be (or may be under platforms/).
34 if (fs.existsSync(path.join(dir, 'www', 'config.xml'))) {
35 return 1;
36 }
37 }
38 return 0;
39}
40
41// Runs up the directory chain looking for a .cordova directory.
42// IF it is found we are in a Cordova project.
43// Omit argument to use CWD.
44function isCordova (dir) {
45 if (!dir) {
46 // Prefer PWD over cwd so that symlinked dirs within your PWD work correctly (CB-5687).
47 var pwd = process.env.PWD;
48 var cwd = process.cwd();
49 if (pwd && pwd !== cwd && pwd !== 'undefined') {
50 return isCordova(pwd) || isCordova(cwd);
51 }
52 return isCordova(cwd);
53 }
54 var bestReturnValueSoFar = false;
55 for (var i = 0; i < 1000; ++i) {
56 var result = isRootDir(dir);
57 if (result === 2) {
58 return dir;
59 }
60 if (result === 1) {
61 bestReturnValueSoFar = dir;
62 }
63 var parentDir = path.normalize(path.join(dir, '..'));
64 // Detect fs root.
65 if (parentDir === dir) {
66 return bestReturnValueSoFar;
67 }
68 dir = parentDir;
69 }
70 console.error('Hit an unhandled case in CordovaCheck.isCordova');
71 return false;
72}
73
74module.exports = {
75 findProjectRoot: isCordova
76};