yeoman / generator-generator

Generate a Yeoman generator
1.22k stars 237 forks source link

default: this.appname in custom generator, can't get right current directory name #240

Open HJM515 opened 3 years ago

HJM515 commented 3 years ago

yeoman-generator version: 4.12.0 When the current directory name is like my-project, the appname is my project. This is not in my mind. The current directory is empty, has not bower.json or package.json. You know, if use generator to get package.json, the name like "my project" is not valid. If the current directory name is like my_project, I can get right appname. Can we get the right directory name? or give a reason to help me understand what happen?

ENV

yeoman-generator: 4.12.0
node: 12.18.4
yo: 3.1.1

Code of custom generator

const Generator = require("yeoman-generator");
module.exports = class extends Generator {
  prompting() {
    return this.prompt([
      {
        type: "input",
        name: "name",
        message: "Your project name",
        default: this.appname,
      },
    ]).then((answers) => {
      this.answers = answers;
    });
  }
  writing() {
    const tmpl = this.templatePath("package.json");
    const output = this.destinationPath("package.json");
    const context = this.answers;
    this.fs.copyTpl(tmpl, output, context);
  }
};