Skip to content

Commit a0f5d81

Browse files
Broccohansl
authored andcommitted
fix(@angular/cli): Verify workspace file for inProject commands
1 parent 83ef4af commit a0f5d81

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

packages/@angular/cli/models/command-runner.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import { logging } from '@angular-devkit/core';
1111
import { camelize } from '@angular-devkit/core/src/utils/strings';
1212

1313
import * as yargsParser from 'yargs-parser';
14+
import * as fs from 'fs';
15+
import { join } from 'path';
1416

1517
export interface CommandMap {
1618
[key: string]: CommandConstructor;
@@ -74,6 +76,7 @@ export async function runCommand(commandMap: CommandMap,
7476
return await runHelp(command, options);
7577
} else {
7678
verifyCommandInScope(command, executionScope);
79+
verifyWorkspace(command, executionScope, context.project.root);
7780
delete options.h;
7881
delete options.help;
7982
return await validateAndRunCommand(command, options);
@@ -204,6 +207,21 @@ function verifyCommandInScope(command: Command, scope = CommandScope.everywhere)
204207
}
205208
}
206209

210+
function verifyWorkspace(command: Command, executionScope: CommandScope, root: string): void {
211+
if (command.scope === CommandScope.everywhere) {
212+
return;
213+
}
214+
if (executionScope === CommandScope.inProject) {
215+
if (fs.existsSync(join(root, 'angular.json'))) {
216+
return;
217+
}
218+
if (fs.existsSync(join(root, '.angular.json'))) {
219+
return;
220+
}
221+
throw new Error('Invalid project: missing workspace file.');
222+
}
223+
}
224+
207225
// Execute a command's `printHelp`.
208226
async function runHelp(command: Command, options: any): Promise<void> {
209227
return await command.printHelp(options);
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import {deleteFile} from '../../utils/fs';
2+
import {ng} from '../../utils/process';
3+
import { expectToFail } from '../../utils/utils';
4+
5+
6+
export default function() {
7+
return ng('generate', 'component', 'foo', '--dry-run')
8+
.then(() => deleteFile('angular.json'))
9+
// fails because it needs to be inside a project
10+
// without a workspace file
11+
.then(() => expectToFail(() => ng('generate', 'component', 'foo', '--dry-run')))
12+
.then(() => ng('version'));
13+
}

0 commit comments

Comments
 (0)