From aa930148cb9521ef0372dc2129698bee7fef5c2a Mon Sep 17 00:00:00 2001 From: Angelo Verlain Date: Tue, 20 Aug 2024 19:09:29 +0200 Subject: [PATCH] feat: compile typescript demos to javascript at build time --- build-aux/library.js | 21 +++++++++++++++++++ src/langs/typescript/meson.build | 7 +++++++ .../typescript/template/tsconfig.demos.json | 14 +++++++++++++ 3 files changed, 42 insertions(+) create mode 100644 src/langs/typescript/template/tsconfig.demos.json diff --git a/build-aux/library.js b/build-aux/library.js index fa7b4fe91..c5fe3c967 100755 --- a/build-aux/library.js +++ b/build-aux/library.js @@ -24,6 +24,8 @@ Gio._promisify( Gio._promisify(Gio.File.prototype, "copy_async", "copy_finish"); +Gio._promisify(Gio.Subprocess.prototype, "wait_async", "wait_finish"); + const loop = new GLib.MainLoop(null, false); const [pkgdatadir] = programArgs; @@ -95,6 +97,9 @@ const demos = []; demos.push(demo); } + // compile TypeScript demos to JavaScript + await compileTypeScriptFiles(); + await Gio.File.new_for_path(pkgdatadir) .get_child("demos/index.json") .replace_contents_async( @@ -114,6 +119,22 @@ const demos = []; loop.quit(); }); +async function compileTypeScriptFiles() { + const tsc_launcher = new Gio.SubprocessLauncher(); + tsc_launcher.set_cwd( + Gio.File.new_for_path(pkgdatadir).get_child("demos").get_path() + ); + + const tsc = tsc_launcher.spawnv(["tsc"]); + await tsc.wait_async(null); + + // TODO: maybe throw when the result is false + const successful = tsc.get_successful(); + tsc_launcher.close(); + + if (!successful) throw new Error("One or several errors happened while compiling TypeScript demos to JavaScript") +} + async function copyDemo(source, destination) { try { destination.make_directory_with_parents(null); diff --git a/src/langs/typescript/meson.build b/src/langs/typescript/meson.build index 015df1fa4..be7f4e3fd 100644 --- a/src/langs/typescript/meson.build +++ b/src/langs/typescript/meson.build @@ -5,6 +5,13 @@ configure_file( configuration: bin_conf, ) +configure_file( + input: 'template/tsconfig.demos.json', + output: 'tsconfig.json', + install_dir: join_paths(pkgdatadir, 'demos'), + configuration: bin_conf, +) + configure_file( input: 'types/workbench.d.ts', output: 'workbench.d.ts', diff --git a/src/langs/typescript/template/tsconfig.demos.json b/src/langs/typescript/template/tsconfig.demos.json new file mode 100644 index 000000000..733fb9c0c --- /dev/null +++ b/src/langs/typescript/template/tsconfig.demos.json @@ -0,0 +1,14 @@ +{ + "compilerOptions": { + "module": "ES2022", + "moduleResolution": "Bundler", + "target": "ES2022", + "skipLibCheck": true, + "lib": ["ES2022"], + }, + "include": [ + "./**/*.ts", + "@pkgdatadir@/langs/typescript/gi-types/index.d.ts", + "@pkgdatadir@/langs/typescript/types/workbench.d.ts", + ] +}