"use strict";var he=Object.create;var v=Object.defineProperty;var $e=Object.getOwnPropertyDescriptor;var Ee=Object.getOwnPropertyNames;var be=Object.getPrototypeOf,ve=Object.prototype.hasOwnProperty;var ke=(e,t)=>{for(var o in t)v(e,o,{get:t[o],enumerable:!0})},ie=(e,t,o,r)=>{if(t&&typeof t=="object"||typeof t=="function")for(let n of Ee(t))!ve.call(e,n)&&n!==o&&v(e,n,{get:()=>t[n],enumerable:!(r=$e(t,n))||r.enumerable});return e};var s=(e,t,o)=>(o=e!=null?he(be(e)):{},ie(t||!e||!e.__esModule?v(o,"default",{value:e,enumerable:!0}):o,e)),Ce=e=>ie(v({},"__esModule",{value:!0}),e);var Fe={};ke(Fe,{default:()=>xe});module.exports=Ce(Fe);var j=s(require("path"));var J=s(require("path")),se=s(require("fs/promises")),d=require("@colors/colors/safe");async function k(e,t){let o=J.default.basename(e);console.error((0,d.bold)((0,d.red)(`
Conflict! Path to ${(0,d.underline)(o)} includes conflicting files:
`)));for(let r of t){let n=await se.default.lstat(J.default.join(e,r));console.error(n.isDirectory()?`\u{1F4C1} - ${r}`:`\u{1F4C4} - ${r}`)}console.error(`
You need to either rename/remove the files listed above, or choose a new directory name for your extension.`),console.error(`
${(0,d.bold)("Path to conflicting directory")}: \`${(0,d.underline)(e)}\``)}var ae=s(require("path"));function C(e){let t=ae.default.basename(e);console.error(`Failed to write in the destination directory. Path for \`${t}\` is not writable.`),console.error(`Ensure you have write permissions for this folder.
Path: ${t}`)}var le=s(require("path")),ce=s(require("prefers-yarn")),l=require("@colors/colors/safe");function T(e,t){let o=le.default.relative(process.cwd(),e);console.log(`\u{1F9E9} - ${(0,l.bold)((0,l.green)("Success!"))} Extension ${(0,l.bold)(t)} created.`);let r=(0,ce.default)()?"yarn":"npm run";console.log(`
Now ${(0,l.blue)(`cd ${(0,l.underline)(o)}`)} and ${(0,l.blue)((0,l.bold)(`${r} dev`))} to open a new browser instance
with your extension installed, loaded, and enabled for development.

You are ready. Time to hack on your extension!
  `)}function S(){console.log("You need to provide an extension name to create one. \nSee `--help` for command info.")}function D(){console.log("URLs are not allowed as a project path. Either write a name or a path to a local folder.")}var fe=s(require("path")),O=s(require("fs/promises")),h=require("@colors/colors/safe");var pe=s(require("fs/promises")),de=require("@colors/colors/safe");async function I(e,t){try{return console.log(`\u{1F91D} - Ensuring ${(0,de.bold)(t)} folder exists...`),await pe.default.mkdir(e,{recursive:!0}),!0}catch(o){return console.error("Error while checking directory writability:",o),!1}}var Te=["LICENSE","node_modules"];async function M(e,t){console.log(`\u{1F423} - Starting a new browser extension named ${(0,h.bold)(t)}...`);try{let o=await I(e,t);console.log("\u{1F91E} - Checking if destination path is writeable..."),o||(C(e),process.exit(1));let r=await O.default.readdir(e);console.log("\u{1F50E} - Scanning for potential conflicting files...");let n=await Promise.all(r.filter(i=>!i.startsWith(".")).filter(i=>!i.endsWith(".log")).filter(i=>!Te.includes(i)).map(async i=>(await O.default.lstat(fe.default.join(e,i))).isDirectory()?`${i}/`:`${i}`));n.length>0&&(await k(e,n),process.exit(1))}catch(o){console.error(`\u{1F9E9} ${(0,h.bold)("Extension")} ${(0,h.red)("\u2716\uFE0E\u2716\uFE0E\u2716\uFE0E")} Can't create directory ${(0,h.bold)(t)}. ${o}`),process.exit(1)}}var H=s(require("path")),c=require("@colors/colors/safe");var N=s(require("fs/promises")),R=s(require("path"));async function F(e,t){let o=await N.default.readdir(e,{withFileTypes:!0});return await N.default.mkdir(t,{recursive:!0}),await Promise.all(o.map(async r=>{let n=R.default.join(e,r.name),i=R.default.join(t,r.name);r.isDirectory()?await F(n,i):await N.default.copyFile(n,i)}))}var Se=H.default.resolve(__dirname,"templates");async function V(e,t,o){let r=H.default.join(Se,o,"template");try{console.log(`\u{1F9F0} - Installing ${(0,c.bold)(t)} from ${(0,c.blue)((0,c.bold)(o))} template...`),await F(r,e)}catch(n){console.error(`\u{1F9E9} ${(0,c.bold)("Extension")} ${(0,c.red)("\u2716\uFE0E\u2716\uFE0E\u2716\uFE0E")} Can't copy template ${(0,c.blue)((0,c.bold)(o))} for ${(0,c.bold)(t)}. ${n}`),process.exit(1)}}var b=s(require("path")),U=s(require("fs/promises")),f=require("@colors/colors/safe");var _=s(require("path"));function X(e,t){let o=_.default.basename(t),r=_.default.resolve(__dirname,"templates",o);return _.default.resolve(e,r)}function w(e){return e.startsWith("https://")}var De={dev:process.env.EXTENSION_ENV==="development"?"node node_modules/extension dev":"extension dev",start:process.env.EXTENSION_ENV==="development"?"node node_modules/extension start":"extension start",build:process.env.EXTENSION_ENV==="development"?"node node_modules/extension build":"extension build"};async function L(e,t,o){let r=X(process.cwd(),o),n=w(o)?b.default.join(e,"package.json"):b.default.join(r,"template.json"),i=await U.default.readFile(n),a=JSON.parse(i.toString());a.scripts=a.scripts||{},a.dependencies=a.dependencies||{},a.devDependencies={...a.devDependencies||{},extension:process.env.EXTENSION_ENV==="development"?"*":"latest"};let p={...a,name:b.default.basename(e),private:!0,version:"0.0.0",scripts:{...a.scripts,...De},dependencies:a.dependencies,devDependencies:a.devDependencies};try{console.log(`\u{1F4DD} - Writing ${(0,f.yellow)("package.json")} metadata...`),await U.default.writeFile(b.default.join(e,"package.json"),JSON.stringify(p,null,2))}catch(E){console.error(`\u{1F9E9} ${(0,f.bold)("extension")} ${(0,f.red)("\u2716\uFE0E\u2716\uFE0E\u2716\uFE0E")} Can't write ${(0,f.yellow)("package.json")} for ${(0,f.bold)(t)}. ${E}`),process.exit(1)}}var ue=s(require("path")),ye=require("cross-spawn"),we=require("child_process"),g=require("@colors/colors/safe");function $(){let e=process.env.npm_config_user_agent;return e?e.startsWith("yarn")?"yarn":e.startsWith("pnpm")?"pnpm":"npm":"npm"}var ge=s(require("path")),Y=require("cross-spawn");async function q(e){let t=ge.default.join(__dirname,"../../cli"),o=$(),r=["link"];await new Promise((n,i)=>{(0,Y.spawn)(o,r,{cwd:t}).on("close",p=>{(0,Y.spawn)(o,[...r,"extension"],{stdio:"inherit",cwd:e}).on("close",y=>{y===0?(console.log("Symlink created successfully."),n()):i(new Error(`Failed to create symlink: ${o} ${r.join(" ")}`))})})}),console.log("Symlink creation completed.")}function Ne(){return["install","--silent"]}async function z(e,t){let o=ue.default.join(e,"node_modules"),r=$(),n=Ne();console.log("\u{1F6E0}  - Installing dependencies..."),process.env.EXTENSION_ENV==="development"&&await q(e);try{let i=process.cwd();process.chdir(e),(0,we.execSync)(`mkdir -p ${o}`);let a=(0,ye.spawn)(r,n,{stdio:"inherit"});await new Promise((p,E)=>{a.on("close",y=>{process.chdir(i),y!==0?E(new Error(`Command ${r} ${n.join(" ")} failed with exit code ${y}`)):p()}),a.on("error",y=>{process.chdir(i),console.error(`\u{1F9E9} ${(0,g.bold)("Extension")} ${(0,g.red)("\u2716\uFE0E\u2716\uFE0E\u2716\uFE0E")} Child process error: Can't install dependencies for ${(0,g.bold)(t)}. ${y.message}`),E(y)})})}catch(i){console.error(`\u{1F9E9} ${(0,g.bold)("Extension")} ${(0,g.red)("\u2716\uFE0E\u2716\uFE0E\u2716\uFE0E")} Can't install dependencies for ${(0,g.bold)(t)}. ${i.message||i.toString()}`),process.exit(1)}}var B=s(require("fs/promises")),m=require("@colors/colors/safe");async function G(e,t,o){console.log(`\u{1F9E9} ${(0,m.bold)("Extension")} ${(0,m.red)("\u2716\uFE0E\u2716\uFE0E\u2716\uFE0E")} Aborting installation of ${o}.`),e.command?console.log(`\u{1F9E9} ${(0,m.bold)("Extension")} ${(0,m.red)("\u2716\uFE0E\u2716\uFE0E\u2716\uFE0E")} ${e.command} has failed.`):(console.log(`\u{1F9E9} ${(0,m.bold)("Extension")} \u{1F6A8}\u{1F6A8}\u{1F6A8} Unexpected creation error. This is a bug. Please report: "${e}"`),console.log((0,m.blue)((0,m.underline)("https://github.com/cezaraugusto/extension/issues/")))),console.log("\u{1F9F9} - Removing files generated from project in:"),console.log(`\`${t}\``),await B.default.mkdir(t,{recursive:!0}),await B.default.rm(t,{recursive:!0,force:!0}),process.exit(1)}var K=s(require("path")),P=s(require("fs/promises")),x=require("@colors/colors/safe");async function Q(e,t,o){let r=K.default.join(e,"README.md");if(!(await P.default.lstat(r)).isFile()||w(o))return;let a=(await P.default.readFile(r,"utf-8")).replaceAll("[projectName]",t).replaceAll("[projectPackageManager]",$());try{console.log(`\u{1F4DD} - Writing ${(0,x.yellow)("README.md")} metadata...`),await P.default.writeFile(K.default.join(e,"README.md"),a)}catch(p){console.error(`\u{1F9E9} ${(0,x.bold)("Extension")} ${(0,x.red)("\u2716\uFE0E\u2716\uFE0E\u2716\uFE0E")} Can't write the README.md file for ${(0,x.bold)(t)}. ${p}`),process.exit(1)}}var A=s(require("path")),Z=s(require("fs/promises")),u=require("@colors/colors/safe");async function ee(e,t,o){let r=A.default.join(e,"manifest.json"),n=await Z.default.readFile(r),a={...JSON.parse(n.toString()),name:A.default.basename(e)};try{console.log(`\u{1F4DC} - Writing ${(0,u.yellow)("manifest.json")} metadata...`),await Z.default.writeFile(A.default.join(e,"manifest.json"),JSON.stringify(a,null,2))}catch(p){console.error(`\u{1F9E9} ${(0,u.bold)("Extension")} ${(0,u.red)("\u2716\uFE0E\u2716\uFE0E\u2716\uFE0E")} Can't write ${(0,u.yellow)("manifest.json")} for ${(0,u.bold)(t)}. ${p}`),process.exit(1)}}var te=s(require("path")),oe=s(require("fs/promises")),W=require("@colors/colors/safe");async function re(e,t){let o=te.default.join(e,"extension-env.d.ts"),r=process.env.EXTENSION_ENV==="development"?te.default.resolve(process.cwd(),"programs/develop/types"):"@extension-create/develop/dist/types",n=`// Required Extension types for TypeScript projects.
// This file auto-generated and should not be excluded.
// If you need extra types, consider creating a new *.d.ts and
// referencing it in the "include" array in your tsconfig.json file.
// See https://www.typescriptlang.org/tsconfig#include for info.
/// <reference types="${r}/index.d.ts" />

// Polyfill types for browser.* APIs.
/// <reference types="${r}/polyfill.d.ts" />
`;try{await oe.default.mkdir(e,{recursive:!0}),console.log(`\u{1F537} - Writing ${t} type definitions...`),await oe.default.writeFile(o,n)}catch(i){console.error(`\u{1F9E9} ${(0,W.bold)("extension")} ${(0,W.red)("\u2716\uFE0E\u2716\uFE0E\u2716\uFE0E")} Failed to write the extension type definition. ${i}`),process.exit(1)}}function ne(e){return w(e)?!1:e==="typescript"||e.startsWith("typescript-")||e.endsWith("-typescript")}async function xe(e,{template:t="new"}){e||(S(),process.exit(1)),e.startsWith("http")&&(D(),process.exit(1));let o=j.default.isAbsolute(e)?e:j.default.join(process.cwd(),e),r=j.default.basename(o);try{await M(o,r),await V(o,r,t),await L(o,r,t),await z(o,r),await Q(o,r,t),await ee(o,r,t),ne(t)&&await re(o,r),T(o,r)}catch(n){await G(n,o,r)}}
