All files / packages/icons/scripts add-icons.ts

0% Statements 0/63
0% Branches 0/22
0% Functions 0/9
0% Lines 0/63

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147                                                                                                                                                                                                                                                                                                     
#!/usr/bin/env tsx
 
/**
 * Add Icons Script
 * Launches Next.js dev server and opens the workflow page
 */
 
import { spawn } from 'child_process';
import { readFile, writeFile, mkdir, access } from 'fs/promises';
import { join } from 'path';
 
const rootDir = join(import.meta.dirname, '..');
 
const WORKFLOW_PATH = join(rootDir, '.workflow');
const CONFIG_PATH = join(WORKFLOW_PATH, 'config.json');
 
interface WorkflowConfig {
  svgPath: string;
  csvPath: string;
}
 
async function main() {
  console.log('\nšŸ“¦ Icon Workflow Assistant Setup\n');
  console.log('NOTE: Before you start, please read the README file for instructions:');
  console.log('https://github.com/salesforce-ux-emu/icons/blob/main/README-dev.md\n');
 
  // Ensure .workflow directory exists
  try {
    await access(WORKFLOW_PATH);
  } catch {
    await mkdir(WORKFLOW_PATH, { recursive: true });
  }
 
  // Load or create config
  let config: WorkflowConfig = { svgPath: '', csvPath: '' };
 
  try {
    const configContent = await readFile(CONFIG_PATH, 'utf-8');
    config = JSON.parse(configContent);
    console.log('āœ… Loaded previous configuration from .workflow/config.json');
  } catch {
    // Config doesn't exist, create default
    await writeFile(CONFIG_PATH, JSON.stringify(config, null, 2), 'utf-8');
    console.log('āœ… Created .workflow/config.json for storing paths');
  }
 
  // Show config values if they exist
  if (config.csvPath || config.svgPath) {
    console.log('\nPrevious paths:');
    if (config.csvPath) {
      console.log(`  CSV: ${config.csvPath}`);
    }
    if (config.svgPath) {
      console.log(`  SVG: ${config.svgPath}`);
    }
  }
 
  console.log('\nšŸš€ Starting Next.js development server...\n');
 
  // Generate icons data first
  console.log('šŸ“Š Generating icons data...');
  const generateProcess = spawn('tsx', ['scripts/generate-icons-data.ts'], {
    cwd: rootDir,
    stdio: 'inherit',
    shell: true,
  });
 
  await new Promise<void>((resolve, reject) => {
    generateProcess.on('close', (code) => {
      if (code === 0) {
        resolve();
      } else {
        reject(new Error(`generate-icons-data.ts exited with code ${code}`));
      }
    });
 
    generateProcess.on('error', reject);
  });
 
  console.log('āœ… Icons data generated\n');
 
  // Start Next.js dev server
  const nextProcess = spawn('next', ['dev'], {
    cwd: rootDir,
    stdio: 'pipe',
    shell: true,
  });
 
  let serverReady = false;
 
  nextProcess.stdout?.on('data', (data) => {
    const output = data.toString();
    process.stdout.write(output);
 
    // Check if server is ready
    if (output.includes('Local:') && !serverReady) {
      serverReady = true;
 
      const urlMatch = output.match(/Local:\s+(http:\/\/localhost:\d+)/);
      const baseUrl = urlMatch ? urlMatch[1] : 'http://localhost:3000';
      const workflowUrl = `${baseUrl}/workflow`;
 
      console.log('\n✨ Server is ready!');
      console.log(`\nšŸŽÆ Open the workflow assistant in your browser:\n`);
      console.log(`   ${workflowUrl}\n`);
 
      // Try to open browser automatically
      const openCommand =
        process.platform === 'darwin' ? 'open' : process.platform === 'win32' ? 'start' : 'xdg-open';
 
      spawn(openCommand, [workflowUrl], {
        stdio: 'ignore',
        detached: true,
        shell: true,
      }).unref();
 
      console.log('šŸ“ Enter paths in the web interface to continue.\n');
    }
  });
 
  nextProcess.stderr?.on('data', (data) => {
    process.stderr.write(data);
  });
 
  nextProcess.on('close', (code) => {
    console.log(`\nNext.js dev server exited with code ${code}`);
    process.exit(code ?? 0);
  });
 
  // Handle exit signals
  process.on('SIGINT', () => {
    console.log('\n\nšŸ‘‹ Shutting down server...\n');
    nextProcess.kill('SIGINT');
    process.exit(0);
  });
 
  process.on('SIGTERM', () => {
    nextProcess.kill('SIGTERM');
    process.exit(0);
  });
}
 
main().catch((error) => {
  console.error('\nāŒ Error:', error);
  process.exit(1);
});