7

I want to create a cli to create admin users I have the user model setup in api/models/User.js

and in cli on

var User, program;

program = require("commander");

User = require("../api/models/User");

program.version("0.0.1");

program.command("create_user").description("Create a user into database").action(function() {
  return console.log(User);
});

program.parse(process.argv);

User log is:

    User = {
      attributes: {
        username: "string",
        password: "string",
      }
    };

and no waterline methods available to use.

Travis Webb
  • 14,688
  • 7
  • 55
  • 109
carlitux
  • 1,217
  • 10
  • 14

2 Answers2

5

You can use sails run <command>.

Simply create <appPath>/commands/index.js with this content:

module.exports = {
    testcommand: function (done) {
        // .. your code
        console.log('this is my testcommand');
        done();
    }
}

now you can run sails run testcommand

Edy
  • 101
  • 3
  • 6
  • Nice, which versions supports this? – carlitux Oct 31 '13 at 01:16
  • In sails 0.10.0 `sails run` was removed. – Edy Mar 05 '14 at 16:17
  • @Edy Was `sails run` removed entirely, or replaced with something similar? – InternalFX Apr 19 '14 at 18:49
  • @InternalFX it was removed in 0.10 entirely. there is an open issue to bring `sails run` back: https://github.com/balderdashy/sails/issues/1421 – Edy Apr 19 '14 at 21:55
  • sails is not going to add this.. as there is a 'sails console' and a 'sails load' which can be used alternatively to do something similar. This here details the reason: http://stackoverflow.com/questions/24123090/using-waterline-model-outside-sailsjs-api –  May 03 '15 at 17:20
2

If any one needs this:

sails = require("sails");
sails.lift({
  log: {
    level: 'silent'
  }
}, function() {
  User.create(obj);
  process.stdin.destroy();
});
carlitux
  • 1,217
  • 10
  • 14