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

Print out yarn install when yarn.lock file is present #6888

Merged
merged 4 commits into from Mar 24, 2017

Conversation

samdemaeyer
Copy link

@samdemaeyer samdemaeyer commented Mar 22, 2017

As some might know, when you are using yarn and you remove /node_modules, you will have a comment when you try to run ember server

node_modules appears empty, you may need to run `npm install`

screen shot 2017-03-22 at 16 21 42

I fixed something similar in ember-cli-dependency-checker.
It's quite confusing for people who are not that familiar with the package managers.

As this is my first PR to ember-cli, I am guessing that there might be a better way for this fix. But at least the idea is out there now.

In this PR, I will print out

node_modules appears empty, you may need to run `yarn`

screen shot 2017-03-22 at 16 35 18

when there is a yarn.lock file present.

I know that yarn is not used by ember-cli by default yet, but it might be ok to check this as in the team I am working in, it got confusing for some people .. 😅

[1/4] Resolving packages...
[2/4] Fetching packages...
[3/4] Linking dependencies...
[4/4] Building fresh packages...
Done in 9.74s. rather than  when yarn.lock file is present
@@ -288,7 +288,11 @@ let Command = CoreObject.extend({

if (this.works === 'insideProject') {
if (!this.project.hasDependencies()) {
throw new SilentError('node_modules appears empty, you may need to run `npm install`');
let installInstuction = '`npm install`';
if (this.project.hasYarnLockFile()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer to check it here directly, and not add another method to project.

If however, we do keep the project method around, we should update other locations that search for yarn.lock to use it (example here).

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rwjblue, we only use it in /lib/commands/init.js:64 and lib/tasks/npm-task.js:45
I can take it out and check it directly if you like.
Than I just need another approach to test it, ATM, we overwrite the hasYarnLockFile function in the project class:

command.project.hasYarnLockFile = function() { return true; };

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rwjblue, in this commit I will check it directly. To test this behaviour, I am renaming the ember-cli project's actual yarn.lock file before the test runs and after the test, rewrite it back to the original name.
Could you advise me if this is a good approach, or might it be to risky? If so, what would you suggest for testing?

Copy link
Member

@Turbo87 Turbo87 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice work!

throw new SilentError('node_modules appears empty, you may need to run `npm install`');
let installInstuction = '`npm install`';
if (existsSync(path.join(this.project.root, 'yarn.lock'))) {
installInstuction = '`yarn`';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we make this yarn install to make it a bit easier to understand?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Turbo87, sure, no prob.

[1/4] Resolving packages...
success Already up-to-date.
Done in 0.56s. to yarn install v0.21.3
[1/4] Resolving packages...
success Already up-to-date.
Done in 0.56s.

return expect(command.validateAndRun(['controller', 'foo'])).to.be.rejected.then(reason => {
expect(reason.message).to.eql('node_modules appears empty, you may need to run `npm install`');
}).then(() => fs.renameSync(dummyYarnLockPath, originalYarnLockPath));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this renameSync will not run if the assertion above fails. you could wrap it in a describe() block though:

describe('without yarn.lock file', function() {
  let originalYarnLockPath, dummyYarnLockPath;

  beforeEach(function() {
    originalYarnLockPath = path.join(command.project.root, 'yarn.lock');
    dummyYarnLockPath = path.join(command.project.root, 'foo.bar');
    fs.renameSync(originalYarnLockPath, dummyYarnLockPath);
  });

  afterEach(function() {
    fs.renameSync(dummyYarnLockPath, originalYarnLockPath);
  });

  it('runs GenerateFromBlueprint but with null nodeModulesPath', function() {
    command.project.hasDependencies = function() { return false; };

    return expect(command.validateAndRun(['controller', 'foo'])).to.be.rejected.then(reason => {
      expect(reason.message).to.eql('node_modules appears empty, you may need to run `npm install`');
    });
  });
});
``

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @Turbo87, makes sense!
Question, would you typically destroy the global variables in the afterEach function?

afterEach(function() {
  fs.renameSync(dummyYarnLockPath, originalYarnLockPath);
  originalYarnLockPath = dummyYarnLockPath = null;
});

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since they are reassigned in every beforeEach() block there is IMHO no need to reset them in afterEach()

@Turbo87
Copy link
Member

Turbo87 commented Mar 24, 2017

@homu r+

@homu
Copy link
Contributor

homu commented Mar 24, 2017

📌 Commit bc1241f has been approved by Turbo87

homu added a commit that referenced this pull request Mar 24, 2017
…bo87

Print out `yarn install` when yarn.lock file is present

As some might know, when you are using yarn and you remove `/node_modules`, you will have a comment when you try to run `ember server`
```
node_modules appears empty, you may need to run `npm install`
```
<img width="497" alt="screen shot 2017-03-22 at 16 21 42" src="https://cloud.githubusercontent.com/assets/7160913/24209231/9c9c5124-0f1d-11e7-92f7-53387a56fdeb.png">

I fixed something similar in [ember-cli-dependency-checker](quaertym/ember-cli-dependency-checker#68).
It's quite confusing for people who are not that familiar with the package managers.

As this is my first PR to ember-cli, I am guessing that there might be a better way for this fix. But at least the idea is out there now.

In this PR, I will print out
```
node_modules appears empty, you may need to run `yarn`
```
<img width="446" alt="screen shot 2017-03-22 at 16 35 18" src="https://cloud.githubusercontent.com/assets/7160913/24209262/aea23474-0f1d-11e7-8af4-c63efe7036e2.png">

when there is a `yarn.lock` file present.

I know that yarn is not used by ember-cli by default yet, but it might be ok to check this as in the team I am working in, it got confusing for some people .. 😅
@homu
Copy link
Contributor

homu commented Mar 24, 2017

⌛ Testing commit bc1241f with merge 2da9de5...

@homu
Copy link
Contributor

homu commented Mar 24, 2017

☀️ Test successful - status

@homu homu merged commit bc1241f into ember-cli:master Mar 24, 2017
@samdemaeyer samdemaeyer deleted the use-yarn-when-yarn-lock-file branch March 24, 2017 13:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants