CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-nx--workspace

The Workspace plugin contains executors and generators that are useful for any Nx workspace and serves as a foundation for other plugins.

Pending

Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

Overview
Eval results
Files

project-types.mddocs/

Project Types

Enums and utilities for managing different types of projects in Nx workspaces. These utilities provide standardized project categorization and directory organization.

Capabilities

Project Type Enumeration

Standardized project types for workspace organization.

/**
 * Enumeration of supported project types in Nx workspace
 */
enum ProjectType {
  /** Applications - executable projects that can be served or built for deployment */
  Application = 'application',
  /** Libraries - reusable code packages that can be imported by other projects */
  Library = 'library'
}

Usage Example:

import { ProjectType } from "@nx/workspace";

// Check project type
function handleProject(type: string) {
  switch (type) {
    case ProjectType.Application:
      console.log("This is an application project");
      break;
    case ProjectType.Library:
      console.log("This is a library project");
      break;
    default:
      console.log("Unknown project type");
  }
}

// Use in generator schemas
interface ProjectSchema {
  name: string;
  type: ProjectType;
  directory?: string;
}

Directory Resolution

Get standard directory paths for different project types.

/**
 * Get the root directory for a given project type
 * @param projectType - The type of project
 * @returns Directory name for the project type
 */
function projectRootDir(projectType: ProjectType): string;

Usage Examples:

import { ProjectType, projectRootDir } from "@nx/workspace";

// Get standard directories
const appsDir = projectRootDir(ProjectType.Application);
console.log(appsDir); // "apps"

const libsDir = projectRootDir(ProjectType.Library);  
console.log(libsDir); // "libs"

// Use in project generation
function generateProject(name: string, type: ProjectType) {
  const rootDir = projectRootDir(type);
  const projectPath = `${rootDir}/${name}`;
  
  console.log(`Creating ${type} at ${projectPath}`);
  // Project creation logic here
}

// Generate different project types
generateProject("my-app", ProjectType.Application);     // Creates in apps/my-app
generateProject("shared-ui", ProjectType.Library);      // Creates in libs/shared-ui

Workspace Organization

Standard Directory Structure

Nx workspaces typically organize projects using these conventions:

workspace-root/
├── apps/                    # Applications (ProjectType.Application)
│   ├── web-app/            # Frontend application
│   ├── api/                # Backend API application  
│   └── mobile-app/         # Mobile application
├── libs/                   # Libraries (ProjectType.Library)
│   ├── shared/             # Shared utilities
│   │   ├── ui/             # UI component library
│   │   ├── data-access/    # Data access layer
│   │   └── utils/          # Utility functions
│   └── feature/            # Feature-specific libraries
│       ├── auth/           # Authentication feature
│       └── dashboard/      # Dashboard feature
└── tools/                  # Development tools and scripts

Project Type Characteristics

Applications (ProjectType.Application):

  • Executable projects that can be served, built, or deployed
  • Located in apps/ directory by default
  • Examples: web apps, APIs, mobile apps, desktop applications
  • Can have build, serve, test, lint, and e2e targets
  • May depend on libraries but libraries should not depend on applications

Libraries (ProjectType.Library):

  • Reusable code packages that can be imported by other projects
  • Located in libs/ directory by default
  • Examples: UI components, utilities, data access layers, business logic
  • Can have build, test, and lint targets
  • Can depend on other libraries and be depended upon by applications or other libraries

Advanced Usage

Custom Project Organization

import { ProjectType, projectRootDir } from "@nx/workspace";

function getProjectPath(name: string, type: ProjectType, scope?: string): string {
  const rootDir = projectRootDir(type);
  
  if (scope) {
    return `${rootDir}/${scope}/${name}`;
  }
  
  return `${rootDir}/${name}`;
}

// Scoped project organization
const uiLibPath = getProjectPath("button", ProjectType.Library, "shared");
console.log(uiLibPath); // "libs/shared/button"

const featureLibPath = getProjectPath("user-management", ProjectType.Library, "feature");
console.log(featureLibPath); // "libs/feature/user-management"

Type Guards and Validation

import { ProjectType } from "@nx/workspace";

function isApplicationProject(type: string): type is ProjectType.Application {
  return type === ProjectType.Application;
}

function isLibraryProject(type: string): type is ProjectType.Library {
  return type === ProjectType.Library;
}

// Validation function
function validateProjectType(type: string): ProjectType {
  if (isApplicationProject(type) || isLibraryProject(type)) {
    return type as ProjectType;
  }
  throw new Error(`Invalid project type: ${type}`);
}

// Usage in generators
function createProject(name: string, type: string) {
  const validatedType = validateProjectType(type);
  const directory = projectRootDir(validatedType);
  
  if (isApplicationProject(validatedType)) {
    // Application-specific setup
    console.log(`Setting up application in ${directory}/${name}`);
  } else {
    // Library-specific setup  
    console.log(`Setting up library in ${directory}/${name}`);
  }
}

Integration with Nx Devkit

These utilities integrate seamlessly with Nx Devkit functions:

import { Tree, addProjectConfiguration } from "@nx/devkit";
import { ProjectType, projectRootDir } from "@nx/workspace";

function addNxProject(
  tree: Tree, 
  name: string, 
  type: ProjectType,
  options: any = {}
) {
  const projectRoot = `${projectRootDir(type)}/${name}`;
  
  addProjectConfiguration(tree, name, {
    root: projectRoot,
    projectType: type,
    sourceRoot: `${projectRoot}/src`,
    targets: {
      build: {
        executor: type === ProjectType.Application 
          ? "@nx/webpack:webpack" 
          : "@nx/js:tsc"
      },
      test: {
        executor: "@nx/jest:jest"
      }
    },
    ...options
  });
}

Types

enum ProjectType {
  Application = 'application',
  Library = 'library'
}

type ProjectTypeString = 'application' | 'library';

Install with Tessl CLI

npx tessl i tessl/npm-nx--workspace

docs

cli-config.md

file-utilities.md

generators.md

index.md

linter.md

project-types.md

typescript-config.md

tile.json