Skip to content

Commit

Permalink
fix(cli): fail gracefully when installed in parent folder
Browse files Browse the repository at this point in the history
closes #811
  • Loading branch information
JeroenVinke committed Mar 20, 2018
1 parent 947b0dd commit 9407c87
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 19 deletions.
15 changes: 9 additions & 6 deletions lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ exports.CLI = class {
this.container = new Container();
this.ui = new ui.ConsoleUI(this.options);
this.configureContainer();
this.configureLogger();

this.logger = LogManager.getLogger('CLI');
}

run(cmd, args) {
Expand All @@ -26,14 +29,14 @@ exports.CLI = class {
if (project) {
this.project = project;
this.container.registerInstance(Project, project);
} else if (this.options.runningLocally) {
this.logger.error('It appears that the Aurelia CLI is running locally from ' + __dirname + '. However, no project directory could be found. ' +
'The Aurelia CLI has to be installed globally (npm install -g aurelia-cli) and locally (npm install aurelia-cli) in an Aurelia CLI project directory');
return Promise.resolve();
}

return this.createCommand(cmd, args);
})
.then((command) => {
this.configureLogger();

return command.execute(args);
return this.createCommand(cmd, args)
.then((command) => command.execute(args));
});
}

Expand Down
39 changes: 26 additions & 13 deletions spec/lib/cli.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ describe('The cli', () => {
resolve(project);
}));
spyOn(cli.container, 'registerInstance');
spyOn(cli, 'createCommand').and.returnValue({ execute: () => {} });
spyOn(cli, 'createCommand').and.returnValue(Promise.resolve({ execute: () => {} }));

cli.run()
.then(() => {
Expand All @@ -153,7 +153,7 @@ describe('The cli', () => {
resolve(project);
}));
spyOn(cli.container, 'registerInstance');
spyOn(cli, 'createCommand').and.returnValue({ execute: () => {} });
spyOn(cli, 'createCommand').and.returnValue(Promise.resolve({ execute: () => {} }));

cli.run().then(() => {
expect(cli.container.registerInstance)
Expand All @@ -164,7 +164,7 @@ describe('The cli', () => {
it('creates the command', done => {
const command = 'run';
const args = {};
spyOn(cli, 'createCommand').and.returnValue({ execute: () => {} });
spyOn(cli, 'createCommand').and.returnValue(Promise.resolve({ execute: () => {} }));

cli.run(command, args).then(() => {
expect(cli.createCommand).toHaveBeenCalledWith(command, args);
Expand All @@ -173,26 +173,40 @@ describe('The cli', () => {

it('executes the command', done => {
const command = {
execute: () => {}
execute: jasmine.createSpy('execute').and.returnValue(Promise.resolve({}))
};
const args = {};
spyOn(cli, '_establishProject').and.returnValue(new Promise(resolve =>
resolve(project)
));
spyOn(command, 'execute').and.returnValue(new Promise(resolve => resolve({})));
spyOn(cli, 'createCommand').and.returnValue(command);
spyOn(cli, '_establishProject').and.returnValue(Promise.resolve(project));
spyOn(cli, 'createCommand').and.returnValue(Promise.resolve(command));

cli.run('run', args).then(() => {
expect(command.execute).toHaveBeenCalledWith(args);
}).catch(fail).then(done);
});

it('fails gracefully when Aurelia-CLI is ran from a root folder (non-project directory)', done => {
cli.options.runningLocally = true;
const command = {
execute: jasmine.createSpy('execute').and.returnValue(Promise.resolve({}))
};
const args = {};
spyOn(cli, '_establishProject').and.returnValue(Promise.resolve(null)); // no project could be found
spyOn(cli, 'createCommand').and.returnValue(Promise.resolve(command));
const errorSpy = spyOn(cli.logger, 'error');

cli.run('', args).then(() => {
expect(command.execute).not.toHaveBeenCalledWith(args);
expect(errorSpy).toHaveBeenCalled();
expect(errorSpy.calls.first().args[0]).toContain('It appears that the Aurelia CLI is running locally');
}).catch(fail).then(done);
});
});

describe('The config command', () => {
it('creates the command', done => {
const command = 'config';
const args = {};
spyOn(cli, 'createCommand').and.returnValue({ execute: () => {} });
spyOn(cli, 'createCommand').and.returnValue(Promise.resolve({ execute: () => {} }));

cli.run(command, args).then(() => {
expect(cli.createCommand).toHaveBeenCalledWith(command, args);
Expand All @@ -201,14 +215,13 @@ describe('The cli', () => {

it('executes the command', done => {
const command = {
execute: () => {}
execute: jasmine.createSpy('execute').and.returnValue(Promise.resolve({}))
};
const args = {};
spyOn(cli, '_establishProject').and.returnValue(new Promise(resolve =>
resolve(project)
));
spyOn(command, 'execute').and.returnValue(new Promise(resolve => resolve({})));
spyOn(cli, 'createCommand').and.returnValue(command);
spyOn(cli, 'createCommand').and.returnValue(Promise.resolve(command));

cli.run('config', args).then(() => {
expect(command.execute).toHaveBeenCalledWith(args);
Expand Down

0 comments on commit 9407c87

Please sign in to comment.