re: How to Build Custom Tools in a Nrwl Nx Workspace VIEW POST

TOP OF THREAD FULL DISCUSSION
re: I recently migrated an existing enterprise scale angular application over to an nx workspace and extracted loads of modules into libraries. It's an...
 

Sure!

Since schematics don't allow conditional prompts, I used enquirer to create the wizard. Each response is saved in a variable. Those variables ultimately get sent to a function that builds the correct schematic execution string. A child_process is then spawned which executes the schematic correctly.

Here's a little bit of code for ideas:

buildExecString(options: UserResponses): string {
  let schematicType: 'angular' | 'workspace';

  // Angular schematics add an NgModule to the library.
  options.isAngular
    ? (schematicType = 'angular')
    : (schematicType = 'workspace');

  let commandBuilder: string[] = [`ng g @nrwl/${schematicType}:library`];

  if (options.domain === 'shared') {
    commandBuilder.push(options.libraryName);
    commandBuilder.push(`--directory=shared/${options.libraryType}`);
  } else if (options.domain === 'app-specific') {
    commandBuilder.push(`${options.libraryType}-${options.libraryName}`);
    commandBuilder.push(`--directory=${options.appName}`);
  } else if (options.domain === 'grouped app-specific') {
    commandBuilder.push(`${options.libraryType}-${options.libraryName}`);
    commandBuilder.push(`--directory=shared/${options.appName}`);
  }

  commandBuilder.push(`--tags=${this.buildTags(options)}`);

  if (options.isDryRun) {
    commandBuilder.push('--dry-run');
  }

  console.log(commandBuilder.join(' '));
  return commandBuilder.join(' ');
}
 
code of conduct - report abuse