Skip to content

Commit be532fd

Browse files
committed
fix: avoid early exist of "au new"
Don't try to find local aurelia project when running "au new". Always use global aurelia-cli to execute "au new". Log cli version. closes #977
1 parent 70f6407 commit be532fd

File tree

4 files changed

+26
-20
lines changed

4 files changed

+26
-20
lines changed

bin/aurelia-cli.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,18 @@ Please upgrade to latest Node.js https://nodejs.org`);
1313

1414
process.title = 'aurelia';
1515

16+
const userArgs = process.argv.slice(2);
17+
const commandName = userArgs[0];
18+
const commandArgs = userArgs.slice(1);
19+
1620
let originalBaseDir = process.cwd();
1721

1822
resolve('aurelia-cli', {
1923
basedir: originalBaseDir
2024
}, function(error, projectLocalCli) {
2125
let cli;
2226

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

3135
cli.options.originalBaseDir = originalBaseDir;
3236

33-
let userArgs = process.argv.slice(2);
34-
let commandName = userArgs[0];
35-
let commandArgs = userArgs.slice(1);
36-
3737
cli.run(commandName, commandArgs).catch(err => {
3838
console.log(err);
3939
process.exit(1);

lib/cli.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,30 @@ exports.CLI = class {
1717
this.logger = LogManager.getLogger('CLI');
1818
}
1919

20+
// Note: cannot use this.logger.error inside run()
21+
// because logger is not configured yet!
22+
// this.logger.error prints nothing in run(),
23+
// directly use this.ui.log.
2024
run(cmd, args) {
25+
const version = `${this.options.runningGlobally ? 'Global' : 'Local'} aurelia-cli v${require('../package.json').version}`;
26+
2127
if (cmd === '--version' || cmd === '-v') {
22-
return this.ui.log(require('../package.json').version);
28+
return this.ui.log(version);
2329
}
2430

25-
return this._establishProject(this.options)
31+
return (cmd === 'new' ? Promise.resolve() : this._establishProject())
2632
.then(project => {
33+
this.ui.log(version);
34+
2735
if (project && this.options.runningLocally) {
2836
this.project = project;
2937
this.container.registerInstance(Project, project);
3038
} else if (project && this.options.runningGlobally) {
31-
this.logger.error('The current directory is likely an Aurelia-CLI project, but no local installation of Aurelia-CLI could be found. ' +
39+
this.ui.log('The current directory is likely an Aurelia-CLI project, but no local installation of Aurelia-CLI could be found. ' +
3240
'(Do you need to restore node modules using npm install?)');
3341
return Promise.resolve();
3442
} else if (!project && this.options.runningLocally) {
35-
this.logger.error('It appears that the Aurelia CLI is running locally from ' + __dirname + '. However, no project directory could be found. ' +
43+
this.ui.log('It appears that the Aurelia CLI is running locally from ' + __dirname + '. However, no project directory could be found. ' +
3644
'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');
3745
return Promise.resolve();
3846
}
@@ -101,7 +109,7 @@ exports.CLI = class {
101109
return this.container.get(require('./commands/help/command'));
102110
}
103111

104-
_establishProject(options) {
112+
_establishProject() {
105113
return determineWorkingDirectory(process.cwd())
106114
.then(dir => dir ? Project.establish(this.ui, dir) : this.ui.log('No Aurelia project found.'));
107115
}

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

spec/lib/cli.spec.js

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ describe('The cli', () => {
2222
aureliaProject = 'aurelia_project';
2323
const fsConfig = {};
2424
fsConfig[dir] = {};
25-
fsConfig['package.json'] = {};
25+
fsConfig['package.json'] = '{"version": "1.0.0"}';
2626
mockfs(fsConfig);
2727
});
2828

@@ -109,16 +109,13 @@ describe('The cli', () => {
109109
function getVersionSpec(command) {
110110
return () => {
111111
beforeEach(() => {
112-
mockfs({
113-
'package.json': '{"version": "1.0.0"}'
114-
});
115112
spyOn(cli.ui, 'log')
116113
.and.callFake(() => new Promise(resolve => resolve()));
117114
});
118115

119116
it('logs the cli version', () => {
120117
cli.run(command);
121-
expect(cli.ui.log).toHaveBeenCalledWith('1.0.0');
118+
expect(cli.ui.log).toHaveBeenCalledWith('Local aurelia-cli v1.0.0');
122119
});
123120

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

144141
cli.run()
145142
.then(() => {
146-
expect(cli._establishProject).toHaveBeenCalledWith(cli.options);
143+
expect(cli._establishProject).toHaveBeenCalled();
147144
}).catch(fail).then(done);
148145
});
149146

@@ -194,12 +191,13 @@ describe('The cli', () => {
194191
const args = {};
195192
spyOn(cli, '_establishProject').and.returnValue(Promise.resolve(null)); // no project could be found
196193
spyOn(cli, 'createCommand').and.returnValue(Promise.resolve(command));
197-
const errorSpy = spyOn(cli.logger, 'error');
194+
const errorSpy = spyOn(cli.ui, 'log');
198195

199196
cli.run('', args).then(() => {
200197
expect(command.execute).not.toHaveBeenCalledWith(args);
201-
expect(errorSpy).toHaveBeenCalled();
202-
expect(errorSpy.calls.first().args[0]).toContain('It appears that the Aurelia CLI is running locally');
198+
expect(errorSpy).toHaveBeenCalledTimes(2);
199+
expect(errorSpy.calls.first().args[0]).toContain('Local aurelia-cli');
200+
expect(errorSpy.calls.argsFor(1)[0]).toContain('It appears that the Aurelia CLI is running locally');
203201
}).catch(fail).then(done);
204202
});
205203
});

0 commit comments

Comments
 (0)