Example #1
0
var yargs = require('yargs');

var GrassHopper = require('gh-core/lib/api');
var log = require('gh-core/lib/logger').logger('app');

var argv = yargs
    .usage('$0 [--config <path/to/config.js>]')
    .alias('c', 'config')
    .describe('c', 'Specify an alternative config file')
    .default('c', './config.js')
    .argv;

// If a relative path that starts with `./` has been provided,
// we turn it into an absolute path based on the current working directory
if (argv.config.match(/^\.\//)) {
    argv.config = process.cwd() + argv.config.substring(1);
// If a different non-absolute path has been provided, we turn
// it into an absolute path based on the current working directory
} else if (!argv.config.match(/^\//)) {
    argv.config = process.cwd() + '/' + argv.config;
}

var config = require(argv.config);

// Initialize the app server
GrassHopper.init(config, function(err) {
    if (!err) {
        log().info('The server has started. Happy Hopping!');
    }
});
GrassHopper.init(config, function(err) {
    if (err) {
        log().error({'err': err}, 'Failed to spin up the application container');
        process.exit(1);
    }

    // Get a global administrator
    AdminsDAO.getGlobalAdminByUsername('administrator', function(err, globalAdmin) {
        if (err) {
            log().error({'err': err}, 'Failed to get the global administrator');
            process.exit(1);
        }

        // Get the app
        var ctx = new Context(null, globalAdmin);
        AppsAPI.getApp(ctx, argv.app, function(err, app) {
            if (err) {
                log().error({'err': err}, 'Failed to get the provided app');
                process.exit(1);
            }

            ctx = new Context(app, globalAdmin);

            log().info({'input': argv.input}, 'Reading the input file');
            fs.readFile(argv.input, function(err, courses) {
                if (err) {
                    log().error({'err': err, 'input': argv.input}, 'Failed to read the input file');
                    process.exit(1);
                }

                log().info('Parsing courses');
                courses = JSON.parse(courses);

                log().info('Checking whether some courses already exist');
                _checkIfCoursesExist(courses, function() {

                    log().info('Creating courses');
                    _createCourses(ctx, courses.slice(), function() {

                        // Take a deep copy of the courses before we import them. The import
                        // operation might make in-place modifications (such as deleting series)
                        // which would trip up the borrowing process later
                        var copyCourses = _.cloneDeep(courses);
                        log().info('Importing the tree under each course');
                        _importCourses(ctx, courses.slice(), function() {

                            // Export each imported course so we have the ids of any created
                            // organisational units or series. This will allow us to borrow the
                            // correct series under other modules later
                            log().info('Exporting the tree under each course');
                            _exportCourses(ctx, copyCourses.slice(), function() {

                                // Take another pass and take care of borrowed series
                                log().info('Borrowing series where appropriate');
                                _handleBorrowing(ctx, copyCourses, function() {

                                    log().info('All courses have been imported');
                                    process.exit(0);
                                });
                            });
                        });
                    });
                });
            });
        });
    });
});