9pros/nextjs-auto-writer icon
public
Published on 5/19/2025
NextJS Auto Writer

Project File Manager

node -e const fs = require('fs');
const path = require('path');
const { execSync } = require('child_process');

// Parse input from continue.dev
const input = JSON.parse(process.argv[2] || '{}');
const operation = input.operation || '';

// Function to create a Next.js app
function createNextApp(name) {
  try {
    console.log(`Creating Next.js app: ${name}...`);
    execSync(`npx --yes create-next-app ${name}`, { stdio: 'inherit' });
    console.log(`Successfully created Next.js app: ${name}`);
  } catch (error) {
    console.error(`Error creating Next.js app:`, error);
  }
}

// Function to write or edit files automatically
function autoWriteFile(filePath, content) {
  try {
    // Ensure directory exists
    const dirPath = path.dirname(filePath);
    fs.mkdirSync(dirPath, { recursive: true });
    
    // Write the file
    fs.writeFileSync(filePath, content);
    console.log(`Successfully wrote to ${filePath}`);
  } catch (error) {
    console.error(`Error writing to ${filePath}:`, error);
  }
}

// Function to modify existing files
function modifyFile(filePath, searchText, replaceText) {
  try {
    if (!fs.existsSync(filePath)) {
      console.error(`File ${filePath} does not exist`);
      return;
    }
    
    let content = fs.readFileSync(filePath, 'utf8');
    const modifiedContent = content.replace(new RegExp(searchText, 'g'), replaceText);
    
    // Write the modified content back to the file
    fs.writeFileSync(filePath, modifiedContent);
    console.log(`Successfully modified ${filePath}`);
  } catch (error) {
    console.error(`Error modifying ${filePath}:`, error);
  }
}

// Handle operations based on the input
switch (operation) {
  case 'create-next-app':
    if (input.name) {
      createNextApp(input.name);
    } else {
      console.error('Missing app name for create-next-app operation');
    }
    break;
    
  case 'write-file':
    if (input.filePath && input.content) {
      autoWriteFile(input.filePath, input.content);
    } else {
      console.error('Missing filePath or content for write-file operation');
    }
    break;
    
  case 'modify-file':
    if (input.filePath && input.searchText && input.replaceText) {
      modifyFile(input.filePath, input.searchText, input.replaceText);
    } else {
      console.error('Missing required parameters for modify-file operation');
    }
    break;
    
  default:
    console.error('Unknown operation. Please specify one of: create-next-app, write-file, modify-file');
}