All files / bin/commands init.js

100% Statements 274/274
94.44% Branches 17/18
100% Functions 1/1
100% Lines 274/274

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 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275250x 250x 250x 250x 250x 250x 250x 250x 250x 250x 250x 250x 250x 250x 250x 250x 250x 250x 51x 51x 51x 51x 51x 51x 51x 50x 50x 50x 50x 50x 50x 50x 50x 50x 51x 51x 51x 51x 51x 51x 51x 51x 51x 51x 51x 306x 306x 306x 300x 300x 51x 51x 51x 51x 51x 50x 50x 50x 50x 51x 51x 51x 51x 51x 51x 50x 50x 50x 50x 51x 51x 51x 50x 50x 50x 50x 51x 51x 51x 51x 50x 50x 50x 51x 51x 51x 51x 50x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 50x 49x 49x 49x 49x 49x 50x 50x 51x 51x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 51x 51x 51x 51x 50x 50x 50x 50x 50x 50x 50x 50x 50x 50x 50x 50x 50x 50x 50x 50x 50x 50x 50x 50x 50x 51x 51x 51x 51x 51x 50x 50x 50x 51x 51x 51x 51x 50x 50x 50x 50x 50x 50x 51x 51x 51x 51x 51x 51x 1x 1x 51x  
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
import { readTemplate } from '../utils.js';
import { runWizard } from '../wizard.js';
import { getInitialHtml } from './serve.js';
import { bold, cyan, green } from '../colors.js';
 
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const packageJson = JSON.parse(fs.readFileSync(path.join(__dirname, '../../package.json'), 'utf8'));
 
/**
 * Initializes a new Avenx project structure.
 * @param {object} cli - AvenxCLI instance.
 * @param {string[]} [args] - CLI arguments.
 */
export async function initProject(cli, args = []) {
  const { stylePreprocessor, layoutTemplate, isInteractive } = await runWizard(args);
 
  console.log(bold(cyan(`🚀 Initializing new Avenx-JS project (Style: ${stylePreprocessor}, Layout: ${layoutTemplate})...`)));
 
  // Write avenx.config.json if preprocessor option is configured
  const configPath = path.join(cli.baseDir, 'avenx.config.json');
  if (!fs.existsSync(configPath)) {
    const userConfig = {
      style: {
        preprocessor: stylePreprocessor,
      },
    };
    fs.writeFileSync(configPath, JSON.stringify(userConfig, null, 2) + '\n');
    console.log('  Created: avenx.config.json');
    cli.config = { ...cli.config, ...userConfig };
  }
 
  const dirs = [
    `${cli.config.srcDir}/components`,
    `${cli.config.srcDir}/pages`,
    `${cli.config.srcDir}/global`,
    `${cli.config.srcDir}/guards`,
    cli.config.distDir,
    '.vscode',
  ];
 
  dirs.forEach((dir) => {
    const fullPath = path.join(cli.baseDir, dir);
    const created = fs.mkdirSync(fullPath, { recursive: true });
    if (created) {
      console.log(`  Created: ${dir}`);
    }
  });
 
  // Create initial .vscode files
  const jsConfigPath = path.join(cli.baseDir, '.vscode/jsconfig.json');
  if (!fs.existsSync(jsConfigPath)) {
    const template = readTemplate(cli.baseDir, cli.config, cli.frameworkDir, 'vscode', 'jsconfig.json.template');
    fs.writeFileSync(jsConfigPath, template);
    console.log('  Created: .vscode/jsconfig.json');
  }
 
  // Teaches the editor's HTML mode about Avenx's own tags, which is what turns
  // component files from "markup the editor does not understand" into files
  // with completion and hover documentation. Referenced by settings.json.
  const htmlDataPath = path.join(cli.baseDir, '.vscode/avenx.html-data.json');
  if (!fs.existsSync(htmlDataPath)) {
    const template = readTemplate(cli.baseDir, cli.config, cli.frameworkDir, 'vscode', 'avenx.html-data.json.template');
    fs.writeFileSync(htmlDataPath, template);
    console.log('  Created: .vscode/avenx.html-data.json');
  }
 
  const settingsPath = path.join(cli.baseDir, '.vscode/settings.json');
  if (!fs.existsSync(settingsPath)) {
    const template = readTemplate(cli.baseDir, cli.config, cli.frameworkDir, 'vscode', 'settings.json.template');
    fs.writeFileSync(settingsPath, template);
    console.log('  Created: .vscode/settings.json');
  }
 
  // Create initial index.html
  const indexHtmlPath = path.join(cli.baseDir, 'index.html');
  if (!fs.existsSync(indexHtmlPath)) {
    fs.writeFileSync(indexHtmlPath, getInitialHtml(cli));
    console.log('  Created: index.html');
  }
 
  // Create initial main.app.js
  const mainAppPath = path.join(cli.baseDir, cli.config.srcDir, 'main.app.js');
  if (!fs.existsSync(mainAppPath)) {
    if (layoutTemplate === 'routing') {
      fs.writeFileSync(
        mainAppPath,
        "import { AvenxApp } from 'avenx-core/runtime';\n" +
          "import Navbar from './components/navbar/navbar.component.js';\n\n" +
          "const app = new AvenxApp({ target: '#app' });\n\n" +
          "app.register('Navbar', Navbar);\n\n" +
          'app.initRouter({\n' +
          "  '': 'Home',\n" +
          "  '#/': 'Home',\n" +
          "  '#/about': 'About',\n" +
          '});\n',
      );
    } else {
      fs.writeFileSync(
        mainAppPath,
        "import { AvenxApp } from 'avenx-core/runtime';\n\nconst app = new AvenxApp({ target: '#app' });\n",
      );
    }
    console.log(`  Created: ${cli.config.srcDir}/main.app.js`);
  }
 
  if (layoutTemplate === 'routing') {
    const homePageJsPath = path.join(cli.baseDir, cli.config.srcDir, 'pages', 'home.page.js');
    const homePageCssPath = path.join(cli.baseDir, cli.config.srcDir, 'pages', 'home.page.css');
    const aboutPageJsPath = path.join(cli.baseDir, cli.config.srcDir, 'pages', 'about.page.js');
    const aboutPageCssPath = path.join(cli.baseDir, cli.config.srcDir, 'pages', 'about.page.css');
 
    const navbarDir = path.join(cli.baseDir, cli.config.srcDir, 'components', 'navbar');
    if (!fs.existsSync(navbarDir)) {
      fs.mkdirSync(navbarDir, { recursive: true });
    }
    const navbarJsPath = path.join(navbarDir, 'navbar.component.js');
    const navbarCssPath = path.join(navbarDir, 'navbar.component.css');
 
    // Write Home page
    if (!fs.existsSync(homePageJsPath)) {
      fs.writeFileSync(
        homePageJsPath,
        '<state title="Home" />\n\n' +
          '<Navbar />\n\n' +
          '<div class="page-container">\n' +
          '    <h1>Home Page</h1>\n' +
          '    <p>Welcome to the home page of your new Avenx application!</p>\n' +
          '    <p>This layout template demonstrates hash-based routing using AvenxRouter.</p>\n' +
          '</div>\n',
      );
      console.log(`  Created: ${cli.config.srcDir}/pages/home.page.js`);
    }
    if (!fs.existsSync(homePageCssPath)) {
      fs.writeFileSync(
        homePageCssPath,
        '<@css>\n' +
          '.page-container {\n' +
          '    padding: 20px;\n' +
          '    font-family: sans-serif;\n' +
          '    max-width: 800px;\n' +
          '    margin: 0 auto;\n' +
          '}\n' +
          '</@css>\n',
      );
      console.log(`  Created: ${cli.config.srcDir}/pages/home.page.css`);
    }
 
    // Write About page
    if (!fs.existsSync(aboutPageJsPath)) {
      fs.writeFileSync(
        aboutPageJsPath,
        '<state title="About" />\n\n' +
          '<Navbar />\n\n' +
          '<div class="page-container">\n' +
          '    <h1>About Page</h1>\n' +
          '    <p>Welcome to the about page.</p>\n' +
          '</div>\n',
      );
      console.log(`  Created: ${cli.config.srcDir}/pages/about.page.js`);
    }
    if (!fs.existsSync(aboutPageCssPath)) {
      fs.writeFileSync(
        aboutPageCssPath,
        '<@css>\n' +
          '.page-container {\n' +
          '    padding: 20px;\n' +
          '    font-family: sans-serif;\n' +
          '    max-width: 800px;\n' +
          '    margin: 0 auto;\n' +
          '}\n' +
          '</@css>\n',
      );
      console.log(`  Created: ${cli.config.srcDir}/pages/about.page.css`);
    }
 
    // Write Navbar component
    if (!fs.existsSync(navbarJsPath)) {
      fs.writeFileSync(
        navbarJsPath,
        '<state activeRoute="" />\n\n' +
          '<nav>\n' +
          '    <@css container />\n' +
          '    <a @css link href="#/">Home</a>\n' +
          '    <a @css link href="#/about">About</a>\n' +
          '</nav>\n',
      );
      console.log(`  Created: ${cli.config.srcDir}/components/navbar/navbar.component.js`);
    }
    if (!fs.existsSync(navbarCssPath)) {
      fs.writeFileSync(
        navbarCssPath,
        '<@global>\n' +
          '    @def primary #6366f1;\n' +
          '    @def dark #1e1b4b;\n' +
          '    @def gray #e2e8f0;\n' +
          '</@global>\n\n' +
          '<@css>\n' +
          '    container {\n' +
          '        display: flex;\n' +
          '        gap: 1.5rem;\n' +
          '        padding: 1rem 2rem;\n' +
          '        background: @dark;\n' +
          '        border-bottom: 2px solid @primary;\n' +
          '    }\n\n' +
          '    link {\n' +
          '        color: white;\n' +
          '        text-decoration: none;\n' +
          '        font-weight: 500;\n' +
          '        font-family: sans-serif;\n' +
          '    }\n\n' +
          '    link:hover {\n' +
          '        color: @primary;\n' +
          '    }\n' +
          '</@css>\n',
      );
      console.log(`  Created: ${cli.config.srcDir}/components/navbar/navbar.component.css`);
    }
  }
 
  // Create initial package.json
  const packageJsonPath = path.join(cli.baseDir, 'package.json');
  if (!fs.existsSync(packageJsonPath)) {
    const projectName =
      path
        .basename(cli.baseDir)
        .toLowerCase()
        .replace(/[^a-z0-9-_]/g, '') || 'avenx-app';
    const packageContent = {
      name: projectName,
      version: '1.0.0',
      type: 'module',
      scripts: {
        dev: 'avenx serve',
        build: 'avenx build',
        serve: 'avenx serve',
      },
      dependencies: {
        'avenx-core': `^${packageJson.version}`,
      },
    };
    fs.writeFileSync(packageJsonPath, JSON.stringify(packageContent, null, 2) + '\n');
    console.log('  Created: package.json');
  }
 
  // Create initial .gitignore
  const gitignorePath = path.join(cli.baseDir, '.gitignore');
 
  if (!fs.existsSync(gitignorePath)) {
    fs.writeFileSync(gitignorePath, `node_modules/\n${cli.config.distDir}/\n.DS_Store\n`);
    console.log('  Created: .gitignore');
  }
  // Create initial .prettierrc
  const prettierPath = path.join(cli.baseDir, '.prettierrc');
 
  if (!fs.existsSync(prettierPath)) {
    const template = readTemplate(cli.baseDir, cli.config, cli.frameworkDir, 'init', '.prettierrc');
 
    fs.writeFileSync(prettierPath, template);
 
    console.log('  Created: .prettierrc');
  }
 
  // Last, so the success line is the last thing printed. It used to be
  // followed by another "Created:" line, which reads as work continuing after
  // the command claimed to be finished.
  console.log(green('✅ Project initialized successfully!'));
  if (isInteractive) {
    process.stdin.pause();
  }
}