Skip to content

Commit

Permalink
Merge pull request #1002 from huochunpeng/au-new-error
Browse files Browse the repository at this point in the history
fix: avoid early exist of "au new"
  • Loading branch information
EisenbergEffect authored Dec 19, 2018
2 parents 70f6407 + be532fd commit 9d85d49
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 20 deletions.
10 changes: 5 additions & 5 deletions bin/aurelia-cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,18 @@ Please upgrade to latest Node.js https://nodejs.org`);

process.title = 'aurelia';

const userArgs = process.argv.slice(2);
const commandName = userArgs[0];
const commandArgs = userArgs.slice(1);

let originalBaseDir = process.cwd();

resolve('aurelia-cli', {
basedir: originalBaseDir
}, function(error, projectLocalCli) {
let cli;

if (error) {
if (commandName === 'new' || error) {
cli = new (require('../lib/index').CLI);
cli.options.runningGlobally = true;
} else {
Expand All @@ -30,10 +34,6 @@ resolve('aurelia-cli', {

cli.options.originalBaseDir = originalBaseDir;

let userArgs = process.argv.slice(2);
let commandName = userArgs[0];
let commandArgs = userArgs.slice(1);

cli.run(commandName, commandArgs).catch(err => {
console.log(err);
process.exit(1);
Expand Down
18 changes: 13 additions & 5 deletions lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,30 @@ exports.CLI = class {
this.logger = LogManager.getLogger('CLI');
}

// Note: cannot use this.logger.error inside run()
// because logger is not configured yet!
// this.logger.error prints nothing in run(),
// directly use this.ui.log.
run(cmd, args) {
const version = `${this.options.runningGlobally ? 'Global' : 'Local'} aurelia-cli v${require('../package.json').version}`;

if (cmd === '--version' || cmd === '-v') {
return this.ui.log(require('../package.json').version);
return this.ui.log(version);
}

return this._establishProject(this.options)
return (cmd === 'new' ? Promise.resolve() : this._establishProject())
.then(project => {
this.ui.log(version);

if (project && this.options.runningLocally) {
this.project = project;
this.container.registerInstance(Project, project);
} else if (project && this.options.runningGlobally) {
this.logger.error('The current directory is likely an Aurelia-CLI project, but no local installation of Aurelia-CLI could be found. ' +
this.ui.log('The current directory is likely an Aurelia-CLI project, but no local installation of Aurelia-CLI could be found. ' +
'(Do you need to restore node modules using npm install?)');
return Promise.resolve();
} else if (!project && this.options.runningLocally) {
this.logger.error('It appears that the Aurelia CLI is running locally from ' + __dirname + '. However, no project directory could be found. ' +
this.ui.log('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();
}
Expand Down Expand Up @@ -101,7 +109,7 @@ exports.CLI = class {
return this.container.get(require('./commands/help/command'));
}

_establishProject(options) {
_establishProject() {
return determineWorkingDirectory(process.cwd())
.then(dir => dir ? Project.establish(this.ui, dir) : this.ui.log('No Aurelia project found.'));
}
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 7 additions & 9 deletions spec/lib/cli.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ describe('The cli', () => {
aureliaProject = 'aurelia_project';
const fsConfig = {};
fsConfig[dir] = {};
fsConfig['package.json'] = {};
fsConfig['package.json'] = '{"version": "1.0.0"}';
mockfs(fsConfig);
});

Expand Down Expand Up @@ -109,16 +109,13 @@ describe('The cli', () => {
function getVersionSpec(command) {
return () => {
beforeEach(() => {
mockfs({
'package.json': '{"version": "1.0.0"}'
});
spyOn(cli.ui, 'log')
.and.callFake(() => new Promise(resolve => resolve()));
});

it('logs the cli version', () => {
cli.run(command);
expect(cli.ui.log).toHaveBeenCalledWith('1.0.0');
expect(cli.ui.log).toHaveBeenCalledWith('Local aurelia-cli v1.0.0');
});

it('returns an empty promise', done => {
Expand All @@ -143,7 +140,7 @@ describe('The cli', () => {

cli.run()
.then(() => {
expect(cli._establishProject).toHaveBeenCalledWith(cli.options);
expect(cli._establishProject).toHaveBeenCalled();
}).catch(fail).then(done);
});

Expand Down Expand Up @@ -194,12 +191,13 @@ describe('The cli', () => {
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');
const errorSpy = spyOn(cli.ui, 'log');

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');
expect(errorSpy).toHaveBeenCalledTimes(2);
expect(errorSpy.calls.first().args[0]).toContain('Local aurelia-cli');
expect(errorSpy.calls.argsFor(1)[0]).toContain('It appears that the Aurelia CLI is running locally');
}).catch(fail).then(done);
});
});
Expand Down

0 comments on commit 9d85d49

Please sign in to comment.