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 |
|
20 | var fs = require('fs'),
|
21 | path = require('path');
|
22 |
|
23 | // Some helpful utility stuff copied from cordova-lib. This is a bit nicer than taking a dependency on cordova-lib just
|
24 | // to get this minimal stuff. Hopefully we won't need the platform stuff (finding platform www_dir) once it is moved
|
25 | // into the actual platform.
|
26 |
|
27 | var platforms = {
|
28 | amazon_fireos: {www_dir: 'assets/www'},
|
29 | android: {www_dir: 'assets/www'},
|
30 | blackberry10: {www_dir: 'www'},
|
31 | browser: {www_dir: 'www'},
|
32 | firefoxos: {www_dir: 'www'},
|
33 | ios: {www_dir: 'www'},
|
34 | ubuntu: {www_dir: 'www'},
|
35 | windows: {www_dir: 'www'},
|
36 | wp8: {www_dir: 'www'}
|
37 | };
|
38 |
|
39 | /**
|
40 | * @desc Look for a Cordova project's root directory, starting at the specified directory (or CWD if none specified).
|
41 | * @param {string=} dir - the directory to start from (we check this directory then work up), or CWD if none specified.
|
42 | * @returns {string} - the Cordova project's root directory, or null if not found.
|
43 | */
|
44 | function cordovaProjectRoot(dir) {
|
45 | if (!dir) {
|
46 | // Prefer PWD over cwd so that symlinked dirs within your PWD work correctly.
|
47 | var pwd = process.env.PWD;
|
48 | var cwd = process.cwd();
|
49 | if (pwd && pwd != cwd && pwd != 'undefined') {
|
50 | return cordovaProjectRoot(pwd) || cordovaProjectRoot(cwd);
|
51 | }
|
52 | return cordovaProjectRoot(cwd);
|
53 | }
|
54 |
|
55 | var bestReturnValueSoFar = null;
|
56 | for (var i = 0; i < 1000; ++i) {
|
57 | var result = isRootDir(dir);
|
58 | if (result === 2) {
|
59 | return dir;
|
60 | }
|
61 | if (result === 1) {
|
62 | bestReturnValueSoFar = dir;
|
63 | }
|
64 | var parentDir = path.normalize(path.join(dir, '..'));
|
65 | // Detect fs root.
|
66 | if (parentDir == dir) {
|
67 | return bestReturnValueSoFar;
|
68 | }
|
69 | dir = parentDir;
|
70 | }
|
71 | return null;
|
72 | }
|
73 |
|
74 | function getPlatformWwwRoot(cordovaProjectRoot, platformName) {
|
75 | var platform = platforms[platformName];
|
76 | if (!platform) {
|
77 | throw new Error ('Unrecognized platform: ' + platformName);
|
78 | }
|
79 | return path.join(cordovaProjectRoot, 'platforms', platformName, platform.www_dir);
|
80 | }
|
81 |
|
82 | function isRootDir(dir) {
|
83 | if (fs.existsSync(path.join(dir, 'www'))) {
|
84 | if (fs.existsSync(path.join(dir, 'config.xml'))) {
|
85 | // For sure is.
|
86 | if (fs.existsSync(path.join(dir, 'platforms'))) {
|
87 | return 2;
|
88 | } else {
|
89 | return 1;
|
90 | }
|
91 | }
|
92 | // Might be (or may be under platforms/).
|
93 | if (fs.existsSync(path.join(dir, 'www', 'config.xml'))) {
|
94 | return 1;
|
95 | }
|
96 | }
|
97 | return 0;
|
98 | }
|
99 |
|
100 | module.exports = {
|
101 | cordovaProjectRoot: cordovaProjectRoot,
|
102 | getPlatformWwwRoot: getPlatformWwwRoot,
|
103 | platforms: platforms
|
104 | };
|