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
Parsers and manipulators for workspace configuration and build targets. These utilities help manage Nx workspace configuration files and target definitions.
Locate workspace configuration files (deprecated but still functional).
/**
* Get the path to workspace configuration file
* @deprecated Nx no longer supports workspace.json
* @param host - The file system tree
* @returns Path to workspace config file or undefined
*/
function getWorkspacePath(host: Tree): string;Usage Example:
import { Tree } from "@angular-devkit/schematics";
import { getWorkspacePath } from "@nx/workspace";
function findWorkspaceConfig(tree: Tree) {
const workspacePath = getWorkspacePath(tree);
if (workspacePath) {
console.log("Found workspace config at:", workspacePath);
// Could be '/angular.json' or '/workspace.json'
} else {
console.log("No workspace configuration found");
}
}Parse target strings into structured objects for manipulation.
/**
* Parse a target string into its components
* @param targetString - Target string in format "project:target:config"
* @returns Parsed target object
*/
function parseTarget(targetString: string): ParsedTarget;
interface ParsedTarget {
/** Name of the project */
project: string;
/** Name of the target */
target: string;
/** Optional configuration name */
config?: string;
}Usage Examples:
import { parseTarget } from "@nx/workspace";
// Parse simple target
const simpleTarget = parseTarget("my-app:build");
console.log(simpleTarget);
// { project: "my-app", target: "build", config: undefined }
// Parse target with configuration
const configTarget = parseTarget("my-app:build:production");
console.log(configTarget);
// { project: "my-app", target: "build", config: "production" }
// Parse complex target
const complexTarget = parseTarget("libs-shared-ui:test:ci");
console.log(complexTarget);
// { project: "libs-shared-ui", target: "test", config: "ci" }Edit target configurations using callback functions.
/**
* Edit a target string using callback function
* @param targetString - Original target string
* @param callback - Function to modify the parsed target
* @returns Modified target string
*/
function editTarget(targetString: string, callback: (target: ParsedTarget) => ParsedTarget): string;Usage Examples:
import { editTarget } from "@nx/workspace";
// Change target name
const newTarget = editTarget("my-app:build:production", (target) => {
target.target = "compile";
return target;
});
console.log(newTarget); // "my-app:compile:production"
// Add configuration
const withConfig = editTarget("my-app:build", (target) => {
target.config = "development";
return target;
});
console.log(withConfig); // "my-app:build:development"
// Change project name
const renamedProject = editTarget("old-name:test", (target) => {
target.project = "new-name";
return target;
});
console.log(renamedProject); // "new-name:test"Convert target objects back to strings (deprecated utility).
/**
* Serialize a target object to string format
* @deprecated Use the utility from nx/src/utils instead
* @param target - Target object to serialize
* @returns Target string in "project:target:config" format
*/
function serializeTarget(target: ParsedTarget): string;Usage Example:
import { serializeTarget } from "@nx/workspace";
const target = {
project: "my-app",
target: "build",
config: "production"
};
const targetString = serializeTarget(target);
console.log(targetString); // "my-app:build:production"
// Without configuration
const simpleTarget = {
project: "my-lib",
target: "test"
};
const simpleString = serializeTarget(simpleTarget);
console.log(simpleString); // "my-lib:test"Target strings follow the format: project:target[:configuration]
// Simple targets
"my-app:build" // Build my-app with default configuration
"shared-lib:test" // Test shared-lib
"api:serve" // Serve the api project
// Targets with configuration
"my-app:build:production" // Build my-app with production configuration
"shared-lib:test:ci" // Test shared-lib with ci configuration
"api:serve:development" // Serve api with development configuration
// Complex project names
"libs-shared-ui:build" // Library with dashed name
"apps-admin-dashboard:serve:local" // Nested project with configurationimport { parseTarget, editTarget } from "@nx/workspace";
function updateTargetsForProject(targets: string[], newProjectName: string): string[] {
return targets.map(targetString => {
return editTarget(targetString, (target) => {
target.project = newProjectName;
return target;
});
});
}
const originalTargets = [
"old-app:build",
"old-app:test",
"old-app:serve:development"
];
const updatedTargets = updateTargetsForProject(originalTargets, "new-app");
console.log(updatedTargets);
// ["new-app:build", "new-app:test", "new-app:serve:development"]import { parseTarget, serializeTarget } from "@nx/workspace";
function addProductionConfig(targetString: string): string {
const target = parseTarget(targetString);
// Only add production config if no config exists
if (!target.config) {
target.config = "production";
}
return serializeTarget(target);
}
const targets = ["my-app:build", "my-app:test", "my-app:serve:development"];
const productionTargets = targets.map(addProductionConfig);
console.log(productionTargets);
// ["my-app:build:production", "my-app:test:production", "my-app:serve:development"]import { parseTarget } from "@nx/workspace";
function validateTargetString(targetString: string): boolean {
try {
const target = parseTarget(targetString);
return !!(target.project && target.target);
} catch {
return false;
}
}
function getValidTargets(targets: string[]): string[] {
return targets.filter(validateTargetString);
}
const mixedTargets = [
"valid-app:build",
"invalid",
"another-app:test:ci",
":build", // Missing project
"app:" // Missing target
];
const validTargets = getValidTargets(mixedTargets);
console.log(validTargets);
// ["valid-app:build", "another-app:test:ci"]interface ParsedTarget {
/** Name of the project */
project: string;
/** Name of the target */
target: string;
/** Optional configuration name */
config?: string;
}
type TargetCallback = (target: ParsedTarget) => ParsedTarget;Install with Tessl CLI
npx tessl i tessl/npm-nx--workspace