"use strict"; var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); // src/index.ts var import_chalk2 = __toESM(require("chalk"), 1); var import_figlet = __toESM(require("figlet"), 1); var import_commander = require("commander"); // src/actions/create.ts var import_got = __toESM(require("got"), 1); var import_prompts = __toESM(require("prompts"), 1); var import_tar = require("tar"); var import_util = require("util"); var import_stream = require("stream"); var import_fs2 = require("fs"); var import_child_process3 = require("child_process"); // src/helpers/resolvePkgManager.ts var import_child_process = require("child_process"); var resolvePkgManager = () => { try { const userAgent = process.env.npm_config_user_agent; if (userAgent?.startsWith("yarn")) { return "yarn"; } if (userAgent?.startsWith("pnpm")) { return "pnpm"; } try { (0, import_child_process.execSync)("yarn --version", { stdio: "ignore" }); return "yarn"; } catch (_) { (0, import_child_process.execSync)("pnpm --version", { stdio: "ignore" }); return "pnpm"; } } catch (_) { return "npm"; } }; // src/helpers/setProjectName.ts var import_os = require("os"); var import_path = require("path"); var import_fs = require("fs"); var setProjectName = (path, name) => { const packagePath = (0, import_path.join)(path, "package.json"); const packageContent = (0, import_fs.readFileSync)(packagePath); const packageJson = JSON.parse(packageContent.toString()); if (packageJson) { packageJson.name = name; } (0, import_fs.writeFileSync)(packagePath, JSON.stringify(packageJson, null, 2) + import_os.EOL); }; // src/helpers/tryGitInit.ts var import_child_process2 = require("child_process"); var tryGitInit = () => { try { (0, import_child_process2.execSync)("git --version", { stdio: "ignore" }); if (isInGitRepository() || isInMercurialRepository()) { return false; } (0, import_child_process2.execSync)("git init", { stdio: "ignore" }); (0, import_child_process2.execSync)("git checkout -b main", { stdio: "ignore" }); (0, import_child_process2.execSync)("git add -A", { stdio: "ignore" }); (0, import_child_process2.execSync)('git commit -m "Initial commit from npx mesh-create-dapp"', { stdio: "ignore" }); return true; } catch (_) { return false; } }; var isInGitRepository = () => { try { (0, import_child_process2.execSync)("git rev-parse --is-inside-work-tree", { stdio: "ignore" }); return true; } catch (_) { } return false; }; var isInMercurialRepository = () => { try { (0, import_child_process2.execSync)("hg --cwd . root", { stdio: "ignore" }); return true; } catch (_) { } return false; }; // src/utils/logger.ts var import_chalk = __toESM(require("chalk"), 1); var logError = (message) => { console.log(import_chalk.default.redBright(message + "\n")); }; var logSuccess = (message) => { console.log(import_chalk.default.greenBright(message + "\n")); }; var logInfo = (message) => { console.log(import_chalk.default.blueBright(message + "\n")); }; // src/actions/create.ts var create = async (name, options) => { const template = options.template ?? await askUser("What template do you want to use?", [ { title: "Aiken", value: "mesh-aiken" }, { title: "NextJS", value: "mesh-nextjs" } ]); console.log("\n"); try { createDirectory(name); logInfo("\u{1F4E1} - Downloading files..., This might take a moment."); await fetchRepository(template); logInfo("\u{1F3E0} - Starting a new git repository..."); setNameAndCommitChanges(name); logInfo("\u{1F9F6} - Installing project dependencies..."); installDependencies(); } catch (error) { logError(error); process.exit(1); } }; var askUser = async (question, choices) => { const response = await (0, import_prompts.default)( { type: "select", message: question, name: "selection", choices }, { onCancel: () => process.exit(0) } ); return response.selection; }; var createDirectory = (name) => { const path = `${process.cwd()}/${name}`; if ((0, import_fs2.existsSync)(path)) { logError(`\u2757 A directory with name: "${name}" already exists.`); process.exit(1); } if ((0, import_fs2.mkdirSync)(path, { recursive: true }) === void 0) { logError("\u274C Unable to create a project in current directory."); process.exit(1); } logInfo("\u{1F3D7}\uFE0F - Creating a new mesh dApp in current directory..."); process.chdir(path); }; var fetchRepository = async (template) => { const pipe = (0, import_util.promisify)(import_stream.pipeline); const name = `${template}-template`; const link = `https://codeload.github.com/MeshJS/${name}/tar.gz/main`; await pipe(import_got.default.stream(link), (0, import_tar.extract)({ strip: 1 }, [`${name}-main`])); }; var setNameAndCommitChanges = (name) => { try { setProjectName(process.cwd(), name); } catch (_) { logError("\u{1F6AB} Failed to re-name package.json, continuing..."); } tryGitInit(); }; var installDependencies = () => { try { const pkgManager = resolvePkgManager(); (0, import_child_process3.execSync)(`${pkgManager} install`, { stdio: [0, 1, 2] }); } catch (_) { logError("\u{1F6AB} Failed to install project dependencies, continuing..."); } }; // src/index.ts var main = async () => { console.clear(); console.info( import_chalk2.default.blueBright( import_figlet.default.textSync("MeshJS", { font: "Larry 3D", horizontalLayout: "full" }) ) ); console.log("\n"); const program = (0, import_commander.createCommand)(); program.name("meshjs").description( "A quick and easy way to bootstrap your dApps on Cardano using Mesh." ).version("1.0.0"); program.addArgument( (0, import_commander.createArgument)("name", "Set a name for your dApp.").argParser((name) => { if (/^([A-Za-z\-\\_\d])+$/.test(name)) return name; throw new import_commander.InvalidArgumentError( import_chalk2.default.redBright( "\u2757 Only letters, numbers, underscores and, hashes are allowed." ) ); }).argRequired() ).addOption( (0, import_commander.createOption)( "-t, --template ", `The template to start your project from.` ).choices(["nextjs", "aiken"]) ).addOption( (0, import_commander.createOption)( "-s, --stack ", `The tech stack you want to build on.` ).choices(["next"]) ).addOption( (0, import_commander.createOption)( "-l, --language ", `The language you want to use.` ).choices(["ts"]) ).action(create); await program.parseAsync(process.argv); }; main().then(() => { logSuccess("\u2728\u2728 Welcome to Web 3.0! \u2728\u2728"); process.exit(0); }).catch((error) => { logError(error); process.exit(1); });