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/.github/workflows/npm-publish-github-packages.yml +73 -36
- package/CHANGELOG.md +109 -80
- package/LICENSE +21 -21
- package/README.md +154 -109
- package/bin/index.js +133 -126
- package/bin/transpiler.js +950 -35
- package/package.json +63 -53
- package/types/typenative.d.ts +247 -177
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
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
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
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
await fs.
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
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
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
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
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
}
|
|
111
|
-
|
|
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
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
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
|
-
|
|
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
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
You can
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
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
|
}
|