Hone logo
Hone
Problems

Angular Architect Target Implementation

This challenge focuses on implementing a custom target for Angular Architect, allowing you to define and execute a specific build or deployment process tailored to your project's needs. Architect is Angular's command-line interface (CLI) tool for building and deploying applications, and extending it with custom targets provides powerful flexibility. This is useful for automating complex workflows, integrating with third-party tools, or creating specialized build configurations.

Problem Description

You are tasked with creating a new Angular Architect target named my-custom-target. This target should perform the following actions when invoked:

  1. Log a message: The target should first log a message to the console indicating that the custom target is running. The message should include the current project name.
  2. Execute a simple build script: The target should then execute a simple shell script (simulated here by a function call) that prints a message indicating a build process has started and finished. This simulates a more complex build process.
  3. Handle errors gracefully: If any error occurs during the execution of the shell script simulation, the target should catch the error and log an appropriate error message to the console, including the error details.

The target should be defined as a function that accepts the Architect object and the options object as arguments. The options object will contain any arguments passed to the target when it is invoked. You do not need to implement argument parsing for this challenge; assume the options object is empty.

Examples

Example 1:

Input: Running `ng my-custom-target` in a project named "my-app"
Output:
[INFO] Running my-custom-target for my-app
[INFO] Starting build process...
[INFO] Build process completed successfully.

Explanation: The target logs the initial message, then executes the simulated build script, which prints its messages.

Example 2:

Input: Running `ng my-custom-target` in a project named "another-app" and the simulated build script throws an error.
Output:
[INFO] Running my-custom-target for another-app
[ERROR] Error during build process: Simulated build script failed.

Explanation: The target logs the initial message, then attempts to execute the simulated build script. Because the script throws an error, the target catches the error and logs an error message.

Constraints

  • The target must be implemented as a TypeScript function.
  • The target must accept the Architect and options objects as arguments.
  • The target must log messages to the console using console.log for informational messages and console.error for error messages.
  • The simulated build script should be represented by a function that can throw an error.
  • The target must handle errors thrown by the simulated build script gracefully.
  • The target should not require any external dependencies beyond Angular Architect.

Notes

  • Consider using try...catch blocks to handle potential errors during the execution of the simulated build script.
  • The Architect object provides access to other Architect targets and utilities. However, for this challenge, you only need to use it for its type definition.
  • Focus on the structure and error handling of the target function. The specific implementation of the simulated build script is not critical.
  • The options object is provided for completeness but is not required for this challenge. You can safely ignore it.
import { Architect } from '@angular-devkit/architect';

export default function myCustomTarget(
  architect: Architect,
  options: any
): any {
  const projectName = 'my-app'; // Replace with a way to get the project name if needed

  console.log(`[INFO] Running my-custom-target for ${projectName}`);

  const simulateBuild = (): void => {
    return new Promise<void>((resolve, reject) => {
      setTimeout(() => {
        const shouldFail = Math.random() < 0.2; // Simulate a 20% chance of failure
        if (shouldFail) {
          reject(new Error('Simulated build script failed.'));
        } else {
          console.log('[INFO] Starting build process...');
          console.log('[INFO] Build process completed successfully.');
          resolve();
        }
      }, 50);
    });
  };

  try {
    await simulateBuild();
  } catch (error: any) {
    console.error(`[ERROR] Error during build process: ${error.message}`);
  }

  return {};
}
Loading editor...
typescript