remove typescript

This commit is contained in:
TopchetoEU 2024-01-10 16:51:40 +02:00
parent 3f826cc85d
commit d1937fdb63
Signed by: topchetoeu
GPG Key ID: 6531B8583E5F6ED4
3 changed files with 29 additions and 151 deletions

2
.gitignore vendored
View File

@ -2,7 +2,6 @@
!/src !/src
!/src/**/* !/src/**/*
/src/assets/js/ts.js
!/doc !/doc
!/doc/**/* !/doc/**/*
@ -10,7 +9,6 @@
!/tests !/tests
!/tests/**/* !/tests/**/*
!/.github !/.github
!/.github/**/* !/.github/**/*

View File

@ -38,84 +38,6 @@ function run(suppressOutput, cmd, ...args) {
}) })
} }
async function downloadTypescript(outFile) {
try {
// Import the required libraries, without the need of a package.json
console.log('Importing modules...');
await run(true, 'npm', 'i', 'tar', 'zlib', 'uglify-js');
await fs.mkdir(pt.dirname(outFile), { recursive: true });
await fs.mkdir('tmp', { recursive: true });
const tar = require('tar');
const zlib = require('zlib');
const { minify } = await import('uglify-js');
// Download the package.json file of typescript
const packageDesc = await (await fetch('https://registry.npmjs.org/typescript/latest')).json();
const url = packageDesc.dist.tarball;
console.log('Extracting typescript...');
await new Promise(async (res, rej) => Readable.fromWeb((await fetch(url)).body)
.pipe(zlib.createGunzip())
.pipe(tar.x({ cwd: 'tmp', filter: v => v === 'package/lib/typescript.js' }))
.on('end', res)
.on('error', rej)
);
console.log('Compiling typescript to ES5...');
const ts = require('./tmp/package/lib/typescript');
const program = ts.createProgram([ 'tmp/package/lib/typescript.js' ], {
outFile: "tmp/typescript-es5.js",
target: ts.ScriptTarget.ES5,
module: ts.ModuleKind.None,
downlevelIteration: true,
allowJs: true,
});
program.emit();
console.log('Minifying typescript...');
const minified = minify((await fs.readFile('tmp/typescript-es5.js')).toString());
// const minified = { code: (await fs.readFile('tmp/typescript-es5.js')).toString() };
if (minified.error) throw minified.error;
// Patch unsupported regex syntax
minified.code = minified.code.replaceAll('[-/\\\\^$*+?.()|[\\]{}]', '[-/\\\\^$*+?.()|\\[\\]{}]');
const stream = await fs.open(outFile, 'w');
// Write typescript's license
await stream.write(new TextEncoder().encode(`
/*! *****************************************************************************
Copyright (c) Microsoft Corporation. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License"); you may not use
this file except in compliance with the License. You may obtain a copy of the
License at http://www.apache.org/licenses/LICENSE-2.0
THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
MERCHANTABLITY OR NON-INFRINGEMENT.
See the Apache Version 2.0 License for specific language governing permissions
and limitations under the License.
The following is a minified version of the unmodified Typescript 5.2
***************************************************************************** */
`));
await stream.write(minified.code);
console.log('Typescript bundling done!');
}
finally {
// Clean up all stuff left from typescript bundling
await fs.rm('tmp', { recursive: true, force: true });
await fs.rm('package.json');
await fs.rm('package-lock.json');
await fs.rm('node_modules', { recursive: true });
}
}
async function compileJava(conf) { async function compileJava(conf) {
try { try {
await fs.writeFile('Metadata.java', (await fs.readFile('src/me/topchetoeu/jscript/common/Metadata.java')).toString() await fs.writeFile('Metadata.java', (await fs.readFile('src/me/topchetoeu/jscript/common/Metadata.java')).toString()
@ -150,10 +72,6 @@ async function jar(conf, project, mainClass) {
(async () => { (async () => {
try { try {
if (argv[2] === 'init-ts') {
await downloadTypescript('src/me/topchetoeu/jscript/utils/assets/js/ts.js');
}
else {
const conf = { const conf = {
name: "java-jscript", name: "java-jscript",
author: "TopchetoEU", author: "TopchetoEU",
@ -167,24 +85,19 @@ async function jar(conf, project, mainClass) {
try { await fs.rm('dst', { recursive: true }); } catch {} try { await fs.rm('dst', { recursive: true }); } catch {}
await Promise.all([ await Promise.all([
(async () => { copy('src', 'dst/classes', v => !v.endsWith('.java')),
await copy('src', 'dst/classes', v => !v.endsWith('.java'));
await downloadTypescript('dst/classes/me/topchetoeu/jscript/utils/assets/js/ts.js');
})(),
compileJava(conf), compileJava(conf),
]); ]);
await Promise.all([ await Promise.all([
jar(conf, 'me.topchetoeu.jscript.common'), jar(conf, 'me.topchetoeu.jscript.common'),
jar(conf, 'me.topchetoeu.jscript.core'), jar(conf, 'me.topchetoeu.jscript.core'),
jar(conf, 'me.topchetoeu.jscript.lib'),
jar(conf, 'me.topchetoeu.jscript.utils'), jar(conf, 'me.topchetoeu.jscript.utils'),
jar(conf, 'me.topchetoeu.jscript', 'me.topchetoeu.jscript.utils.JScriptRepl'), jar(conf, 'me.topchetoeu.jscript', 'me.topchetoeu.jscript.utils.JScriptRepl'),
]); ]);
console.log('Done!'); console.log('Done!');
} }
}
catch (e) { catch (e) {
if (argv[2] === 'debug') throw e; if (argv[2] === 'debug') throw e;
console.log(e.toString()); console.log(e.toString());

View File

@ -46,8 +46,6 @@ public class JScriptRepl {
try { try {
for (var arg : args) { for (var arg : args) {
try { try {
if (arg.equals("--ts")) initTypescript();
else {
var file = Path.of(arg); var file = Path.of(arg);
var raw = Files.readString(file); var raw = Files.readString(file);
var res = engine.pushMsg( var res = engine.pushMsg(
@ -58,7 +56,6 @@ public class JScriptRepl {
Values.printValue(null, res); Values.printValue(null, res);
System.out.println(); System.out.println();
} }
}
catch (EngineException e) { Values.printError(e, null); } catch (EngineException e) { Values.printError(e, null); }
} }
for (var i = 0; ; i++) { for (var i = 0; ; i++) {
@ -128,36 +125,6 @@ public class JScriptRepl {
engineTask = engine.start(); engineTask = engine.start();
debugTask = debugServer.start(new InetSocketAddress("127.0.0.1", 9229), true); debugTask = debugServer.start(new InetSocketAddress("127.0.0.1", 9229), true);
} }
private static void initTypescript() throws IOException {
var tsEnv = Internals.apply(new Environment());
var bsEnv = Internals.apply(new Environment());
try {
tsEnv.global.define(null, "module", false, new ObjectValue());
engine.pushMsg(
false, tsEnv,
new Filename("jscript", "ts.js"),
Reading.resourceToString("me/topchetoeu/jscript/utils/assets/js/ts.js"), null
).await();
System.out.println("Loaded typescript!");
var typescript = tsEnv.global.get(new Context(engine, bsEnv), "ts");
var libs = new ArrayValue(null, Reading.resourceToString("me/topchetoeu/jscript/utils/assets/js/lib.d.ts"));
engine.pushMsg(
false, bsEnv,
new Filename("jscript", "bootstrap.js"), Reading.resourceToString("me/topchetoeu/jscript/utils/assets/js/bootstrap.js"), null,
typescript, new EnvironmentLib(environment), libs
).await();
}
catch (EngineException e) {
Values.printError(e, "(while initializing TS)");
}
bsEnv.add(Environment.HIDE_STACK, true);
tsEnv.add(Environment.HIDE_STACK, true);
}
public static void main(String args[]) { public static void main(String args[]) {
System.out.println(String.format("Running %s v%s by %s", Metadata.name(), Metadata.version(), Metadata.author())); System.out.println(String.format("Running %s v%s by %s", Metadata.name(), Metadata.version(), Metadata.author()));