/**
 * Minified by jsDelivr using Terser v5.39.0.
 * Original file: /npm/create-sst@2.43.5/src/index.mjs
 *
 * Do NOT use SRI with dynamically generated files! More information: https://www.jsdelivr.com/using-sri-with-dynamic-files
 */
import fs from"fs/promises";import fetch from"node-fetch";import path from"path";import{execSync}from"child_process";import{applyOperation}from"fast-json-patch/index.mjs";import{pathToFileURL}from"url";import{loadFile,writeFile}from"magicast";export function extract(){return{type:"extract"}}export function magicast(t){return{type:"magicast",...t}}export function remove(t){return{type:"remove",path:t}}export function patch(t){return{type:"patch",...t}}export function str_replace(t){return{type:"str_replace",...t}}export function append(t){return{type:"append",...t}}export function install(t){return{type:"install",...t}}export function cmd(t){return{type:"cmd",...t}}export function extend(t){return{type:"extend",path:t}}export async function execute(t){const e=path.resolve(t.source),a=(await import(pathToFileURL(path.join(e,"preset.mjs")).href)).default;for(const i of a)switch(i.type){case"magicast":{const e=path.join(t.destination,i.file),a=await loadFile(e);i.fn(a),await writeFile(a)}case"extract":{const a=path.join(e,"templates"),i=await listFiles(a);for(const e of i){const i=path.relative(a,e.replace("gitignore",".gitignore")),r=path.join(t.destination,i);await fs.mkdir(path.dirname(r),{recursive:!0}),await fs.copyFile(e,r)}break}case"extend":await execute({source:i.path,destination:t.destination,parameters:t.parameters,extended:!0});break;case"remove":await fs.rm(path.join(t.destination,i.path),{recursive:!0,force:!0});break;case"patch":{const e=path.join(t.destination,i.file),a=JSON.parse(await fs.readFile(e,"utf8"));for(const t of i.operations)applyOperation(a,t);await fs.writeFile(e,JSON.stringify(a,null,2));break}case"str_replace":{const e=path.join(t.destination,i.file),a=await fs.readFile(e,"utf8");await fs.writeFile(e,a.replace(i.pattern,i.replacement));break}case"append":{const e=path.join(t.destination,i.file);try{const t=await fs.readFile(e,"utf8");await fs.writeFile(e,`${t.trimEnd()}${i.string}`)}catch(t){if("ENOENT"!==t.code)throw t}break}case"cmd":execSync(i.cmd,{cwd:path.join(t.destination,i.cwd||"")});break;case"install":{const e=path.join(t.destination,i.path||".","package.json"),a=JSON.parse(await fs.readFile(e,"utf8")),r=i.dev?"devDependencies":"dependencies";a[r]=a[r]||{};const n=await Promise.all(i.packages.map((async t=>{let[,e]=t.substring(1).split("@");return e||(e="^"+await getLatestPackageVersion(t)),[t.replace("@"+e,""),e]})));for(const[t,e]of n)a[r][t]=e;await fs.writeFile(e,JSON.stringify(a,null,2));break}}if(!t.extended){const e=path.basename(t.destination).replace(/_/g,"-").replace(/[^A-Za-z0-9-]/g,""),a=e.replace(/[^a-zA-Z0-9]/g,"");for(const i of await listFiles(t.destination))if(!i.includes(".git")&&[".ts",".js",".tsx",".jsx",".json"].some((t=>i.endsWith(t))))try{let r=await fs.readFile(i,"utf8");r=r.replace(/\@\@app/g,e),r=r.replace(/\@\@normalizedapp/g,a),Object.entries(t.parameters).forEach((([t,e])=>{const a=new RegExp(`\\@\\@${t}`,"g");r=r.replace(a,e)})),await fs.writeFile(i,r)}catch(t){continue}}}async function listFiles(t){if(t.endsWith("node_modules"))return[];const e=[];for(const a of await fs.readdir(t)){const i=path.join(t,a);(await fs.stat(i)).isDirectory()?e.push(...await listFiles(i)):e.push(i)}return e}async function getLatestPackageVersion(t){return fetch(`https://registry.npmjs.org/${t}/latest`).then((t=>t.json())).then((t=>t.version))}
//# sourceMappingURL=/sm/adfc145875bcc401173c5e0b2d5b6f517da714f4ff225e1b9b4750c6cbe9ce5d.map