Created
February 17, 2016 08:05
-
-
Save phynet/245cb06a81de248376fd to your computer and use it in GitHub Desktop.
Script to compile cordova project (need a packgae.json and a gulp file)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var path = require('path'); | |
var exec = require('child_process').exec; | |
var spawn = require('child_process').spawn; | |
var versioniOS = '@4.0.1'; | |
var versionAndroid = '@5.0.0'; | |
function logError(messageError, messageSpinner){ | |
console.log(messageError); | |
cli.spinner(messageSpinner, true); | |
reject(error); | |
} | |
function createCordovaProject(filePath, name, platforms, cordova){ | |
return new Promise((resolve, reject) => { | |
var pathFile = path.resolve(__dirname,filePath,name); | |
var command = 'mkdir '+filePath+' && cd '+filePath+' && '+ cordova + ' create '+name+' com.bbva.cells.plugins.'+name+' '+name; | |
console.log('creating project '+ 'in path: '+pathFile); | |
return exec(command, (error,stdout,stderr) =>{ | |
if (error !== null) { | |
var messageError = 'exec error creating cordova project: ' + error + ' if \'cordova\' command not find, please refer to the correct cordova symlink in options.'; | |
var messageSpinner = 'Compiling done..!'; | |
return logError(messageError,messageSpinner); | |
}else{ | |
createPlatform(filePath, name, platforms, cordova); | |
resolve(); | |
} | |
}); | |
}); | |
}; | |
function createPlatform(filePath, name, platforms, cordova){ | |
return new Promise((resolve, reject) => { | |
var pathFile = path.resolve(__dirname,filePath,name); | |
console.log('CREATE PLATFORM: '+platforms + ' filePath ' + filePath); | |
if (platforms == 'ios'){ | |
platforms = platforms + versioniOS; | |
}else if (platforms == 'android'){ | |
platforms = platforms + versionAndroid; | |
}else{ | |
platforms = 'ios'+versioniOS+' android'+versionAndroid; | |
} | |
var command = 'cd '+pathFile+' && '+ cordova + ' platform add '+ platforms; | |
console.log('creating platforms '+platforms+ ' in path: '+pathFile); | |
cli.spinner('Installing...'); | |
var child = exec(command, (error,stdout,stderr) =>{ | |
if (error !== null) { | |
var messageError = 'error in createPlatform ' + error + ' stdout '+stdout + ' stderr '+stderr; | |
var messageSpinner = 'Error creating platforms :('; | |
return logError(messageError,messageSpinner); | |
}else{ | |
cli.spinner('installation complete..!', true); | |
startGulpProcess(filePath,name,cordova); | |
resolve(); | |
} | |
}); | |
}); | |
}; | |
function downloadGulpPlugins(filePath,name,cordova){ | |
return new Promise((resolve, reject) => { | |
var finalPathProj = filePath; | |
var pathFile = path.resolve(__dirname,filePath,'..'); | |
console.log(filePath); | |
var command = 'cd '+pathFile+' npm install gulp && npm install gulp-cordova'; | |
cli.spinner('installing gulp...'); | |
var child = exec(command,{maxBuffer: 1024 * 500}, (error,stdout,stderr) =>{ | |
console.log(stdout); | |
if (error !== null) { | |
var messageError = 'exec error in downloadGulpPlugins: ' + error; | |
var messageSpinner = 'Error dowloading gulp package... :_('; | |
return logError(messageError,messageSpinner); | |
}else{ | |
cli.spinner('gulp installed..!', true); | |
startGulpProcess(filePath,name, cordova); | |
resolve(); | |
} | |
}); | |
}); | |
}; | |
function startGulpProcess(filePath,name, cordova){ | |
return new Promise((resolve, reject) => { | |
var pathFile = path.resolve(__dirname,filePath,name); | |
console.log('installing plugins from package.json...wait till ends downloading.'); | |
var command = 'cd '+pathFile+' && gulp cordova:init'; | |
cli.spinner('Installing plugin..'); | |
var child = exec(command,{maxBuffer: 1024 * 500}, (error,stdout,stderr) =>{ | |
console.log(stdout); | |
if (error !== null) { | |
var messageError = 'exec error in startGulpProcess: ' + error; | |
var messageSpinner = 'Error installing plugins...'; | |
return logError(messageError,messageSpinner); | |
}else{ | |
cli.spinner('Plugin installed, compilation done..!', true); | |
listAllPlugins(filePath,name, cordova); | |
resolve(); | |
} | |
}); | |
}); | |
}; | |
function cordovaBuild(filePath,name, cordova){ | |
return new Promise((resolve, reject) => { | |
var pathFile = path.resolve(__dirname,filePath,name); | |
var command = 'cd '+pathFile + ' && ' +cordova+' build'; | |
console.log('compiling... ' + command); | |
cli.spinner('Compiling...'); | |
var child = exec(command, (error,stdout,stderr) =>{ | |
console.log(stdout); | |
console.log(stderr); | |
if (error !== null) { | |
var messageError = 'exec error in cordovaBuild: ' + error + ' stdout ' + stdout + ' stderr '+stderr; | |
var messageSpinner = 'Error building cordova...'; | |
return logError(messageError,messageSpinner); | |
}else{ | |
cli.spinner('Cordova Build done..', true); | |
listAllPlugins(filePath,name, cordova); | |
resolve(); | |
} | |
}); | |
}); | |
}; | |
function listAllPlugins(filePath,name, cordova){ | |
return new Promise((resolve, reject) => { | |
var pathFile = path.resolve(__dirname,filePath,name); | |
if (cordova == undefined) cordova = 'cordova'; | |
var command = 'cd '+pathFile+' && '+cordova+' plugin list'; | |
cli.spinner('Searching all plugins installed...'); | |
var child = exec(command, (error,stdout,stderr) =>{ | |
console.log('\n'+stdout); | |
console.log('\n'+stderr); | |
if (error !== null) { | |
var messageError = 'exec error in listAllPlugins: ' + error + ' stdout ' + stdout + ' stderr '+stderr; | |
var messageSpinner = 'Error building cordova...'; | |
return logError(messageError,messageSpinner); | |
}else{ | |
cli.spinner('Plugin installed done', true); | |
console.log("END PROCESS"); | |
resolve(); | |
} | |
}); | |
}); | |
} | |
var finalPath = ""; | |
var finalName = ""; | |
function startProcess(file, projectName, platforms, cordova){ | |
pathProject = file || 'project'; | |
name = projectName || 'test'; | |
finalName = name; | |
platforms = platforms || 'ios android'; | |
finalPath = pathProject; | |
cordova = cordova || 'cordova'; | |
createCordovaProject(pathProject, name, platforms, cordova) | |
.catch(error => { | |
console.error('error', error); | |
process.exit(-1); | |
}); | |
}; | |
/** | |
* | |
* Add new platform when -a option is used | |
* | |
*/ | |
function startProcessAddingNewPlatform(params){ | |
params = params || {}; | |
params.platforms = params.platforms || 'ios android'; | |
params.file = params.file || 'project'; | |
params.project = params.project || 'test'; | |
params.cordova = params.cordova || 'cordova'; | |
pathProject = params.file; | |
finalPath = pathProject; | |
name = params.project; | |
finalName = name; | |
cordova = params.cordova; | |
createPlatform(pathProject, name, params.platforms, cordova) | |
.catch(error => { | |
console.error('error', error); | |
process.exit(-1); | |
}); | |
}; | |
/** | |
* | |
* Script Begins | |
* | |
*/ | |
//optional carpeta contenedora --f | |
//optional nombre fichero --n | |
//optional platforma --p | |
var cli = require('cli'); | |
var options = cli.parse({ | |
file: ['f', 'container name', 'string', 'project'], | |
project: ['n', 'project name', 'string', 'test'], | |
platforms: ['p', 'platform value', 'string', 'ios android'], | |
cordova: ['c', 'cordova value', 'string', 'cordova'], | |
addPlatform: ['a', 'add new platform', 'string', '-'], | |
update: ['u', 'update plugins', 'bool', false], | |
build: ['b', 'build platforms', 'bool', false], | |
list: ['l','list platforms', 'bool', false] | |
}); | |
platforms = (options.platforms != 'ios' && options.platforms != 'android' && options.platforms != 'ios android') ? console.log('Error: enter a valid platform') : options.platforms; | |
if (options.addPlatform != '-'){ | |
var objParam = { | |
platforms: options.addPlatform, | |
file: options.file, | |
project: options.project, | |
cordova: options.cordova | |
}; | |
startProcessAddingNewPlatform(objParam); | |
} | |
if (options.update && !options.build){ | |
//file and project name else is default | |
console.log('Updating plugins using file name: '+ options.file + ' and project name: '+ options.project); | |
startGulpProcess(options.file,options.project, options.cordova) | |
.catch(error => { | |
console.error('error', error); | |
process.exit(-1); | |
}); | |
} | |
if (options.build && !options.list){ | |
cordovaBuild(options.file, options.project, options.cordova) | |
.catch(error => { | |
console.error('error', error); | |
process.exit(-1); | |
}); | |
} | |
if (options.list && !options.build){ | |
listAllPlugins(options.file, options.project, options.cordova) | |
.catch(error => { | |
console.error('error', error); | |
process.exit(-1); | |
}); | |
} | |
if (platforms && options.addPlatform == '-' && !options.update && !options.build && !options.list){ | |
console.log('options.platforms ' + options.platforms); | |
startProcess(options.file, options.project, options.platforms, options.cordova); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment