UNPKG

2.89 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 CordovaCliCreate = function () {
21
22};
23
24/**
25 * provides logic for exposing cordova-lib create functionality to the command line
26 * the create argument is implied from the call to this function, all other cl arguments should be passed in unmodified
27 *
28 * @args -
29 * @undashed
30 */
31CordovaCliCreate.prototype.run = function (args, undashed) {
32 var cfg = {},
33 customWww;
34
35 // parseConfig will determine if there's a valid config JSON string
36 cfg = this.parseConfig(undashed[4]);
37
38 // create(dir, id, name, cfg)
39 cordova.raw.create( undashed[1] // dir to create the project in
40 , undashed[2] // App id
41 , undashed[3] // App name
42 , cfg
43 ).done();
44};
45
46/**
47 * parseConfig
48 * generic parser, if it's valid json, returns the resulting object
49 * if anything resolving to false is passed in, return an empty object
50 * invalid json results in an error message and process exit with status code 2.
51 *
52 * jsondata - a json data string
53 *
54 */
55CordovaCliCreate.prototype.parseConfig = function (jsondata) {
56 if (!jsondata) return {};
57
58 try {
59 cfg = JSON.parse(jsondata);
60 } catch (e) {
61 console.error('Error while parsing json data\nError: '+ e +'\nData:' + jsondata);
62 process.exit(2);
63 }
64};
65
66CordovaCliCreate.prototype.customWww = function (args) {
67 // handle custom www
68 if (customWww = args['copy-from'] || args['link-to']) {
69
70 if (customWww.indexOf(':') != -1) {
71 throw new CordovaError(
72 'Only local paths for custom www assets are supported.'
73 );
74
75 }
76
77 if ( customWww.substr(0,1) === '~' ) { // resolve tilde in a naive way.
78 customWww = path.join(process.env.HOME, customWww.substr(1));
79 }
80
81 customWww = path.resolve(customWww);
82 var wwwCfg = { uri: customWww };
83 if (args['link-to']) {
84 wwwCfg.link = true;
85 }
86
87 cfg.lib = cfg.lib || {};
88 cfg.lib.www = wwwCfg;
89 }
90};
91
92module.exports = new CordovaCliCreate();