C0ZEN
C0ZEN

Reputation: 934

How to get the current path of the running Yeoman generator

I have my own Yeoman generator.
I created a sub-generator to create a new view folder.

Basically, the usage is:

This view sub-generator prompt a folder name.

For example:

If I want to create the view authentication on the default views directory.

cd views
yo my-generator:view

The result should be:

views //- Already created by the main generator
├── authentication
│   ├── authentication.controller.js
│   ├── authentication.template.html

Now, if I want to create a sub-view login for the authentication view.

cd views/authentication
yo my-generator:view

The result should be:

views //- Already created by the main generator
├── authentication
│   ├── authentication.controller.js
│   ├── authentication.template.html
│   ├── login
│   │   ├── login.controller.js
│   │   ├── login.template.html

Instead, the current (wrong) result is:

views //- Already created by the main generator
├── authentication
│   ├── authentication.controller.js
│   ├── authentication.template.html
├── login
│   ├── login.controller.js
│   ├── login.template.html

My struggle here is that I don't know how to get the current path when I run the command.

Actually, I just create the new folder with a default prefix path which is app/views/.
This is why Authentication example works.

However when my current path is deeper in the views folder, it will add the new folder at the root of the views folder.
If I could get the current path (of the cmd), I should be able to add this path as the prefix instead of setting a default and not static one.
This is why Login example doesn't works.

Some code example:

I use a .txt file as template and then create the controller.js file.

const filePrefix = 'app/views/' + $that.viewNameCamel + '/' + $that.viewNameCamel + '.';

const exampleData = {
   controllerAlias: 'vm',
   otherVar: 'example'
};

$that.fs.copyTpl(
   $that.templatePath('controller.txt'), 
   filePrefix + 'controller.js', 
   exampleData
);

Tried:

Similar:

So guys, do you have a clue on how do I get the current folder path ?
Is there an alternative solution here ?

Thanks !

EDIT 1:


The problem here is the .yo-rc.json present on the root directory of the projet.
The file rewrite the path so I should delete it to fix the problem.
However if I delete this file, the user configuration will no longer be saved.
And I need it for later sub-generator usage.

Is there another way to save the user configuration ?
Or once again, is there another way to get the current real path ?

Upvotes: 1

Views: 1809

Answers (2)

Mo3ty
Mo3ty

Reputation: 11

I know this is kinda old but for any one who comes later, from the documentation https://yeoman.io/authoring/file-system.html

If you want to know from where the user is running yo, then you can get the path with this.contextRoot. This is the raw path where yo was invoked from; before we determine the project root with .yo-rc.json.

from my experience removing the .yo-rc.json would mean that you need to manually check if the path provided is ok, having a fixed root point is something helpful.

Upvotes: 1

Nandu Kalidindi
Nandu Kalidindi

Reputation: 6280

Just remove the .yo-rc.json file from your root directory. This is the file that is responsible for locating your root irrespective of where you are in the file system.

I am not sure about the repercussions of removing it but nothing seem to have happened to the generators I built.

Now you can use process.cwd() and it will get the correct working directory.

For your use case to have a prefix app/views, you probably need to write some Javascript to append or not append the prefix based on where you are, which should be trivial.

Upvotes: 0

Related Questions