From d1937fdb632a064d7f9f3c7d1e14dcdaaac7a4a4 Mon Sep 17 00:00:00 2001 From: TopchetoEU <36534413+TopchetoEU@users.noreply.github.com> Date: Wed, 10 Jan 2024 16:51:40 +0200 Subject: [PATCH] remove typescript --- .gitignore | 2 - build.js | 127 +++--------------- .../topchetoeu/jscript/utils/JScriptRepl.java | 51 ++----- 3 files changed, 29 insertions(+), 151 deletions(-) diff --git a/.gitignore b/.gitignore index fa97304..1e780dc 100644 --- a/.gitignore +++ b/.gitignore @@ -2,7 +2,6 @@ !/src !/src/**/* -/src/assets/js/ts.js !/doc !/doc/**/* @@ -10,7 +9,6 @@ !/tests !/tests/**/* - !/.github !/.github/**/* diff --git a/build.js b/build.js index b42b6f4..55f485f 100644 --- a/build.js +++ b/build.js @@ -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) { try { await fs.writeFile('Metadata.java', (await fs.readFile('src/me/topchetoeu/jscript/common/Metadata.java')).toString() @@ -150,40 +72,31 @@ async function jar(conf, project, mainClass) { (async () => { try { - if (argv[2] === 'init-ts') { - await downloadTypescript('src/me/topchetoeu/jscript/utils/assets/js/ts.js'); - } - else { - const conf = { - name: "java-jscript", - author: "TopchetoEU", - javahome: "", - version: argv[3] - }; + const conf = { + name: "java-jscript", + author: "TopchetoEU", + javahome: "", + version: argv[3] + }; - if (conf.version.startsWith('refs/tags/')) conf.version = conf.version.substring(10); - if (conf.version.startsWith('v')) conf.version = conf.version.substring(1); + if (conf.version.startsWith('refs/tags/')) conf.version = conf.version.substring(10); + if (conf.version.startsWith('v')) conf.version = conf.version.substring(1); - try { await fs.rm('dst', { recursive: true }); } catch {} + try { await fs.rm('dst', { recursive: true }); } catch {} - await Promise.all([ - (async () => { - await copy('src', 'dst/classes', v => !v.endsWith('.java')); - await downloadTypescript('dst/classes/me/topchetoeu/jscript/utils/assets/js/ts.js'); - })(), - compileJava(conf), - ]); + await Promise.all([ + copy('src', 'dst/classes', v => !v.endsWith('.java')), + compileJava(conf), + ]); - await Promise.all([ - jar(conf, 'me.topchetoeu.jscript.common'), - jar(conf, 'me.topchetoeu.jscript.core'), - jar(conf, 'me.topchetoeu.jscript.lib'), - jar(conf, 'me.topchetoeu.jscript.utils'), - jar(conf, 'me.topchetoeu.jscript', 'me.topchetoeu.jscript.utils.JScriptRepl'), - ]); + await Promise.all([ + jar(conf, 'me.topchetoeu.jscript.common'), + jar(conf, 'me.topchetoeu.jscript.core'), + jar(conf, 'me.topchetoeu.jscript.utils'), + jar(conf, 'me.topchetoeu.jscript', 'me.topchetoeu.jscript.utils.JScriptRepl'), + ]); - console.log('Done!'); - } + console.log('Done!'); } catch (e) { if (argv[2] === 'debug') throw e; diff --git a/src/me/topchetoeu/jscript/utils/JScriptRepl.java b/src/me/topchetoeu/jscript/utils/JScriptRepl.java index 5e22fbb..9fafb57 100644 --- a/src/me/topchetoeu/jscript/utils/JScriptRepl.java +++ b/src/me/topchetoeu/jscript/utils/JScriptRepl.java @@ -46,18 +46,15 @@ public class JScriptRepl { try { for (var arg : args) { try { - if (arg.equals("--ts")) initTypescript(); - else { - var file = Path.of(arg); - var raw = Files.readString(file); - var res = engine.pushMsg( - false, environment, - Filename.fromFile(file.toFile()), - raw, null - ).await(); - Values.printValue(null, res); - System.out.println(); - } + var file = Path.of(arg); + var raw = Files.readString(file); + var res = engine.pushMsg( + false, environment, + Filename.fromFile(file.toFile()), + raw, null + ).await(); + Values.printValue(null, res); + System.out.println(); } catch (EngineException e) { Values.printError(e, null); } } @@ -128,36 +125,6 @@ public class JScriptRepl { engineTask = engine.start(); 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[]) { System.out.println(String.format("Running %s v%s by %s", Metadata.name(), Metadata.version(), Metadata.author()));