create-netsuite-react-app
Version:
CLI tool to create a production-ready NetSuite React TypeScript app with full API integration
110 lines (96 loc) • 2.97 kB
JavaScript
import fs from 'fs-extra';
import path from 'path';
import chalk from 'chalk';
/**
* Validates project name for allowed characters and naming conventions
*/
export function validateProjectName(projectName) {
if (!projectName) {
return {
valid: false,
message: 'Project name cannot be empty'
};
}
// Only allow snake_case (lowercase with underscores)
if (!/^[a-z0-9_]+$/.test(projectName)) {
return {
valid: false,
message: 'Project name must be in snake_case (lowercase with underscores)'
};
}
return { valid: true };
}
/**
* Checks if a directory exists and is empty
*/
export function checkDirectoryExists(directoryPath) {
if (fs.existsSync(directoryPath)) {
const files = fs.readdirSync(directoryPath);
if (files.length > 0) {
return {
exists: true,
empty: false
};
}
return {
exists: true,
empty: true
};
}
return {
exists: false,
empty: true
};
}
/**
* Creates a directory if it doesn't exist
*/
export function createDirectoryIfNotExists(directoryPath) {
if (!fs.existsSync(directoryPath)) {
fs.mkdirSync(directoryPath, { recursive: true });
console.log(`${chalk.green('✓')} Created directory: ${directoryPath}`);
}
}
/**
* Copy template directory to destination
*/
export async function copyTemplateFiles(templatePath, destinationPath) {
await fs.copy(templatePath, destinationPath);
console.log(`${chalk.green('✓')} Copied template files to ${destinationPath}`);
}
/**
* Read and update content in a file
*/
export async function replaceInFile(filePath, replacements) {
if (!fs.existsSync(filePath)) {
console.log(`${chalk.yellow('⚠')} File not found: ${filePath}`);
return false;
}
let content = fs.readFileSync(filePath, 'utf8');
let changed = false;
for (const [pattern, newText] of replacements) {
const regex = new RegExp(pattern, 'g');
const originalContent = content;
// Debug: show what we're looking for
console.log(`Searching for pattern "${pattern}" in ${filePath}`);
if (regex.test(content)) {
content = content.replace(regex, newText);
if (content !== originalContent) {
changed = true;
console.log(`${chalk.green('✓')} Replaced pattern "${pattern}" in ${filePath}`);
console.log(` Old: ${originalContent.match(regex)?.[0] || 'not found'}`);
console.log(` New: ${newText}`);
}
} else {
console.log(`${chalk.yellow('⚠')} Pattern "${pattern}" not found in ${filePath}`);
// Show first few lines of file content for debugging
const lines = content.split('\n').slice(0, 5);
console.log(` File content preview:`, lines);
}
}
if (changed) {
fs.writeFileSync(filePath, content);
return true;
}
return false;
}