@titanpl/cli 2.0.2 → 2.0.4

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.
Files changed (46) hide show
  1. package/package.json +5 -5
  2. package/src/commands/init.js +23 -2
  3. package/src/engine.js +18 -1
  4. package/templates/common/Dockerfile +66 -0
  5. package/templates/common/_dockerignore +35 -0
  6. package/templates/common/_gitignore +33 -0
  7. package/templates/common/app/t.native.d.ts +2043 -0
  8. package/templates/common/app/t.native.js +39 -0
  9. package/templates/extension/README.md +69 -0
  10. package/templates/extension/index.d.ts +27 -0
  11. package/templates/extension/index.js +17 -0
  12. package/templates/extension/jsconfig.json +14 -0
  13. package/templates/extension/native/Cargo.toml +9 -0
  14. package/templates/extension/native/src/lib.rs +5 -0
  15. package/templates/extension/package-lock.json +522 -0
  16. package/templates/extension/package.json +26 -0
  17. package/templates/extension/titan.json +18 -0
  18. package/templates/js/app/actions/getuser.js +9 -0
  19. package/templates/js/app/app.js +7 -0
  20. package/templates/js/eslint.config.js +5 -0
  21. package/templates/js/jsconfig.json +27 -0
  22. package/templates/js/package.json +28 -0
  23. package/templates/rust-js/app/actions/getuser.js +9 -0
  24. package/templates/rust-js/app/actions/rust_hello.rs +14 -0
  25. package/templates/rust-js/app/app.js +9 -0
  26. package/templates/rust-js/eslint.config.js +5 -0
  27. package/templates/rust-js/jsconfig.json +27 -0
  28. package/templates/rust-js/package.json +27 -0
  29. package/templates/rust-js/titan/bundle.js +157 -0
  30. package/templates/rust-js/titan/dev.js +323 -0
  31. package/templates/rust-js/titan/titan.js +126 -0
  32. package/templates/rust-ts/app/actions/getuser.ts +9 -0
  33. package/templates/rust-ts/app/actions/rust_hello.rs +14 -0
  34. package/templates/rust-ts/app/app.ts +9 -0
  35. package/templates/rust-ts/eslint.config.js +12 -0
  36. package/templates/rust-ts/package.json +29 -0
  37. package/templates/rust-ts/titan/bundle.js +163 -0
  38. package/templates/rust-ts/titan/dev.js +435 -0
  39. package/templates/rust-ts/titan/titan.d.ts +19 -0
  40. package/templates/rust-ts/titan/titan.js +124 -0
  41. package/templates/rust-ts/tsconfig.json +28 -0
  42. package/templates/ts/app/actions/getuser.ts +9 -0
  43. package/templates/ts/app/app.ts +7 -0
  44. package/templates/ts/eslint.config.js +12 -0
  45. package/templates/ts/package.json +30 -0
  46. package/templates/ts/tsconfig.json +28 -0
@@ -0,0 +1,124 @@
1
+ import { bundle } from "./bundle.js";
2
+ import fs from "fs";
3
+ import path from "path";
4
+
5
+ const cyan = (t) => `\x1b[36m${t}\x1b[0m`;
6
+ const green = (t) => `\x1b[32m${t}\x1b[0m`;
7
+
8
+ const routes = {};
9
+ const dynamicRoutes = {};
10
+ const actionMap = {};
11
+
12
+ function addRoute(method, route) {
13
+ const key = `${method.toUpperCase()}:${route}`;
14
+
15
+ return {
16
+ reply(value) {
17
+ routes[key] = {
18
+ type: typeof value === "object" ? "json" : "text",
19
+ value
20
+ };
21
+ },
22
+
23
+ action(name) {
24
+ if (route.includes(":")) {
25
+ if (!dynamicRoutes[method]) dynamicRoutes[method] = [];
26
+ dynamicRoutes[method].push({
27
+ method: method.toUpperCase(),
28
+ pattern: route,
29
+ action: name
30
+ });
31
+ } else {
32
+ routes[key] = {
33
+ type: "action",
34
+ value: name
35
+ };
36
+ actionMap[key] = name;
37
+ }
38
+ }
39
+ };
40
+ }
41
+
42
+ /**
43
+ * @typedef {Object} RouteHandler
44
+ * @property {(value: any) => void} reply - Send a direct response
45
+ * @property {(name: string) => void} action - Bind to a server-side action
46
+ */
47
+
48
+ /**
49
+ * Titan App Builder
50
+ */
51
+ const t = {
52
+ /**
53
+ * Define a GET route
54
+ * @param {string} route
55
+ * @returns {RouteHandler}
56
+ */
57
+ get(route) {
58
+ return addRoute("GET", route);
59
+ },
60
+
61
+ /**
62
+ * Define a POST route
63
+ * @param {string} route
64
+ * @returns {RouteHandler}
65
+ */
66
+ post(route) {
67
+ return addRoute("POST", route);
68
+ },
69
+
70
+ log(module, msg) {
71
+ console.log(`[\x1b[35m${module}\x1b[0m] ${msg}`);
72
+ },
73
+
74
+ /**
75
+ * Start the Titan Server
76
+ * @param {number} [port=3000]
77
+ * @param {string} [msg=""]
78
+ */
79
+ async start(port = 3000, msg = "") {
80
+ try {
81
+ console.log(cyan("[Titan] Preparing runtime..."));
82
+ await bundle();
83
+
84
+ const base = path.join(process.cwd(), "server");
85
+ if (!fs.existsSync(base)) {
86
+ fs.mkdirSync(base, { recursive: true });
87
+ }
88
+
89
+ const routesPath = path.join(base, "routes.json");
90
+ const actionMapPath = path.join(base, "action_map.json");
91
+
92
+ fs.writeFileSync(
93
+ routesPath,
94
+ JSON.stringify(
95
+ {
96
+ __config: { port },
97
+ routes,
98
+ __dynamic_routes: Object.values(dynamicRoutes).flat()
99
+ },
100
+ null,
101
+ 2
102
+ )
103
+ );
104
+
105
+ fs.writeFileSync(
106
+ actionMapPath,
107
+ JSON.stringify(actionMap, null, 2)
108
+ );
109
+
110
+ console.log(green("✔ Titan metadata written successfully"));
111
+ if (msg) console.log(cyan(msg));
112
+
113
+ } catch (e) {
114
+ console.error(`\x1b[31m[Titan] Build Error: ${e.message}\x1b[0m`);
115
+ process.exit(1);
116
+ }
117
+ }
118
+ };
119
+
120
+ /**
121
+ * Titan App Builder (Alias for t)
122
+ */
123
+ export const Titan = t;
124
+ export default t;
@@ -0,0 +1,28 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "ESNext",
4
+ "module": "ESNext",
5
+ "moduleResolution": "node",
6
+ "esModuleInterop": true,
7
+ "forceConsistentCasingInFileNames": true,
8
+ "strict": true,
9
+ "skipLibCheck": true,
10
+ "baseUrl": ".",
11
+ "paths": {
12
+ "@titanpl/native": [
13
+ "app/t.native"
14
+ ],
15
+ "@titanpl/route": [
16
+ "./titan/titan"
17
+ ],
18
+ "*": [
19
+ "./app/*"
20
+ ]
21
+ }
22
+ },
23
+ "include": [
24
+ "app/**/*",
25
+ "titan/**/*",
26
+ "node_modules/**/titan-ext.d.ts"
27
+ ]
28
+ }
@@ -0,0 +1,9 @@
1
+ import { log, defineAction } from "@titanpl/native";
2
+
3
+ export const getuser = defineAction((req) => {
4
+ log("Handling user request...");
5
+ return {
6
+ message: "Hello from TypeScript action!",
7
+ user_id: req.params.id
8
+ };
9
+ });
@@ -0,0 +1,7 @@
1
+ import t from "@titanpl/route";
2
+
3
+ t.get("/user/:id<number>").action("getuser") // pass a json payload { "name": "titan" }
4
+
5
+ t.get("/").reply("Ready to land on Titan Planet 🚀");
6
+
7
+ t.start(5100, "Titan Running!");
@@ -0,0 +1,12 @@
1
+ import { titanpl } from 'eslint-plugin-titanpl';
2
+ import tsParser from "@typescript-eslint/parser";
3
+
4
+ export default [
5
+ titanpl,
6
+ {
7
+ files: ["**/*.ts"],
8
+ languageOptions: {
9
+ parser: tsParser,
10
+ },
11
+ },
12
+ ];
@@ -0,0 +1,30 @@
1
+ {
2
+ "name": "{{name}}",
3
+ "description": "A Titan Planet server (TypeScript)",
4
+ "type": "module",
5
+ "titan": {
6
+ "template": "ts"
7
+ },
8
+ "dependencies": {
9
+ "@titanpl/cli": "2.0.4",
10
+ "@titanpl/route": "2.0.4",
11
+ "@titanpl/native": "2.0.4",
12
+ "@titanpl/core": "latest",
13
+ "@titanpl/node": "latest",
14
+ "@titanpl/packet": "2.0.4",
15
+ "typescript": "^5.0.0"
16
+ },
17
+ "scripts": {
18
+ "build": "titan build",
19
+ "dev": "titan dev",
20
+ "start": "titan start",
21
+ "lint": "eslint .",
22
+ "lint:fix": "eslint . --fix"
23
+ },
24
+ "devDependencies": {
25
+ "eslint": "^9.39.2",
26
+ "eslint-plugin-titanpl": "latest",
27
+ "@typescript-eslint/parser": "^8.54.0"
28
+ },
29
+ "version": "2.0.4"
30
+ }
@@ -0,0 +1,28 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "ESNext",
4
+ "module": "ESNext",
5
+ "moduleResolution": "node",
6
+ "esModuleInterop": true,
7
+ "forceConsistentCasingInFileNames": true,
8
+ "strict": true,
9
+ "skipLibCheck": true,
10
+ "baseUrl": ".",
11
+ "paths": {
12
+ "@titanpl/native": [
13
+ "app/t.native"
14
+ ],
15
+ "@titanpl/route": [
16
+ "./titan/titan"
17
+ ],
18
+ "*": [
19
+ "./app/*"
20
+ ]
21
+ }
22
+ },
23
+ "include": [
24
+ "app/**/*",
25
+ "titan/**/*",
26
+ "node_modules/**/titan-ext.d.ts"
27
+ ]
28
+ }