UNPKG

create-apppaaaul

Version:

Create projects as paaauldev would

250 lines (246 loc) 11.5 kB
#!/usr/bin/env node 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 )); // node_modules/.pnpm/tsup@8.5.0_typescript@5.9.3_yaml@2.4.5/node_modules/tsup/assets/cjs_shims.js var getImportMetaUrl = () => typeof document === "undefined" ? new URL(`file:${__filename}`).href : document.currentScript && document.currentScript.src || new URL("main.js", document.baseURI).href; var importMetaUrl = /* @__PURE__ */ getImportMetaUrl(); // index.ts var import_node_path = __toESM(require("path")); var import_node_url = require("url"); var import_promises = require("fs/promises"); var import_child_process = require("child_process"); var import_util = require("util"); var import_picocolors = require("picocolors"); var import_glob = require("glob"); var import_picocolors2 = __toESM(require("picocolors")); var import_prompts = __toESM(require("prompts")); var import_yargs = __toESM(require("yargs")); var import_helpers = require("yargs/helpers"); // helpers/install.ts var import_cross_spawn = __toESM(require("cross-spawn")); async function install() { const packageManager = "pnpm"; const args2 = ["install"]; return new Promise((resolve, reject) => { const child = (0, import_cross_spawn.default)(packageManager, args2, { stdio: "inherit", env: { ...process.env, ADBLOCK: "1", // we set NODE_ENV to development as pnpm skips dev // dependencies when production NODE_ENV: "development", DISABLE_OPENCOLLECTIVE: "1" } }); child.on("close", (code) => { if (code !== 0) { reject({ command: `${packageManager} ${args2.join(" ")}` }); return; } resolve(); }); }); } // index.ts var execAsync = (0, import_util.promisify)(import_child_process.exec); var TEMPLATES = [ { title: "Nextjs ts with db setup Landing with prisma", value: "nextjs-ts-landing-prisma" }, { title: "Nextjs ts clean", value: "nextjs-ts-clean" } ]; var args = (0, import_yargs.default)((0, import_helpers.hideBin)(process.argv)).options({ name: { alias: "n", type: "string", description: "Name of the project" }, template: { alias: "t", type: "string", description: "Template to use" } }); import_prompts.default.override(args.argv); async function main() { const { _: [initialName, initialProject] } = await args.argv; const currentDirName = import_node_path.default.basename(process.cwd()); const defaultProjectName = initialName || currentDirName; const project = await (0, import_prompts.default)( [ { type: "text", name: "name", message: `What is the name of your project? (suggested: ${currentDirName})`, initial: defaultProjectName, validate: (value) => { if (value !== "." && value.match(/[^a-zA-Z0-9-_]+/g)) { return "Project name can only contain letters, numbers, dashes, underscores, or be '.' for the current directory"; } return true; } }, { type: "select", name: "template", message: `Which template would you like to use?`, initial: initialProject || 0, choices: TEMPLATES } ], { onCancel: () => { console.log("\nBye \u{1F44B}\n"); process.exit(0); } } ); const template = import_node_path.default.join( import_node_path.default.dirname((0, import_node_url.fileURLToPath)(importMetaUrl)), "templates", project.template ); const destination = project.name === "." || project.name === currentDirName ? process.cwd() : import_node_path.default.join(process.cwd(), project.name); await (0, import_promises.cp)(import_node_path.default.join(template, "project"), destination, { recursive: true }); try { const aditionalsPath = import_node_path.default.join(template, "aditionals"); await (0, import_promises.access)(aditionalsPath); await (0, import_promises.cp)(aditionalsPath, destination, { recursive: true }); console.log(`${import_picocolors2.default.green("\u2713")} Copied additional files`); } catch (error) { console.log(`${import_picocolors2.default.yellow("\u26A0")} Additional files folder not found in template, skipping...`); } const aditionalsFiles = await (0, import_glob.glob)(`**/*`, { nodir: true, cwd: destination, absolute: true }); for await (const file of aditionalsFiles) { const basename = import_node_path.default.basename(file); if (basename.startsWith("%%")) { const newPath = import_node_path.default.join(import_node_path.default.dirname(file), basename.slice(2)); await (0, import_promises.rename)(file, newPath); } } try { await (0, import_promises.rename)(import_node_path.default.join(destination, ".env.test"), import_node_path.default.join(destination, ".env")); console.log(`${import_picocolors2.default.green("\u2713")} Renamed .env.test to .env`); } catch (error) { console.log(`${import_picocolors2.default.yellow("\u26A0")} .env.test file not found, skipping rename...`); } const files = await (0, import_glob.glob)(`**/*`, { nodir: true, cwd: destination, absolute: true }); for await (const file of files) { try { const data = await (0, import_promises.readFile)(file, "utf8"); const draft = data.replace(/{{name}}/g, project.name); await (0, import_promises.writeFile)(file, draft, "utf8"); } catch (error) { console.log(`${import_picocolors2.default.yellow("\u26A0")} Could not process file: ${import_node_path.default.basename(file)}`); } } console.log("\n\u2728 Project created \u2728"); if (project.name !== "." && project.name !== currentDirName) { try { await execAsync(`cd ${project.name}`); console.log(` ${import_picocolors2.default.green(`cd`)} ${project.name}`); const suggestedFolderName = project.name.toLowerCase().replace(/[^a-z0-9]/g, "-"); const isDefaultFolder = destination.endsWith(suggestedFolderName); if (isDefaultFolder) { console.log(`${import_picocolors2.default.green("\u2713")} Using default folder name, checking git status...`); try { await execAsync("git status", { cwd: destination }); console.log(`${import_picocolors2.default.green("\u2713")} Git repository detected`); await execAsync("git add .", { cwd: destination }); console.log(`${import_picocolors2.default.green("\u2713")} Files added to git`); await execAsync('git commit -m "First commit"', { cwd: destination }); console.log(`${import_picocolors2.default.green("\u2713")} First commit created`); try { await execAsync("git push", { cwd: destination }); console.log(`${import_picocolors2.default.green("\u2713")} Pushed to origin`); } catch (pushError) { console.log(`${import_picocolors2.default.yellow("\u26A0")} Could not push to origin (remote may not be configured)`); } await execAsync("git checkout -b dev", { cwd: destination }); console.log(`${import_picocolors2.default.green("\u2713")} Created dev branch`); try { await execAsync("git push -u origin dev", { cwd: destination }); console.log(`${import_picocolors2.default.green("\u2713")} Pushed dev branch to origin`); } catch (pushError) { console.log(`${import_picocolors2.default.yellow("\u26A0")} Could not push dev branch to origin`); } } catch (gitError) { console.log(`${import_picocolors2.default.yellow("\u26A0")} Git not initialized, initializing git repository...`); await execAsync("git init", { cwd: destination }); console.log(`${import_picocolors2.default.green("\u2713")} Git repository initialized`); await execAsync("git add .", { cwd: destination }); console.log(`${import_picocolors2.default.green("\u2713")} Files added to git`); await execAsync('git commit -m "First commit"', { cwd: destination }); console.log(`${import_picocolors2.default.green("\u2713")} First commit created`); await execAsync("git checkout -b dev", { cwd: destination }); console.log(`${import_picocolors2.default.green("\u2713")} Created dev branch`); } } else { try { await execAsync("git status", { cwd: destination }); console.log(`${import_picocolors2.default.green("\u2713")} Git repository detected`); await execAsync("git checkout -b dev", { cwd: destination }); console.log(`${import_picocolors2.default.green("\u2713")} Created dev branch`); try { await execAsync("git push -u origin dev", { cwd: destination }); console.log(`${import_picocolors2.default.green("\u2713")} Pushed dev branch to origin`); } catch (pushError) { console.log(`${import_picocolors2.default.yellow("\u26A0")} Could not push to origin (remote may not be configured)`); } } catch (gitError) { console.log(`${import_picocolors2.default.yellow("\u26A0")} Git not initialized, initializing git repository...`); await execAsync("git init", { cwd: destination }); console.log(`${import_picocolors2.default.green("\u2713")} Git repository initialized`); await execAsync("git add .", { cwd: destination }); console.log(`${import_picocolors2.default.green("\u2713")} Files added to git`); await execAsync('git commit -m "First commit"', { cwd: destination }); console.log(`${import_picocolors2.default.green("\u2713")} First commit created`); await execAsync("git checkout -b dev", { cwd: destination }); console.log(`${import_picocolors2.default.green("\u2713")} Created dev branch`); } } } catch (error) { console.error(`Error executing commands: ${error}`); } } try { await execAsync("cursor ."); console.log("Installing packages. This might take a couple of minutes."); console.log(); await install(); console.log(); console.log(`${(0, import_picocolors.green)("Success!")} App installed successfully.`); } catch (error) { console.error(`Error executing commands: ${error}`); } } main().catch(console.error); //# sourceMappingURL=index.js.map