maxserver 0.0.3 → 0.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -15,17 +15,16 @@
15
15
  - **HTTPS** support (when configured)
16
16
 
17
17
 
18
- ---
19
18
 
20
- ## Quick Start
19
+ ## Usage
20
+
21
+ ### Install
22
+ npm install maxserver
23
+
24
+ ### Install with template
25
+ npx maxserver [appname]
21
26
 
22
- ```bash
23
- npx @max-matinpalo/maxserver my-app
24
- cd my-app
25
- npm run dev
26
- ```
27
27
 
28
- ---
29
28
 
30
29
  ## Setup
31
30
  maxserver(options) forwards options to fastify(options).
Binary file
package/bin/init.js ADDED
@@ -0,0 +1,66 @@
1
+ #!/usr/bin/env node
2
+
3
+ import fs from "fs";
4
+ import path from "path";
5
+ import { fileURLToPath } from "url";
6
+ import { execSync } from "child_process";
7
+
8
+
9
+ function main() {
10
+ const { projectName, targetDir, templateDir } = resolveArgs();
11
+
12
+ if (fs.existsSync(targetDir)) {
13
+ console.error(`āŒ Error: Directory "${projectName}" already exists.`);
14
+ process.exit(1);
15
+ }
16
+
17
+ try {
18
+ console.log(`šŸš€ Creating "${projectName}"...`);
19
+ fs.cpSync(templateDir, targetDir, { recursive: true });
20
+
21
+ fixDotfiles(targetDir);
22
+ patchPackageJson(targetDir, projectName);
23
+
24
+ process.chdir(targetDir);
25
+ console.log("šŸ“¦ Installing dependencies...");
26
+ execSync("npm install", { stdio: "inherit" });
27
+
28
+ console.log("\nāœ… Done! Your project is ready. 😊");
29
+ } catch (err) {
30
+ console.error("āŒ Init failed:", err.message);
31
+ process.exit(1);
32
+ }
33
+ }
34
+
35
+
36
+ function resolveArgs() {
37
+ const base = path.dirname(fileURLToPath(import.meta.url));
38
+ const name = process.argv[2] || "maxserver";
39
+
40
+ return {
41
+ projectName: name,
42
+ targetDir: path.resolve(process.cwd(), name),
43
+ templateDir: path.resolve(base, "../templates")
44
+ };
45
+ }
46
+
47
+
48
+ function fixDotfiles(dir) {
49
+ for (const f of ["env", "gitignore", "vscode"]) {
50
+ const src = path.join(dir, f);
51
+ if (fs.existsSync(src)) fs.renameSync(src, path.join(dir, "." + f));
52
+ }
53
+ }
54
+
55
+
56
+ function patchPackageJson(dir, name) {
57
+ const pkgPath = path.join(dir, "package.json");
58
+ if (!fs.existsSync(pkgPath)) return;
59
+
60
+ let content = fs.readFileSync(pkgPath, "utf8");
61
+ content = content.replace(/__NAME__/g, name);
62
+ fs.writeFileSync(pkgPath, content);
63
+ }
64
+
65
+
66
+ main();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "maxserver",
3
- "version": "0.0.3",
3
+ "version": "0.0.5",
4
4
  "description": "Node server setup based fastify",
5
5
  "author": "Max Matinpalo",
6
6
  "type": "module",
@@ -9,10 +9,6 @@
9
9
  "maxserver": "bin/init.js",
10
10
  "maxserver-watcher": "bin/watcher.js"
11
11
  },
12
- "files": [
13
- "src",
14
- "README.mde"
15
- ],
16
12
  "dependencies": {
17
13
  "@fastify/cookie": "^11.0.2",
18
14
  "@fastify/cors": "^11.2.0",
package/templates/env ADDED
@@ -0,0 +1,24 @@
1
+ # Environment
2
+ NODE_ENV = development
3
+
4
+ # Server
5
+ PORT = 3000
6
+
7
+ # CORS(prod only, ignored in dev)
8
+ # CORS_ORIGIN = https://app.example.com
9
+
10
+ # JWT;
11
+ JWT_SECRET = supersecretkey
12
+
13
+ # MongoDB;
14
+ # MONGODB_URI = mongodb://127.0.0.1:27017/testdb
15
+
16
+ # Static files
17
+ # STATIC_DIR =./ public
18
+
19
+ # API Docs in production
20
+ DOCS = 1
21
+
22
+ # Optional HTTPS(direct TLS, no nginx)
23
+ # TLS_KEY = /etc/ssl / private / server.key
24
+ # TLS_CERT = /etc/ssl / certs / server.crt;
@@ -0,0 +1 @@
1
+ node_modules
@@ -0,0 +1,14 @@
1
+ {
2
+ "name": "__NAME__",
3
+ "version": "1.0.0",
4
+ "description": "",
5
+ "main": "server.js",
6
+ "scripts": {
7
+ "start": "node server.js",
8
+ "dev": "maxserver-watcher"
9
+ },
10
+ "type": "module",
11
+ "dependencies": {
12
+ "maxserver": "^0.0.0"
13
+ }
14
+ }
@@ -0,0 +1,10 @@
1
+ import maxserver from "@max-matinpalo/maxserver";
2
+
3
+ const server = await maxserver();
4
+
5
+ await server.listen({
6
+ port: Number(process.env.PORT || 3000),
7
+ });
8
+
9
+ console.log("Server running at ", server.getAddress());
10
+ export default server;
@@ -0,0 +1,11 @@
1
+ // POST /hello
2
+
3
+ export default async function handler(req, rep) {
4
+
5
+ console.log("POST /hello");
6
+
7
+ return {
8
+ message: `Hello ${req.body.name}`,
9
+ user: req.user || null,
10
+ };
11
+ }
@@ -0,0 +1,34 @@
1
+ export const schema = {
2
+ tags: ["Test"],
3
+ summary: "Create greeting",
4
+ description: "Accepts a name and returns a greeting.",
5
+
6
+ body: {
7
+ type: "object",
8
+ required: ["name"],
9
+ properties: {
10
+ name: {
11
+ type: "string",
12
+ example: "Max",
13
+ },
14
+ },
15
+ },
16
+
17
+ response: {
18
+ 200: {
19
+ type: "object",
20
+ properties: {
21
+ message: {
22
+ type: "string",
23
+ example: "Hello Max",
24
+ },
25
+ user: {
26
+ type: "object",
27
+ example: {
28
+ userId: "64f1c2e9b1a2c3d4e5f67890",
29
+ },
30
+ },
31
+ },
32
+ },
33
+ },
34
+ };
@@ -0,0 +1,8 @@
1
+ // GET /hello
2
+
3
+ export default async function handler(req, rep) {
4
+
5
+ console.log("GET /hello");
6
+
7
+ return { message: `Hello`, };
8
+ }
@@ -0,0 +1,18 @@
1
+ {
2
+ "handler": {
3
+ "prefix": "handler",
4
+ "body": [
5
+ "// ${1|GET, POST, PUT, PATCH, DELETE|} /$2",
6
+ "",
7
+ "export default async function ${TM_FILENAME_BASE}(req, res) {",
8
+ "",
9
+ "\tconst db = this.mongo.db;",
10
+ "\tconst userId = req.user.userid;",
11
+ "",
12
+ "\t$0",
13
+ "",
14
+ "\treturn res.send({msg: \"${TM_FILENAME_BASE}\"});",
15
+ "}"
16
+ ]
17
+ }
18
+ }
@@ -0,0 +1,23 @@
1
+ // Comment task in to autostart server on dir open
2
+ /*
3
+
4
+
5
+ {
6
+ "version": "1.0.0",
7
+ "tasks": [
8
+ {
9
+ "label": "devserver šŸ“Ÿ",
10
+ "type": "shell",
11
+ "command": "npm run dev",
12
+ "presentation": {
13
+ "reveal": "always",
14
+ "panel": "dedicated"
15
+ },
16
+ "runOptions": {
17
+ "runOn": "folderOpen"
18
+ }
19
+ }
20
+ ]
21
+ }
22
+
23
+ */