typenative 0.0.16 → 0.0.18

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/bin/index.js CHANGED
@@ -8,143 +8,150 @@ import { fileURLToPath } from 'url';
8
8
  const __filename = fileURLToPath(import.meta.url);
9
9
  const __dirname = path.dirname(__filename);
10
10
  (async function main() {
11
- const scriptMode = process.argv.findIndex((a) => a === '--script') > -1;
12
- const newCommand = process.argv.findIndex((a) => a === '--new') > -1;
13
- const sourceIndex = process.argv.findIndex((a) => a === '--source');
14
- const source = sourceIndex > -1 ? process.argv[sourceIndex + 1] : null;
15
- const outputIndex = process.argv.findIndex((a) => a === '--output');
16
- const output = outputIndex > -1 ? process.argv[outputIndex + 1] : null;
17
- const answers = await inquirer.prompt([
18
- {
19
- type: 'input',
20
- name: 'projectName',
21
- message: 'Enter Project Name:',
22
- when: newCommand,
23
- validate: (input) => input.trim() !== ''
24
- },
25
- {
26
- type: 'confirm',
27
- name: 'installDependencies',
28
- message: 'Do you want to install dependencies?',
29
- when: newCommand
30
- },
31
- {
32
- type: 'input',
33
- name: 'path',
34
- message: 'Enter Path to typescript main file:',
35
- when: !newCommand && !scriptMode && !source,
36
- validate: (input) => input.trim() !== ''
37
- },
38
- {
39
- type: 'input',
40
- name: 'output',
41
- message: 'Enter Output Path:',
42
- when: !newCommand && !scriptMode && !output,
43
- validate: (input) => input.trim() !== ''
44
- },
45
- {
46
- type: 'editor',
47
- name: 'tsCode',
48
- message: 'Write your typescript code here:',
49
- when: !newCommand && scriptMode && !source,
50
- default: `console.log('Hello, World!');`
51
- }
52
- ]);
53
- if (newCommand) {
54
- const projectName = answers.projectName.trim();
55
- await fs.ensureDir(projectName);
56
- await fs.writeFile(path.join(projectName, 'main.ts'), `// Write your TypeScript code here\nconsole.log('Hello, World!');\n`, { encoding: 'utf-8' });
57
- await fs.writeFile(path.join(projectName, 'tsconfig.json'), getTsConfig(), {
58
- encoding: 'utf-8'
59
- });
60
- await fs.writeFile(path.join(projectName, 'package.json'), getPackageJson(projectName), {
61
- encoding: 'utf-8'
62
- });
63
- await fs.writeFile(path.join(projectName, '.gitignore'), getGitIgnore(), {
64
- encoding: 'utf-8'
65
- });
66
- await fs.writeFile(path.join(projectName, 'README.md'), getReadMe(projectName), {
67
- encoding: 'utf-8'
68
- });
69
- console.log(`Project "${projectName}" created successfully!`);
70
- if (answers.installDependencies) {
71
- console.log('Installing dependencies...');
72
- await execa('npm install', { cwd: projectName, stdio: 'inherit' });
73
- console.log('Dependencies installed successfully!');
74
- }
75
- return;
11
+ const scriptMode = process.argv.findIndex((a) => a === '--script') > -1;
12
+ const newCommand = process.argv.findIndex((a) => a === '--new') > -1;
13
+ const sourceIndex = process.argv.findIndex((a) => a === '--source');
14
+ const source = sourceIndex > -1 ? process.argv[sourceIndex + 1] : null;
15
+ const outputIndex = process.argv.findIndex((a) => a === '--output');
16
+ const output = outputIndex > -1 ? process.argv[outputIndex + 1] : null;
17
+ const answers = await inquirer.prompt([
18
+ {
19
+ type: 'input',
20
+ name: 'projectName',
21
+ message: 'Enter Project Name:',
22
+ when: newCommand,
23
+ validate: (input) => input.trim() !== ''
24
+ },
25
+ {
26
+ type: 'confirm',
27
+ name: 'installDependencies',
28
+ message: 'Do you want to install dependencies?',
29
+ when: newCommand
30
+ },
31
+ {
32
+ type: 'input',
33
+ name: 'path',
34
+ message: 'Enter Path to typescript main file:',
35
+ when: !newCommand && !scriptMode && !source,
36
+ validate: (input) => input.trim() !== ''
37
+ },
38
+ {
39
+ type: 'input',
40
+ name: 'output',
41
+ message: 'Enter Output Path:',
42
+ when: !newCommand && !scriptMode && !output,
43
+ validate: (input) => input.trim() !== ''
44
+ },
45
+ {
46
+ type: 'editor',
47
+ name: 'tsCode',
48
+ message: 'Write your typescript code here:',
49
+ when: !newCommand && scriptMode && !source,
50
+ default: `console.log('Hello, World!');`
76
51
  }
77
- const tsCode = answers.tsCode
78
- ? answers.tsCode
79
- : await fs.readFile(source ?? answers.path, { encoding: 'utf-8' });
80
- const nativeCode = transpileToNative(tsCode);
81
- await fs.ensureDir('dist');
82
- await fs.writeFile('dist/code.go', nativeCode, { encoding: 'utf-8' });
83
- await execa('go build -o dist/native.exe dist/code.go', {
84
- stdio: 'inherit'
52
+ ]);
53
+ if (newCommand) {
54
+ const projectName = answers.projectName.trim();
55
+ await fs.ensureDir(projectName);
56
+ await fs.writeFile(
57
+ path.join(projectName, 'main.ts'),
58
+ `// Write your TypeScript code here\nconsole.log('Hello, World!');\n`,
59
+ { encoding: 'utf-8' }
60
+ );
61
+ await fs.writeFile(path.join(projectName, 'tsconfig.json'), getTsConfig(), {
62
+ encoding: 'utf-8'
85
63
  });
86
- // await fs.remove('dist/code.go');
87
- if (scriptMode) {
88
- await execa('dist/native.exe', {
89
- stdio: 'inherit'
90
- });
91
- //await fs.remove('dist/native.exe');
92
- }
93
- else if (output || answers.output) {
94
- await fs.copy('dist/native.exe', output ?? answers.output, { overwrite: true });
95
- //await fs.remove('dist/native.exe');
96
- console.log(`Created native executable at: ${output ?? answers.output}`);
64
+ await fs.writeFile(path.join(projectName, 'package.json'), getPackageJson(projectName), {
65
+ encoding: 'utf-8'
66
+ });
67
+ await fs.writeFile(path.join(projectName, '.gitignore'), getGitIgnore(), {
68
+ encoding: 'utf-8'
69
+ });
70
+ await fs.writeFile(path.join(projectName, 'README.md'), getReadMe(projectName), {
71
+ encoding: 'utf-8'
72
+ });
73
+ console.log(`Project "${projectName}" created successfully!`);
74
+ if (answers.installDependencies) {
75
+ console.log('Installing dependencies...');
76
+ await execa('npm', ['install'], { cwd: projectName, stdio: 'inherit' });
77
+ console.log('Dependencies installed successfully!');
97
78
  }
79
+ return;
80
+ }
81
+ const tsCode = answers.tsCode
82
+ ? answers.tsCode
83
+ : await fs.readFile(source ?? answers.path, { encoding: 'utf-8' });
84
+ const nativeCode = transpileToNative(tsCode);
85
+ const exeName = process.platform === 'win32' ? 'native.exe' : 'native';
86
+ const exePath = `dist/${exeName}`;
87
+ await fs.ensureDir('dist');
88
+ await fs.writeFile('dist/code.go', nativeCode, { encoding: 'utf-8' });
89
+ await execa('go', ['build', '-o', exePath, 'dist/code.go'], {
90
+ stdio: 'inherit'
91
+ });
92
+ // await fs.remove('dist/code.go');
93
+ if (scriptMode) {
94
+ await execa(exePath, {
95
+ stdio: 'inherit'
96
+ });
97
+ //await fs.remove(exePath);
98
+ } else if (output || answers.output) {
99
+ await fs.copy(exePath, output ?? answers.output, { overwrite: true });
100
+ //await fs.remove(exePath);
101
+ console.log(`Created native executable at: ${output ?? answers.output}`);
102
+ }
98
103
  })();
99
104
  function getPackageJson(projectName) {
100
- const pckg = {
101
- name: projectName,
102
- version: '1.0.0',
103
- scripts: {
104
- execute: 'npx typenative --source main.ts --script',
105
- build: `npx typenative --source main.ts --output bin/${projectName}.exe`
106
- },
107
- devDependencies: {
108
- typenative: '^0.0.16'
109
- }
110
- };
111
- return JSON.stringify(pckg, null, 2);
105
+ const exeName = process.platform === 'win32' ? `${projectName}.exe` : projectName;
106
+ const pckg = {
107
+ name: projectName,
108
+ version: '1.0.0',
109
+ scripts: {
110
+ execute: 'npx typenative --source main.ts --script',
111
+ build: `npx typenative --source main.ts --output bin/${exeName}`
112
+ },
113
+ devDependencies: {
114
+ typenative: '^0.0.16'
115
+ }
116
+ };
117
+ return JSON.stringify(pckg, null, 2);
112
118
  }
113
119
  function getTsConfig() {
114
- const tsConfig = {
115
- include: ['**/*.ts'],
116
- compilerOptions: {
117
- target: 'es2020',
118
- lib: [],
119
- types: ['./node_modules/typenative/types/typenative.d.ts'],
120
- rootDir: '.',
121
- strict: true,
122
- noImplicitAny: true
123
- }
124
- };
125
- return JSON.stringify(tsConfig, null, 2);
120
+ const tsConfig = {
121
+ include: ['**/*.ts'],
122
+ compilerOptions: {
123
+ target: 'es2020',
124
+ lib: [],
125
+ types: ['./node_modules/typenative/types/typenative.d.ts'],
126
+ rootDir: '.',
127
+ strict: true,
128
+ noImplicitAny: true
129
+ }
130
+ };
131
+ return JSON.stringify(tsConfig, null, 2);
126
132
  }
127
133
  function getGitIgnore() {
128
- return `# TypeNative generated files
129
- node_modules/
130
- dist/
131
- bin/
134
+ return `# TypeNative generated files
135
+ node_modules/
136
+ dist/
137
+ bin/
132
138
  `;
133
139
  }
134
140
  function getReadMe(projectName) {
135
- return `# ${projectName}
136
-
137
- This project was created using TypeNative, a tool to transpile TypeScript code to native Go code.
138
-
139
- ## How to Run
140
-
141
- You can write your TypeScript code in the \`main.ts\` file. The code will be transpiled to Go and compiled into a native executable.
142
- You can also run the code directly in script mode using \`npm run execute\`.
143
-
144
- ## How to Build
145
-
146
- 1. Install dependencies: \`npm install\` (if not done already)
147
- 2. Build the project: \`npm run build\`
148
- 3. Run the executable: \`./bin/${projectName}.exe\`
141
+ const exeName = process.platform === 'win32' ? `${projectName}.exe` : projectName;
142
+ return `# ${projectName}
143
+
144
+ This project was created using TypeNative, a tool to transpile TypeScript code to native Go code.
145
+
146
+ ## How to Run
147
+
148
+ You can write your TypeScript code in the \`main.ts\` file. The code will be transpiled to Go and compiled into a native executable.
149
+ You can also run the code directly in script mode using \`npm run execute\`.
150
+
151
+ ## How to Build
152
+
153
+ 1. Install dependencies: \`npm install\` (if not done already)
154
+ 2. Build the project: \`npm run build\`
155
+ 3. Run the executable: \`./bin/${exeName}\`
149
156
  `;
150
157
  }