tsdk-cli 1.0.0

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 (3) hide show
  1. package/bin/tsdk.js +95 -0
  2. package/index.js +133 -0
  3. package/package.json +23 -0
package/bin/tsdk.js ADDED
@@ -0,0 +1,95 @@
1
+ #!/usr/bin/env node
2
+
3
+ const { compileFile, run, requireTs } = require('../index.js');
4
+ const path = require('path');
5
+ const fs = require('fs');
6
+
7
+ const args = process.argv.slice(2);
8
+
9
+ if (args.length === 0) {
10
+ console.log(`
11
+ tsdk-cli - TypeScript Development Kit
12
+
13
+ Usage:
14
+ tsdk <file> [args...] Run a TypeScript file
15
+ tsdk -e <code> Evaluate TypeScript code
16
+ tsdk -v, --version Show version
17
+ tsdk -h, --help Show this help
18
+
19
+ Examples:
20
+ tsdk app.ts
21
+ tsdk server.ts --port 3000
22
+ tsdk -e "console.log('Hello TypeScript!')"
23
+ `);
24
+ process.exit(0);
25
+ }
26
+
27
+ const version = require('../package.json').version;
28
+
29
+ if (args[0] === '-v' || args[0] === '--version') {
30
+ console.log(`tsdk-cli v${version}`);
31
+ process.exit(0);
32
+ }
33
+
34
+ if (args[0] === '-h' || args[0] === '--help') {
35
+ console.log(`
36
+ tsdk-cli - TypeScript Development Kit
37
+
38
+ Usage:
39
+ tsdk <file> [args...] Run a TypeScript file
40
+ tsdk -e <code> Evaluate TypeScript code
41
+ tsdk -v, --version Show version
42
+ tsdk -h, --help Show this help
43
+
44
+ Examples:
45
+ tsdk app.ts
46
+ tsdk server.ts --port 3000
47
+ tsdk -e "console.log('Hello TypeScript!')"
48
+ `);
49
+ process.exit(0);
50
+ }
51
+
52
+ if (args[0] === '-e') {
53
+ const code = args[1];
54
+ if (!code) {
55
+ console.error('Error: No code provided with -e option');
56
+ process.exit(1);
57
+ }
58
+
59
+ try {
60
+ const { compile } = require('../index.js');
61
+ const { code: compiled } = compile(code, { loader: 'ts' });
62
+ eval(compiled);
63
+ process.exit(0);
64
+ } catch (error) {
65
+ console.error(`Error: ${error.message}`);
66
+ process.exit(1);
67
+ }
68
+ }
69
+
70
+ const filePath = args[0];
71
+ const fileArgs = args.slice(1);
72
+
73
+ const ext = path.extname(filePath).toLowerCase();
74
+
75
+ if (!ext || (ext !== '.ts' && ext !== '.tsx')) {
76
+ console.error(`Error: Unsupported file type ${ext}. Only .ts and .tsx files are supported.`);
77
+ process.exit(1);
78
+ }
79
+
80
+ const fullPath = path.resolve(filePath);
81
+
82
+ if (!fs.existsSync(fullPath)) {
83
+ console.error(`Error: File not found: ${fullPath}`);
84
+ process.exit(1);
85
+ }
86
+
87
+ process.argv = [process.argv[0], process.argv[1], ...fileArgs];
88
+
89
+ try {
90
+ requireTs(fullPath);
91
+ } catch (error) {
92
+ console.error(`Error: ${error.message}`);
93
+ console.error(error.stack);
94
+ process.exit(1);
95
+ }
package/index.js ADDED
@@ -0,0 +1,133 @@
1
+ const { transformSync } = require('esbuild');
2
+ const path = require('path');
3
+ const fs = require('fs');
4
+
5
+ const defaultOptions = {
6
+ target: 'es2020',
7
+ platform: 'node',
8
+ sourcemap: 'inline',
9
+ loader: 'ts'
10
+ };
11
+
12
+ function compile(code, options = {}) {
13
+ const mergedOptions = { ...defaultOptions, ...options };
14
+
15
+ try {
16
+ const result = transformSync(code, {
17
+ target: mergedOptions.target,
18
+ platform: mergedOptions.platform,
19
+ sourcemap: mergedOptions.sourcemap,
20
+ loader: mergedOptions.loader
21
+ });
22
+
23
+ return {
24
+ code: result.code,
25
+ map: result.map
26
+ };
27
+ } catch (error) {
28
+ throw new Error(`Compilation error: ${error.message}`);
29
+ }
30
+ }
31
+
32
+ function compileFile(filePath, options = {}) {
33
+ const fullPath = path.resolve(filePath);
34
+
35
+ if (!fs.existsSync(fullPath)) {
36
+ throw new Error(`File not found: ${fullPath}`);
37
+ }
38
+
39
+ const code = fs.readFileSync(fullPath, 'utf8');
40
+ const ext = path.extname(filePath).toLowerCase();
41
+
42
+ const loader = ext === '.tsx' ? 'tsx' : 'ts';
43
+
44
+ return compile(code, { ...options, loader });
45
+ }
46
+
47
+ function run(filePath, options = {}) {
48
+ const { code } = compileFile(filePath, options);
49
+
50
+ const moduleWrapper = `
51
+ const module = { exports: {} };
52
+ const require = (path) => {
53
+ const resolved = require.resolve(path);
54
+ return require(resolved);
55
+ };
56
+ require.resolve = (path) => {
57
+ return require.resolve(path);
58
+ };
59
+ const exports = module.exports;
60
+ ${code}
61
+ return module.exports;
62
+ `;
63
+
64
+ return new Function('require', 'module', 'exports', '__dirname', '__filename', code)(
65
+ require,
66
+ { exports: {} },
67
+ {},
68
+ path.dirname(path.resolve(filePath)),
69
+ path.resolve(filePath)
70
+ );
71
+ }
72
+
73
+ function requireTs(modulePath) {
74
+ const ext = path.extname(modulePath);
75
+ const filePath = ext ? modulePath : `${modulePath}.ts`;
76
+ const fullPath = path.resolve(filePath);
77
+
78
+ if (!fs.existsSync(fullPath)) {
79
+ throw new Error(`Module not found: ${fullPath}`);
80
+ }
81
+
82
+ const { code } = compileFile(fullPath);
83
+
84
+ const module = { exports: {} };
85
+ const requireFn = (reqPath) => {
86
+ let resolved = reqPath;
87
+ if (!path.isAbsolute(reqPath)) {
88
+ resolved = path.resolve(path.dirname(fullPath), reqPath);
89
+ if (!fs.existsSync(resolved)) {
90
+ resolved += '.js';
91
+ }
92
+ if (!fs.existsSync(resolved)) {
93
+ resolved = require.resolve(reqPath);
94
+ }
95
+ }
96
+ if (resolved.endsWith('.ts') || resolved.endsWith('.tsx')) {
97
+ return requireTs(resolved);
98
+ }
99
+ return require(resolved);
100
+ };
101
+
102
+ requireFn.resolve = (reqPath) => require.resolve(reqPath);
103
+
104
+ const dirname = path.dirname(fullPath);
105
+
106
+ const wrappedCode = code.replace(/export\s+const\s+(\w+)\s*=\s*/g, 'module.exports.$1 = ')
107
+ .replace(/export\s+function\s+(\w+)/g, 'module.exports.$1 = function $1')
108
+ .replace(/export\s+default\s+/g, 'module.exports = ')
109
+ .replace(/export\s+\{\s*([^}]+)\s*\}/g, (match, exports) => {
110
+ return exports.split(',').map(e => {
111
+ const name = e.trim();
112
+ return `module.exports['${name}'] = ${name};`;
113
+ }).join('\n');
114
+ });
115
+
116
+ new Function('require', 'module', 'exports', '__dirname', '__filename', wrappedCode)(
117
+ requireFn,
118
+ module,
119
+ module.exports,
120
+ dirname,
121
+ fullPath
122
+ );
123
+
124
+ return module.exports;
125
+ }
126
+
127
+ module.exports = {
128
+ compile,
129
+ compileFile,
130
+ run,
131
+ requireTs,
132
+ defaultOptions
133
+ };
package/package.json ADDED
@@ -0,0 +1,23 @@
1
+ {
2
+ "name": "tsdk-cli",
3
+ "version": "1.0.0",
4
+ "description": "TypeScript Development Kit - Run TypeScript directly in Node.js",
5
+ "main": "index.js",
6
+ "bin": {
7
+ "tsdk": "./bin/tsdk.js"
8
+ },
9
+ "scripts": {
10
+ "test": "node test/index.js"
11
+ },
12
+ "keywords": ["typescript", "node", "cli", "runtime", "ts-node", "esbuild"],
13
+ "author": "Vexify",
14
+ "license": "Apache-2.0",
15
+ "files": [
16
+ "index.js",
17
+ "bin/",
18
+ "LICENSE"
19
+ ],
20
+ "dependencies": {
21
+ "esbuild": "^0.21.0"
22
+ }
23
+ }