Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added cli commands support

  • Loading branch information...
commit 0db5aa7d8d63349c56d90b983f95605439801aec 1 parent 1713f76
@shkuropat shkuropat authored
View
74 cli/migration.js
@@ -0,0 +1,74 @@
+"use strict";
+
+
+/*global nodeca*/
+
+// nodeca
+var NLib = require('nlib');
+
+var Async = NLib.Vendor.Async;
+
+module.exports.commandName ="migrate";
+
+module.exports.parserParameters= {
+ addHelp:true,
+ description: 'Run migrations',
+ help: 'A help'
+};
+
+
+module.exports.commandLineArguments = [];
+
+/**
+ * run(args, callback)-> void
+ * - args (array): parsed arguments
+ * - callback(function): callback function
+ *
+ * args example:
+ * { command_name: 'migration', foo: 'baz', bar: '1' }
+ *
+ **/
+module.exports.run = function (args, callback) {
+ Async.series([
+ require('../lib/init/mongoose'),
+ NLib.init.loadModels,
+ NLib.init.loadSharedApiSubtree
+ ], function (err) {
+ if (err){
+ console.log(err);
+ process.exit(1);
+ }
+
+ var migration_model = nodeca.models.core.migration;
+ var migrator = nodeca.runtime.migrator;
+
+ // fetch used migrations from db
+ migration_model.getLastState(function(err, last_state){
+ // find new migrations
+ migrator.checkMigrations(last_state, function(err, new_migrations){
+ Async.forEachSeries(new_migrations, function(migration, next_migration){
+ migrator.runMigration(migration, function(err){
+ if (err){
+ next_migration(err);
+ return;
+ }
+
+ // All ok. Write step to db
+ migration_model.markPassed( migration.app_name, migration.step, function(err){
+ if (!err){
+ nodeca.logger.log(migration.app_name + ': ' + migration.step +' successfully migrated');
+ }
+ next_migration(err);
+ });
+ });
+ }, function(err) {
+ if (err) {
+ nodeca.logger.log(err);
+ process.exit(1);
+ }
+ process.exit(0);
+ });
+ });
+ });
+ });
+};
View
132 cli/seed.js
@@ -0,0 +1,132 @@
+"use strict";
+
+
+/*global nodeca*/
+
+var Path = require('path');
+
+// nodeca
+var NLib = require('nlib');
+
+var Async = NLib.Vendor.Async;
+var FsTools = NLib.Vendor.FsTools;
+
+var SEEDS_DIR = 'db/seeds';
+
+function get_app_path(app_name){
+ for (var i=0; i < nodeca.runtime.apps.length; i++) {
+ if (app_name === nodeca.runtime.apps[i].name){
+ return nodeca.runtime.apps[i].root;
+ }
+ }
+ return null;
+}
+
+module.exports.parserParameters= {
+ addHelp:true,
+ description: 'That will run `.<app_name>/db/seeds/<seed_name>.js` if exists. ' +
+ 'Or, all seeds from `./db/seeds/seed-name/` folder. If <seed-name>' +
+ 'missed, then script will show all available seeds for given app. ' +
+ 'If `-a` missed, then all seed for all apps will be shown.',
+ epilog: 'Note: Loading seeds is limited to development/test enviroment.' +
+ 'If you really need to run seed on production/stageing, use ' +
+ 'option -f.',
+ help: 'A help'
+};
+
+module.exports.commandLineArguments = [
+ {
+ args: ['-f'],
+ options: {
+ help:'run without env checking',
+ action: 'storeTrue'
+ }
+ },
+ {
+ args: ['-a', '--app'],
+ options: {
+ help: 'application name',
+ type: 'string'}
+ },
+ {
+ args: ['seed_name'],
+ options: {
+ metaver: 'SEED_NAME',
+ help:'seed name',
+ nargs:'?',
+ defaultValue:null
+ }
+ },
+];
+
+/**
+ * run(callback, args)-> void
+ * - callback(function): callback function
+ * - args (array): parsed arguments
+ *
+ * args example:
+ * { command_name: 'migration', foo: 'baz', bar: '1' }
+ *
+ **/
+module.exports.run = function (args, callback) {
+ var app_name = args.app;
+ var seed_name = args.seed;
+ Async.series([
+ require('../lib/init/mongoose'),
+ NLib.init.loadModels,
+ NLib.init.loadSharedApiSubtree
+ ], function (err) {
+ if (err){
+ console.log(err);
+ process.exit(1);
+ }
+
+ // execute seed
+ if (!!app_name && !!seed_name){
+ var env = nodeca.runtime.env;
+ if ('development' !== env && 'testing' !== env && !args.force) {
+ console.log('Error: Can\'t run seed from ' + env + ' enviroment. Please, use -f to force.');
+ process.exit(1);
+ }
+
+ var seed_path = Path.join(get_app_path(app_name), SEEDS_DIR, seed_name);
+ if (!Path.existsSync(seed_path)){
+ console.log('Error: Application "' + app_name + '"does not have "' + seed_name);
+ process.exit(1);
+ }
+
+ require(seed_path)(function(err){
+ if (err) {
+ nodeca.logger.log(err);
+ process.exit(1);
+ }
+ process.exit(0);
+ });
+ }
+ else {
+ var apps;
+ if (app_name) {
+ apps = [{name: app_name, root: get_app_path(app_name)}];
+ }
+ else {
+ apps = nodeca.runtime.apps;
+ }
+
+ console.log('Avaliable seeds:');
+
+ Async.forEachSeries(apps, function(app, next_app){
+ var seed_dir = Path.join(app.root, SEEDS_DIR);
+ FsTools.walk(seed_dir, /w*\.js$/, function(file, stats, next_file) {
+ console.log(' ' + app.name + ':' + Path.basename(file));
+ next_file();
+ }, next_app);
+ }, function(err){
+ if (err) {
+ nodeca.logger.log(err);
+ process.exit(1);
+ }
+ process.exit(0);
+ });
+ }
+ });
+};
View
41 cli/server.js
@@ -0,0 +1,41 @@
+"use strict";
+
+
+/*global nodeca*/
+
+// nodeca
+var NLib = require('nlib');
+
+var Async = NLib.Vendor.Async;
+
+module.exports.parserParameters= {
+ version: nodeca.runtime.version,
+ addHelp:true,
+ help: 'controls nodeca server',
+ description: 'Controls nodeca server ...'
+};
+
+module.exports.commandLineArguments = [];
+
+module.exports.run = function (args, callback) {
+ Async.series([
+ require('../lib/init/redis'),
+ require('../lib/init/mongoose'),
+
+ NLib.init.loadModels,
+ NLib.init.loadServerApiSubtree,
+ NLib.init.loadSharedApiSubtree,
+ NLib.init.loadClientApiSubtree,
+ NLib.init.loadSettings,
+ NLib.init.initTranslations,
+ NLib.init.buildBundles,
+
+ require('../lib/init/http_assets'),
+
+ NLib.init.initRouter,
+
+ require('../lib/init/migrations_check'),
+ require('../lib/init/http_server'),
+ require('../lib/init/filters')
+ ], callback);
+};
View
10 index.js
@@ -16,13 +16,3 @@ module.exports = NLib.Application.create({
callback();
}
});
-
-
-nodeca.hooks.init.before('init-start', require('./lib/init/redis'));
-nodeca.hooks.init.before('init-start', require('./lib/init/mongoose'));
-
-nodeca.hooks.init.after('bundles', require('./lib/init/http_assets'));
-
-nodeca.hooks.init.after('init-complete', require('./lib/init/migrations_check'));
-nodeca.hooks.init.after('init-complete', require('./lib/init/http_server'));
-nodeca.hooks.init.after('init-complete', require('./lib/init/filters'));
View
2  lib/init/migrations_check.js
@@ -16,7 +16,7 @@ module.exports = function (next) {
nodeca.runtime.migrator.checkMigrations(last_state, function(err, new_migrations){
if (!err && new_migrations.length > 0){
err = "Can't start: database changed. Please, run migration tool:" +
- "\n ./bin/db-migrate";
+ "\n ./nodeca.js migrate";
}
next(err);
Please sign in to comment.
Something went wrong with that request. Please try again.