@titanpl/cli 2.0.1 → 2.0.2

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.
@@ -1,90 +1,90 @@
1
- import fs from 'fs';
2
- import path from 'path';
3
- import { fileURLToPath } from 'url';
4
- import chalk from 'chalk';
5
-
6
- const __filename = fileURLToPath(import.meta.url);
7
- const __dirname = path.dirname(__filename);
8
-
9
- export async function updateCommand() {
10
- const root = process.cwd();
11
- console.log(chalk.cyan(`\n→ Updating Titan project to latest...`));
12
-
13
- const pkgPath = path.join(root, 'package.json');
14
- if (!fs.existsSync(pkgPath)) {
15
- console.log(chalk.red(`✖ No package.json found. Are you in a project root ? `));
16
- return;
17
- }
18
-
19
- // 1. Update package.json versions
20
- try {
21
- const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf8'));
22
- let modified = false;
23
-
24
- const titanDeps = [
25
- 'titanpl',
26
- 'titanpl-sdk',
27
- '@titanpl/cli',
28
- '@titanpl/route',
29
- '@titanpl/native',
30
- '@titanpl/packet',
31
- '@titanpl/core',
32
- '@titanpl/node'
33
- ];
34
-
35
- if (pkg.dependencies) {
36
- for (const dep of titanDeps) {
37
- if (pkg.dependencies[dep]) {
38
- pkg.dependencies[dep] = "latest";
39
- modified = true;
40
- }
41
- }
42
- }
43
-
44
- if (pkg.devDependencies) {
45
- for (const dep of titanDeps) {
46
- if (pkg.devDependencies[dep]) {
47
- pkg.devDependencies[dep] = "latest";
48
- modified = true;
49
- }
50
- }
51
- }
52
-
53
- if (modified) {
54
- fs.writeFileSync(pkgPath, JSON.stringify(pkg, null, 2) + '\n');
55
- console.log(chalk.green(` ✔ Updated Titan dependencies in package.json`));
56
- }
57
- } catch (e) {
58
- console.log(chalk.yellow(` ⚠️ Failed to update package.json: ${e.message}`));
59
- }
60
-
61
- // 2. Refresh Dockerfile and dotfiles from templates
62
- const commonDir = path.resolve(__dirname, '..', '..', '..', '..', 'templates', 'common');
63
- if (fs.existsSync(commonDir)) {
64
- const filesToSync = [
65
- ['Dockerfile', 'Dockerfile'],
66
- ['_dockerignore', '.dockerignore'],
67
- ['_gitignore', '.gitignore'],
68
- ];
69
-
70
- for (const [srcName, destName] of filesToSync) {
71
- const src = path.join(commonDir, srcName);
72
- const dest = path.join(root, destName);
73
- if (fs.existsSync(src)) {
74
- fs.copyFileSync(src, dest);
75
- console.log(chalk.green(` ✔ Synchronized ${destName}`));
76
- }
77
- }
78
-
79
- // Also update app/t.native.d.ts if it exists
80
- const nativeTypesSrc = path.join(commonDir, 'app', 't.native.d.ts');
81
- const nativeTypesDest = path.join(root, 'app', 't.native.d.ts');
82
- if (fs.existsSync(nativeTypesSrc) && fs.existsSync(path.join(root, 'app'))) {
83
- fs.copyFileSync(nativeTypesSrc, nativeTypesDest);
84
- console.log(chalk.green(` ✔ Updated app/t.native.d.ts`));
85
- }
86
- }
87
-
88
- console.log(chalk.green(`\n✔ Update complete!\n`));
89
- console.log(chalk.yellow(` Please run 'npm install' to apply changes.\n`));
90
- }
1
+ import fs from 'fs';
2
+ import path from 'path';
3
+ import { fileURLToPath } from 'url';
4
+ import chalk from 'chalk';
5
+
6
+ const __filename = fileURLToPath(import.meta.url);
7
+ const __dirname = path.dirname(__filename);
8
+
9
+ export async function updateCommand() {
10
+ const root = process.cwd();
11
+ console.log(chalk.cyan(`\n→ Updating Titan project to latest...`));
12
+
13
+ const pkgPath = path.join(root, 'package.json');
14
+ if (!fs.existsSync(pkgPath)) {
15
+ console.log(chalk.red(`✖ No package.json found. Are you in a project root ? `));
16
+ return;
17
+ }
18
+
19
+ // 1. Update package.json versions
20
+ try {
21
+ const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf8'));
22
+ let modified = false;
23
+
24
+ const titanDeps = [
25
+ 'titanpl',
26
+ 'titanpl-sdk',
27
+ '@titanpl/cli',
28
+ '@titanpl/route',
29
+ '@titanpl/native',
30
+ '@titanpl/packet',
31
+ '@titanpl/core',
32
+ '@titanpl/node'
33
+ ];
34
+
35
+ if (pkg.dependencies) {
36
+ for (const dep of titanDeps) {
37
+ if (pkg.dependencies[dep]) {
38
+ pkg.dependencies[dep] = "latest";
39
+ modified = true;
40
+ }
41
+ }
42
+ }
43
+
44
+ if (pkg.devDependencies) {
45
+ for (const dep of titanDeps) {
46
+ if (pkg.devDependencies[dep]) {
47
+ pkg.devDependencies[dep] = "latest";
48
+ modified = true;
49
+ }
50
+ }
51
+ }
52
+
53
+ if (modified) {
54
+ fs.writeFileSync(pkgPath, JSON.stringify(pkg, null, 2) + '\n');
55
+ console.log(chalk.green(` ✔ Updated Titan dependencies in package.json`));
56
+ }
57
+ } catch (e) {
58
+ console.log(chalk.yellow(` ⚠️ Failed to update package.json: ${e.message}`));
59
+ }
60
+
61
+ // 2. Refresh Dockerfile and dotfiles from templates
62
+ const commonDir = path.resolve(__dirname, '..', '..', '..', '..', 'templates', 'common');
63
+ if (fs.existsSync(commonDir)) {
64
+ const filesToSync = [
65
+ ['Dockerfile', 'Dockerfile'],
66
+ ['_dockerignore', '.dockerignore'],
67
+ ['_gitignore', '.gitignore'],
68
+ ];
69
+
70
+ for (const [srcName, destName] of filesToSync) {
71
+ const src = path.join(commonDir, srcName);
72
+ const dest = path.join(root, destName);
73
+ if (fs.existsSync(src)) {
74
+ fs.copyFileSync(src, dest);
75
+ console.log(chalk.green(` ✔ Synchronized ${destName}`));
76
+ }
77
+ }
78
+
79
+ // Also update app/t.native.d.ts if it exists
80
+ const nativeTypesSrc = path.join(commonDir, 'app', 't.native.d.ts');
81
+ const nativeTypesDest = path.join(root, 'app', 't.native.d.ts');
82
+ if (fs.existsSync(nativeTypesSrc) && fs.existsSync(path.join(root, 'app'))) {
83
+ fs.copyFileSync(nativeTypesSrc, nativeTypesDest);
84
+ console.log(chalk.green(` ✔ Updated app/t.native.d.ts`));
85
+ }
86
+ }
87
+
88
+ console.log(chalk.green(`\n✔ Update complete!\n`));
89
+ console.log(chalk.yellow(` Please run 'npm install' to apply changes.\n`));
90
+ }
package/src/engine.js CHANGED
@@ -1,152 +1,152 @@
1
- import os from 'os';
2
- import path from 'path';
3
- import fs from 'fs';
4
- import { createRequire } from 'module';
5
- import { spawn } from 'child_process';
6
- import { fileURLToPath } from 'url';
7
-
8
- const __filename = fileURLToPath(import.meta.url);
9
- const __dirname = path.dirname(__filename);
10
-
11
- const require = createRequire(import.meta.url);
12
-
13
- /**
14
- * Resolves the engine binary path. Returns null if not found (non-fatal).
15
- * Used by the dev command to pre-resolve and inject the path via env.
16
- */
17
- export function resolveEngineBinaryPath() {
18
- const platform = os.platform();
19
- const arch = os.arch();
20
- const pkgName = `@titanpl/engine-${platform}-${arch}`;
21
- const binName = platform === 'win32' ? 'titan-server.exe' : 'titan-server';
22
-
23
- // 1. Monorepo search (local dev)
24
- const searchPaths = [
25
- __dirname,
26
- process.cwd(),
27
- path.join(process.cwd(), '..'),
28
- path.join(process.cwd(), '..', '..')
29
- ];
30
-
31
- for (let startPath of searchPaths) {
32
- let current = startPath;
33
- for (let i = 0; i < 8; i++) {
34
- const potential = path.join(current, 'engine', 'target', 'release', binName);
35
- if (fs.existsSync(potential)) return potential;
36
- const parent = path.dirname(current);
37
- if (parent === current) break;
38
- current = parent;
39
- }
40
- }
41
-
42
- // 2. Resolve from CLI's own context (correct require context for optionalDependencies)
43
- try {
44
- const pkgPath = require.resolve(`${pkgName}/package.json`);
45
- const binaryPath = path.join(path.dirname(pkgPath), 'bin', binName);
46
- if (fs.existsSync(binaryPath)) return binaryPath;
47
- } catch (e) { }
48
-
49
- // 3. Fallback: sibling in node_modules (global install layout)
50
- const cliParent = path.dirname(path.dirname(__dirname)); // up from cli/src → cli → parent
51
- const siblingBin = path.join(cliParent, pkgName, 'bin', binName);
52
- if (fs.existsSync(siblingBin)) return siblingBin;
53
-
54
- return null;
55
- }
56
-
57
- /**
58
- * Resolves the engine binary path. Exits with a fatal error if not found.
59
- * Used by the start command.
60
- */
61
- export function getEngineBinaryPath() {
62
- const platform = os.platform();
63
- const arch = os.arch();
64
- const pkgName = `@titanpl/engine-${platform}-${arch}`;
65
-
66
- const resolved = resolveEngineBinaryPath();
67
- if (resolved) return resolved;
68
-
69
- console.error(`\n[TITAN FATAL] Unsupported platform: ${platform} (${arch})`);
70
- console.error(`Or the optional dependency '${pkgName}' failed to install.`);
71
- console.error(`Try: npm install -g @titanpl/cli\n`);
72
- process.exit(1);
73
- }
74
-
75
- export function startEngine(watchMode = false) {
76
- const distPath = path.resolve(process.cwd(), 'dist');
77
-
78
- if (!fs.existsSync(distPath)) {
79
- console.error("❌ 'dist/' directory not found. Please run 'titan build' first.");
80
- process.exit(1);
81
- }
82
-
83
- const binaryPath = getEngineBinaryPath();
84
-
85
- // Arguments passed to Rust backend
86
- const args = ['run', distPath];
87
- if (watchMode) args.push('--watch');
88
-
89
- console.log(`🚀 Starting Titan Engine...`);
90
-
91
- const engineProcess = spawn(binaryPath, args, {
92
- stdio: ['inherit', 'pipe', 'pipe'],
93
- env: {
94
- ...process.env,
95
- TITAN_ENV: watchMode ? 'development' : 'production',
96
- Titan_Dev: watchMode ? '1' : '0'
97
- }
98
- });
99
-
100
- let stderrBuffer = "";
101
-
102
- engineProcess.stdout.pipe(process.stdout);
103
- engineProcess.stderr.on('data', (data) => {
104
- const chunk = data.toString();
105
- stderrBuffer += chunk;
106
- process.stderr.write(data);
107
- });
108
-
109
- engineProcess.on('close', (code) => {
110
- if (code !== 0 && code !== null) {
111
- // Check for port binding errors
112
- const isPortError = stderrBuffer.includes("Address already in use") ||
113
- stderrBuffer.includes("address in use") ||
114
- stderrBuffer.includes("os error 10048") || // Windows
115
- stderrBuffer.includes("EADDRINUSE") ||
116
- stderrBuffer.includes("AddrInUse");
117
-
118
- if (isPortError) {
119
- // Try to read intended port
120
- let port = 5100;
121
- try {
122
- const routesPath = path.join(process.cwd(), "dist", "routes.json");
123
- if (fs.existsSync(routesPath)) {
124
- const routesConfig = JSON.parse(fs.readFileSync(routesPath, "utf8"));
125
- if (routesConfig && routesConfig.__config && routesConfig.__config.port) {
126
- port = routesConfig.__config.port;
127
- }
128
- }
129
- } catch (e) { }
130
-
131
- const cyan = (t) => `\x1b[36m${t}\x1b[0m`;
132
- const yellow = (t) => `\x1b[33m${t}\x1b[0m`;
133
- const red = (t) => `\x1b[31m${t}\x1b[0m`;
134
-
135
- console.log("");
136
- console.log(red("⏣ Your application cannot enter this orbit"));
137
- console.log(red(`↳ Another application is already bound to port ${port}.`));
138
- console.log("");
139
-
140
- console.log(yellow("Recommended Actions:"));
141
- console.log(yellow(" 1.") + " Release the occupied orbit (stop the other service).");
142
- console.log(yellow(" 2.") + " Assign your application to a new orbit in " + cyan("app/app.js"));
143
- console.log(yellow(" Example: ") + cyan(`t.start(${port + 1}, "Titan Running!")`));
144
- console.log("");
145
- } else {
146
- console.error(`\n❌ [Titan Engine died with exit code ${code}]`);
147
- }
148
- }
149
- });
150
-
151
- return engineProcess;
152
- }
1
+ import os from 'os';
2
+ import path from 'path';
3
+ import fs from 'fs';
4
+ import { createRequire } from 'module';
5
+ import { spawn } from 'child_process';
6
+ import { fileURLToPath } from 'url';
7
+
8
+ const __filename = fileURLToPath(import.meta.url);
9
+ const __dirname = path.dirname(__filename);
10
+
11
+ const require = createRequire(import.meta.url);
12
+
13
+ /**
14
+ * Resolves the engine binary path. Returns null if not found (non-fatal).
15
+ * Used by the dev command to pre-resolve and inject the path via env.
16
+ */
17
+ export function resolveEngineBinaryPath() {
18
+ const platform = os.platform();
19
+ const arch = os.arch();
20
+ const pkgName = `@titanpl/engine-${platform}-${arch}`;
21
+ const binName = platform === 'win32' ? 'titan-server.exe' : 'titan-server';
22
+
23
+ // 1. Monorepo search (local dev)
24
+ const searchPaths = [
25
+ __dirname,
26
+ process.cwd(),
27
+ path.join(process.cwd(), '..'),
28
+ path.join(process.cwd(), '..', '..')
29
+ ];
30
+
31
+ for (let startPath of searchPaths) {
32
+ let current = startPath;
33
+ for (let i = 0; i < 8; i++) {
34
+ const potential = path.join(current, 'engine', 'target', 'release', binName);
35
+ if (fs.existsSync(potential)) return potential;
36
+ const parent = path.dirname(current);
37
+ if (parent === current) break;
38
+ current = parent;
39
+ }
40
+ }
41
+
42
+ // 2. Resolve from CLI's own context (correct require context for optionalDependencies)
43
+ try {
44
+ const pkgPath = require.resolve(`${pkgName}/package.json`);
45
+ const binaryPath = path.join(path.dirname(pkgPath), 'bin', binName);
46
+ if (fs.existsSync(binaryPath)) return binaryPath;
47
+ } catch (e) { }
48
+
49
+ // 3. Fallback: sibling in node_modules (global install layout)
50
+ const cliParent = path.dirname(path.dirname(__dirname)); // up from cli/src → cli → parent
51
+ const siblingBin = path.join(cliParent, pkgName, 'bin', binName);
52
+ if (fs.existsSync(siblingBin)) return siblingBin;
53
+
54
+ return null;
55
+ }
56
+
57
+ /**
58
+ * Resolves the engine binary path. Exits with a fatal error if not found.
59
+ * Used by the start command.
60
+ */
61
+ export function getEngineBinaryPath() {
62
+ const platform = os.platform();
63
+ const arch = os.arch();
64
+ const pkgName = `@titanpl/engine-${platform}-${arch}`;
65
+
66
+ const resolved = resolveEngineBinaryPath();
67
+ if (resolved) return resolved;
68
+
69
+ console.error(`\n[TITAN FATAL] Unsupported platform: ${platform} (${arch})`);
70
+ console.error(`Or the optional dependency '${pkgName}' failed to install.`);
71
+ console.error(`Try: npm install -g @titanpl/cli\n`);
72
+ process.exit(1);
73
+ }
74
+
75
+ export function startEngine(watchMode = false) {
76
+ const distPath = path.resolve(process.cwd(), 'dist');
77
+
78
+ if (!fs.existsSync(distPath)) {
79
+ console.error("❌ 'dist/' directory not found. Please run 'titan build' first.");
80
+ process.exit(1);
81
+ }
82
+
83
+ const binaryPath = getEngineBinaryPath();
84
+
85
+ // Arguments passed to Rust backend
86
+ const args = ['run', distPath];
87
+ if (watchMode) args.push('--watch');
88
+
89
+ console.log(`🚀 Starting Titan Engine...`);
90
+
91
+ const engineProcess = spawn(binaryPath, args, {
92
+ stdio: ['inherit', 'pipe', 'pipe'],
93
+ env: {
94
+ ...process.env,
95
+ TITAN_ENV: watchMode ? 'development' : 'production',
96
+ Titan_Dev: watchMode ? '1' : '0'
97
+ }
98
+ });
99
+
100
+ let stderrBuffer = "";
101
+
102
+ engineProcess.stdout.pipe(process.stdout);
103
+ engineProcess.stderr.on('data', (data) => {
104
+ const chunk = data.toString();
105
+ stderrBuffer += chunk;
106
+ process.stderr.write(data);
107
+ });
108
+
109
+ engineProcess.on('close', (code) => {
110
+ if (code !== 0 && code !== null) {
111
+ // Check for port binding errors
112
+ const isPortError = stderrBuffer.includes("Address already in use") ||
113
+ stderrBuffer.includes("address in use") ||
114
+ stderrBuffer.includes("os error 10048") || // Windows
115
+ stderrBuffer.includes("EADDRINUSE") ||
116
+ stderrBuffer.includes("AddrInUse");
117
+
118
+ if (isPortError) {
119
+ // Try to read intended port
120
+ let port = 5100;
121
+ try {
122
+ const routesPath = path.join(process.cwd(), "dist", "routes.json");
123
+ if (fs.existsSync(routesPath)) {
124
+ const routesConfig = JSON.parse(fs.readFileSync(routesPath, "utf8"));
125
+ if (routesConfig && routesConfig.__config && routesConfig.__config.port) {
126
+ port = routesConfig.__config.port;
127
+ }
128
+ }
129
+ } catch (e) { }
130
+
131
+ const cyan = (t) => `\x1b[36m${t}\x1b[0m`;
132
+ const yellow = (t) => `\x1b[33m${t}\x1b[0m`;
133
+ const red = (t) => `\x1b[31m${t}\x1b[0m`;
134
+
135
+ console.log("");
136
+ console.log(red("⏣ Your application cannot enter this orbit"));
137
+ console.log(red(`↳ Another application is already bound to port ${port}.`));
138
+ console.log("");
139
+
140
+ console.log(yellow("Recommended Actions:"));
141
+ console.log(yellow(" 1.") + " Release the occupied orbit (stop the other service).");
142
+ console.log(yellow(" 2.") + " Assign your application to a new orbit in " + cyan("app/app.js"));
143
+ console.log(yellow(" Example: ") + cyan(`t.start(${port + 1}, "Titan Running!")`));
144
+ console.log("");
145
+ } else {
146
+ console.error(`\n❌ [Titan Engine died with exit code ${code}]`);
147
+ }
148
+ }
149
+ });
150
+
151
+ return engineProcess;
152
+ }