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
Safe file and directory operations with error handling and workspace-aware functionality. These utilities provide reliable file system operations for generators and other workspace tools.
Update JSON files safely with error handling and proper formatting.
/**
* Update a JSON file using the filesystem with a callback function
* @param path - Path of the JSON file on the filesystem
* @param callback - Function to manipulate the JSON data
*/
function updateJsonFile(path: string, callback: (json: any) => any): void;Usage Example:
import { updateJsonFile } from "@nx/workspace";
// Update package.json
updateJsonFile("./package.json", (json) => {
json.scripts = json.scripts || {};
json.scripts.build = "nx build";
json.scripts.test = "nx test";
return json;
});
// Update project configuration
updateJsonFile("./project.json", (json) => {
json.targets = json.targets || {};
json.targets.lint = {
executor: "@nx/linter:eslint",
options: {
lintFilePatterns: ["src/**/*.ts"]
}
};
return json;
});Copy files from source to target directory with stream-based operations.
/**
* Copy a file from source to target directory
* @param file - Source file path
* @param target - Target directory path
*/
function copyFile(file: string, target: string): void;Usage Example:
import { copyFile } from "@nx/workspace";
// Copy configuration file to new location
copyFile("./src/config/default.json", "./dist/config/");
// Copy template files
copyFile("./templates/component.ts", "./src/components/");Rename files and directories with comprehensive error handling and validation.
/**
* Safely rename files/directories with callback error handling
* @param from - Source path
* @param to - Destination path
* @param cb - Callback function receiving error or null on success
*/
function renameSync(
from: string,
to: string,
cb: (err: Error | null) => void
): void;Usage Example:
import { renameSync } from "@nx/workspace";
// Rename a project directory
renameSync(
"./libs/old-name",
"./libs/new-name",
(err) => {
if (err) {
console.error("Failed to rename:", err.message);
} else {
console.log("Successfully renamed project");
}
}
);
// Rename configuration file
renameSync(
"./workspace.json",
"./workspace.json.backup",
(err) => {
if (err) {
console.error("Backup failed:", err.message);
}
}
);Create directories with proper error handling (re-exported from Nx core utilities).
/**
* Create directory recursively if it doesn't exist
* @param path - Directory path to create
*/
function createDirectory(path: string): void;Usage Example:
import { createDirectory } from "@nx/workspace";
// Create nested directory structure
createDirectory("./libs/shared/components/ui");
// Create build output directory
createDirectory("./dist/apps/my-app");Utility functions for checking file and directory existence (re-exported from Nx core).
/**
* Check if a file exists
* @param path - File path to check
* @returns True if file exists, false otherwise
*/
function fileExists(path: string): boolean;
/**
* Check if a directory exists
* @param path - Directory path to check
* @returns True if directory exists, false otherwise
*/
function directoryExists(path: string): boolean;
/**
* Check if a path is relative
* @param path - Path to check
* @returns True if path is relative, false if absolute
*/
function isRelativePath(path: string): boolean;Usage Examples:
import { fileExists, directoryExists, isRelativePath } from "@nx/workspace";
// Check before performing operations
if (fileExists("./package.json")) {
console.log("Package.json found");
}
if (directoryExists("./libs/shared")) {
console.log("Shared library directory exists");
}
// Validate path format
if (isRelativePath("./src/app")) {
console.log("Using relative path");
}All file utilities include proper error handling:
type FileCallback = (err: Error | null) => void;Install with Tessl CLI
npx tessl i tessl/npm-nx--workspace