Skip to content

Commit

Permalink
fix(@angular/cli): allow global config command outside project
Browse files Browse the repository at this point in the history
Fixes #12296
  • Loading branch information
clydin authored and hansl committed Sep 25, 2018
1 parent fb4e818 commit 91cf2d7
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 29 deletions.
6 changes: 5 additions & 1 deletion packages/angular/cli/commands/config-impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
} from '@angular-devkit/core';
import { writeFileSync } from 'fs';
import { Command } from '../models/command';
import { Arguments } from '../models/interface';
import { Arguments, CommandScope } from '../models/interface';
import {
getWorkspace,
getWorkspaceRaw,
Expand Down Expand Up @@ -178,6 +178,10 @@ export class ConfigCommand extends Command<ConfigCommandSchema> {
public async run(options: ConfigCommandSchema & Arguments) {
const level = options.global ? 'global' : 'local';

if (!options.global) {
await this.validateScope(CommandScope.InProject);
}

let config =
(getWorkspace(level) as {} as { _workspace: experimental.workspace.WorkspaceSchema });

Expand Down
2 changes: 1 addition & 1 deletion packages/angular/cli/commands/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"$longDescription": "",

"$aliases": [],
"$scope": "in",
"$scope": "all",
"$type": "native",
"$impl": "./config-impl#ConfigCommand",

Expand Down
4 changes: 2 additions & 2 deletions packages/angular/cli/models/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,8 @@ export abstract class Command<T extends BaseCommandOptions = BaseCommandOptions>
}
}

async validateScope(): Promise<void> {
switch (this.description.scope) {
async validateScope(scope?: CommandScope): Promise<void> {
switch (scope === undefined ? this.description.scope : scope) {
case CommandScope.OutProject:
if (this.workspace.configFile) {
this.logger.fatal(tags.oneLine`
Expand Down
65 changes: 40 additions & 25 deletions tests/legacy-cli/e2e/tests/commands/config/config-global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,44 @@ import { ng } from '../../../utils/process';
import { expectToFail } from '../../../utils/utils';


export default function() {
return Promise.resolve()
.then(() => expectToFail(() => ng('config', '--global', 'schematics.@schematics/angular.component.inlineStyle')))
.then(() => ng('config', '--global', 'schematics.@schematics/angular.component.inlineStyle', 'false'))
.then(() => ng('config', '--global', 'schematics.@schematics/angular.component.inlineStyle'))
.then(({ stdout }) => {
if (!stdout.match(/false\n?/)) {
throw new Error(`Expected "false", received "${JSON.stringify(stdout)}".`);
}
})
// This test requires schema querying capabilities
// .then(() => expectToFail(() => {
// return ng('config', '--global', 'schematics.@schematics/angular.component.inlineStyle', 'INVALID_BOOLEAN');
// }))
.then(() => ng('config', '--global', 'schematics.@schematics/angular.component.inlineStyle', 'true'))
.then(() => ng('config', '--global', 'schematics.@schematics/angular.component.inlineStyle'))
.then(({ stdout }) => {
if (!stdout.match(/true\n?/)) {
throw new Error(`Expected "true", received "${JSON.stringify(stdout)}".`);
}
})
.then(() => expectToFail(() => ng('config', '--global', 'cli.warnings.notreal', 'true')))
.then(() => ng('config', '--global', 'cli.warnings.versionMismatch', 'false'))
.then(() => expectFileToExist(path.join(homedir(), '.angular-config.json')))
.then(() => deleteFile(path.join(homedir(), '.angular-config.json')));
export default async function() {
await expectToFail(() => ng(
'config',
'--global',
'schematics.@schematics/angular.component.inlineStyle',
));

await ng('config', '--global', 'schematics.@schematics/angular.component.inlineStyle', 'false');
let output = await ng(
'config',
'--global',
'schematics.@schematics/angular.component.inlineStyle',
);
if (!output.stdout.match(/false\n?/)) {
throw new Error(`Expected "false", received "${JSON.stringify(output.stdout)}".`);
}

// This test requires schema querying capabilities
// .then(() => expectToFail(() => {
// return ng('config', '--global', 'schematics.@schematics/angular.component.inlineStyle', 'INVALID_BOOLEAN');
// }))

const cwd = process.cwd();
process.chdir('/');
try {
await ng('config', '--global', 'schematics.@schematics/angular.component.inlineStyle', 'true');
} finally {
process.chdir(cwd);
}

output = await ng('config', '--global', 'schematics.@schematics/angular.component.inlineStyle');
if (!output.stdout.match(/true\n?/)) {
throw new Error(`Expected "true", received "${JSON.stringify(output.stdout)}".`);
}

await expectToFail(() => ng('config', '--global', 'cli.warnings.notreal', 'true'));

await ng('config', '--global', 'cli.warnings.versionMismatch', 'false');
await expectFileToExist(path.join(homedir(), '.angular-config.json'));
await deleteFile(path.join(homedir(), '.angular-config.json'));
}

0 comments on commit 91cf2d7

Please sign in to comment.