Skip to content
Merged
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
27 changes: 26 additions & 1 deletion packages/angular/cli/commands/lint-impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@
* found in the LICENSE file at https://angular.io/license
*/

import { spawnSync } from 'child_process';
import * as path from 'path';
import { ArchitectCommand } from '../models/architect-command';
import { Arguments } from '../models/interface';
import { askConfirmation } from '../utilities/prompt';
import { Schema as LintCommandSchema } from './lint';

const MissingBuilder = `
Expand All @@ -22,11 +25,33 @@ For example:
export class LintCommand extends ArchitectCommand<LintCommandSchema> {
override readonly target = 'lint';
override readonly multiTarget = true;
override readonly missingTargetError = MissingBuilder;

override async initialize(options: LintCommandSchema & Arguments): Promise<number | void> {
if (!options.help) {
return super.initialize(options);
}
}

override async onMissingTarget(): Promise<void | number> {
this.logger.warn(MissingBuilder);

const shouldAdd = await askConfirmation('Would you like to add ESLint now?', true, false);
if (shouldAdd) {
// Run `ng add @angular-eslint/schematics`
const binPath = path.resolve(__dirname, '../bin/ng.js');
const { status, error } = spawnSync(
process.execPath,
[binPath, 'add', '@angular-eslint/schematics'],
{
stdio: 'inherit',
},
);

if (error) {
throw error;
}

return status ?? 0;
}
}
}
34 changes: 21 additions & 13 deletions packages/angular/cli/models/architect-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,23 @@ export abstract class ArchitectCommand<
target: string | undefined;
missingTargetError: string | undefined;

protected async onMissingTarget(projectName?: string): Promise<void | number> {
if (this.missingTargetError) {
this.logger.fatal(this.missingTargetError);

return 1;
}

if (projectName) {
this.logger.fatal(`Project '${projectName}' does not support the '${this.target}' target.`);
} else {
this.logger.fatal(`No projects support the '${this.target}' target.`);
}

return 1;
}

// eslint-disable-next-line max-lines-per-function
public override async initialize(options: T & Arguments): Promise<number | void> {
this._registry = new json.schema.CoreSchemaRegistry();
this._registry.addPostTransform(json.schema.transforms.addUndefinedDefaults);
Expand Down Expand Up @@ -87,21 +104,12 @@ export abstract class ArchitectCommand<
}
}

if (targetProjectNames.length === 0) {
this.logger.fatal(
this.missingTargetError || `No projects support the '${this.target}' target.`,
);

return 1;
}

if (projectName && !targetProjectNames.includes(projectName)) {
this.logger.fatal(
this.missingTargetError ||
`Project '${projectName}' does not support the '${this.target}' target.`,
);
return await this.onMissingTarget(projectName);
}

return 1;
if (targetProjectNames.length === 0) {
return await this.onMissingTarget();
}

if (!projectName && commandLeftovers && commandLeftovers.length > 0) {
Expand Down