Legacy wrapper providing comprehensive Nx devkit utilities for creating plugins, generators, and executors
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Utility functions for string manipulation, path operations, and code transformations. These utilities provide consistent naming conventions, path handling, and string processing across different platforms and use cases.
Functions for generating consistent naming conventions from base strings.
/**
* Generate various name formats from a base name
* @param name - Base name to transform
* @returns Object with different name formats
*/
function names(name: string): {
/** Original name as provided */
name: string;
/** PascalCase class name (e.g., "MyComponent") */
className: string;
/** camelCase property name (e.g., "myComponent") */
propertyName: string;
/** CONSTANT_CASE name (e.g., "MY_COMPONENT") */
constantName: string;
/** kebab-case file name (e.g., "my-component") */
fileName: string;
};Usage Examples:
import { names } from "@nrwl/devkit";
function generateNames() {
const nameVariants = names('my-awesome-feature');
console.log(nameVariants);
// Output:
// {
// name: 'my-awesome-feature',
// className: 'MyAwesomeFeature',
// propertyName: 'myAwesomeFeature',
// constantName: 'MY_AWESOME_FEATURE',
// fileName: 'my-awesome-feature'
// }
// Use in template generation
const templateVars = {
...nameVariants,
template: '' // Remove .template extension
};
// Results in files like:
// - my-awesome-feature.component.ts (using fileName)
// - MyAwesomeFeature class (using className)
// - myAwesomeFeature property (using propertyName)
// - MY_AWESOME_FEATURE constant (using constantName)
}Functions for handling file paths consistently across platforms.
/**
* Join path fragments safely
* @param fragments - Path segments to join
* @returns Joined path string
*/
function joinPathFragments(...fragments: string[]): string;
/**
* Normalize file paths to use forward slashes
* @param osSpecificPath - OS-specific path to normalize
* @returns Normalized path with forward slashes
*/
function normalizePath(osSpecificPath: string): string;
/**
* Calculate relative path from a directory to workspace root
* @param dir - Directory path relative to workspace root
* @returns Relative path to workspace root (e.g., "../../")
*/
function offsetFromRoot(dir: string): string;
/**
* Workspace root directory (absolute path)
*/
const workspaceRoot: string;
/**
* @deprecated Use workspaceRoot instead
* Application root path
*/
const appRootPath: string;Usage Examples:
import {
joinPathFragments,
normalizePath,
offsetFromRoot,
workspaceRoot
} from "@nrwl/devkit";
function handlePaths() {
// Join path fragments safely
const configPath = joinPathFragments('libs', 'my-lib', 'src', 'config.ts');
console.log(configPath); // "libs/my-lib/src/config.ts"
// Handle Windows paths
const windowsPath = 'libs\\my-lib\\src\\index.ts';
const normalized = normalizePath(windowsPath);
console.log(normalized); // "libs/my-lib/src/index.ts"
// Calculate offset to workspace root
const libDir = 'libs/shared/utils';
const offset = offsetFromRoot(libDir);
console.log(offset); // "../../../"
// Get workspace root
console.log('Workspace root:', workspaceRoot);
// Build absolute paths
const absoluteConfigPath = joinPathFragments(
workspaceRoot,
'libs',
'my-lib',
'project.json'
);
}Functions for processing and transforming strings.
/**
* Remove common leading indentation from multi-line strings
* @param strings - Template string array
* @param values - Template interpolation values
* @returns String with leading indentation stripped
*/
function stripIndents(strings: TemplateStringsArray, ...values: any[]): string;Usage Examples:
import { stripIndents } from "@nrwl/devkit";
function generateCode() {
const className = 'MyService';
const interfaceName = 'IMyService';
// Generate formatted code with proper indentation
const code = stripIndents`
export interface ${interfaceName} {
process(data: any): Promise<void>;
validate(input: string): boolean;
}
export class ${className} implements ${interfaceName} {
async process(data: any): Promise<void> {
// Implementation here
console.log('Processing:', data);
}
validate(input: string): boolean {
return input && input.length > 0;
}
}
`;
console.log(code);
// Output will have proper indentation without leading spaces
}Advanced string manipulation for code modifications.
/**
* Types of string change operations
*/
enum ChangeType {
Insert = 'insert',
Delete = 'delete'
}
/**
* Base interface for string changes
*/
interface StringChange {
type: ChangeType;
index: number;
}
/**
* String insertion operation
*/
interface StringInsertion extends StringChange {
type: ChangeType.Insert;
/** Position to insert at */
index: number;
/** Text to insert */
text: string;
}
/**
* String deletion operation
*/
interface StringDeletion extends StringChange {
type: ChangeType.Delete;
/** Start position */
index: number;
/** Number of characters to delete */
length: number;
}
/**
* Union type for all string changes
*/
type StringChange = StringInsertion | StringDeletion;
/**
* Apply a series of changes to a string
* @param text - Original text
* @param changes - Array of changes to apply
* @returns Modified text
*/
function applyChangesToString(text: string, changes: StringChange[]): string;Usage Examples:
import {
applyChangesToString,
StringInsertion,
StringDeletion,
ChangeType
} from "@nrwl/devkit";
function modifySourceCode() {
const originalCode = `
export class MyClass {
constructor() {}
}
`;
const changes: (StringInsertion | StringDeletion)[] = [
// Insert a method after constructor
{
type: ChangeType.Insert,
index: originalCode.indexOf('constructor() {}') + 'constructor() {}'.length,
text: '\n\n public getValue(): string {\n return "value";\n }'
},
// Insert import at the beginning
{
type: ChangeType.Insert,
index: 0,
text: 'import { Injectable } from "@angular/core";\n\n'
}
];
const modifiedCode = applyChangesToString(originalCode, changes);
console.log(modifiedCode);
}Utilities for working with directory structures and file movements.
/**
* Move files from one directory to another and update import statements
* @param tree - Virtual file system tree
* @param from - Source directory path
* @param to - Destination directory path
* @param exclude - Files to exclude from moving
*/
function moveFilesToNewDirectory(
tree: Tree,
from: string,
to: string,
exclude?: string[]
): void;
/**
* Get workspace layout configuration
* @param tree - Virtual file system tree
* @returns Workspace layout information
*/
function getWorkspaceLayout(tree: Tree): {
appsDir: string;
libsDir: string;
npmScope?: string;
};
/**
* Extract layout directory information from configuration
* @param nxJson - Nx configuration object
* @returns Layout directory configuration
*/
function extractLayoutDirectory(nxJson?: NxJsonConfiguration): {
appsDir: string;
libsDir: string;
};Usage Examples:
import {
Tree,
moveFilesToNewDirectory,
getWorkspaceLayout
} from "@nrwl/devkit";
function reorganizeFiles(tree: Tree) {
// Get workspace layout
const layout = getWorkspaceLayout(tree);
console.log(`Apps in: ${layout.appsDir}`);
console.log(`Libs in: ${layout.libsDir}`);
// Move files from old location to new location
moveFilesToNewDirectory(
tree,
'libs/old-location',
'libs/new-location',
['*.spec.ts', '*.test.ts'] // Exclude test files
);
// This will:
// 1. Move all files from old-location to new-location
// 2. Update import statements that reference the moved files
// 3. Preserve directory structure
// 4. Skip excluded files
}Additional string processing functions for specialized use cases.
/**
* Convert string to various naming conventions
* @param str - Input string
* @returns Object with different naming formats
*/
function classify(str: string): string;
function camelize(str: string): string;
function underscore(str: string): string;
function dasherize(str: string): string;
function capitalize(str: string): string;
/**
* Check if string is a valid identifier
* @param str - String to check
* @returns Whether string is a valid identifier
*/
function isValidIdentifier(str: string): boolean;
/**
* Escape string for use in regular expressions
* @param str - String to escape
* @returns Escaped string
*/
function escapeRegExp(str: string): string;Usage Examples:
import {
classify,
camelize,
underscore,
dasherize,
isValidIdentifier
} from "@nrwl/devkit";
function stringTransformations() {
const input = 'my-awesome-component';
console.log('Classify:', classify(input)); // "MyAwesomeComponent"
console.log('Camelize:', camelize(input)); // "myAwesomeComponent"
console.log('Underscore:', underscore(input)); // "my_awesome_component"
console.log('Dasherize:', dasherize(input)); // "my-awesome-component"
// Validation
console.log('Valid identifier:', isValidIdentifier('myVar')); // true
console.log('Valid identifier:', isValidIdentifier('my-var')); // false
console.log('Valid identifier:', isValidIdentifier('123var')); // false
}Install with Tessl CLI
npx tessl i tessl/npm-nrwl--devkit