CB-11653 copied findProjectRoot from cordova-lib

 This closes #474


Project: http://git-wip-us.apache.org/repos/asf/cordova-lib/repo
Commit: http://git-wip-us.apache.org/repos/asf/cordova-lib/commit/a429358e
Tree: http://git-wip-us.apache.org/repos/asf/cordova-lib/tree/a429358e
Diff: http://git-wip-us.apache.org/repos/asf/cordova-lib/diff/a429358e

Branch: refs/heads/common-1.4.x
Commit: a429358eb76fef6597c35d4fdf69df7db79af31c
Parents: 3e1a5cd
Author: carynbear <caryn.t...@berkeley.edu>
Authored: Wed Jul 20 16:16:49 2016 -0700
Committer: Steve Gill <stevengil...@gmail.com>
Committed: Wed Aug 3 09:55:36 2016 -0700

----------------------------------------------------------------------
 cordova-common/cordova-common.js         |   1 +
 cordova-common/spec/CordovaCheck.spec.js | 104 ++++++++++++++++++++++++++
 cordova-common/src/CordovaCheck.js       |  76 +++++++++++++++++++
 3 files changed, 181 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-lib/blob/a429358e/cordova-common/cordova-common.js
----------------------------------------------------------------------
diff --git a/cordova-common/cordova-common.js b/cordova-common/cordova-common.js
index dcaf7a4..801d510 100644
--- a/cordova-common/cordova-common.js
+++ b/cordova-common/cordova-common.js
@@ -27,6 +27,7 @@ addProperty(module, 'superspawn', './src/superspawn');
 addProperty(module, 'ActionStack', './src/ActionStack');
 addProperty(module, 'CordovaError', './src/CordovaError/CordovaError');
 addProperty(module, 'CordovaLogger', './src/CordovaLogger');
+addProperty(module, 'CordovaCheck', './src/CordovaCheck');
 addProperty(module, 'CordovaExternalToolErrorContext', 
'./src/CordovaError/CordovaExternalToolErrorContext');
 addProperty(module, 'PlatformJson', './src/PlatformJson');
 addProperty(module, 'ConfigParser', './src/ConfigParser/ConfigParser');

http://git-wip-us.apache.org/repos/asf/cordova-lib/blob/a429358e/cordova-common/spec/CordovaCheck.spec.js
----------------------------------------------------------------------
diff --git a/cordova-common/spec/CordovaCheck.spec.js 
b/cordova-common/spec/CordovaCheck.spec.js
new file mode 100644
index 0000000..a1f37fa
--- /dev/null
+++ b/cordova-common/spec/CordovaCheck.spec.js
@@ -0,0 +1,104 @@
+/**
+    Licensed to the Apache Software Foundation (ASF) under one
+    or more contributor license agreements.  See the NOTICE file
+    distributed with this work for additional information
+    regarding copyright ownership.  The ASF licenses this file
+    to you under the Apache License, Version 2.0 (the
+    "License"); you may not use this file except in compliance
+    with the License.  You may obtain a copy of the License at
+
+    http://www.apache.org/licenses/LICENSE-2.0
+
+    Unless required by applicable law or agreed to in writing,
+    software distributed under the License is distributed on an
+    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+    KIND, either express or implied.  See the License for the
+    specific language governing permissions and limitations
+    under the License.
+*/
+
+var shell = require('shelljs'),
+    path = require('path'),
+    CordovaCheck = require('../src/CordovaCheck');
+
+var cwd = process.cwd();
+var home = process.env[(process.platform == 'win32') ? 'USERPROFILE' : 'HOME'];
+var origPWD = process.env.PWD;
+
+describe('findProjectRoot method', function() {
+    afterEach(function() {
+        process.env.PWD = origPWD;
+        process.chdir(cwd);
+    });
+    it('should return false if it hits the home directory', function() {
+        var somedir = path.join(home, 'somedir');
+        this.after(function() {
+            shell.rm('-rf', somedir);
+        });
+        shell.mkdir(somedir);
+        expect(CordovaCheck.findProjectRoot(somedir)).toEqual(false);
+    });
+    it('should return false if it cannot find a .cordova directory up the 
directory tree', function() {
+        var somedir = path.join(home, '..');
+        expect(CordovaCheck.findProjectRoot(somedir)).toEqual(false);
+    });
+    it('should return the first directory it finds with a .cordova folder in 
it', function() {
+        var somedir = path.join(home,'somedir');
+        var anotherdir = path.join(somedir, 'anotherdir');
+        this.after(function() {
+            shell.rm('-rf', somedir);
+        });
+        shell.mkdir('-p', anotherdir);
+        shell.mkdir('-p', path.join(somedir, 'www', 'config.xml'));
+        expect(CordovaCheck.findProjectRoot(somedir)).toEqual(somedir);
+    });
+    it('should ignore PWD when its undefined', function() {
+        delete process.env.PWD;
+        var somedir = path.join(home,'somedir');
+        var anotherdir = path.join(somedir, 'anotherdir');
+        this.after(function() {
+            shell.rm('-rf', somedir);
+        });
+        shell.mkdir('-p', anotherdir);
+        shell.mkdir('-p', path.join(somedir, 'www'));
+        shell.mkdir('-p', path.join(somedir, 'config.xml'));
+        process.chdir(anotherdir);
+        expect(CordovaCheck.findProjectRoot()).toEqual(somedir);
+    });
+    it('should use PWD when available', function() {
+        var somedir = path.join(home,'somedir');
+        var anotherdir = path.join(somedir, 'anotherdir');
+        this.after(function() {
+            shell.rm('-rf', somedir);
+        });
+        shell.mkdir('-p', anotherdir);
+        shell.mkdir('-p', path.join(somedir, 'www', 'config.xml'));
+        process.env.PWD = anotherdir;
+        process.chdir(path.sep);
+        expect(CordovaCheck.findProjectRoot()).toEqual(somedir);
+    });
+    it('should use cwd as a fallback when PWD is not a cordova dir', 
function() {
+        var somedir = path.join(home,'somedir');
+        var anotherdir = path.join(somedir, 'anotherdir');
+        this.after(function() {
+            shell.rm('-rf', somedir);
+        });
+        shell.mkdir('-p', anotherdir);
+        shell.mkdir('-p', path.join(somedir, 'www', 'config.xml'));
+        process.env.PWD = path.sep;
+        process.chdir(anotherdir);
+        expect(CordovaCheck.findProjectRoot()).toEqual(somedir);
+    });
+    it('should ignore platform www/config.xml', function() {
+        var somedir = path.join(home,'somedir');
+        var anotherdir = path.join(somedir, 'anotherdir');
+        this.after(function() {
+            shell.rm('-rf', somedir);
+        });
+        shell.mkdir('-p', anotherdir);
+        shell.mkdir('-p', path.join(anotherdir, 'www', 'config.xml'));
+        shell.mkdir('-p', path.join(somedir, 'www'));
+        shell.mkdir('-p', path.join(somedir, 'config.xml'));
+        expect(CordovaCheck.findProjectRoot(anotherdir)).toEqual(somedir);
+    });
+});
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/cordova-lib/blob/a429358e/cordova-common/src/CordovaCheck.js
----------------------------------------------------------------------
diff --git a/cordova-common/src/CordovaCheck.js 
b/cordova-common/src/CordovaCheck.js
new file mode 100644
index 0000000..46e733f
--- /dev/null
+++ b/cordova-common/src/CordovaCheck.js
@@ -0,0 +1,76 @@
+/**
+    Licensed to the Apache Software Foundation (ASF) under one
+    or more contributor license agreements.  See the NOTICE file
+    distributed with this work for additional information
+    regarding copyright ownership.  The ASF licenses this file
+    to you under the Apache License, Version 2.0 (the
+    "License"); you may not use this file except in compliance
+    with the License.  You may obtain a copy of the License at
+
+    http://www.apache.org/licenses/LICENSE-2.0
+
+    Unless required by applicable law or agreed to in writing,
+    software distributed under the License is distributed on an
+    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+    KIND, either express or implied.  See the License for the
+    specific language governing permissions and limitations
+    under the License.
+*/
+
+var fs = require('fs'),
+    path = require('path');
+
+function isRootDir(dir) {
+    if (fs.existsSync(path.join(dir, 'www'))) {
+        if (fs.existsSync(path.join(dir, 'config.xml'))) {
+            // For sure is.
+            if (fs.existsSync(path.join(dir, 'platforms'))) {
+                return 2;
+            } else {
+                return 1;
+            }
+        }
+        // Might be (or may be under platforms/).
+        if (fs.existsSync(path.join(dir, 'www', 'config.xml'))) {
+            return 1;
+        }
+    }
+    return 0;
+}
+
+// Runs up the directory chain looking for a .cordova directory.
+// IF it is found we are in a Cordova project.
+// Omit argument to use CWD.
+function isCordova(dir) {
+    if (!dir) {
+        // Prefer PWD over cwd so that symlinked dirs within your PWD work 
correctly (CB-5687).
+        var pwd = process.env.PWD;
+        var cwd = process.cwd();
+        if (pwd && pwd != cwd && pwd != 'undefined') {
+            return isCordova(pwd) || isCordova(cwd);
+        }
+        return isCordova(cwd);
+    }
+    var bestReturnValueSoFar = false;
+    for (var i = 0; i < 1000; ++i) {
+        var result = isRootDir(dir);
+        if (result === 2) {
+            return dir;
+        }
+        if (result === 1) {
+            bestReturnValueSoFar = dir;
+        }
+        var parentDir = path.normalize(path.join(dir, '..'));
+        // Detect fs root.
+        if (parentDir == dir) {
+            return bestReturnValueSoFar;
+        }
+        dir = parentDir;
+    }
+    console.error('Hit an unhandled case in CordovaCheck.isCordova');
+    return false;
+}
+
+module.exports = {
+    findProjectRoot : isCordova
+};


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@cordova.apache.org
For additional commands, e-mail: commits-h...@cordova.apache.org

Reply via email to