Skip to content

Commit

Permalink
fix(@angular/cli): Verify workspace file for inProject commands
Browse files Browse the repository at this point in the history
  • Loading branch information
Brocco authored and hansl committed Mar 30, 2018
1 parent 83ef4af commit a0f5d81
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
18 changes: 18 additions & 0 deletions packages/@angular/cli/models/command-runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import { logging } from '@angular-devkit/core';
import { camelize } from '@angular-devkit/core/src/utils/strings';

import * as yargsParser from 'yargs-parser';
import * as fs from 'fs';
import { join } from 'path';

export interface CommandMap {
[key: string]: CommandConstructor;
Expand Down Expand Up @@ -74,6 +76,7 @@ export async function runCommand(commandMap: CommandMap,
return await runHelp(command, options);
} else {
verifyCommandInScope(command, executionScope);
verifyWorkspace(command, executionScope, context.project.root);
delete options.h;
delete options.help;
return await validateAndRunCommand(command, options);
Expand Down Expand Up @@ -204,6 +207,21 @@ function verifyCommandInScope(command: Command, scope = CommandScope.everywhere)
}
}

function verifyWorkspace(command: Command, executionScope: CommandScope, root: string): void {
if (command.scope === CommandScope.everywhere) {
return;
}
if (executionScope === CommandScope.inProject) {
if (fs.existsSync(join(root, 'angular.json'))) {
return;
}
if (fs.existsSync(join(root, '.angular.json'))) {
return;
}
throw new Error('Invalid project: missing workspace file.');
}
}

// Execute a command's `printHelp`.
async function runHelp(command: Command, options: any): Promise<void> {
return await command.printHelp(options);
Expand Down
13 changes: 13 additions & 0 deletions tests/e2e/tests/misc/workspace-verification.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import {deleteFile} from '../../utils/fs';
import {ng} from '../../utils/process';
import { expectToFail } from '../../utils/utils';


export default function() {
return ng('generate', 'component', 'foo', '--dry-run')
.then(() => deleteFile('angular.json'))
// fails because it needs to be inside a project
// without a workspace file
.then(() => expectToFail(() => ng('generate', 'component', 'foo', '--dry-run')))
.then(() => ng('version'));
}

0 comments on commit a0f5d81

Please sign in to comment.