如何使用尚未安装但没有错误的节点模块?

时间:2015-10-28 21:54:22

标签: javascript node.js npm

我想准备一个设置脚本我的NodeJS应用程序。

它会:

  • 为用户创建系统角色
  • 为帖子
  • 创建根类别
  • 最后为首次登录创建系统管理员用户

并将其保存到数据库中。

但我还需要脚本同时下载NodeJS依赖项。

setup.js

/*
 * @author: Furkan Başaran <frknbasaran@gmail.com>
 * @date: 29.10.2015
 *
 * Application basic dependencies initalize script
 * */

var sys = require('sys')
var exec = require('child_process').exec;

console.log("Newspaper IO - @FurkanBaşaran");
console.log("Tıme to take off");
console.log("Application Back-End dependencies installing..");
console.log("You may need enter root password in this stage")
exec("sudo npm install");

var mongoose = require('mongoose');
var md5      = require('md5');


var UserSchema = require('./models/User');
var CategorySchema = require('./models/Category');
var RoleSchema = require('./models/Role');



/*
 * Create standart user roles
 * */
console.log("Step 1: Roles creating..");

/*
 * Create Mongoose Model from Schema
 * */
var RoleModel = mongoose.model('Role', RoleSchema);

/*
 * Create Role Instances
 * */
var Author = new RoleModel({"name": "Author"});
var Moderator = new RoleModel({"name": "Moderator"});
var Admin = new RoleModel({"name": "Admin"});

/*
 * Define an array object includes roles
 * */
var Roles = [Author, Moderator, Admin];

/*
 * Save standard roles to database
 * */
Roles.forEach(function (Role) {
    Role.save(function (err, role) {
        if (!err) console.log("Rol created: " + role.name);
        else console.log("Rol couldn't created, error: " + err)
    })
})

/*
 * Create Root Category Object
 * */
console.log("Step 2: Root category creating..")

/*
 * Create Mongoose Model from Schema
 * */
var CategoryModel = mongoose.model('Category', CategorySchema);

/*
 * Create Root Category Object
 * */
var Root = new CategoryModel({"name": "Root"});

/*
 * Save category object into database
 * */
Root.save(function (err, root) {
    if (!err) console.log("Category created successfully:" + root.name)
    else console.log("Category couldn't created, error:" + err);
})

/*
* Create a admin user for first logIn
* */
console.log("Step 3: System Admin user account creating..")

/*
* Create Mongoose Model from Schema
* */
var UserModel = mongoose.model('User', UserSchema);

/*
* Create User with admin privileges
* */
var SystemAdmin = new UserModel({"name":"System Admin", "username":"sysadmin", "email":"admin@willowsoft.com", "password": md5('mysecretpassword'), 'role': Admin});

/*
* Save System Admin user into database
* */
SystemAdmin.save(function(err, sysadmin) {
    if(!err) console.log("System Admin Account created, \nEmail: admin@willowsoft.com\nUsername: sysadmin");
})

console.log("Back-End Installation completed.");
console.log("Application Front-End dependencies initalizing..");
exec("cd public");
console.log("Bower Packages downloading..")
exec("bower install");
exec("gulp");
console.info("You can change application port and database connectionString on config.js file")
exec("cd ..");
console.log("MongoDB running up..");
exec("sudo mkdir database");
exec("sudo mongod --dbpath database");
console.log("Application starting with nodemon");
exec("sudo nodemon server");

运行此脚本的一些可能性没有错误:

  • 我可以在需要之前检查模块,如果存在则需要。 (我之前从未见过这样的事。这甚至可能吗?)
  • 首先下载此脚本中的模块,并让已完成的进程需要另一个文件并使用该文件中的模块。
  • 或者我可以在shell脚本中运行此命令并将NodeJS文件调用到该脚本中。这很容易,但如果可能的话,我想学习艰难的方法。

有任何想法或好方法吗?

2 个答案:

答案 0 :(得分:3)

我整理了一个使用npm为你安装模块的快速脚本

var npm = require('npm');

npm.load(function(err, npm) {
  npm.commands.install(['require'], function(err, data) {
    console.log('dependencies installed');
  })
});

答案 1 :(得分:2)

来自Nodejs docs

exec可以将回调函数作为参数,这意味着您可以执行以下操作:

var sys = require('sys')
var exec = require('child_process').exec;

console.log("Newspaper IO - @FurkanBaşaran");
console.log("Tıme to take off");
console.log("Application Back-End dependencies installing..");
console.log("You may need enter root password in this stage")
exec("sudo npm install", function(error, stdout, stderr) {
    var mongoose = require('mongoose');
    var md5      = require('md5');

    ... // etc
});