1

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

Basically, the usage is:

  • open a new terminal
  • cd into the parent folder
  • run the yeoman command yo my-generator:view
  • follow the instructions

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:

  • $that is the current generator object
  • $that.viewNameCamel is the name of the folder set by the user

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:

  • $that.env.cwd
  • process.cwd()
  • __dirname
  • path.js library

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 ?

C0ZEN
  • 894
  • 11
  • 41

2 Answers2

1

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.

Mo3ty
  • 11
  • 4
0

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.

Nandu Kalidindi
  • 6,075
  • 1
  • 23
  • 36
  • Thanks for the answer nevertheless I need to keep this file alive. The data in it are from another generator and I need them for the view generator (like app name, current language, author, ...). I should be able to access these data at any time (eventually, I could save these data in a new custom file, I will try some stuff). – C0ZEN Dec 16 '17 at 22:29
  • Hmm, if you are willing to create a `.yo-rc.json` with empty config in every directory then you can achieve this but that would be an ugly directory structure. Or may be there is a way to create the file, recognise the working directory and then delete the file? Too far fetched but worth a try I guess. – Nandu Kalidindi Dec 16 '17 at 22:43
  • If simply I could simple delete the `.yo-rc.json` file to get the real path, I will find the solution to have the correct path, no worry about it. Then, I must change all the referances to `x.config.set(...)` and `x.config.get(...)` to avoid creating and using the file. The alternative is could be to use https://www.npmjs.com/package/edit-json-file which is pretty likewise. Then I will have my own file and the stuff will be pretty much the same. Even if this solution sounds possible to me, that seems a little bit overkill to just work around the path finding. – C0ZEN Dec 16 '17 at 23:36
  • **Test 1 :** you are right, removing the `.yo-rc.json` just fix the wrong path (with $that.env.cwd and process.cwd()). – C0ZEN Dec 16 '17 at 23:40
  • **Test 2 :** removing `'app/views/' + ` from the filePrefix plus the **Test 1** stuff make sub-generator *view* works like charm. The final step is now to replace the whole native config by my own. – C0ZEN Dec 17 '17 at 09:53
  • **Test 3 :** `edit-json-file` actually edit the files in the npm generator package... I can't access the actual generated project folder or I just don't know how but I just read the full yo doc and nothing... – C0ZEN Dec 17 '17 at 10:23
  • Well, why not add a different json file like `config.json` and parse it for config information, only downside is you won't be able to use the Yeoman API for config but that is a fair compromise I think. – Nandu Kalidindi Dec 18 '17 at 05:32
  • I don't want to throw away the `.yo-rc.json` file because it helps me to work with the root path for some other sub-generators. And I don't want to find a "none yo solution" like recreating my own storage, my own module to find appropriate path, etc... – C0ZEN Dec 18 '17 at 09:10
  • Hmm, then let’s go into yeoman source code to find a possible loophole. Will give it a try later this week. Or you can try posting a new issue in their Github repository. – Nandu Kalidindi Dec 18 '17 at 13:54