(Solved) Meteor: method call to start the shell (clone git repo)

Problem:

In Meteor, how can I call a method from the client (passing name), the server executes some shell commands?

The function of the method is basically: create a directory and then clone the git repo with the given name.

This is very simple material, but the Meteor just won't do it. I drove around for hours. everything works in normal bash or node. Per minute:

Directory is created

-> the server restarts → the meteorite gives an error stating that the directory already exists → meteor deletes the directory → the server restarts

the code:

var cmd, exec, fs;

if (Meteor.isClient) {
  Template.app.events({
    'click button': function() {
        Meteor.call('clone', "NAMEHERE", function(error, result) {
          if (error) {
            console.log(error);
          } else {
            console.log(result);
          }
        });
    }
  });
}

if (Meteor.isServer) {
  fs = Npm.require('fs');
  exec = Npm.require('child_process').exec;
  cmd = Meteor.wrapAsync(exec);
  Meteor.methods({
    'clone': function(name) {
      var dir;
      dir = process.env.PWD + "/projects/" + name;
      cmd("mkdir " + dir + "; git clone git@gitlab.com:username/" + name + ".git " + dir, Meteor.bindEnvironment(function(error, stdout, stderr) {
        if (error) {
          throw new Meteor.Error('error...');
        } else {
          console.log('done');
        }
      }));
      return 'cloning...';
    }
  });
}

update 1

The following code successfully clones the repo if I create the folder manually in advance:

if (Meteor.isClient) {
  Template.all.events({
    'click button': function() {
      Meteor.call('clone', this.name);
    }
  });
}


if (Meteor.isServer) {
  exec = Npm.require('child_process').exec;
  cmd = Meteor.wrapAsync(exec);
  Meteor.methods({
    'clone': function(name) {
      var dir, res;
      dir = process.env.PWD + "/projects/" + name;
      res = cmd(git clone git@gitlab.com:username/" + name + ".git " + dir);
      return res;
    }
  });
}

However, if I add "mkdir " + dirin cmd, I will still have the same problem:

Directory is created

-> the server restarts → the meteorite gives an error stating that the directory already exists → meteor deletes the directory → the server restarts

Decision:

, - (projects). . .

update 1/@Rebolon answer.

+4
2

Meteor.call , , , : wrapAsync exec npm, cmd var: :

var res = cmd("mkdir " + dir + "; git clone git@gitlab.com:username/" + name + ".git " + dir);
return res;

, :

  • ,

, wrapAync cmd (...) , , "..." , "...".

, , ? , , (//ok/error...), , .

, , exec ( retry) : https://github.com/MeteorLyon/satis-easy

+1

Meteor.call . - :

if (Meteor.isClient) {
  Template.app.events({
    'click button': function() {
        Meteor.call('clone', "NAMEHERE", function(error, result) {
          if (error) {
            return console.log(error);
          } else {
            return console.log(result);
          }
        });
    }
  });
}
0

All Articles