Skip to content

fix(@angular/cli): require to run in a project if the help flag is present #16396

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions packages/angular/cli/models/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,7 @@ export abstract class Command<T extends BaseCommandOptions = BaseCommandOptions>
abstract async run(options: T & Arguments): Promise<number | void>;

async validateAndRun(options: T & Arguments): Promise<number | void> {
if (!(options.help === true || options.help === 'json' || options.help === 'JSON')) {
await this.validateScope();
}
await this.validateScope();
await this.initialize(options);

if (options.help === true) {
Expand Down
23 changes: 18 additions & 5 deletions scripts/validate-user-analytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,26 @@ async function _checkDimensions(dimensionsTable: string, logger: logging.Logger)

const commands = require('../packages/angular/cli/commands.json');
const ngPath = path.join(newProjectRoot, 'node_modules/.bin/ng');
for (const commandName of Object.keys(commands)) {
const options = { cwd: newProjectRoot };
const childLogger = logger.createChild(commandName);
const stdout = _exec(ngPath, [commandName, '--help=json'], options, childLogger);
commandDescription[commandName] = JSON.parse(stdout.trim());
const options = { cwd: newProjectRoot };

function execCommands(commands: string[]) {
for (const commandName of commands) {
const childLogger = logger.createChild(commandName);
const stdout = _exec(ngPath, [commandName, '--help=json'], options, childLogger);
commandDescription[commandName] = JSON.parse(stdout.trim());
}
}

const commandsToRunInAProject = Object.keys(commands).filter(commandName => commandName !== 'new');
// `ng new` is the only command that's required to run outside of an Angular project
const commandsToRunOutsideOfAProject = ['new'];

execCommands(commandsToRunInAProject);
const angularJsonPath = path.join(newProjectRoot, 'angular.json');
// Remove `angular.json` to ensure `ng new` is runnable in the same directory
fs.unlinkSync(angularJsonPath);
execCommands(commandsToRunOutsideOfAProject);

function _checkOptionsForAnalytics(options: Option[]) {
for (const option of options) {
if (option.subcommands) {
Expand Down
2 changes: 1 addition & 1 deletion tests/legacy-cli/e2e/tests/commands/help/help-hidden.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export default function() {
`);
}
})
.then(() => silentNg('--help', 'new'))
.then(() => silentNg('--help', 'generate'))
.then(({ stdout }) => {
if (stdout.match(/--link-cli/)) {
throw new Error(oneLine`
Expand Down
26 changes: 19 additions & 7 deletions tests/legacy-cli/e2e/tests/commands/help/help-json.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,27 @@
import { silentNg } from '../../../utils/process';

import { deleteFile } from '../../../utils/fs';

export default async function() {
const commands = require('@angular/cli/commands.json');
for (const commandName of Object.keys(commands)) {
const { stdout } = await silentNg(commandName, '--help=json');

if (stdout.trim()) {
JSON.parse(stdout);
} else {
console.warn(`No JSON output for command [${commandName}].`);
async function execCommands(commands: string[]) {
for (const commandName of commands) {
const { stdout } = await silentNg(commandName, '--help=json');

if (stdout.trim()) {
JSON.parse(stdout);
} else {
console.warn(`No JSON output for command [${commandName}].`);
}
}
}

const commandsToRunInAProject = Object.keys(commands).filter(commandName => commandName !== 'new');
// `ng new` is the only command that's required to run outside of an Angular project
const commandsToRunOutsideOfAProject = ['new'];

await execCommands(commandsToRunInAProject);
// Remove `angular.json` to ensure `ng new` is runnable in the same directory
await deleteFile('angular.json');
await execCommands(commandsToRunOutsideOfAProject);
}