### Install next-zombie for development Source: https://github.com/relkimm/next-zombie/blob/main/README.md Install next-zombie as a development dependency using npm, pnpm, or yarn. This allows you to integrate it into your project's build scripts. ```bash npm install -D next-zombie ``` ```bash pnpm add -D next-zombie ``` ```bash yarn add -D next-zombie ``` -------------------------------- ### Run next-zombie via npx Source: https://github.com/relkimm/next-zombie/blob/main/README.md Execute next-zombie directly using npx for a quick start without installation. This command will automatically detect and run your project's development script. ```bash npx next-zombie ``` -------------------------------- ### Configure next-zombie in package.json Source: https://github.com/relkimm/next-zombie/blob/main/README.md Integrate next-zombie into your project by modifying the 'dev' script in your package.json. This ensures that 'next-zombie' is run whenever you start your development server. ```json { "scripts": { "dev": "next-zombie" } } ``` -------------------------------- ### CLI Entry Point Source: https://context7.com/relkimm/next-zombie/llms.txt The main command-line interface for next-zombie. It wraps your Next.js dev server with automatic restart and cache cleanup. It can run the default 'dev' script or a custom script, and can pass additional arguments. ```APIDOC ## CLI Entry Point ### Description Command-line interface that wraps your Next.js dev server with automatic restart and cache cleanup capabilities. Accepts optional script name and arguments, defaulting to running the 'dev' script. ### Usage ```bash # Run with default 'dev' script npx next-zombie # Run with custom script name npx next-zombie start # Pass additional arguments to the dev script npx next-zombie dev -- --port 3001 --hostname 0.0.0.0 # Use in package.json # package.json: # { # "scripts": { # "dev": "next-zombie" # } # } # # Then run with: npm run dev ``` ``` -------------------------------- ### buildArgs(script, extra) Source: https://context7.com/relkimm/next-zombie/llms.txt Constructs the full argument array for spawning a package manager process, correctly formatting the run command and including any extra arguments. ```APIDOC ## buildArgs(script, extra) ### Description Constructs the full argument array for spawning a package manager process, properly formatting the run command with extra arguments. ### Parameters #### Path Parameters - **script** (string) - Required - The name of the script to run (e.g., 'dev', 'build'). - **extra** (array) - Required - An array of additional arguments to pass to the script. ### Request Example ```javascript const { buildArgs } = require('./lib'); // Basic script execution const args1 = buildArgs('dev', []); console.log(args1); // Output: ['run', 'dev'] // Script with extra arguments (includes -- separator) const args2 = buildArgs('dev', ['--port', '3001']); console.log(args2); // Output: ['run', 'dev', '--', '--port', '3001'] // Different script const args3 = buildArgs('build', []); console.log(args3); // Output: ['run', 'build'] // Usage with spawn: const spawn = require('cross-spawn'); const pm = 'pnpm'; const args = buildArgs('dev', ['--port', '3001']); const child = spawn(pm, args, { stdio: 'inherit' }); ``` ### Response #### Success Response (200) - **args** (array) - The formatted array of arguments for the package manager process. ``` -------------------------------- ### JavaScript: buildArgs() for Constructing Spawn Arguments Source: https://context7.com/relkimm/next-zombie/llms.txt The buildArgs function constructs the argument array needed for spawning a child process via a package manager. It correctly formats the 'run' command and includes any extra arguments, separated by '--' when necessary. ```javascript const { buildArgs } = require('./lib'); // Basic script execution const args1 = buildArgs('dev', []); console.log(args1); // Output: ['run', 'dev'] // Script with extra arguments (includes -- separator) const args2 = buildArgs('dev', ['--port', '3001']); console.log(args2); // Output: ['run', 'dev', '--', '--port', '3001'] // Different script const args3 = buildArgs('build', []); console.log(args3); // Output: ['run', 'build'] // Usage with spawn: const spawn = require('cross-spawn'); const pm = 'pnpm'; const args = buildArgs('dev', ['--port', '3001']); const child = spawn(pm, args, { stdio: 'inherit' }); ``` -------------------------------- ### CLI Usage: next-zombie for Next.js Dev Servers Source: https://context7.com/relkimm/next-zombie/llms.txt Demonstrates how to use the next-zombie CLI to wrap and manage Next.js development servers. It shows default usage, custom script names, passing arguments, and integration within package.json scripts. ```bash # Run with default 'dev' script npx next-zombie # Run with custom script name npx next-zombie start # Pass additional arguments to the dev script npx next-zombie dev -- --port 3001 --hostname 0.0.0.0 # Use in package.json # package.json: { "scripts": { "dev": "next-zombie" } } # Then run with: npm run dev ``` -------------------------------- ### JavaScript: detectPM() for Package Manager Detection Source: https://context7.com/relkimm/next-zombie/llms.txt The detectPM function identifies the package manager (npm, pnpm, yarn, bun) used in a project by checking for lockfiles or the npm user agent. It provides a fallback mechanism and returns the detected package manager as a string. ```javascript const { detectPM } = require('./lib'); // Detect from current working directory const pm = detectPM(); console.log(pm); // Output: 'pnpm' (if pnpm-lock.yaml exists) // Detect from specific directory const pm = detectPM('/path/to/project'); console.log(pm); // Output: 'yarn' (if yarn.lock exists) // Detection priority: // 1. pnpm-lock.yaml → 'pnpm' // 2. yarn.lock → 'yarn' // 3. bun.lockb → 'bun' // 4. package-lock.json → 'npm' // 5. Fallback to npm_config_user_agent env var // 6. Default to 'npm' ``` -------------------------------- ### detectPM(cwd) Source: https://context7.com/relkimm/next-zombie/llms.txt Detects the package manager used in the project by checking for lockfiles. It supports pnpm, yarn, bun, and npm, with a fallback mechanism. ```APIDOC ## detectPM(cwd) ### Description Detects the package manager being used in the project by checking for lockfiles, with fallback to the npm user agent environment variable. Returns one of: 'pnpm', 'yarn', 'bun', or 'npm'. ### Parameters #### Path Parameters - **cwd** (string) - Optional - The directory to check for lockfiles. Defaults to the current working directory. ### Request Example ```javascript const { detectPM } = require('./lib'); // Detect from current working directory const pm = detectPM(); console.log(pm); // Output: 'pnpm' (if pnpm-lock.yaml exists) // Detect from specific directory const pm = detectPM('/path/to/project'); console.log(pm); // Output: 'yarn' (if yarn.lock exists) ``` ### Response #### Success Response (200) - **pm** (string) - The detected package manager ('pnpm', 'yarn', 'bun', or 'npm'). ### Detection Priority 1. pnpm-lock.yaml → 'pnpm' 2. yarn.lock → 'yarn' 3. bun.lockb → 'bun' 4. package-lock.json → 'npm' 5. Fallback to npm_config_user_agent env var 6. Default to 'npm' ``` -------------------------------- ### JavaScript: Auto-Recovery Process Flow for Next.js Source: https://context7.com/relkimm/next-zombie/llms.txt This JavaScript code demonstrates the core auto-recovery logic for a Next.js development server. It uses `cross-spawn` to monitor the server process, detects errors via stdout/stderr, clears the `.next` cache, and schedules restarts. ```javascript const spawn = require('cross-spawn'); const fs = require('fs'); const path = require('path'); const { detectPM, matchError, parseArgs, buildArgs } = require('./lib'); const CACHE = path.join(process.cwd(), '.next'); const DELAY = 500; // Wait before restart on detected error const INTERVAL = 200; // Wait before restart on crash let child = null; let pending = false; function clearCache() { if (!fs.existsSync(CACHE)) return; fs.rmSync(CACHE, { recursive: true, force: true }); console.log('Cleaned .next cache'); } function killChild() { if (!child) return; try { process.kill(-child.pid, 'SIGTERM'); // Kill process group } catch { child.kill('SIGTERM'); } } function start() { const pm = detectPM(); const { script, extra } = parseArgs(process.argv.slice(2)); const args = buildArgs(script, extra); console.log(`Starting: ${pm} ${args.join(' ')}`); child = spawn(pm, args, { stdio: ['inherit', 'pipe', 'pipe'], env: { ...process.env, FORCE_COLOR: '1' }, detached: true }); // Monitor stdout and stderr [child.stdout, child.stderr].forEach(stream => { stream.on('data', (data) => { const text = data.toString(); process.stdout.write(text); // Check for cache errors if (matchError(text) && !pending) { pending = true; console.log('Cache error detected! Restarting in 0.5s...'); setTimeout(() => { killChild(); }, DELAY); } }); }); child.on('exit', (code, signal) => { child = null; if (pending) { pending = false; clearCache(); setTimeout(start, INTERVAL); return; } if (code !== 0 && signal !== 'SIGINT') { console.log(`Crashed with code ${code}. Restarting...`); clearCache(); setTimeout(start, INTERVAL); } }); } // Handle Ctrl+C gracefully process.on('SIGINT', () => { killChild(); process.exit(0); }); // Initial startup clearCache(); start(); ``` -------------------------------- ### Run next-zombie with a custom script Source: https://github.com/relkimm/next-zombie/blob/main/README.md Execute a specific script defined in your package.json using next-zombie. This provides flexibility for different development server configurations. ```bash next-zombie start ``` -------------------------------- ### Run next-zombie with arguments Source: https://github.com/relkimm/next-zombie/blob/main/README.md Pass arguments to your Next.js development server when using next-zombie. This allows you to customize aspects like the port number. ```bash next-zombie dev --port 3001 ``` -------------------------------- ### parseArgs(args) Source: https://context7.com/relkimm/next-zombie/llms.txt Parses command-line arguments to extract the script name and any additional parameters intended for the Next.js script. ```APIDOC ## parseArgs(args) ### Description Parses command-line arguments to extract the script name and additional parameters. Returns an object with 'script' and 'extra' properties. ### Parameters #### Path Parameters - **args** (array) - Required - An array of command-line arguments. ### Request Example ```javascript const { parseArgs } = require('./lib'); // No arguments - defaults to 'dev' const result1 = parseArgs([]); console.log(result1); // Output: { script: 'dev', extra: [] } // Custom script name const result2 = parseArgs(['build']); console.log(result2); // Output: { script: 'build', extra: [] } // Script with extra arguments const result3 = parseArgs(['dev', '--port', '3001', '--turbo']); console.log(result3); // Output: { script: 'dev', extra: ['--port', '3001', '--turbo'] } // Arguments starting with dash (treated as flags to dev script) const result4 = parseArgs(['--port', '3001']); console.log(result4); // Output: { script: 'dev', extra: ['--port', '3001'] } ``` ### Response #### Success Response (200) - **script** (string) - The name of the script to run (defaults to 'dev'). - **extra** (array) - An array of additional arguments for the script. ``` -------------------------------- ### JavaScript: Next.js Error Pattern Matching for Restarts Source: https://context7.com/relkimm/next-zombie/llms.txt This JavaScript snippet defines and uses regular expression patterns to detect specific Next.js cache-related errors. These patterns are crucial for the auto-recovery mechanism to decide when to trigger a server restart. ```javascript const { PATTERNS } = require('./lib'); // Available patterns console.log(PATTERNS); // Output: [ // /_buildManifest/.js.tmp/, // /ENOENT:.*.next/ // ] // Test against various errors const errors = [ "Error: ENOENT: no such file or directory, open '.next/static/development/_buildManifest.js.tmp'", "ENOENT: no such file or directory, open '/path/.next/server/app/page.js'", "[Error: ENOENT: no such file or directory, open '.next/server/app/page/app-build-manifest.json']" ]; errors.forEach(error => { const matches = PATTERNS.some(pattern => pattern.test(error)); console.log(`"${error}" → ${matches ? 'RESTART' : 'IGNORE'}`); }); // Output: // ".next/static/development/_buildManifest.js.tmp" → RESTART // ".next/server/app/page.js" → RESTART // ".next/server/app/page/app-build-manifest.json" → RESTART ``` -------------------------------- ### matchError(text) Source: https://context7.com/relkimm/next-zombie/llms.txt Tests if a given text string contains error patterns associated with Next.js cache corruption. Returns true if any pattern matches, false otherwise. ```APIDOC ## matchError(text) ### Description Tests if a given text string contains error patterns associated with Next.js cache corruption. Returns true if any pattern matches, false otherwise. ### Parameters #### Path Parameters - **text** (string) - Required - The text string to test for error patterns. ### Request Example ```javascript const { matchError } = require('./lib'); // Match _buildManifest.js.tmp error const error1 = "Error: ENOENT: no such file or directory, open '.next/static/development/_buildManifest.js.tmp'"; console.log(matchError(error1)); // Output: true // Match .next ENOENT error const error2 = "ENOENT: no such file or directory, open '/path/.next/server/app/page.js'"; console.log(matchError(error2)); // Output: true // Non-matching errors console.log(matchError('SyntaxError: Unexpected token')); // Output: false console.log(matchError('TypeError: Cannot read property')); // Output: false console.log(matchError('GET /api/users 200 in 50ms')); // Output: false ``` ### Response #### Success Response (200) - **match** (boolean) - True if an error pattern is matched, false otherwise. ### Detected Patterns - /_buildManifest\.js\.tmp/ - /ENOENT:.*\.next/ ``` -------------------------------- ### JavaScript: parseArgs() for CLI Argument Parsing Source: https://context7.com/relkimm/next-zombie/llms.txt The parseArgs function processes command-line arguments for the CLI, separating the intended script name from any additional parameters. It returns an object containing the 'script' and an array of 'extra' arguments. ```javascript const { parseArgs } = require('./lib'); // No arguments - defaults to 'dev' const result1 = parseArgs([]); console.log(result1); // Output: { script: 'dev', extra: [] } // Custom script name const result2 = parseArgs(['build']); console.log(result2); // Output: { script: 'build', extra: [] } // Script with extra arguments const result3 = parseArgs(['dev', '--port', '3001', '--turbo']); console.log(result3); // Output: { script: 'dev', extra: ['--port', '3001', '--turbo'] } // Arguments starting with dash (treated as flags to dev script) const result4 = parseArgs(['--port', '3001']); console.log(result4); // Output: { script: 'dev', extra: ['--port', '3001'] } ``` -------------------------------- ### JavaScript: matchError() for Next.js Cache Error Detection Source: https://context7.com/relkimm/next-zombie/llms.txt The matchError function checks if a given string contains specific error patterns indicative of Next.js cache corruption or file system issues (like ENOENT). It returns true if a match is found, false otherwise. ```javascript const { matchError } = require('./lib'); // Match _buildManifest.js.tmp error const error1 = "Error: ENOENT: no such file or directory, open '.next/static/development/_buildManifest.js.tmp'"; console.log(matchError(error1)); // Output: true // Match .next ENOENT error const error2 = "ENOENT: no such file or directory, open '/path/.next/server/app/page.js'"; console.log(matchError(error2)); // Output: true // Non-matching errors console.log(matchError('SyntaxError: Unexpected token')); // Output: false console.log(matchError('TypeError: Cannot read property')); // Output: false console.log(matchError('GET /api/users 200 in 50ms')); // Output: false // Detected patterns: // - /_buildManifest\.js\.tmp/ // - /ENOENT:.*\.next/ ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.