Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The generator doesn't provides a constructor and option value doesn't pass from environment to generator #1749

Open
helxsz opened this issue Feb 25, 2021 · 0 comments

Comments

@helxsz
Copy link

helxsz commented Feb 25, 2021

I am using yeoman to build a simple generator.
my yeoman version is as followed.

    "yeoman-environment": "^3.0.0-rc.1",
    "yeoman-generator": "^5.0.0",

the following code will initialise the generator and pass the option value to it.

var yeoman = require('yeoman-environment');
var env = yeoman.createEnv();
env.register(require.resolve('./generators/index.js'), 'express');
env.run("express", {
    entityName: 'entity',
    entityFields: {'abc':1,'cde':2},
});

generator code

    module.exports = class extends Generator {
      constructor(args, opts) {
          super(args, opts);
          console.log('constructor  ',args, opts);
      }
      initializing() {
          const companyName = this.options.companyName; // shows undefined 
          const entityName = this.options.entityName;   // shows undefined 
      }
    }

however running the generator shows two problems .

  1. issue 1
(node:47412) UnhandledPromiseRejectionWarning: TypeError: The generator doesn't provides a constructor.
    at Environment._findGeneratorClass (/Users/xisizhe/Documents/projects/server/modules/app-generator/server/controllers/node_modules/_yeoman-environment@3.0.1@yeoman-environment/lib/environment.js:641:13)
    at Environment.get (/Users/xisizhe/Documents/projects/server/modules/app-generator/server/controllers/node_modules/_yeoman-environment@3.0.1@yeoman-environment/lib/environment.js:598:17)
    at Environment.create (/Users/xisizhe/Documents/projects/server/modules/app-generator/server/controllers/node_modules/_yeoman-environment@3.0.1@yeoman-environment/lib/environment.js:678:35)
    at instantiateAndRun (/Users/xisizhe/Documents/projects/server/modules/app-generator/server/controllers/node_modules/_yeoman-environment@3.0.1@yeoman-environment/lib/environment.js:851:30)
    at Environment.run (/Users/xisizhe/Documents/projects/server/modules/app-generator/server/controllers/node_modules/_yeoman-environment@3.0.1@yeoman-environment/lib/environment.js:873:12)
  1. issue 2

the option value didn't pass from environment to generator.

constructor(args, opts) {
super(args, opts);
console.log('constructor ',args, opts);
}

args are []

and opts are

{ sharedData: {},
  forwardErrorToEnvironment: false,
  skipLocalCache: true,
  initialGenerator: true,
  env:
   Environment {
     domain: null,
     _events: [Object: null prototype] {},
     _eventsCount: 0,
     _maxListeners: 100,
     options: {},
     adapter:
      TerminalAdapter {
        promptModule: [Function],
        console: [Console],
        log: [Function],
        tracker: [EventEmitter] },
     cwd: '/Users/asf/Documents/projects/server',
     logCwd: '/Users/asf/Documents/projects/server',
     store:
      Store {
        _generators: [Object],
        _meta: [Object],
        _packagesPaths: {},
        _packagesNS: [Array] },
     command: undefined,
     runLoop:
      Queue {
        queueNames: [Array],
        __queues__: [Object],
        runOnAdd: false,
        _maxListeners: 0 },
     sharedFs:
      Store {
        domain: null,
        _events: [Object],
        _eventsCount: 1,
        _maxListeners: 0 },
     fs: EditionInterface { store: [Store] },
     lookups: [ '.', 'generators', 'lib/generators', 'dist/generators' ],
     aliases: [ [Object] ],
     sharedOptions:
      { sharedData: {},
        forwardErrorToEnvironment: false,
        skipLocalCache: true },
     repository:
      YeomanRepository {
        log: [Function],
        tracker: [EventEmitter],
        _repositoryPath: '/Users/asf/Documents/projects/server/.yo-repository',
        _nodeModulesPath:
         '/Users/asf/Documents/projects/server/.yo-repository/node_modules' },
     _generators: {} },
  resolved:
   '/Users/asf/Documents/projects/server/modules/app-generator/server/controllers/generators/app/index.js',
  namespace: 'express',
  _: [],
  help: undefined,
  h: undefined,
  'skip-cache': false,
  'skip-install': false,
  'force-install': false,
  'ask-answered': false }

I don't know why, I have the constructor function in the code and this constructor function did execute to print out

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant