generator icon indicating copy to clipboard operation
generator copied to clipboard

Documentation not up-to-date for "Install actions"

Open schorfES opened this issue 3 years ago • 25 comments

Since version 5.0.0 the Install action is deprecated and is not included by default to the generator instance. This is currently not documented/reflected in here:

  • https://yeoman.github.io/generator/Generator.html#installDependencies
  • https://yeoman.io/authoring/dependencies.html

schorfES avatar May 05 '21 09:05 schorfES

How is that best to be solved now? I urgently need an npm install solution. Tonight I have a live stream and wanted to show my viewers Yeoman with TypeScript.

GregorBiswanger avatar May 14 '21 15:05 GregorBiswanger

Okay, I saw that it then installed automatically. It reports that no standard package manager has been stored and that it uses npm. How can you fix a package manager like yarn here?

In principle, can the install method also be viewed as deprecated?

The TypeScript Type Definitions would also have to be adapted. Here npmInstall methods etc. are also offered.

GregorBiswanger avatar May 14 '21 15:05 GregorBiswanger

According to the 5.0.0 changelog there should be a way to access:

this.addDependencies({lit: '2.0.0'}) this.addDevDependencies({mocha: '8.4.0'})

fernandopasik avatar May 14 '21 21:05 fernandopasik

Background about this change: https://github.com/yeoman/generator/issues/1283.

Force yarn using cli: yo --node-package-manager yarn

Force yarn inside a generator: this.env.options.nodePackageManager = yarn;

mshima avatar May 15 '21 14:05 mshima

We have created a generator that is supposed to create a new project directory with package.json. Only npm install will then not be executed automatically. Only if the package.json is created in the current directory where the generator is being executed. We tried this.destinationRoot(this.answers.kataname);

Although it shows during the debug that it has found the package.json, no installation is started.

Please urgently need a solution.

This is the generator: https://github.com/GregorBiswanger/generator-create-kata

GregorBiswanger avatar May 15 '21 15:05 GregorBiswanger

In that case you should set the environment cwd this.env.cwd = this.answers.kataname; too. Or execute yarn using a custom install task: https://github.com/yeoman/generator/blob/84551ee52a5e2fbb1472f34a77c40cb43bdc0391/lib/index.js#L127-L128

Something like:

constructor(args, opts)
  super(args, opts, {customInstallTask: preferredPm => this.spawnCommandSync(`${preferredPm} install`)});
}

Edit: Updated to use preferredPm

mshima avatar May 15 '21 15:05 mshima

We use npm :)

Thanks for your help!

GregorBiswanger avatar May 15 '21 15:05 GregorBiswanger

The TypeScript Type Definitions would also have to be adapted. Here npmInstall methods etc. are also offered.

@GregorBiswanger the definitions were updated a few hours ago https://github.com/DefinitelyTyped/DefinitelyTyped/pull/52666

fernandopasik avatar May 17 '21 08:05 fernandopasik

@mshima Unfortunately the cwd assignment does not work. He then runs npm install from global user profile c:\user\name. Although CWD gets the correct path:

    async prompting() {
        this.answers = await this.prompt(...

        const destinationRoot = this.destinationRoot(_.kebabCase(this.answers.kataname));
        this.env.cwd = destinationRoot;
    }

GregorBiswanger avatar May 17 '21 15:05 GregorBiswanger

Any update on this?

I can't, for the life of me, find a way to configure installDependencies. this.installDependencies always errors out with is not a function.

And also, I just cannot find a way to disable the unchanged package.json message (No change to package.json was detected. No package manager install will be executed.).

gbrlmngr avatar May 20 '21 22:05 gbrlmngr

Any update on this?

I can't, for the life of me, find a way to configure installDependencies. this.installDependencies always errors out with is not a function.

And also, I just cannot find a way to disable the unchanged package.json message (No change to package.json was detected. No package manager install will be executed.).

Downgrading yeoman-generator to version 4 seem to have fixed the issue.

gbrlmngr avatar May 23 '21 12:05 gbrlmngr

@gbrlmngr just do like @mshima suggested, adding this.env.options.nodePackageManager = 'yarn'; to the generator solves the problem

phatpham9 avatar Jun 07 '21 11:06 phatpham9

@gbrlmngr just do like @mshima suggested, adding this.env.options.nodePackageManager = 'yarn'; to the generator solves the problem

@phatpham9: Are you suggesting to specifically use yarn or to set nodePackageManager to some valid value (in my case, npm)?

gbrlmngr avatar Jun 07 '21 11:06 gbrlmngr

@gbrlmngr just do like @mshima suggested, adding this.env.options.nodePackageManager = 'yarn'; to the generator solves the problem

@phatpham9: Are you suggesting to specifically use yarn or to set nodePackageManager to some valid value (in my case, npm)?

yeah if you want to force it to use npm, just replace the value with 'npm'

updated: don't forget to remove the install() {...} and add this.env.options.nodePackageManager = 'npm'; somewhere in your generator and it just works

phatpham9 avatar Jun 07 '21 13:06 phatpham9

@gbrlmngr just do like @mshima suggested, adding this.env.options.nodePackageManager = 'yarn'; to the generator solves the problem

@phatpham9: Are you suggesting to specifically use yarn or to set nodePackageManager to some valid value (in my case, npm)?

yeah if you want to force it to use npm, just replace the value with 'npm'

updated: don't forget to remove the install() {...} and add this.env.options.nodePackageManager = 'npm'; somewhere in your generator and it just works

Hello,

I'm having the same issue like everybody here... I tried your suggestion but when i first run my generator, the message No change to package.json was detected. No package manager install will be executed. still remains. I need the installation to be executed when you first run the generator...if not this is pointless and the decision to remove the "install actions" is more a setback than an improvement.

Is there a way to trigger the installation of the packages manually?

alexandra-c avatar Jun 29 '21 12:06 alexandra-c

Any update on this? I can't, for the life of me, find a way to configure installDependencies. this.installDependencies always errors out with is not a function. And also, I just cannot find a way to disable the unchanged package.json message (No change to package.json was detected. No package manager install will be executed.).

Downgrading yeoman-generator to version 4 seem to have fixed the issue.

Did this to get the old install behaviour back:

const Generator = require('yeoman-generator')
require('lodash').extend(Generator.prototype, require('yeoman-generator/lib/actions/install'))

Hopefully it will also be available in future releases until a more complete version of package.json manipulation will be released.

alexandra-c avatar Jun 29 '21 12:06 alexandra-c

@alexandra-c downgrading to the previous major version worked for me, with everything working as expected.

gbrlmngr avatar Jun 29 '21 18:06 gbrlmngr

An additional information. package.json detection and install is done at conflicts priority, before install and end. So addDependencies must be called before that. I would need a minimal reproduction step.

mshima avatar Jul 05 '21 21:07 mshima

import { exec } from 'child_process'; ... const execAsync = util.promisify(exec); .... public async install(){ try { await execAsync(cd ${projectPath} && npm i); } catch (error) { throw Messages.INSTALLATION_FAILED_MESSAGE; } }

bgdopus avatar Jul 07 '21 07:07 bgdopus

Now that we are missing the cwd option yeoman is adding dependencies in the wrong folder. I tried changing directories using process.chdir but that doesn’t help either. Here is the reference https://github.com/axelerant/kashmir/pull/125/files#diff-fb3c51194730e282793310751066bf26defbbee94fc60924bbde9d954f646e6fL116

skippednote avatar Jul 27 '21 14:07 skippednote

I would need a minimal reproduction step.

@mshima This is not quite minimal, but this branch of my generator has the problem: https://github.com/jwalton/node-jwalton-generator-typescript/tree/upgrade-yeoman

In my case, if you try to create a module with the same name as the current folder, I write it to the current folder, and it does the npm install as expected. If you try to create a module with a different name, I create a subfolder and then this.destinationRoot() to update the root folder:

    default() {
        if (path.basename(this.destinationPath()) !== this.props.githubProjectName) {
            this.log(`Creating folder ${this.props.githubProjectName}.`);
            mkdirp(this.props.githubProjectName);
            this.destinationRoot(this.destinationPath(this.props.githubProjectName));
        } else {
            this.log(`Not creating folder ${this.props.githubProjectName}.`);
        }
    }

And in this case I get the No change to package.json was detected. Seems like the check for package.json should be getting package.json from this.destinationRoot() instead of from this.cwd?

jwalton avatar Aug 10 '21 21:08 jwalton

I agree with @alexandra-c - this seems like an arbitrary set back to the api. But even worse - is the only way to figure out how things are working anymore is to read the actual code... Even the JSDoc is wrong/outdated.

@mshima - while your work is appreciated, and I know documentation is pain...this is a heavily depended upon package and changes affect a lot of people.

Do you think you could at least possibly find the time to update the JSDoc with all the code and API changes so that users aren't completely left hanging out to dry?

EDIT: Upon further looking and digging, it is documented in the changelog- for anyone else wondering.

spence-s avatar Aug 24 '21 15:08 spence-s

I ran into this today while trying to install packages where I need to pass options. It's unclear, with the automatic install, how to set options. Specifically, in my case, I want to omit some peer dependencies using npm 7 and need to pass a flag at install to --omit the specific dependencies I do not want.

To give an example of what I mean.

Package A:

{
  "peerDependencies": {
    "b": "^1.0.0",
    "c": "^1.0.0"
  }
}

Packages B and C are separate tools that invoke package A but package A never actually uses them. The user can choose to either bring in package B or C to execute package A - but they do not need both. With npm 7 dependency resolution changes so that peerDependencies that are not satisfied are brought in by default. This wouldn't be an issue except package B and C are very large and due to size limitations we can only fit B or C but not both.

Automatic install is fine. But it would be nice to have a way documented to set extra options during the npm install for this case and any others that need install flags / args.

mneil avatar Jan 07 '22 16:01 mneil

But it would be nice to have a way documented to set extra options during the npm install for this case and any others that need install flags / args.

Npm is executed once and you can customize it. See https://github.com/yeoman/generator/issues/1294#issuecomment-841679391

mshima avatar Jan 08 '22 11:01 mshima

Stitching bits and pieces of this thread together I was able to create a workaround:

  1. Set the customInstallTask flag when constructing the generator super(args, opts, { customInstallTask: true });
  2. Change the directory before installing dependencies
async install() {
  this.log.info('Installing yarn packages');
  await exec(`yarn install --cwd ${this.destinationPath()}`);
}

Overall though I agree with the sentiment that this is a behavioral regression. Being able to manage packages programmatically through yeoman is a potentially nice feature, but we shouldn't be forced to do it. If I just want to list all of the dependencies in the template file it shouldn't be hard to just install them once.

noahw3 avatar Jan 22 '22 00:01 noahw3