Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generator creates all app files in the root folder instead of specified [windows] #55

Closed
deepu105 opened this issue Feb 24, 2016 · 8 comments

Comments

@deepu105
Copy link

Platform : windows

I was checking out yeoman-app and found that when running a generator all the generator files are created in the folder where I run the app irrespective of whet folder I choose I raised an issue here yeoman/yeoman-app#104

But then I wrote a simple test and tried it out as below and the issue seems to be in yeoman-environment itself, say I executed this in a folder d:/app folder and i have a sub folder there testDir and I switched to the sub directory before invoking env.run() but still files were created in the d:/app folder instead of d:/app/testDir folder. But there is a empty set of all the folders alone created inside d:/app/testDir

Seems like the mkdir commands are executed on the current directory while copy files are executed on the directory where environment was initilized

var fs = require('fs');
var _ = require('lodash');
var yeoman = require('yeoman-environment');
var env = yeoman.createEnv();

var init = function () {
  //process.chdir('./testDir');
  console.log('init yeoman-environment');
  console.log(process.cwd());
  // Here we register a jhipster generator based on its path. Providing the namespace
  env.register(require.resolve('generator-jhipster'), 'jhipster:app');
};

var runApp = function () {
  console.log('running yo app');
  run('jhipster:app', './testDir')
};

function sendCommandToAppWindow(name, data) {
  if (data instanceof Error) {
    console.error(data);
    data = data.toString();
  }
}

function run(generatorName, cwd) {

  if (!generatorName) {
    return sendCommandToAppWindow('generator:error', new Error('You must provide a generator name'));
  }

  if (!fs.existsSync(cwd)) {
    return sendCommandToAppWindow('generator:error', new Error('The given path does not exist or is not a directory'));
  }
  console.log(process.cwd());
  process.chdir(cwd);
  console.log(process.cwd());

  function done(err) {
    if (err) {
      return sendCommandToAppWindow('generator:error', err);
    }
  }

  // invoke generator
  env.run(generatorName, { 'skip-install': true }, done);
}

module.exports = {
  init : init,
  runApp : runApp
}

@SBoudrias
Copy link
Member

You probably have a .yo-rc.json file in your root folder.

yo doctor should warn you about this.

@deepu105
Copy link
Author

you are right for this I didnt notice I had a .yo-rc file in the root as I
generated the sample with yo :(

Thanks & Regards,
Deepu

On Thu, Feb 25, 2016 at 3:22 AM, Simon Boudrias [email protected]
wrote:

Closed #55 #55.


Reply to this email directly or view it on GitHub
#55 (comment).

@deepu105
Copy link
Author

@SBoudrias Im sorry seems I was wrong, this is still in an issue even if I remove the yo-rc file from the root. The cwd into the desired folder has no effect as the files are still created at the root folder from where the program was invoked.
The behaviour is the same irrespective of .yo-rc.json file on the root folder, I checked with and without the files and its still not working as expected.

So this still seems like a bug,could you plz reopen this? could this be the problem?

if I do the cwd before yeoman.createEnv() this seems to work but if do cwd after env = yeoman.createEnv(); its doesnt work.
The below creates files in the correct directory coz I did a process.chdir('./testDir') before yeoman.createEnv() but if I do process.chdir('./testDir') after yeoman.createEnv() its doesn't work files are still created at root

var fs = require('fs');
var _ = require('lodash');
var yeoman = require('yeoman-environment');
var env;

var init = function () {
  console.log('init yeoman-environment');
  process.chdir('./testDir');
  console.log(process.cwd());
  env = yeoman.createEnv();
  // Here we register a jhipster generator based on its path. Providing the namespace
  env.register(require.resolve('generator-jhipster'), 'jhipster:app');
};

var runApp = function () {
  console.log('running yo app');
  run('jhipster:app', './testDir')
};

function sendCommandToAppWindow(name, data) {
  if (data instanceof Error) {
    console.error(data);
    data = data.toString();
  }
}

function run(generatorName, cwd) {

  if (!generatorName) {
    return sendCommandToAppWindow('generator:error', new Error('You must provide a generator name'));
  }

  /*if (!fs.existsSync(cwd)) {
    return sendCommandToAppWindow('generator:error', new Error('The given path does not exist or is not a directory'));
  }*/
  console.log(process.cwd());
  //process.chdir(cwd);
  console.log(process.cwd());

  function done(err) {
    if (err) {
      return sendCommandToAppWindow('generator:error', err);
    }
  }

  var triggerInstall = _.once(_.partial(sendCommandToAppWindow, 'generator:install'));

  // invoke generator
  env.run(generatorName, { 'skip-install': true }, done);
}

module.exports = {
  init : init,
  runApp : runApp
}

@deepu105
Copy link
Author

I found a workaround. This creates the files in the directory i want, without having to cwd into the directory

env = yeoman.createEnv([],{cwd:'./testDir'});

@SBoudrias
Copy link
Member

any .yo-rc.json file in a parent folder will change the project root.

For testing check http://yeoman.io/authoring/testing.html - especially the part about using tmp directories.

@deepu105
Copy link
Author

I dont have any .yo-rc.json file in any of the parent directory where I ran the above and its still the same.

seems like yeoman-environment dumps all files into the folder where yeoman.createEnv() is triggered any change of folder after that doesnt seem to have any effect atleast in Windows os.

of course this will not have issues when normal yo generators are run as the environment will be instantiated at the folder from were it is run.

but if we want to use yeoman-environment from other tools I guess this behavior is not acceptable.

@SBoudrias
Copy link
Member

And that's happening only with the yeoman app?

I don't know of anyone else running in this issue using yo.

@deepu105
Copy link
Author

yes coz with yo you dont have an option to choose a folder to generate to,
its always generated at the folder where you run yo command

Thanks & Regards,
Deepu

On Thu, Feb 25, 2016 at 3:27 PM, Simon Boudrias [email protected]
wrote:

And that's happening only with the yeoman app?

I don't know of anyone else running in this issue using yo.


Reply to this email directly or view it on GitHub
#55 (comment).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants