The Workspace plugin contains executors and generators that are useful for any Nx workspace and serves as a foundation for other plugins.
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Enums and utilities for managing different types of projects in Nx workspaces. These utilities provide standardized project categorization and directory organization.
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;
}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-uiNx 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 scriptsApplications (ProjectType.Application):
apps/ directory by defaultLibraries (ProjectType.Library):
libs/ directory by defaultimport { 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"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}`);
}
}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
});
}enum ProjectType {
Application = 'application',
Library = 'library'
}
type ProjectTypeString = 'application' | 'library';Install with Tessl CLI
npx tessl i tessl/npm-nx--workspace