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

FULL DISCUSSION
 

I recently migrated an existing enterprise scale angular application over to an nx workspace and extracted loads of modules into libraries. It's an awesome architecture to segregate teams and enforce ownership, but I'm having trouble getting all the rest of the devs to follow the "wizard" diagram when extracting modules to libs. I think the add-lib schematic you mention would be extremely useful. Would you be willing to provide more details on that schematic specifically?

 

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