Friday 19 May 2017

firebase function using npm-cmd-run

I have a firebase project which is setup completely, RealTime Database is setup and working properly. I want to execute a cmd command in windows if a change in realtime database happens. I used npm module 'npm-cmd-run' for this, I used cloud functions of firebase to implement this. npm-cmd-run is working fine if I run the node.js file by local server.But when uploaded it isnt working Below is the code Thanks in advance

'use strict';


const functions = require('firebase-functions');
const nrc=require('node-run-cmd');


const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
exports.run = functions.database.ref('values/volume')

    .onWrite(event => {

      const original = event.data.val();
      console.log('executing', event.params.pushId, original);

      var commands = 'nircmd mutesysvolume 1';
      //the above cmd changes volume of windows


     nrc.run(commands);//run cmd command

    event.data.ref.parent.child('commands').set(commands);
      //write command into db for reference

    });


/*
 exports.helloWorld = functions.https.onRequest((request, response) => {


    var commands = [
        { command: 'nicmd mutesysvolume 1'}

    ];


  nrc.run(commands).then(function(exitCodes) {
   console.log('went in')
    }, function(err) {
      console.log('Command failed to run with error: ', err);
    });



  response.send("Hello from nnav!");

*/



via Prannav

No comments:

Post a Comment