UNPKG

5.29 kBMarkdownView Raw
1<!--
2#
3# Licensed to the Apache Software Foundation (ASF) under one
4# or more contributor license agreements. See the NOTICE file
5# distributed with this work for additional information
6# regarding copyright ownership. The ASF licenses this file
7# to you under the Apache License, Version 2.0 (the
8# "License"); you may not use this file except in compliance
9# with the License. You may obtain a copy of the License at
10#
11# http://www.apache.org/licenses/LICENSE-2.0
12#
13# Unless required by applicable law or agreed to in writing,
14# software distributed under the License is distributed on an
15# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16# KIND, either express or implied. See the License for the
17# specific language governing permissions and limitations
18# under the License.
19#
20-->
21
22[![Build status](https://ci.appveyor.com/api/projects/status/wxkmo0jalsr8gane?svg=true)](https://ci.appveyor.com/project/ApacheSoftwareFoundation/cordova-common/branch/master)
23[![Build Status](https://travis-ci.org/apache/cordova-common.svg?branch=master)](https://travis-ci.org/apache/cordova-common)
24[![NPM](https://nodei.co/npm/cordova-common.png)](https://nodei.co/npm/cordova-common/)
25
26# cordova-common
27Expoeses shared functionality used by [cordova-lib](https://github.com/apache/cordova-lib/) and Cordova platforms.
28## Exposed APIs
29
30### `events`
31
32Represents special instance of NodeJS EventEmitter which is intended to be used to post events to cordova-lib and cordova-cli
33
34Usage:
35```js
36var events = require('cordova-common').events;
37events.emit('warn', 'Some warning message')
38```
39
40There are the following events supported by cordova-cli: `verbose`, `log`, `info`, `warn`, `error`.
41
42### `CordovaError`
43
44An error class used by Cordova to throw cordova-specific errors. The CordovaError class is inherited from Error, so CordovaError instances is also valid Error instances (`instanceof` check succeeds).
45
46Usage:
47
48```js
49var CordovaError = require('cordova-common').CordovaError;
50throw new CordovaError('Some error message', SOME_ERR_CODE);
51```
52
53See [CordovaError](src/CordovaError/CordovaError.js) for supported error codes.
54
55### `ConfigParser`
56
57Exposes functionality to deal with cordova project `config.xml` files. For ConfigParser API reference check [ConfigParser Readme](src/ConfigParser/README.md).
58
59Usage:
60```js
61var ConfigParser = require('cordova-common').ConfigParser;
62var appConfig = new ConfigParser('path/to/cordova-app/config.xml');
63console.log(appconfig.name() + ':' + appConfig.version());
64```
65
66### `PluginInfoProvider` and `PluginInfo`
67
68`PluginInfo` is a wrapper for cordova plugins' `plugin.xml` files. This class may be instantiated directly or via `PluginInfoProvider`. The difference is that `PluginInfoProvider` caches `PluginInfo` instances based on plugin source directory.
69
70Usage:
71```js
72var PluginInfo: require('cordova-common').PluginInfo;
73var PluginInfoProvider: require('cordova-common').PluginInfoProvider;
74
75// The following instances are equal
76var plugin1 = new PluginInfo('path/to/plugin_directory');
77var plugin2 = new PluginInfoProvider().get('path/to/plugin_directory');
78
79console.log('The plugin ' + plugin1.id + ' has version ' + plugin1.version)
80```
81
82### `ActionStack`
83
84Utility module for dealing with sequential tasks. Provides a set of tasks that are needed to be done and reverts all tasks that are already completed if one of those tasks fail to complete. Used internally by cordova-lib and platform's plugin installation routines.
85
86Usage:
87```js
88var ActionStack = require('cordova-common').ActionStack;
89var stack = new ActionStack()
90
91var action1 = stack.createAction(task1, [<task parameters>], task1_reverter, [<reverter_parameters>]);
92var action2 = stack.createAction(task2, [<task parameters>], task2_reverter, [<reverter_parameters>]);
93
94stack.push(action1);
95stack.push(action2);
96
97stack.process()
98.then(function() {
99 // all actions succeded
100})
101.catch(function(error){
102 // One of actions failed with error
103})
104```
105
106### `superspawn`
107
108Module for spawning child processes with some advanced logic.
109
110Usage:
111```js
112var superspawn = require('cordova-common').superspawn;
113superspawn.spawn('adb', ['devices'])
114.progress(function(data){
115 if (data.stderr)
116 console.error('"adb devices" raised an error: ' + data.stderr);
117})
118.then(function(devices){
119 // Do something...
120})
121```
122
123### `xmlHelpers`
124
125A set of utility methods for dealing with xml files.
126
127Usage:
128```js
129var xml = require('cordova-common').xmlHelpers;
130
131var xmlDoc1 = xml.parseElementtreeSync('some/xml/file');
132var xmlDoc2 = xml.parseElementtreeSync('another/xml/file');
133
134xml.mergeXml(doc1, doc2); // doc2 now contains all the nodes from doc1
135```
136
137### Other APIs
138
139The APIs listed below are also exposed but are intended to be only used internally by cordova plugin installation routines.
140
141```
142PlatformJson
143ConfigChanges
144ConfigKeeper
145ConfigFile
146mungeUtil
147```
148
149## Setup
150* Clone this repository onto your local machine
151 `git clone https://git-wip-us.apache.org/repos/asf/cordova-lib.git`
152* In terminal, navigate to the inner cordova-common directory
153 `cd cordova-lib/cordova-common`
154* Install dependencies and npm-link
155 `npm install && npm link`
156* Navigate to cordova-lib directory and link cordova-common
157 `cd ../cordova-lib && npm link cordova-common && npm install`