generator
generator copied to clipboard
Yo doesn't check parent directories for `yo-rc.json`
Type of issue: Bug
My environment
-
OS version/details:
MacOs 11.13
-
Node version:
14.16.1
(runnode --version
in your terminal) -
npm version:
6.14.12
(runnpm --version
in your terminal) -
Version of yo :
4.0.0
(runyo --version
in your terminal)
Expected behavior
Yo correctly determines tmp
(or whatever folder it was first run in) as the project root, and places files correctly even when run from a subdirectory. The subdirectory is not mistakenly marked as a new project root, the subdirectory does not mistakenly include a yo-rc.json
itself.
Current behavior
- Instead of creating new files in
tmp/files
, the following file structure occurs
tmp
│ yo-rc.json
└───files
│ │ <files here>
└───subdir
│ │ yo-rc.json
│ └───subfolder
│ │ <files here>
Steps to reproduce the behavior
- Set up simple project
- Create a "new" BaseGenerator, which all other generators extend from
- Write an
initialise
function as follows
_initialize() {
const defaults: Config = {
// Some defaults here
}
this.config.defaults(defaults)
}
- In each Generator or Subgenerator, call
_initialise
in theinitializing
context. Generators create files either withthis.destinationRoot
or any configuration option in the config - Setup Generator with
npm link
- Create a directory, we'll call it
tmp
- Run generator in
tmp
- Observe it working as intended. Files are created in
tmp/files
. Config fileyo-rc.json
is created intmp
, marking project root - Now, create a subdirectory,
tmp/subdir
- Cd into
subdir
- Run Generator again
- Observe wrong behaviour, as seen above
This is the [email protected] behavior. While there are some use cases it may make sense, with workspaces it doesn't.
workspaces
│ yo-rc.json (generator-workspaces)
└───packages
│ └───package1 (generator-node)
│ └ yo-rc.json
│ └───package2 (generator-node)
│ └ yo-rc.json
@mshima Is there any way to get the intended behaviour? Otherwise, the whole deal with yo-rc.json doesn't seem to make sense to me, or the documentation about it is outdated
Install and load 'find-up' package
Add at your constructor something like:
const rootPath = findUp.sync('.yo-rc.json', {
cwd: this.destinationRoot()
});
if (rootPath) {
this.destinationRoot(rootPath);
}
I used the solution suggested by @mshima and found an incompleteness: the rootPath
value at the end includes the '.yo-rc.json' filename. Consider removing it before passing rootPath
to this.destinationRoot
, or you will get an error on writing.
Also keep in mind that since version 6.0.0 find-up uses ESM. The last CJS version of it is 5.0.0.