UNPKG

create-moost

Version:
373 lines (364 loc) 11.3 kB
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' }); //#region \0rolldown/runtime.js 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 (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) { key = keys[i]; if (!__hasOwnProp.call(to, key) && key !== except) { __defProp(to, key, { get: ((k) => from[k]).bind(null, key), enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } } } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod)); //#endregion let _moostjs_event_cli = require("@moostjs/event-cli"); let _wooksjs_event_cli = require("@wooksjs/event-cli"); let fs = require("fs"); let prompts = require("prompts"); prompts = __toESM(prompts); let _prostojs_rewrite = require("@prostojs/rewrite"); let path = require("path"); //#region packages/create-moost/src/prompts.ts const defaultProjectName = "moost-app"; async function getPrompts(inputs) { const predefined = { targetDir: inputs.name || "", projectName: inputs.name || "", packageName: inputs.name || "" }; if (inputs.ws) predefined.ws = true; if (inputs.wf) predefined.wf = true; if (inputs.ssr) predefined.ssr = true; if (inputs.oxc) predefined.oxc = true; if (inputs.force) predefined.overwrite = true; try { const results = await (0, prompts.default)([ { name: "projectName", type: predefined.targetDir ? null : "text", message: "Project name:", initial: defaultProjectName, onState: (state) => predefined.targetDir = String(state.value).trim() || defaultProjectName }, { name: "overwrite", type: () => canSkipEmptying(predefined.targetDir) || inputs.force ? null : "confirm", message: () => { return `${predefined.targetDir === "." ? "Current directory" : `Target directory "${predefined.targetDir}"`} is not empty. Remove existing files and continue?`; } }, { name: "overwriteChecker", type: (prev, values) => { if (values.overwrite === false) throw new Error("Operation cancelled"); return null; } }, { name: "type", type: () => { const selected = [ "http", "cli", "ws", "ssr" ].filter((t) => inputs[t] && !(t === "ws" && inputs.http)); if (selected.length === 1) { predefined.type = selected[0]; return null; } return "select"; }, message: "Moost Adapter:", choices: [ { title: "HTTP (Web) Application", value: "http" }, { title: "Vue + Moost (SSR/SPA)", value: "ssr" }, { title: "WebSocket Application", value: "ws" }, { title: "CLI Application", value: "cli" } ] }, { name: "packageName", type: () => isValidPackageName(predefined.targetDir) ? null : "text", message: "Package name:", initial: () => toValidPackageName(predefined.targetDir), validate: (dir) => isValidPackageName(dir) || "Invalid package.json name" }, { name: "ssr", type: (prev, values) => { if ((values.type || predefined.type) !== "ssr" || inputs.ssr) return null; return "toggle"; }, message: "Enable SSR (Server-Side Rendering)?", initial: true, active: "Yes", inactive: "No (SPA only)" }, { name: "ws", type: (prev, values) => { if ((values.type || predefined.type) !== "http" || inputs.ws) return null; return "toggle"; }, message: "Add WebSockets?", initial: false, active: "Yes", inactive: "No" }, { name: "wf", type: (prev, values) => { const type = values.type || predefined.type; if (inputs.wf || type === "cli" || type === "ws") return null; return "toggle"; }, message: "Add Moost Workflows Example?", initial: false, active: "Yes", inactive: "No" }, { name: "oxc", type: () => inputs.oxc ? null : "toggle", message: "Add OXC lint and formatter (oxlint + oxfmt)?", initial: false, active: "Yes", inactive: "No" } ], { onCancel: () => { throw new Error("Operation cancelled"); } }); return { ...predefined, ...results, packageName: results.packageName || results.targetDir || predefined.targetDir }; } catch (error) { console.log(error.message); process.exit(1); } } function canSkipEmptying(dir) { if (!(0, fs.existsSync)(dir)) return true; const files = (0, fs.readdirSync)(dir); if (files.length === 0) return true; if (files.length === 1 && files[0] === ".git") return true; return false; } function isValidPackageName(projectName) { return /^(?:@[\d*a-z~-][\d*._a-z~-]*\/)?[\da-z~-][\d._a-z~-]*$/.test(projectName); } function toValidPackageName(projectName) { return projectName.trim().toLowerCase().replaceAll(/\s+/g, "-").replace(/^[._]/, "").replaceAll(/[^\da-z~-]+/g, "-"); } //#endregion //#region packages/create-moost/src/scaffold.ts const rw = new _prostojs_rewrite.ProstoRewrite({ textPattern: [ "*.{js,jsx,ts,tsx,txt,json,jsonc,yml,yaml,md,ini,css,html}", "Dockerfile", "*config", ".gitignore", ".oxlintrc.json", ".oxfmtrc.json" ] }); const root = process.cwd(); const { version } = JSON.parse((0, fs.readFileSync)((0, path.join)(__dirname, "../package.json")).toString()); async function scaffold(data) { const projectDir = (0, path.join)(root, data.targetDir); if ((0, fs.existsSync)(projectDir)) { if (data.overwrite) emptyDirectorySync(projectDir); } else (0, fs.mkdirSync)(projectDir); const templatePath = (0, path.join)(__dirname, "../templates", data.type); const commonPath = (0, path.join)(__dirname, "../templates/common"); const wfPath = (0, path.join)(__dirname, "../templates/wf"); const wsAddonPath = (0, path.join)(__dirname, "../templates/ws-addon"); const context = { ...data, version }; const excludeCommon = []; if (!data.oxc) { excludeCommon.push(".oxlintrc.json"); excludeCommon.push(".oxfmtrc.json"); } if (data.type === "ssr") excludeCommon.push("tsconfig.json"); const renameFile = (filename) => { if (filename.endsWith(".jsonc")) return filename.replace(/c$/, ""); return filename; }; const excludeTemplate = []; if (data.type === "ssr" && !data.ssr) excludeTemplate.push("src/entry-server.ts"); await rw.rewriteDir({ baseDir: templatePath, output: projectDir, exclude: excludeTemplate, renameFile }, context); await rw.rewriteDir({ baseDir: commonPath, output: projectDir, exclude: excludeCommon, renameFile }, context); if (data.wf && data.type === "http") await rw.rewriteDir({ baseDir: wfPath, output: projectDir, renameFile }, context); if (data.ws && data.type === "http") await rw.rewriteDir({ baseDir: wsAddonPath, output: projectDir, renameFile }, context); } function emptyDirectorySync(directory) { if ((0, fs.existsSync)(directory)) (0, fs.readdirSync)(directory).forEach((file) => { const currentPath = (0, path.join)(directory, file); if ((0, fs.lstatSync)(currentPath).isDirectory()) { emptyDirectorySync(currentPath); (0, fs.rmdirSync)(currentPath); } else (0, fs.unlinkSync)(currentPath); }); } //#endregion //#region packages/create-moost/src/index.ts function _ts_decorate(decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; } function _ts_metadata(k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); } function _ts_param(paramIndex, decorator) { return function(target, key) { decorator(target, key, paramIndex); }; } let Commands = class Commands extends _moostjs_event_cli.CliApp { root() { return this.execute(); } withName(name) { return this.execute(name); } async execute(name) { if ((0, _wooksjs_event_cli.useAutoHelp)()) process.exit(0); const prompts = await getPrompts({ name, http: !!(0, _wooksjs_event_cli.useCliOption)("http"), cli: !!(0, _wooksjs_event_cli.useCliOption)("cli"), ws: !!(0, _wooksjs_event_cli.useCliOption)("ws"), wf: !!(0, _wooksjs_event_cli.useCliOption)("wf"), ssr: !!(0, _wooksjs_event_cli.useCliOption)("ssr"), oxc: !!(0, _wooksjs_event_cli.useCliOption)("oxc"), force: !!(0, _wooksjs_event_cli.useCliOption)("force") }); console.log("\nScaffolding a new project..."); await scaffold(prompts); const cli = prompts.type === "cli"; const ws = prompts.type === "ws"; const ssr = prompts.type === "ssr"; return ` Success! Your new "${prompts.projectName}" project has been created successfully.  Follow these next steps to start your development server: 1. Navigate to your new project: cd ${prompts.targetDir}  2. Install the dependencies: npm install  ${cli ? ` 3. Make bin.js executable: chmod +x ./bin.js  ` : ""} ${cli ? "4" : "3"}. ${ws ? "Build and start" : "Start the development server"}: npm run dev${cli ? " -- hello World" : ""} You're all set!${ws ? "" : " The development server will help you in building your application."} Enjoy coding, and build something amazing! ${ssr ? ` Vue app: http://localhost:3000 API routes: http://localhost:3000/api/hello/World ` : ""}`; } }; _ts_decorate([ (0, _moostjs_event_cli.Cli)(""), _ts_metadata("design:type", Function), _ts_metadata("design:paramtypes", []), _ts_metadata("design:returntype", void 0) ], Commands.prototype, "root", null); _ts_decorate([ (0, _moostjs_event_cli.Cli)(":name"), _ts_param(0, (0, _moostjs_event_cli.Param)("name")), _ts_metadata("design:type", Function), _ts_metadata("design:paramtypes", [String]), _ts_metadata("design:returntype", void 0) ], Commands.prototype, "withName", null); function run() { new Commands().useOptions([ { keys: ["http"], description: "Use Moost HTTP", type: Boolean }, { keys: ["cli"], description: "Use Moost CLI", type: Boolean }, { keys: ["ws"], description: "Use Moost WebSocket", type: Boolean }, { keys: ["wf"], description: "Add Workflow Adapter", type: Boolean }, { keys: ["ssr"], description: "Vue + Moost (SSR/SPA)", type: Boolean }, { keys: ["oxc"], description: "Add OXC lint and formatter (oxlint + oxfmt)", type: Boolean }, { keys: ["force"], description: "Force Overwrite", type: Boolean } ]).start(); } //#endregion exports.run = run;