import * as child_process from "child_process" ; import * as fs from "fs" ; import * as http from "http" ; import * as os from "os" ; import { CordovaProjectHelper } from "../src/utils/cordovaProjectHelper" ; export function executeCordovaCommand ( cwd , command ) { return new Promise ( ( resolve , reject ) => { let cordovaCmd = os . platform ( ) === "win32" ? "cordova.cmd" : "cordova" ; let commandToExecute = cordovaCmd + " " + command ; let process = child_process . exec ( commandToExecute , { cwd : cwd } ) ; let stderr = "" ; process . stderr . on ( "data" , ( data ) => { stderr += data . toString ( ) ; } ) ; process . stdout . on ( "data" , ( data ) => { console . log ( data . toString ( ) ) ; } ) ; process . on ( "error" , function ( err ) : void { reject ( err ) ; } ) ; process . on ( "close" , exitCode => { if ( exitCode !== 0 ) { return reject ( `Cordova command failed with exit code ${ exitCode }. Error: ${ stderr }` ) ; } resolve ( { } ) ; } ) ; } ) ; } export function createCordovaProject ( cwd , projectName ) { let cordovaCommandToRun = "create " + projectName ; return executeCordovaCommand ( cwd , cordovaCommandToRun ) ; } export function addCordovaComponents ( componentName , projectRoot , componentsToAdd ) { let cordovaCommandToRun = componentName + " add " + componentsToAdd . join ( " " ) ; return executeCordovaCommand ( projectRoot , cordovaCommandToRun ) ; } export function removeCordovaComponents ( componentName , projectRoot , componentsToRemove ) { let cordovaCommandToRun = componentName + " remove " + componentsToRemove . join ( " " ) ; return executeCordovaCommand ( projectRoot , cordovaCommandToRun ) ; } export function enumerateListOfTypeDefinitions ( projectRoot ) { let typeDefsFolder = CordovaProjectHelper . getCordovaPluginTypeDefsPath ( projectRoot ) ; if ( ! CordovaProjectHelper . existsSync ( typeDefsFolder ) ) { return [ ] ; } else { return fs . readdirSync ( typeDefsFolder ) ; } } export function isUrlReachable ( url ) { return new Promise ( ( resolve , reject ) => { http . get ( url , ( res ) => { resolve ( true ) ; res . resume ( ) ; } ) . on ( "error" , ( _err ) => { resolve ( false ) ; } ) ; } ) ; } export function convertWindowsPathToUnixOne ( path ) { return path . replace ( /\\/g , "/" ) ; }