typenative 0.0.1

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2022 danisss9
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,19 @@
1
+ # TypeNative
2
+
3
+ Build native applications using Typescript.
4
+
5
+ ## Get Started
6
+
7
+ - Write a file `test.ts` with content `console.log('Hello World!'); getchar()` or any other message.
8
+ - Run `npx typenative --target test.ts`
9
+ - Run the executable in `dist/native.exe` and see your message
10
+
11
+ ## Todo
12
+
13
+ Develpment:
14
+ - Everything (👉゚ヮ゚)👉
15
+
16
+ Examples:
17
+ - Database (using sqlite)
18
+ - Web Server (using libmicrohttpd)
19
+ - UI App (using webview)
package/bin/index.js ADDED
@@ -0,0 +1,43 @@
1
+ import inquirer from 'inquirer';
2
+ import shell from 'shelljs';
3
+ import fs from 'fs-extra';
4
+ import path from 'path';
5
+ import { transpileToC } from './transpiler.js';
6
+ (async function main() {
7
+ const scriptMode = process.argv.findIndex((a) => a === '--script') > -1;
8
+ const targetIndex = process.argv.findIndex((a) => a === '--source');
9
+ const target = targetIndex > -1 ? process.argv[targetIndex + 1] : null;
10
+ const answers = await inquirer.prompt([
11
+ {
12
+ type: 'input',
13
+ name: 'path',
14
+ message: 'Enter Path to typescript main file:',
15
+ when: !scriptMode && !target
16
+ },
17
+ {
18
+ type: 'input',
19
+ name: 'output',
20
+ message: 'Enter Output Path:',
21
+ when: !scriptMode && !target
22
+ },
23
+ {
24
+ type: 'editor',
25
+ name: 'tsCode',
26
+ message: 'Write your typescript code and save',
27
+ when: scriptMode && !target
28
+ }
29
+ ]);
30
+ const tsCode = answers.tsCode
31
+ ? answers.tsCode
32
+ : await fs.readFile(target ?? answers.path, { encoding: 'utf-8' });
33
+ const cCode = transpileToC(tsCode);
34
+ await fs.ensureDir('dist');
35
+ await fs.writeFile('dist/code.c', cCode, { encoding: 'utf-8' });
36
+ shell.exec(`${path.resolve()}\\tcc\\tcc.exe -g ${path.resolve()}/dist/code.c -o ${path.resolve()}/dist/native.exe ${scriptMode ? '-run' : ''}`);
37
+ if (answers.output) {
38
+ await fs.copy('dist/native.exe', answers.output, { overwrite: true });
39
+ }
40
+ if (!scriptMode) {
41
+ console.log('DONE');
42
+ }
43
+ })();
@@ -0,0 +1,39 @@
1
+ import ts from 'typescript';
2
+ export function transpileToC(code) {
3
+ const sourceFile = ts.createSourceFile('main.ts', code, ts.ScriptTarget.ES2020, true, ts.ScriptKind.TS);
4
+ return `
5
+ #define console.log(message) printf(message)
6
+
7
+ #include <stdio.h>
8
+ #include <string.h>
9
+
10
+
11
+ int main() {
12
+ ${visit(sourceFile)}
13
+ return 0;
14
+ }
15
+ `;
16
+ }
17
+ export function visit(node) {
18
+ let code = '';
19
+ if (ts.isIdentifier(node)) {
20
+ return node.text;
21
+ }
22
+ else if (ts.isStringLiteral(node)) {
23
+ return `"${node.text}"`;
24
+ }
25
+ else if (ts.isPropertyAccessExpression(node)) {
26
+ return `${visit(node.expression)}.${visit(node.name)}`;
27
+ }
28
+ else if (ts.isCallExpression(node)) {
29
+ const expr = visit(node.expression);
30
+ const leftSide = expr === 'console.log' ? 'printf' : expr;
31
+ return `${leftSide}(${node.arguments.map((a) => visit(a)).join(',')})`;
32
+ }
33
+ else if (ts.isExpressionStatement(node)) {
34
+ return visit(node.expression) + ';\n';
35
+ }
36
+ // console.log(ts.SyntaxKind[node.kind], node.getText());
37
+ ts.forEachChild(node, (subNode) => (code = code + visit(subNode)));
38
+ return code;
39
+ }
package/package.json ADDED
@@ -0,0 +1,40 @@
1
+ {
2
+ "name": "typenative",
3
+ "version": "0.0.1",
4
+ "description": "",
5
+ "main": "bin/index.js",
6
+ "bin": {
7
+ "typenative": "bin/index.js"
8
+ },
9
+ "type": "module",
10
+ "scripts": {
11
+ "build": "tsc",
12
+ "watch": "tsc --watch",
13
+ "start": "node ./bin/index",
14
+ "start:script": "node ./bin/index --script",
15
+ "test": "node ./bin/index --source test/test.ts --script",
16
+ "publish": "npm publish"
17
+ },
18
+ "repository": {
19
+ "type": "git",
20
+ "url": "git+https://github.com/danisss9/typenative.git"
21
+ },
22
+ "author": "Dani Santos",
23
+ "license": "MIT",
24
+ "bugs": {
25
+ "url": "https://github.com/danisss9/typenative/issues"
26
+ },
27
+ "homepage": "https://github.com/danisss9/typenative#readme",
28
+ "devDependencies": {
29
+ "@types/fs-extra": "^11.0.4",
30
+ "@types/inquirer": "^9.0.7",
31
+ "@types/node": "^20.11.20",
32
+ "@types/shelljs": "^0.8.15"
33
+ },
34
+ "dependencies": {
35
+ "fs-extra": "^11.2.0",
36
+ "inquirer": "^9.2.15",
37
+ "shelljs": "^0.8.5",
38
+ "typescript": "^5.3.3"
39
+ }
40
+ }
@@ -0,0 +1,19 @@
1
+ interface Array<T> {
2
+ /**
3
+ * Gets or sets the length of the array. This is a number one higher than the highest index in the array.
4
+ */
5
+ length: number;
6
+ /**
7
+ * Returns a string representation of an array.
8
+ */
9
+ toString(): string;
10
+
11
+ [n: number]: T;
12
+ }
13
+
14
+ interface Console {
15
+ log(...data: any[]): void;
16
+ }
17
+ declare var console: Console;
18
+
19
+ declare var getchar: () => void;