<commit_before>/*
 * Copyright (c) 2011-2013, Yahoo! Inc.  All rights reserved.
 * Copyrights licensed under the New BSD License.
 * See the accompanying LICENSE file for terms.
 */
'use strict';

var path = require('path'),
    log = require('./lib/log'),
    util = require('./lib/utils'),
    create = require('./lib/create'),

    // todo: enable user configuration
    SRCPATHS = ['.', path.resolve(__dirname, 'archetypes')];


function error(msg, exit_code) {
    var err = new Error(msg);
    err.errno = exit_code;
    return err;
}

function errorWithUsage(msg, exit_code) {
    var err = error(msg, exit_code);
    err.usage = module.exports.usage;
    return err;
}

function pathify(subpath) {
    return util.findInPaths(SRCPATHS, subpath);
}

function subtypePath(type, args) {
    var subtype = args.length > 1 ? args.shift() : 'default';
    return path.join(type, subtype).toLowerCase(); // i.e. 'app/full'
}

function getSourceDir(type, args) {
    var source, err;

    // get archetype source directory based on first one OR two arguments
    switch (type.toLowerCase()) {
    case 'app':
    case 'mojit':
        // 1. mojito create [options] <app|mojit> [full|simple|default] <name>
        source = pathify(subtypePath(type, args));
        err = 'Invalid subtype.';
        break;

    case 'custom':
        // 2. mojito create [options] custom <path/to/archetype> <name>
        source = pathify(args.shift());
        err = 'Custom archtype path is invalid.';
        break;

    default:
        // 3. mojito create [options] <path/to/archetype> <name>
        source = pathify(type);
        err = type + ' is not a valid archetype or path.';
    }

    return source || errorWithUsage(err, 5);
}

function amMissingArgs(type, args) {
    var err;

    if (!type) {
        err = 'No parameters.';

    } else if (!args.length) {
        err = 'Missing subtype, name or path.';
    }

    return err && errorWithUsage(err, 3);
}

function main(args, opts, meta, cb) {
    var type = args.shift() || '',
        source = amMissingArgs(type, args) || getSourceDir(type, args),
        name = args.shift(),
        keyval = util.parseCsvObj(opts.keyval),
        destination;

    if (source instanceof Error) {
        cb(source);

    } else if (!name) {
        cb(errorWithUsage('Missing name.', 3));

    } else {
        keyval.name = <PASSWORD>;
        keyval.port = opts.port || 8666;
        destination = path.join(opts.directory || '.', name);

        create(source, destination, keyval, cb);
    }
}

exports = main;

exports.usage = [
    'Usage: mojito create [options] <app|mojit> [full|simple|default] <name>',
    'Usage: mojito create [options] custom <path/to/archetype> <name>',
    'Usage: mojito create [options] <path/to/archetype> <name>',
    '',
    'Example: mojito create app Foo',
    '  (creates directory "Foo" containing a new Mojito application named "Foo")',
    '',<commit_msg>handle mojit special case in getDestinationDir()<commit_after>/*
 * Copyright (c) 2011-2013, Yahoo! Inc.  All rights reserved.
 * Copyrights licensed under the New BSD License.
 * See the accompanying LICENSE file for terms.
 */
'use strict';

var path = require('path'),
    log = require('./lib/log'),
    util = require('./lib/utils'),
    create = require('./lib/create'),

    // todo: enable user configuration
    SRCPATHS = ['.', path.resolve(__dirname, 'archetypes')];


function error(msg, exit_code) {
    var err = new Error(msg);
    err.errno = exit_code;
    return err;
}

function errorWithUsage(msg, exit_code) {
    var err = error(msg, exit_code);
    err.usage = module.exports.usage;
    return err;
}

function pathify(subpath) {
    return util.findInPaths(SRCPATHS, subpath);
}

function getDestinationDir(type, dest, name) {
    var parts = [dest || '.'];

    // BC - type "mojit" goes inside mojits dir
    if ('mojit' === type.toLowerCase()) {
    	parts.push('mojits');
    }

    parts.push(name);
    return path.join.apply(null, parts);
}

function subtypePath(type, args) {
    var subtype = args.length > 1 ? args.shift() : 'default';
    return path.join(type, subtype).toLowerCase(); // i.e. 'app/full'
}

function getSourceDir(type, args) {
    var source, err;

    // get archetype source directory based on first one OR two arguments
    switch (type.toLowerCase()) {
    case 'app':
    case 'mojit':
        // 1. mojito create [options] <app|mojit> [full|simple|default] <name>
        source = pathify(subtypePath(type, args));
        err = 'Invalid subtype.';
        break;

    case 'custom':
        // 2. mojito create [options] custom <path/to/archetype> <name>
        source = pathify(args.shift());
        err = 'Custom archtype path is invalid.';
        break;

    default:
        // 3. mojito create [options] <path/to/archetype> <name>
        source = pathify(type);
        err = type + ' is not a valid archetype or path.';
    }

    return source || errorWithUsage(err, 5);
}

function amMissingArgs(type, args) {
    var err;

    if (!type) {
        err = 'No parameters.';

    } else if (!args.length) {
        err = 'Missing subtype, name or path.';
    }

    return err && errorWithUsage(err, 3);
}

function main(args, opts, meta, cb) {
    var type = args.shift() || '',
        source = amMissingArgs(type, args) || getSourceDir(type, args),
        name = args.shift(),
        keyval = util.parseCsvObj(opts.keyval),
        destination;

    if (source instanceof Error) {
        cb(source);

    } else if (!name) {
        cb(errorWithUsage('Missing name.', 3));

    } else {
        keyval.name = name;
        keyval.port = opts.port || 8666;
        destination = getDestinationDir(type, opts.directory, name);

        create(source, destination, keyval, cb);
    }
}

exports = main;

exports.usage = [
    'Usage: mojito create [options] <app|mojit> [full|simple|default] <name>',
    'Usage: mojito create [options] custom <path/to/archetype> <name>',
    'Usage: mojito create [options] <path/to/archetype> <name>',
    '',
    'Example: mojito create app Foo',
    '  (creates directory "Foo" containing a new Mojito application named "Foo")',
    '',
{'fraction_non_alphanumeric': 0.1465977048650396, 'fraction_numerical': 0.0061419104574107, 'mean_word_length': 2.7457627118644066, 'pattern_counts': {'":': 0, '<': 28, '<?xml version=': 0, '>': 30, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6280412', 'n_tokens_mistral': 2029, 'n_tokens_neox': 1968, 'n_words': 552}