Skip to content

Commit

Permalink
feat: create-app for npm init
Browse files Browse the repository at this point in the history
  • Loading branch information
dockfries committed Apr 21, 2023
1 parent 8f855a8 commit 0127e44
Show file tree
Hide file tree
Showing 8 changed files with 964 additions and 19 deletions.
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"url": "git+https://github.com/dockfries/omp-node.git"
},
"license": "ISC",
"author": "",
"author": "dockfries",
"main": "dist/bundle.js",
"types": "dist/bundle.d.ts",
"scripts": {
Expand Down
21 changes: 21 additions & 0 deletions packages/create-app/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 Carl You

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
223 changes: 223 additions & 0 deletions packages/create-app/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,223 @@
#! /usr/bin/env node

import inquirer from "inquirer";
import chalk from "chalk";
import decompress from "decompress";

import fs from "fs-extra";

import {
wrapLoading,
downloadGitRepo,
downloadGitRelease,
installPlugin,
} from "./utils/index.js";

function successInstalled(projectName) {
console.log(`\nSuccessfully created project ${chalk.cyan(projectName)}`);
console.log(`\ncd ${chalk.cyan(projectName)}`);
console.log(`pnpm install`);
console.log("pnpm dev\n");
}

async function initializeStarter(relativeGenPath, projectName, isRakNet) {
console.log("\n");
const isMac = process.platform === "darwin";
const isCreatedProject = await fs.ensureDir(relativeGenPath);
if (!isCreatedProject)
throw `The project directory ${projectName} already exists`;

if (isMac) {
console.log(
chalk.yellow(
"You are using a mac system and cannot run directly on the mac system after the configuration is complete"
)
);
}

const starterPath = await downloadGitRepo(
"dockfries",
"omp-node-starter",
relativeGenPath
);

await wrapLoading(
decompress,
`decompress ${starterPath}`,
starterPath,
relativeGenPath,
{ strip: 1 }
);

fs.remove(starterPath);
fs.remove(relativeGenPath + "/.git");
fs.remove(relativeGenPath + "/.husky");
fs.remove(relativeGenPath + "/gamemodes/polyfill.pwn");
fs.remove(relativeGenPath + "/gamemodes/polyfill_raknet.pwn");

if (isRakNet) {
fs.remove(relativeGenPath + "/gamemodes/polyfill.amx");
fs.rename(
relativeGenPath + "/gamemodes/polyfill_raknet.amx",
relativeGenPath + "/gamemodes/polyfill.amx"
);
} else {
fs.remove(relativeGenPath + "/gamemodes/polyfill_raknet.amx");
}
}

function changePkgName(relativeGenPath, projectName) {
const pkgFilePath = relativeGenPath + "/package.json";
const pkg = fs.readJsonSync(pkgFilePath);
pkg.name = projectName;
delete pkg.scripts.prepare;
delete pkg.husky;
fs.writeJson(pkgFilePath, pkg, { spaces: 2 });
}

async function initializeBase(relativeGenPath, isLinux) {
const base = await downloadGitRelease(
isLinux,
"openmultiplayer",
"open.mp",
relativeGenPath
);

await wrapLoading(decompress, `decompress ${base}`, base, relativeGenPath, {
strip: 1,
});

fs.remove(base);

fs.remove(relativeGenPath + "/qawno");
}

function changeRconPass(relativeGenPath, password) {
const configJson = fs.readJsonSync(relativeGenPath + "/config.json");
configJson.rcon.password = password;
fs.writeJSON(relativeGenPath + "/config.json", configJson, { space: 2 });
}

async function installPlugins(relativeGenPath, isLinux, isRakNet) {
fs.ensureDirSync(relativeGenPath + "/plugins");

const plugins = [
{ author: "AmyrAhmady", repo: "samp-node", fileName: "samp-node" },
{
author: "samp-incognito",
repo: "samp-streamer-plugin",
fileName: "streamer",
},
];

if (isRakNet) {
plugins.push({
author: "katursis",
repo: "Pawn.RakNet",
fileName: "pawnraknet",
isComponent: true,
});
}

const downPlugins = plugins.map((p) => {
return () => installPlugin(relativeGenPath, isLinux, p);
});

for (const plugin of downPlugins) {
await plugin();
}
}

async function init() {
const isWin = process.platform === "win32";

try {
const questions = [
{
name: "projectName",
message: "What do you want to call the project?",
default: "my-app",
validate(input) {
if (!/^[^\\/?*":<>|\r\n]+$/.test(input)) {
console.log(
chalk.red.bold(
"\nThe project name cannot contain special characters"
)
);
return false;
}
return true;
},
},
{
name: "env",
type: "list",
message: "Which system environment do you plan to configure in?",
choices: ["win", "linux"],
default: isWin ? 0 : 1,
},
{
name: "isRakNet",
type: "confirm",
message: "Whether you need to install RakNet?",
default: false,
},
{
name: "password",
message: "What password do you want for rcon?",
validate(input) {
if (!input.length) {
console.log(chalk.red.bold("\nYou have to enter a password"));
return false;
}
if (!/^\w+$/.test(input)) {
console.log(
chalk.red.bold(
"\nPlease enter a password consisting of case, digits, and underscores."
)
);
return false;
}
if (input.trim() === "changeme") {
console.log(
chalk.red.bold("\nThe default rcon password cannot be used.")
);
return false;
}
return true;
},
},
];

const { projectName, env, isRakNet, password } = await inquirer.prompt(
questions
);

const isLinux = env === "linux";
const relativeGenPath = "./" + projectName;

await initializeStarter(relativeGenPath, projectName, isRakNet);

changePkgName(relativeGenPath, projectName);

await initializeBase(relativeGenPath, isLinux);

await installPlugins(relativeGenPath, isLinux, isRakNet);

changeRconPass(relativeGenPath, password);

successInstalled(projectName);
} catch (err) {
if (err.isTtyError) {
console.log(
chalk.red.bold(
"\nPrompt couldn't be rendered in the current environment"
)
);
return;
}
console.log(chalk.red.bold(err));
}
}

init();
34 changes: 34 additions & 0 deletions packages/create-app/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"name": "@infernus/create-app",
"version": "0.0.5",
"description": "Used to quickly generate an omp-node-starter template",
"files": [
"dist",
"LICENSE"
],
"keywords": [
"omp",
"init",
"cli",
"create"
],
"license": "ISC",
"author": "dockfries",
"type": "module",
"bin": {
"infernus": "dist/index.js"
},
"scripts": {
"build": "rollup -c",
"prepublishOnly": "pnpm build"
},
"dependencies": {
"chalk": "^5.2.0",
"decompress": "^4.2.1",
"fs-extra": "^11.1.1",
"https-proxy-agent": "^5.0.1",
"inquirer": "^9.1.5",
"node-fetch": "^3.3.1",
"ora": "^6.3.0"
}
}
17 changes: 17 additions & 0 deletions packages/create-app/rollup.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import esbuild from "rollup-plugin-esbuild";
import del from "rollup-plugin-delete";
import externals from "rollup-plugin-node-externals";

const inputPath = "./index.js";
const outputPath = "./dist";
export default [
{
input: inputPath,
output: { file: outputPath + "/index.js", format: "esm" },
plugins: [
del({ targets: outputPath + "/*" }),
esbuild({ minify: true }),
externals(),
],
},
];
Loading

0 comments on commit 0127e44

Please sign in to comment.