rgksugan/generator-boom

View on GitHub
script-base.js

Summary

Maintainability
C
1 day
Test Coverage
'use strict';
var util = require('util');
var path = require('path');
var yeoman = require('yeoman-generator');
var angularUtils = require('./util.js');

var MywebGenerator = module.exports = function MywebGenerator() {
    yeoman.generators.NamedBase.apply(this, arguments);

    try {
        this.appname = require(path.join(process.cwd(), 'bower.json')).name;
    } catch (e) {
        this.appname = path.basename(process.cwd());
    }
    this.appname = this._.slugify(this._.humanize(this.appname));
    this.scriptAppName = this._.camelize(this.appname) + angularUtils.appName(this);

    this.cameledName = this._.camelize(this.name);
    this.classedName = this._.classify(this.name);

    if (typeof this.env.options.appPath === 'undefined') {
        try {
            this.env.options.appPath = require(path.join(process.cwd(), 'bower.json')).appPath;
        } catch (e) {

        }
        this.env.options.appPath = this.env.options.appPath || 'app';
    }

    if (typeof this.env.options.testPath === 'undefined') {
        try {
            this.env.options.testPath = require(path.join(process.cwd(), 'bower.json')).testPath;
        } catch (e) {

        }
        this.env.options.testPath = this.env.options.testPath || 'tests/';
    }

    this.env.options.coffee = this.options.coffee;
    if (typeof this.env.options.coffee === 'undefined') {
        this.option('coffee');

        // attempt to detect if user is using CS or not
        // if cml arg provided, use that; else look for the existence of cs
        if (!this.options.coffee && this.expandFiles(path.join(this.env.options.appPath, '/scripts/**/*.coffee'), {}).length > 0) {
            this.options.coffee = true;
        }

        this.env.options.coffee = this.options.coffee;
    }

    if (typeof this.env.options.minsafe === 'undefined') {
        this.option('minsafe');
        this.env.options.minsafe = this.options.minsafe;
    }

    var sourceRoot = '/templates/javascript';
    this.scriptSuffix = '.js';

    if (this.env.options.coffee) {
        sourceRoot = '/templates/coffeescript';
        this.scriptSuffix = '.coffee';
    }

    if (this.env.options.minsafe) {
        sourceRoot += '-min';
    }

    this.sourceRoot(path.join(__dirname, sourceRoot));
};

util.inherits(MywebGenerator, yeoman.generators.NamedBase);

MywebGenerator.prototype.appTemplate = function (src, dest) {
    yeoman.generators.Base.prototype.template.apply(this, [
        src + this.scriptSuffix,
        path.join(this.env.options.appPath, dest.toLowerCase()) + this.scriptSuffix
    ]);
};

MywebGenerator.prototype.testTemplate = function (src, dest) {
    yeoman.generators.Base.prototype.template.apply(this, [
        src + this.scriptSuffix,
        path.join(this.env.options.testPath, dest.toLowerCase()) + this.scriptSuffix
    ]);
};

MywebGenerator.prototype.htmlTemplate = function (src, dest) {
    yeoman.generators.Base.prototype.template.apply(this, [
        src,
        path.join(this.env.options.appPath, dest.toLowerCase())
    ]);
};

MywebGenerator.prototype.addScriptToIndex = function (script) {
    try {
        var appPath = this.env.options.appPath;
        var fullPath = path.join(appPath, 'index.html');
        angularUtils.rewriteFile({
            file: fullPath,
            needle: '<!-- endbuild -->',
            splicable: [
                '<script src="js/' + script.replace('\\', '/') + '.js"></script>'
            ]
        });
    } catch (e) {
        console.log('\nUnable to find '.yellow + fullPath + '. Reference to '.yellow + script + '.js ' + 'not added.\n'.yellow);
    }
};

MywebGenerator.prototype.generateSourceAndTest = function (appTemplate, testTemplate, targetDirectory, addIndex) {
    // Services use classified names

    // if (this.generatorName.toLowerCase() === 'service') {
    //   this.cameledName = this.classedName;
    // }

    this.appTemplate(appTemplate, path.join('js', targetDirectory, this.name));
    //   this.testTemplate(testTemplate, path.join(targetDirectory, this.name));
    if (addIndex) {
        this.addScriptToIndex(path.join(targetDirectory, this.name));
    }
};

MywebGenerator.prototype.generateTestFile = function (appTemplate, targetDirectory, option) {

    this.testTemplate(appTemplate, path.join(targetDirectory, this.name));
  
    if (option) {

    }
};