create-mserver
Version:
Spin up Paper servers with ease
14 lines (13 loc) • 4.87 kB
JavaScript
import*as s from"@clack/prompts";var N=/^[0-9]+[MG]$/;function w(e){if(!N.test(e))return"Memory must only contain a number ending with M (megabytes) or G (gigabytes)"}var I=/^[a-zA-Z0-9 _-]+$/;function v(e){if(!(e==="."||I.test(e)))return"Server name must only contain alphanumeric characters, ' ', '-' and ''"}async function b(){let e=await s.group({name:()=>s.text({message:"What will your server be called?",validate:v}),version:()=>s.select({message:"Select a server version.",options:[{value:"1.20.4",label:"1.20.4",hint:"latest"},{value:"1.12.2",label:"1.12.2"},{value:"1.8.8",label:"1.8.8"}],initialValue:"1.20.4"}),memory:()=>s.text({message:"How much memory to allocate?",initialValue:"2G",validate:w}),useAikarFlags:()=>s.select({message:"Would you like to use Aikar's Flags?",options:[{value:"yes",label:"Yes",hint:"recommended"},{value:"no",label:"No"}],initialValue:"yes"})},{onCancel(){process.exit(1)}});return{path:e.name,version:e.version,memory:e.memory,useAikarFlags:e.useAikarFlags==="yes"}}import{createHash as R}from"crypto";import*as i from"fs";import*as y from"https";async function X(e,r,n,t){try{let o=await new Promise((u,_)=>{y.get(n,u).on("error",_)}),{statusCode:a,statusMessage:m}=o;if(a!==200)throw new Error(`Got bad response: ${a} ${m}`);i.existsSync(e)||i.mkdirSync(e,{recursive:!0});let f=i.createWriteStream(`${e}/${r}`),p=R("sha256");return o.on("data",u=>{p.update(u)}),o.pipe(f),new Promise((u,_)=>{f.on("finish",()=>{f.close(),p.digest("hex")!==t&&(i.unlinkSync(`${e}/${r}`),_(new Error("File hash mismatch"))),u()}),f.on("error",_)})}catch(o){let a=o instanceof Error?o.message:"Unknown error";throw new Error(a)}}var A={"1.20.4":{build:450,hash:"487a871ded3ebadc5dbf6c1f64e7a29013404df707549e5f3042f88cf5e6a295"},"1.12.2":{build:1620,hash:"3a2041807f492dcdc34ebb324a287414946e3e05ec3df6fd03f5b5f7d9afc210"},"1.8.8":{build:445,hash:"7ff6d2cec671ef0d95b3723b5c92890118fb882d73b7f8fa0a2cd31d97c55f86"}};function x(e,r){return`https://api.papermc.io/v2/projects/paper/versions/${e}/builds/${r}/downloads/paper-${e}-${r}.jar`}function k(e){return A[e]}import H from"ora";import j from"chalk";import g from"chalk";var c={error(...e){console.log(g.red(...e))},warn(...e){console.log(g.yellow(...e))},info(...e){console.log(g.cyan(...e))},success(...e){console.log(g.green(...e))}};async function P(e,r,n){c.info("Downloading server...");let{build:t,hash:o}=k(n),a=x(n,t),m=H(`Downloading file from ${a}`).start();await X(e,r,a,o),m.succeed(j.green(`Successfully downloaded!
`))}import*as $ from"fs";function d(e,r){return new Promise((n,t)=>{$.writeFile(e,r,o=>{if(o){t(o);return}n()})})}async function S(e){await d(`${e}/eula.txt`,"eula=true")}import*as G from"fs";async function h(e,r){return new Promise((n,t)=>{G.chmod(e,r,o=>{if(o){t(o);return}n()})})}var D="-XX:+UseG1GC -XX:+ParallelRefProcEnabled -XX:MaxGCPauseMillis=200 -XX:+UnlockExperimentalVMOptions -XX:+DisableExplicitGC -XX:+AlwaysPreTouch -XX:G1NewSizePercent=30 -XX:G1MaxNewSizePercent=40 -XX:G1HeapRegionSize=8M -XX:G1ReservePercent=20 -XX:G1HeapWastePercent=5 -XX:G1MixedGCCountTarget=4 -XX:InitiatingHeapOccupancyPercent=15 -XX:G1MixedGCLiveThresholdPercent=90 -XX:G1RSetUpdatingPauseTimePercent=5 -XX:SurvivorRatio=32 -XX:+PerfDisableSharedMem -XX:MaxTenuringThreshold=1 -Dusing.aikars.flags=https://mcflags.emc.gs -Daikars.new.flags=true ";function U(e,r,n){let t=`java -Xms${r} -Xmx${r} `;return n&&(t+=D),t+=`-jar ${e}`,t}async function V(e,r,n,t){let o=U(r,n,t);switch(process.platform){case"darwin":let m=`${e}/start.command`;await d(m,`#!/bin/sh
cd "$(dirname "$0")"
exec ${o}`),await h(m,493);break;case"win32":let f=`${e}/start.bat`;await d(f,`@echo off
${o}
pause`);break;default:let p=`${e}/start.sh`;await d(p,`#!/bin/sh
${o}`),await h(p,493);break}}import z from"ora";import F from"chalk";import{basename as L}from"path";function M(e){return e==="."?L(process.cwd()):e}function C(e){let r=M(e);switch(z().succeed(F.cyan(r)+F.green(` created successfully!
`)),c.info("Next steps:"),e!=="."&&c.info(` cd ${e.replaceAll(" ","\\ ")}`),process.platform){case"darwin":c.info(" ./start.command");break;case"win32":c.info(" ./start.bat");break;default:c.info(" ./start.sh");break}}import W from"gradient-string";var B=["#add7ff","#89ddff","#5de4c7","#fae4fc","#d0679d","#fffac2"],O=` ___ ___ ___ _ _____ ___ __ __ ___ ___ _____ _____ ___
/ __| _ \\ __| /_\\_ _| __| | \\/ / __| __| _ \\ \\ / / __| _ \\
| (__| / _| / _ \\| | | _| | |\\/| \\__ \\ _|| /\\ V /| _|| /
\\___|_|_\\___/_/ \\_\\_| |___| |_| |_|___/___|_|_\\ \\_/ |___|_|_\\
`;function T(){let e=W(B);console.log(e.multiline(O))}T();var l=await b();console.log();var E="server.jar";await P(l.path,E,l.version);await S(l.path);await V(l.path,E,l.memory,l.useAikarFlags);C(l.path);