-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
108 lines (101 loc) · 4.18 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
var fs = require('fs'),
path = require('path'),
EventEmitter = require('events').EventEmitter,
Q = require('q'),
shell = require('shelljs'),
getProjectRoot = require('cordova-common').CordovaCheck.findProjectRoot,
ConfigParser = require('cordova-common').ConfigParser;
/**
* Given a PhoneGap project, pin Cordova as a dependency. Installation is not verified if dependency already exists.
*
* 1) Verify that projectDir is a valid project; get root
* 2) Create package.json from config.xml if it does not exist
* 3) If cordova is not a dependency, use npm to install and save it to package.json
*
*
* @param {Path[String]} projectDir [Path to anywhere within the PhoneGap project being modified]
* @param {EventEmitter|False} extEvents [Used for events emitting. If undefined, 'log' and 'warn' will log to console. If false, will silence logging] (optional)
* @return {Promise|Error} [Returns the project root path or a cordova error.]
*/
module.exports.exec = function (projectDir, extEvents) {
extEvents = extEvents ? extEvents : ConsoleEmitter(extEvents);
var PROJECTROOT;
return Q()
.then(function(){
var project = getProjectRoot(projectDir);
if (!project) {
return Q.reject(new Error('"'+projectDir+'" ' + 'does not point to a valid PhoneGap project'));
} else {
PROJECTROOT = project;
return project;
}
}).then(function(projectRoot){
shell.cd(projectRoot);
var config,
pkgJson,
pkgJsonPath = path.resolve(projectRoot, 'package.json');
if (!fs.existsSync(pkgJsonPath)) {
extEvents.emit('warn', 'No package.json was found for your project. Creating one from config.xml');
config = new ConfigParser(path.resolve(projectRoot, 'config.xml'));
pkgJson = {};
pkgJson.name = (config.name() || path.basename(projectRoot)).toLowerCase().replace(' ','');
pkgJson.version = config.version() || '1.0.0';
fs.writeFileSync(pkgJsonPath, JSON.stringify(pkgJson, null, 4), 'utf8');
return pkgJsonPath;
} else {
return pkgJsonPath;
}
}).then(function(pkgJsonPath){
pkgJson = './' + path.relative(__dirname, pkgJsonPath);
pkgJson = require(pkgJson);
//Does not have cordova dependency in package.json
if (!pkgJson.dependencies || !pkgJson.dependencies.hasOwnProperty('cordova')) {
if(!shell.which('npm')) {
return Q.reject(new Error('"npm" command line tool is not installed: make sure it is accessible on your PATH.'));
}
var deferred = Q.defer();
// Find the latest version of Cordova published and install it
var cordovaCommand= 'npm install cordova --save';
extEvents.emit('log', 'Adding Cordova dependency with: "' + cordovaCommand +'"');
/* OR Find the system version of Cordova installed //To/Do: handle dev version case
* var cordovaVersionCommand = 'cordova -v'
* var cordovaVersion = ... use shell.exec
* var cordovaCommand = 'npm install --save cordova@'+ cordovaVersion
*/
var child = shell.exec(cordovaCommand, {silent:true}, function(code, stdout, stderr) {
if (code != 0) {
e = new Error('Error from npm: '+ stdout.replace('\n',''));
e.exitCode = code;
deferred.reject(e);
} else {
//ToDo: @carynbear progress bar is needed to show npm status
extEvents.emit('verbose', 'Project is using Cordova '+ stdout.replace('\n',''))
deferred.resolve(stdout.replace('\n',''));
}
});
child.stdout.on('data', function(data) {
extEvents.emit('raw', data.toString('utf8').replace('\n',''));
});
child.stderr.on('data', function(data) {
extEvents.emit('raw', data.toString('utf8').replace('\n',''));
});
return deferred.promise;
} else {
extEvents.emit('verbose', 'Found Cordova dependency in package.json');
}
}).then(function(){
return PROJECTROOT;
});
}
function ConsoleEmitter(arg) {
if (arg != false) {
var emitter = new EventEmitter();
emitter.on('log', function(msg){
console.log('[LOG]: '+ msg);
})
emitter.on('warn', function(msg){
console.log('[WARN]: '+ msg);
})
}
return emitter;
}