{"version":3,"file":"create-DPbQZOuA.mjs","names":[],"sources":["../src/create/index.ts"],"sourcesContent":["/**\n * Project scaffolding for `pagesmith create`.\n *\n * Supports local templates (bundled) and remote templates (GitHub examples).\n */\n\nimport {\n  cpSync,\n  existsSync,\n  mkdirSync,\n  mkdtempSync,\n  readdirSync,\n  readFileSync,\n  rmSync,\n  unlinkSync,\n  writeFileSync,\n} from \"fs\";\nimport { join, resolve } from \"path\";\nimport { execFileSync, execSync } from \"child_process\";\nimport { tmpdir } from \"os\";\n\nconst GITHUB_REPO = \"sujeet-pro/pagesmith\";\n\nfunction getPackageVersion(): string {\n  try {\n    const pkgPath = resolve(import.meta.dirname, \"..\", \"..\", \"package.json\");\n    const pkg = JSON.parse(readFileSync(pkgPath, \"utf-8\")) as { version?: string };\n    return `^${pkg.version ?? \"0.1.0\"}`;\n  } catch {\n    return \"^0.1.0\";\n  }\n}\n\nexport type Template = {\n  name: string;\n  description: string;\n  source: \"local\" | \"github\";\n  path: string;\n  dependency: \"@pagesmith/core\" | \"@pagesmith/site\" | \"@pagesmith/docs\";\n  scripts: Record<string, string>;\n};\n\nexport const templates: Template[] = [\n  {\n    name: \"docs\",\n    description: \"Documentation site with @pagesmith/docs\",\n    source: \"local\",\n    path: \"templates/docs\",\n    dependency: \"@pagesmith/docs\",\n    scripts: {\n      dev: \"pagesmith-docs dev\",\n      build: \"pagesmith-docs build\",\n      preview: \"pagesmith-docs preview\",\n    },\n  },\n  {\n    name: \"blog\",\n    description: \"Blog with custom layouts using @pagesmith/site\",\n    source: \"github\",\n    path: \"examples/blog-site\",\n    dependency: \"@pagesmith/site\",\n    scripts: { dev: \"vp dev\", build: \"vp build\", check: \"vp check\" },\n  },\n  {\n    name: \"react\",\n    description: \"React SSG site with react-router\",\n    source: \"github\",\n    path: \"examples/frameworks/with-react\",\n    dependency: \"@pagesmith/site\",\n    scripts: {\n      dev: \"vp dev\",\n      build: \"vp build\",\n      check: \"vp check\",\n      preview: \"vp preview\",\n    },\n  },\n  {\n    name: \"solid\",\n    description: \"SolidJS SSG site\",\n    source: \"github\",\n    path: \"examples/frameworks/with-solid\",\n    dependency: \"@pagesmith/site\",\n    scripts: {\n      dev: \"vp dev\",\n      build: \"vp build\",\n      check: \"vp check\",\n      preview: \"vp preview\",\n    },\n  },\n  {\n    name: \"svelte\",\n    description: \"Svelte SSG site\",\n    source: \"github\",\n    path: \"examples/frameworks/with-svelte\",\n    dependency: \"@pagesmith/site\",\n    scripts: {\n      dev: \"vp dev\",\n      build: \"vp build\",\n      check: \"vp check\",\n      preview: \"vp preview\",\n    },\n  },\n  {\n    name: \"ejs\",\n    description: \"Vanilla Node.js + EJS templates\",\n    source: \"github\",\n    path: \"examples/frameworks/with-vanilla-ejs\",\n    dependency: \"@pagesmith/site\",\n    scripts: {\n      dev: \"vp dev\",\n      build: \"vp build\",\n      check: \"vp check\",\n    },\n  },\n  {\n    name: \"hbs\",\n    description: \"Vanilla Node.js + Handlebars templates\",\n    source: \"github\",\n    path: \"examples/frameworks/with-vanilla-hbs\",\n    dependency: \"@pagesmith/site\",\n    scripts: {\n      dev: \"vp dev\",\n      build: \"vp build\",\n      check: \"vp check\",\n    },\n  },\n];\n\nexport function listTemplates(): string {\n  const maxName = Math.max(...templates.map((t) => t.name.length));\n  return templates.map((t) => `  ${t.name.padEnd(maxName + 2)} ${t.description}`).join(\"\\n\");\n}\n\nasync function downloadFromGithub(templatePath: string, destination: string): Promise<void> {\n  const tarballUrl = `https://github.com/${GITHUB_REPO}/archive/refs/heads/main.tar.gz`;\n  console.info(\"Downloading template from GitHub...\");\n\n  const response = await fetch(tarballUrl, { redirect: \"follow\" });\n  if (!response.ok) {\n    throw new Error(`Failed to download: ${response.status} ${response.statusText}`);\n  }\n\n  const tmpFile = join(tmpdir(), `pagesmith-${Date.now()}.tar.gz`);\n  const tmpExtract = mkdtempSync(join(tmpdir(), \"pagesmith-\"));\n\n  try {\n    writeFileSync(tmpFile, Buffer.from(await response.arrayBuffer()));\n\n    // Extract full tarball\n    execSync(`tar xzf ${tmpFile} -C ${tmpExtract}`, { stdio: \"pipe\" });\n\n    // Find the extracted top-level directory\n    const dirs = readdirSync(tmpExtract);\n    const topDir = dirs[0];\n    if (!topDir) throw new Error(\"Empty tarball\");\n\n    const sourcePath = join(tmpExtract, topDir, templatePath);\n    if (!existsSync(sourcePath)) {\n      throw new Error(`Template path not found in repo: ${templatePath}`);\n    }\n\n    mkdirSync(destination, { recursive: true });\n    cpSync(sourcePath, destination, { recursive: true });\n  } finally {\n    unlinkSync(tmpFile);\n    rmSync(tmpExtract, { recursive: true, force: true });\n  }\n}\n\nfunction adaptForStandalone(destination: string, template: Template): void {\n  // Update content.config.mjs paths (from ./posts to ./content/posts)\n  const configPath = join(destination, \"content.config.mjs\");\n  if (existsSync(configPath)) {\n    let config = readFileSync(configPath, \"utf-8\");\n    config = config.replace(/directory: '\\.\\/posts'/g, \"directory: './content/posts'\");\n    config = config.replace(/directory: '\\.\\/pages'/g, \"directory: './content/pages'\");\n    config = config.replace(/directory: '\\.\\/authors'/g, \"directory: './content/authors'\");\n    config = config.replace(/directory: '\\.\\/config'/g, \"directory: './content/config'\");\n    writeFileSync(configPath, config);\n  }\n\n  // Update vite.config.ts shared-content references\n  const viteConfigPath = join(destination, \"vite.config.ts\");\n  if (existsSync(viteConfigPath)) {\n    let config = readFileSync(viteConfigPath, \"utf-8\");\n    config = config.replace(\n      /import content from '\\.\\.\\/shared-content\\/content\\.config'/g,\n      \"import content from './content.config.mjs'\",\n    );\n    config = config.replace(\n      /configPath:\\s*'\\.\\.\\/shared-content\\/content\\.config\\.ts'/g,\n      \"configPath: './content.config.mjs'\",\n    );\n    config = config.replace(\n      /root:\\s*resolve\\(import\\.meta\\.dirname,\\s*'\\.\\.\\/shared-content'\\)/g,\n      \"root: import.meta.dirname\",\n    );\n    writeFileSync(viteConfigPath, config);\n  }\n\n  // Update build.mjs (remove shared-content references)\n  const buildPath = join(destination, \"build.mjs\");\n  if (existsSync(buildPath)) {\n    let script = readFileSync(buildPath, \"utf-8\");\n    // Replace shared-content imports\n    script = script.replace(\n      /import .* from '\\.\\.\\/shared-content\\/content\\.config\\.mjs'/g,\n      \"import content from './content.config.mjs'\",\n    );\n    // Simplify content layer setup\n    script = script.replace(\n      /const contentRoot = resolve\\(root, '\\.\\.\\/shared-content'\\)/g,\n      \"const contentRoot = root\",\n    );\n    // Fix named imports\n    script = script.replace(\n      /import \\{ pages, posts \\} from '\\.\\.\\/shared-content\\/content\\.config\\.mjs'/g,\n      \"import content from './content.config.mjs'\\nconst { pages, posts } = content\",\n    );\n    writeFileSync(buildPath, script);\n  }\n\n  // Remove workspace-specific tsconfig paths\n  const tsconfigPath = join(destination, \"tsconfig.json\");\n  if (existsSync(tsconfigPath)) {\n    const tsconfig = JSON.parse(readFileSync(tsconfigPath, \"utf-8\"));\n    if (tsconfig.compilerOptions?.paths) {\n      delete tsconfig.compilerOptions.paths;\n    }\n    writeFileSync(tsconfigPath, JSON.stringify(tsconfig, null, 2) + \"\\n\");\n  }\n}\n\nfunction writePackageJson(destination: string, projectName: string, template: Template): void {\n  const existingPkg = join(destination, \"package.json\");\n  let pkg: Record<string, any> = {};\n\n  if (existsSync(existingPkg)) {\n    pkg = JSON.parse(readFileSync(existingPkg, \"utf-8\"));\n  }\n\n  pkg.name = projectName;\n  pkg.private = true;\n  pkg.type = \"module\";\n  pkg.version = \"0.0.0\";\n  pkg.scripts = template.scripts;\n\n  // Replace workspace \"*\" deps with actual versions\n  if (pkg.dependencies) {\n    for (const [name, version] of Object.entries(pkg.dependencies)) {\n      if (version === \"*\" && name.startsWith(\"@pagesmith/\")) {\n        pkg.dependencies[name] = getPackageVersion();\n      }\n    }\n  }\n\n  // Ensure the primary dependency is present\n  pkg.dependencies = pkg.dependencies ?? {};\n  pkg.dependencies[template.dependency] = getPackageVersion();\n\n  // Keep Vite+ in standalone examples that already use it\n  if (pkg.devDependencies?.vite && !pkg.devDependencies[\"vite-plus\"]) {\n    pkg.devDependencies[\"vite-plus\"] = \"^0.1.13\";\n  }\n\n  try {\n    const npmVersion = execFileSync(\"npm\", [\"--version\"], { encoding: \"utf-8\" }).trim();\n    pkg.packageManager = `npm@${npmVersion}`;\n  } catch {\n    // Omit packageManager if npm version cannot be detected\n  }\n\n  writeFileSync(existingPkg, JSON.stringify(pkg, null, 2) + \"\\n\");\n}\n\nexport async function createProject(projectName: string, templateName: string): Promise<void> {\n  const template = templates.find((t) => t.name === templateName);\n  if (!template) {\n    throw new Error(`Unknown template \"${templateName}\". Available templates:\\n${listTemplates()}`);\n  }\n\n  const destination = resolve(projectName);\n  if (existsSync(destination) && readdirSync(destination).length > 0) {\n    throw new Error(`Directory \"${projectName}\" already exists and is not empty.`);\n  }\n\n  if (template.source === \"local\") {\n    const templateDir = resolve(\n      import.meta.dirname,\n      \"../../templates\",\n      template.path.split(\"/\").pop()!,\n    );\n    mkdirSync(destination, { recursive: true });\n    cpSync(templateDir, destination, { recursive: true });\n  } else {\n    await downloadFromGithub(template.path, destination);\n    adaptForStandalone(destination, template);\n  }\n\n  writePackageJson(destination, projectName, template);\n\n  console.info(`\\nCreated \"${projectName}\" from the \"${template.name}\" template.\\n`);\n  console.info(\"Next steps:\");\n  console.info(`  cd ${projectName}`);\n  console.info(\"  vp install\");\n  if (template.scripts.dev) {\n    console.info(`  vp run dev`);\n  } else {\n    console.info(`  vp run build`);\n  }\n  console.info(\"\");\n}\n"],"mappings":";;;;;;;;;;AAqBA,MAAM,cAAc;AAEpB,SAAS,oBAA4B;CACnC,IAAI;EACF,MAAM,UAAU,QAAQ,OAAO,KAAK,SAAS,MAAM,MAAM,cAAc;EAEvE,OAAO,IADK,KAAK,MAAM,aAAa,SAAS,OAAO,CACvC,EAAE,WAAW;CAC5B,QAAQ;EACN,OAAO;CACT;AACF;AAWA,MAAa,YAAwB;CACnC;EACE,MAAM;EACN,aAAa;EACb,QAAQ;EACR,MAAM;EACN,YAAY;EACZ,SAAS;GACP,KAAK;GACL,OAAO;GACP,SAAS;EACX;CAC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