angular-fullstack / generator-angular-fullstack

Yeoman generator for an Angular app with an Express server
https://awk34.gitbook.io/generator-angular-fullstack
6.13k stars 1.24k forks source link

Angular-fullstack 5.0.0-rc.3 route subgenerator does not work #2743

Open soccer-coder opened 6 years ago

soccer-coder commented 6 years ago
Item Version
generator-angular-fullstack 5.0.0-rc.3
Node 8.11.2
npm 6.1.0
Operating System MacOs
Item Answer
Transpiler TypeScript
Markup HTML
CSS SCSS / Stylus
Router ngRoute
DB MongoDB

I tried to generate the "route" as below, I ran into an issue:

yo angular-fullstack:route sidenav ? Where would you like to create this route? client/app/ ? What will the url of your route be? sidenav events.js:183 throw er; // Unhandled 'error' event ^

TypeError: Cannot read property 'name' of undefined

I could run "endpoint" without any issue.

Awk34 commented 6 years ago

This was a bug in https://github.com/angular-fullstack/generator-angular-fullstack-component. See https://github.com/angular-fullstack/generator-angular-fullstack-component/commit/b854fdaef805f532e13056b368ed3493c65c5425. This fix was released in 1.0.2. You should be able to reinstall the generator's dependencies or the generator itself for it to get the fixed version.

fcristel commented 5 years ago

Is this still not working? I am running on Windows, on a freshly installed version of the generator (yesterday).

First of all, the generator comes with generator-angular-fullstack-component 1.0.1

And I get this error:

events.js:167
      throw er; // Unhandled 'error' event
      ^

TypeError: Cannot read property 'name' of undefined

If I update the generator-angular-fullstack-component to the 1.0.2, then the error is this one:

events.js:167
      throw er; // Unhandled 'error' event
      ^

Error: Cannot find module 'generator-angular-fullstack-component/generators/route'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:581:15)
    at Function.resolve (internal/modules/cjs/helpers.js:32:19)
    at RouteGenerator.compose (C:\Users\crist\AppData\Roaming\npm\node_modules\generator-angular-fullstack\generators\route\index.js:13:30)
    at Object.<anonymous> (C:\Users\crist\AppData\Roaming\npm\node_modules\generator-angular-fullstack\node_modules\yeoman-generator\lib\index.js:399:25)
    at C:\Users\crist\AppData\Roaming\npm\node_modules\generator-angular-fullstack\node_modules\run-async\index.js:25:25
    at new Promise (<anonymous>)
    at C:\Users\crist\AppData\Roaming\npm\node_modules\generator-angular-fullstack\node_modules\run-async\index.js:24:19
    at self.env.runLoop.add.completed (C:\Users\crist\AppData\Roaming\npm\node_modules\generator-angular-fullstack\node_modules\yeoman-generator\lib\index.js:400:11)
    at runCallback (timers.js:705:18)
    at tryOnImmediate (timers.js:676:5)
    at processImmediate (timers.js:658:5)
Emitted 'error' event at:
    at Immediate.setImmediate (C:\Users\crist\AppData\Roaming\npm\node_modules\generator-angular-fullstack\node_modules\yeoman-generator\lib\index.js:406:18)
    at runCallback (timers.js:705:18)
    at tryOnImmediate (timers.js:676:5)
    at processImmediate (timers.js:658:5)
pschoro commented 4 years ago

Hey guys,

I fixed it with this code :

'use strict';

Object.defineProperty(exports, "__esModule", {
    value: true
});
exports.addModule = addModule;

var _jscodeshift = require('jscodeshift');

var _jscodeshift2 = _interopRequireDefault(_jscodeshift);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

const moduleName = 'Test1Module';
const modulePath = './test0/test1.module';

class NoModulesError extends Error {
    constructor() {
        super(`No NgModules found in app module.
  Are you sure you have the correct path registered in 'appModulePath'?`);
    }
}
class TooManyModulesError extends Error {
    constructor() {
        super(`More than one NgModule found in app module.
  There should be only one.`);
    }
}

/**
 * @param {string} source
 * @param {string} moduleName - ex 'MyModule'
 * @param {string} modulePath - module path relative to appModulePath, ex './thing/my.module'
 */
function addModule(sourceText, moduleName, modulePath) {
    const source = _jscodeshift.withParser('flow')(sourceText);

    // const ngModules = source.find(_jscodeshift2.default.ClassDeclaration, path => path.decorators.some(decorator => decorator.callee.name === 'NgModule'));
    const ngModules = source
        .find(_jscodeshift.ClassDeclaration, path => path.decorators.some(decorator => decorator.expression.callee.name === 'NgModule'));

        if (ngModules.size() === 0) {
        throw new NoModulesError();
    }
    if (ngModules.size() > 1) {
        throw new TooManyModulesError();
    }

    const ngModuleClass = ngModules.get();
    // const ngModule = ngModuleClass.value.decorators.find(decorator => decorator.callee.name === 'NgModule');
    // const imports = ngModule.arguments[0].properties.find(prop => prop.key.name === 'imports');
    const ngModule = ngModuleClass.value.decorators.find(decorator => decorator.expression.callee.name === 'NgModule');
    const imports = ngModule.expression.arguments[0].properties.find(prop => prop.key.name === 'imports');

    if (!imports) {
        console.info('No \'imports\' property? Strange..');
        // TODO: create
    }

    // Push module to `imports` array
    const MyModuleIdentifier = _jscodeshift2.default.identifier(moduleName);
    imports.value.elements.push(MyModuleIdentifier);

    const existingImports = source.find(_jscodeshift.ImportSpecifier);
    if (existingImports.size() === 0) {
        // TODO: Must be using some other module format
    }

    const MyModuleImport = _jscodeshift2.default.importDeclaration([_jscodeshift2.default.importSpecifier(_jscodeshift2.default.identifier(moduleName))], _jscodeshift2.default.literal(modulePath));

    // Insert after last `import {...} from '...'` statement
    (0, _jscodeshift2.default)(existingImports.at(-1).get().parent.insertAfter(MyModuleImport));

    return source.toSource({ quote: 'single' });
}
Awk34 commented 4 years ago

@pschoro would you be willing to make a pull request?

pschoro commented 4 years ago

No need ...it was simply a typo in the variable _jscodeshift...I did not change anything else ^^