"use strict";var $e=Object.create;var b=Object.defineProperty;var he=Object.getOwnPropertyDescriptor;var be=Object.getOwnPropertyNames;var ve=Object.getPrototypeOf,Ee=Object.prototype.hasOwnProperty;var ke=(e,t)=>{for(var o in t)b(e,o,{get:t[o],enumerable:!0})},ie=(e,t,o,r)=>{if(t&&typeof t=="object"||typeof t=="function")for(let n of be(t))!Ee.call(e,n)&&n!==o&&b(e,n,{get:()=>t[n],enumerable:!(r=he(t,n))||r.enumerable});return e};var i=(e,t,o)=>(o=e!=null?$e(ve(e)):{},ie(t||!e||!e.__esModule?b(o,"default",{value:e,enumerable:!0}):o,e)),Ce=e=>ie(b({},"__esModule",{value:!0}),e);var Ne={};ke(Ne,{default:()=>we});module.exports=Ce(Ne);var W=i(require("path"));var J=i(require("path")),se=i(require("fs/promises")),d=require("@colors/colors/safe");async function v(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=i(require("path"));function E(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=i(require("path")),ce=i(require("prefers-yarn")),c=require("@colors/colors/safe");function k(e,t){let o=le.default.relative(process.cwd(),e);console.log(`\u{1F9E9} - ${(0,c.bold)((0,c.green)("Success!"))} Extension ${(0,c.bold)(t)} created.`);let r=(0,ce.default)()?"yarn":"npm run";console.log(`
Now ${(0,c.blue)(`cd ${(0,c.underline)(o)}`)} and ${(0,c.blue)((0,c.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 C(){console.log("You need to provide an extension name to create one. \nSee `--help` for command info.")}function S(){console.log("URLs are not allowed as a project path. Either write a name or a path to a local folder.")}var fe=i(require("path")),I=i(require("fs/promises")),$=require("@colors/colors/safe");var pe=i(require("fs/promises")),de=require("@colors/colors/safe");async function j(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 Se=["LICENSE","node_modules"];async function P(e,t){console.log(`\u{1F423} - Starting a new browser extension named ${(0,$.bold)(t)}...`);try{let o=await j(e,t);console.log("\u{1F91E} - Checking if destination path is writeable..."),o||(E(e),process.exit(1));let r=await I.default.readdir(e);console.log("\u{1F50E} - Scanning for potential conflicting files...");let n=await Promise.all(r.filter(s=>!s.startsWith(".")).filter(s=>!s.endsWith(".log")).filter(s=>!Se.includes(s)).map(async s=>(await I.default.lstat(fe.default.join(e,s))).isDirectory()?`${s}/`:`${s}`));n.length>0&&(await v(e,n),process.exit(1))}catch(o){console.error(`\u{1F9E9} ${(0,$.bold)("extension-create")} ${(0,$.red)("\u2716\uFE0E\u2716\uFE0E\u2716\uFE0E")} Can't create directory ${(0,$.bold)(t)}. ${o}`),process.exit(1)}}var M=i(require("path")),m=require("@colors/colors/safe");var T=i(require("fs/promises")),O=i(require("path"));async function D(e,t){let o=await T.default.readdir(e,{withFileTypes:!0});return await T.default.mkdir(t,{recursive:!0}),await Promise.all(o.map(async r=>{let n=O.default.join(e,r.name),s=O.default.join(t,r.name);r.isDirectory()?await D(n,s):await T.default.copyFile(n,s)}))}var Te=M.default.resolve(__dirname,"templates");async function R(e,t,o){let r=M.default.join(Te,o,"template");try{console.log(`\u{1F9F0} - Installing ${(0,m.bold)(t)} from ${(0,m.blue)((0,m.bold)(o))} template...`),await D(r,e)}catch(n){console.error(`\u{1F9E9} ${(0,m.bold)("extension-create")} ${(0,m.red)("\u2716\uFE0E\u2716\uFE0E\u2716\uFE0E")} Can't copy template ${(0,m.blue)((0,m.bold)(o))} for ${(0,m.bold)(t)}. ${n}`),process.exit(1)}}var h=i(require("path")),V=i(require("fs/promises")),f=require("@colors/colors/safe");var N=i(require("path"));function H(e,t){let o=N.default.basename(t),r=N.default.resolve(__dirname,"templates",o);return N.default.resolve(e,r)}function y(e){return e.startsWith("https://")}var De={dev:process.env.EXTENSION_ENV==="development"?"node node_modules/@extension-create/cli dev":"extension dev",start:process.env.EXTENSION_ENV==="development"?"node node_modules/@extension-create/cli start":"extension start",build:process.env.EXTENSION_ENV==="development"?"node node_modules/@extension-create/cli build":"extension build"};async function X(e,t,o){let r=H(process.cwd(),o),n=y(o)?h.default.join(e,"package.json"):h.default.join(r,"template.json"),s=await V.default.readFile(n),a=JSON.parse(s.toString());a.scripts=a.scripts||{},a.dependencies=a.dependencies||{},a.devDependencies={...a.devDependencies||{},extension:process.env.EXTENSION_ENV==="development"?"*":"latest"};let l={...a,name:h.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 V.default.writeFile(h.default.join(e,"package.json"),JSON.stringify(l,null,2))}catch(ne){console.error(`\u{1F9E9} ${(0,f.bold)("extension-create")} ${(0,f.red)("\u2716\uFE0E\u2716\uFE0E\u2716\uFE0E")} Can't write ${(0,f.yellow)("package.json")} for ${(0,f.bold)(t)}. ${ne}`),process.exit(1)}}var ye=i(require("path")),Y=require("cross-spawn"),g=require("@colors/colors/safe");function w(){let e=process.env.npm_config_user_agent;return e?e.startsWith("yarn")?"yarn":e.startsWith("pnpm")?"pnpm":"npm":"npm"}function ge(e){return w()==="yarn"?["install","--cwd",e,"--silent"]:["install","--prefix",`"${e}"`]}var ue=i(require("path")),U=require("cross-spawn");async function L(e){let t=ue.default.join(__dirname,"../../cli"),o=w(),r=["link"];await new Promise((n,s)=>{(0,U.spawn)(o,r,{cwd:t}).on("close",l=>{(0,U.spawn)(o,[...r,"@extension-create/cli"],{stdio:"inherit",cwd:e}).on("close",xe=>{xe===0?(console.log("Symlink created successfully."),n()):s(new Error(`Failed to create symlink: ${o} ${r.join(" ")}`))})})}),console.log("Symlink creation completed.")}async function q(e,t){let o=w(),r=ge(e);console.log("\u{1F6E0}  - Installing dependencies..."),process.env.EXTENSION_ENV==="development"&&await L(e);try{(0,Y.spawn)("mkdir",["-p",ye.default.join(e,"node_modules")],{stdio:"inherit"});let n=(0,Y.spawn)(o,r,{stdio:"inherit"});await new Promise((s,a)=>{n.on("close",l=>{l!==0?a(new Error({command:`${o} ${r.join(" ")}`}.toString())):s()}),n.on("error",l=>{console.error(`\u{1F9E9} ${(0,g.bold)("extension-create")} ${(0,g.red)("\u2716\uFE0E\u2716\uFE0E\u2716\uFE0E")} Child process error: Can't install dependencies for ${(0,g.bold)(t)}. ${l.message}`),a(l)})})}catch(n){console.log(JSON.stringify({command:`${o} ${r.join(" ")}`,error:n[0],message:n.message[0],projectPath:e},null,2)),console.error(`\u{1F9E9} ${(0,g.bold)("extension-create")} ${(0,g.red)("\u2716\uFE0E\u2716\uFE0E\u2716\uFE0E")} Can't install dependencies for ${(0,g.bold)(t)}. ${n.message||n.toString()}`),process.exit(1)}}var z=i(require("fs/promises")),p=require("@colors/colors/safe");async function B(e,t,o){console.log(`\u{1F9E9} ${(0,p.bold)("extension-create")} ${(0,p.red)("\u2716\uFE0E\u2716\uFE0E\u2716\uFE0E")} Aborting installation of ${o}.`),e.command?console.log(`\u{1F9E9} ${(0,p.bold)("extension-create")} ${(0,p.red)("\u2716\uFE0E\u2716\uFE0E\u2716\uFE0E")} ${e.command} has failed.`):(console.log(`\u{1F9E9} ${(0,p.bold)("extension-create")} \u{1F6A8}\u{1F6A8}\u{1F6A8} Unexpected creation error. This is a bug. Please report: "${e}"`),console.log((0,p.blue)((0,p.underline)("https://github.com/cezaraugusto/extension-create/issues/")))),console.log("\u{1F9F9} - Removing files generated from project in:"),console.log(`\`${t}\``),await z.default.mkdir(t,{recursive:!0}),await z.default.rm(t,{recursive:!0,force:!0}),process.exit(1)}var G=i(require("path")),F=i(require("fs/promises")),x=require("@colors/colors/safe");async function K(e,t,o){let r=G.default.join(e,"README.md");if(!(await F.default.lstat(r)).isFile()||y(o))return;let a=(await F.default.readFile(r,"utf-8")).replaceAll("[projectName]",t).replaceAll("[projectPackageManager]",w());try{console.log(`\u{1F4DD} - Writing ${(0,x.yellow)("README.md")} metadata...`),await F.default.writeFile(G.default.join(e,"README.md"),a)}catch(l){console.error(`\u{1F9E9} ${(0,x.bold)("extension-create")} ${(0,x.red)("\u2716\uFE0E\u2716\uFE0E\u2716\uFE0E")} Can't write the README.md file for ${(0,x.bold)(t)}. ${l}`),process.exit(1)}}var _=i(require("path")),Q=i(require("fs/promises")),u=require("@colors/colors/safe");async function Z(e,t,o){let r=_.default.join(e,"manifest.json"),n=await Q.default.readFile(r),a={...JSON.parse(n.toString()),name:_.default.basename(e)};try{console.log(`\u{1F4DC} - Writing ${(0,u.yellow)("manifest.json")} metadata...`),await Q.default.writeFile(_.default.join(e,"manifest.json"),JSON.stringify(a,null,2))}catch(l){console.error(`\u{1F9E9} ${(0,u.bold)("extension-create")} ${(0,u.red)("\u2716\uFE0E\u2716\uFE0E\u2716\uFE0E")} Can't write ${(0,u.yellow)("manifest.json")} for ${(0,u.bold)(t)}. ${l}`),process.exit(1)}}var ee=i(require("path")),te=i(require("fs/promises")),A=require("@colors/colors/safe");async function oe(e,t){let o=ee.default.join(e,"extension-env.d.ts"),r=process.env.EXTENSION_ENV==="development"?ee.default.resolve(process.cwd(),"programs/develop/types"):"@extension-create/develop/dist/types",n=`// Required extension-create 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 te.default.mkdir(e,{recursive:!0}),console.log(`\u{1F537} - Writing ${t} type definitions...`),await te.default.writeFile(o,n)}catch(s){console.error(`\u{1F9E9} ${(0,A.bold)("extension-create")} ${(0,A.red)("\u2716\uFE0E\u2716\uFE0E\u2716\uFE0E")} Failed to write the extension type definition. ${s}`),process.exit(1)}}function re(e){return y(e)?!1:e==="typescript"||e.startsWith("typescript-")||e.endsWith("-typescript")}async function we(e,{template:t="new"}){e||(C(),process.exit(1)),e.startsWith("http")&&(S(),process.exit(1));let o=W.default.isAbsolute(e)?e:W.default.join(process.cwd(),e),r=W.default.basename(o);try{await P(o,r),await R(o,r,t),await X(o,r,t),await q(o,r),await K(o,r,t),await Z(o,r,t),re(t)&&await oe(o,r),k(o,r)}catch(n){await B(n,o,r)}}
