我想准备一个设置脚本我的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");
运行此脚本的一些可能性没有错误:
有任何想法或好方法吗?
答案 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
});