CLI tool for scaffolding JavaScript and TypeScript third-party libraries with modern development practices
—
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Pending
The risk profile of this skill
Internal utilities for file operations, template processing, and JSON manipulation used by the @js-lib/cli tool. These utilities provide comprehensive file system operations for scaffolding and updating projects.
Check if files or directories exist at the specified path.
/**
* Check if a file or directory exists at the specified path
* @param cmdPath - Base directory path
* @param name - File or directory name to check
* @returns true if file/directory exists, false otherwise
*/
function checkProjectExists(cmdPath: string, name: string): boolean;Usage Examples:
const { checkProjectExists } = require('@js-lib/cli/util/file');
// Check if project directory exists
const exists = checkProjectExists(process.cwd(), 'my-project');
console.log(exists); // true or false
// Check if configuration file exists
const hasConfig = checkProjectExists('/path/to/project', 'jslib.json');Copy, create, and remove directories with comprehensive error handling.
/**
* Copy directory and all contents from source to destination
* @param from - Source directory path
* @param to - Destination directory path
* @param options - Copy options (optional)
*/
function copyDir(from: string, to: string, options?: object): void;
/**
* Remove directory and all contents
* @param dirPath - Directory path to remove
*/
function deleteDir(dirPath: string): void;Copy, read, and manipulate individual files with template processing support.
/**
* Copy file from source to destination
* @param from - Source file path
* @param to - Destination file path
* @param opt - Copy options with cover flag
*/
function copyFile(from: string, to: string, opt?: {cover: boolean}): void;
/**
* Copy template file with variable substitution
* @param from - Source template file path (.tmpl extension)
* @param to - Destination file path
* @param data - Template variables for substitution
* @param opt - Copy options with cover flag
*/
function copyTmpl(from: string, to: string, data?: object, opt?: {cover: boolean}): void;
/**
* Remove file if it exists
* @param filePath - File path to remove
*/
function deleteFile(filePath: string): void;Usage Examples:
const util = require('@js-lib/cli/util/copy');
// Copy regular file
util.copyFile('/source/file.js', '/dest/file.js', { cover: true });
// Process template with variables
util.copyTmpl('/templates/package.json.tmpl', '/project/package.json', {
name: 'my-project',
version: '1.0.0',
author: 'John Doe'
});Process template files with variable substitution using the template_js library.
/**
* Read and process template file with variable substitution
* @param from - Template file path
* @param data - Variables for template substitution
* @returns Processed template content as string
*/
function readTmpl(from: string, data?: object): string;
/**
* Read and parse JSON file
* @param from - JSON file path
* @returns Parsed JSON object
*/
function readJSON(from: string): object;Usage Examples:
const util = require('@js-lib/cli/util/copy');
// Process template file
const content = util.readTmpl('/templates/README.md.tmpl', {
projectName: 'My Awesome Project',
author: 'John Doe',
year: new Date().getFullYear()
});
// Read JSON configuration
const config = util.readJSON('/project/jslib.json');Merge, update, and manipulate JSON files programmatically.
/**
* Merge object into existing JSON file
* @param object - Object to merge into JSON file
* @param to - Path to JSON file to update
*/
function mergeObj2JSON(object: object, to: string): void;
/**
* Merge JSON file into another JSON file
* @param from - Source JSON file path
* @param to - Destination JSON file path
*/
function mergeJSON2JSON(from: string, to: string): void;
/**
* Process template file as JSON and merge into target JSON file
* @param from - Template JSON file path (.tmpl extension)
* @param to - Destination JSON file path
* @param data - Template variables for substitution
*/
function mergeTmpl2JSON(from: string, to: string, data?: object): void;
/**
* Delete specified keys from JSON file
* @param keysObj - Object structure defining keys to delete
* @param to - Path to JSON file to modify
*/
function deleteJSONKeys(keysObj: object, to: string): void;Usage Examples:
const util = require('@js-lib/cli/util/copy');
// Merge object into package.json
util.mergeObj2JSON({
scripts: {
test: 'mocha',
build: 'rollup -c'
},
devDependencies: {
mocha: '^10.0.0'
}
}, '/project/package.json');
// Remove deprecated fields
util.deleteJSONKeys({
'jsnext:main': undefined,
deprecated: undefined
}, '/project/package.json');Search, replace, and modify file contents with regex and line-based operations.
/**
* Replace lines in file that match regex pattern
* @param filepath - File path to modify
* @param match - Regular expression to match lines
* @param to - Replacement text
*/
function replaceFileLine(filepath: string, match: RegExp, to: string): void;
/**
* Delete lines in file that match regex pattern
* @param filepath - File path to modify
* @param match - Regular expression to match lines to delete
*/
function deleteFileLine(filepath: string, match: RegExp): void;
/**
* Replace text in file using multiple find/replace operations
* @param filepath - File path to modify
* @param replacerList - Array of replacement operations
*/
function replaceFileText(filepath: string, replacerList: Array<{
from: RegExp | string;
to: string | Function;
}>): void;
/**
* Insert text into file at specified line
* @param text - Text to insert
* @param filepath - File path to modify (created if doesn't exist)
* @param line - Line number to insert at (-1 for end of file)
*/
function insertText2File(text: string, filepath: string, line?: number): void;Usage Examples:
const util = require('@js-lib/cli/util/copy');
// Replace version line in file
util.replaceFileLine('/project/VERSION', /^version:/, 'version: 2.0.0');
// Complex text replacements
util.replaceFileText('/project/config.js', [
{
from: /const VERSION = '.*?'/,
to: "const VERSION = '2.0.0'"
},
{
from: 'oldFunctionName',
to: 'newFunctionName'
}
]);
// Add line to end of file
util.insertText2File('export default MyClass;', '/project/index.js');Console logging with colored output for better CLI user experience.
/**
* Initialize enhanced console logging with colored output
* Adds console.success method with green text
* Enhances console.error with red text
* Enhances console.warn with orange text
* Enhances console.info with blue text
*/
function init(): void;Usage Examples:
const log = require('@js-lib/cli/util/log');
// Initialize enhanced logging
log.init();
// Use enhanced console methods
console.success('Project created successfully!');
console.error('Build failed!');
console.warn('Using deprecated feature');
console.info('Starting build process...');// File copy options
interface CopyOptions {
cover?: boolean; // Whether to overwrite existing files (default: true)
}
// Text replacement operation
interface TextReplacer {
from: RegExp | string; // Pattern to find
to: string | Function; // Replacement text or function
}
// Template data for variable substitution
interface TemplateData {
[key: string]: any; // Template variables
}