dustinspecker/generator-ng-poly

View on GitHub
lib/module/index.js

Summary

Maintainability
A
1 hr
Test Coverage
'use strict';
import 'babel-polyfill';
import fs from 'fs';
import genBase from '../genBase';
import mkdirp from 'mkdirp';
import ngAddDep from 'ng-add-dep';
import path from 'path';
import pify from 'pify';
import utils from '../utils';

module.exports = genBase.extend({
  initialize() {
    this.module = this.name;

    // used by genBase to know this is not a normal component for module-type structures
    // used for creating TypeScript reference path
    this.isModule = true;

    // if moduleName ends with a slash remove it
    if (this.module.charAt(this.module.length - 1) === '/' || this.module.charAt(this.module.length - 1) === '\\') {
      this.module = this.module.slice(0, this.module.length - 1);
    }
  },

  async writing() {
    let filePathToCheck, parentDir, parentModuleName;

    this.context = await this.getConfig();

    // extract the new module name (last part of path)
    this.context.moduleName = path.basename(this.module);

    // modify context because these were all performed on the ENTIRE module path
    // instead of just the new module's name
    this.context.lowerCamel = utils.lowerCamel(this.context.moduleName);
    this.context.hyphenModule = utils.hyphenName(this.context.moduleName);
    this.context.upperModule = utils.upperCamel(this.context.moduleName);
    this.context.templateUrl = path.join(this.module).replace(/\\/g, '/');
    this.context.modulePath = await utils.normalizeModulePath(this.module);

    // create new module directory
    await pify(mkdirp)(path.join(this.context.appDir, this.context.modulePath));

    // check if path and moduleName are the same
    // if yes - get root app.js file to prepare adding dep
    // else - get parent module file to prepare adding dep
    if (this.context.moduleName === this.module) {
      filePathToCheck = path.join(this.context.appDir, 'app');
    } else {
      // go up one driectory from new module's path to retrieve parent's directory
      parentDir = path.join(this.context.appDir, this.context.modulePath, '..');
      // get parent module's name
      parentModuleName = path.basename(parentDir);

      filePathToCheck = path.join(this.context.appDir, this.context.modulePath, '..', parentModuleName);
    }

    // get module path
    const filePath = await utils.findModuleFile(filePathToCheck);

    // if adding dep to app file, then dep is `module
    // else dep is `parent.module`
    const depName = (this.context.parentModuleName ? this.context.parentModuleName + '.' : '') + this.context.lowerCamel;

    // add dep and save modifications
    await pify(fs.writeFile)(filePath, ngAddDep(await pify(fs.readFile)(filePath, 'utf8'), depName));

    // create new module
    const moduleDest = path.join(this.context.appDir, this.context.modulePath, this.context.hyphenModule);
    await this.copySrcFile('module', `${moduleDest}-module.${this.context.appScript}`, this.context);
    if (this.options && !this.options.empty) {
      await this.copySrcFile('module-routes', `${moduleDest}-routes.${this.context.appScript}`, this.context);
    }
  },

  end() {
    if (this.options && !this.options.empty) {
      this.composeWith('ng-poly:route', {
        args: [utils.lowerCamel(this.module.split(/[\/\\]/).pop())],
        options: {
          module: this.module,
          url: '/' + this.context.hyphenModule,
          'template-url': `${this.context.modulePath}/` +
            `${(this.context.structure === 'module-type' ? 'views/' : '')}${this.context.hyphenModule}.tpl.html`,

          markup: this.options.markup,
          'app-script': this.options['app-script'],
          'controller-as': this.options['controller-as'],
          'pass-func': this.options['pass-func'],
          'named-func': this.options['named-func'],
          'test-script': this.options['test-script'],
          'test-dir': this.options['test-dir'],
          'ng-route': this.options['ng-route'],
          style: this.options.style
        }
      }, {
        local: require.resolve('../route'),
        link: 'strong'
      });
    }
  }
});