From 4b1428ffd78b4b2a572e9230295834ecc565d839 Mon Sep 17 00:00:00 2001 From: redphx <96280+redphx@users.noreply.github.com> Date: Sat, 27 Apr 2024 10:05:47 +0700 Subject: [PATCH] Update build script --- build.ts | 61 +++++++++++++++++++++------ src/header_meta.txt | 5 +++ src/{header.txt => header_script.txt} | 4 +- 3 files changed, 55 insertions(+), 15 deletions(-) create mode 100644 src/header_meta.txt rename src/{header.txt => header_script.txt} (88%) diff --git a/build.ts b/build.ts index c168d9b..43de2b1 100644 --- a/build.ts +++ b/build.ts @@ -1,5 +1,9 @@ #!/usr/bin/env bun import { readFile } from "node:fs/promises"; +import { parseArgs } from "node:util"; +import { sys } from "typescript"; +import txtScriptHeader from "./src/header_script.txt" with { type: "text" }; +import txtMetaHeader from "./src/header_meta.txt" with { type: "text" }; enum BuildTarget { ALL = 'all', @@ -23,20 +27,24 @@ const postProcess = (str: string): string => { return str; } -const build = async (target: BuildTarget, config: any={}) => { - console.log('--- Building:', target); +const build = async (target: BuildTarget, version, config: any={}) => { + console.log('-- Target:', target); const startTime = performance.now(); - let outputFileName = 'better-xcloud'; + let outputScriptName = 'better-xcloud'; if (target !== BuildTarget.ALL) { - outputFileName += `.${target}`; + outputScriptName += `.${target}`; } - outputFileName += '.user.js'; + let outputMetaName = outputScriptName; + outputScriptName += '.user.js'; + outputMetaName += '.meta.js'; + + const outDir = './dist'; let output = await Bun.build({ entrypoints: ['src/index.ts'], - outdir: './dist', - naming: outputFileName, + outdir: outDir, + naming: outputScriptName, define: { 'Bun.env.BUILD_TARGET': JSON.stringify(target), }, @@ -48,20 +56,47 @@ const build = async (target: BuildTarget, config: any={}) => { } const {path} = output.outputs[0]; + // Get generated file let result = postProcess(await readFile(path, 'utf-8')); - const header = await readFile('src/header.txt', 'utf-8'); - await Bun.write(path, header + result); - console.log(`[${target}] done in ${performance.now() - startTime} ms`); + + // Replace [[VERSION]] with real value + const scriptHeader = txtScriptHeader.replace('[[VERSION]]', version); + + // Save to script + await Bun.write(path, scriptHeader + result); + console.log(`---- [${target}] done in ${performance.now() - startTime} ms`); + + // Create meta file + await Bun.write(outDir + '/' + outputMetaName, txtMetaHeader.replace('[[VERSION]]', version)); } const buildTargets = [ BuildTarget.ALL, - BuildTarget.ANDROID_APP, - BuildTarget.MOBILE, + // BuildTarget.ANDROID_APP, + // BuildTarget.MOBILE, // BuildTarget.WEBOS, ]; +const { values, positionals } = parseArgs({ + args: Bun.argv, + options: { + version: { + type: 'string', + + }, + }, + strict: true, + allowPositionals: true, + }); + +if (!values['version']) { + console.log('Missing --version param'); + sys.exit(-1); +} + +console.log('Building: ', values['version']); + const config = {}; for (const target of buildTargets) { - await build(target, config); + await build(target, values['version'], config); } diff --git a/src/header_meta.txt b/src/header_meta.txt new file mode 100644 index 0000000..45ac370 --- /dev/null +++ b/src/header_meta.txt @@ -0,0 +1,5 @@ +// ==UserScript== +// @name Better xCloud +// @namespace https://github.com/redphx +// @version [[VERSION]] +// ==/UserScript== diff --git a/src/header.txt b/src/header_script.txt similarity index 88% rename from src/header.txt rename to src/header_script.txt index 40e5a35..b8e4c93 100644 --- a/src/header.txt +++ b/src/header_script.txt @@ -1,7 +1,7 @@ // ==UserScript== // @name Better xCloud (Beta) // @namespace https://github.com/redphx -// @version 3.5.3 +// @version [[VERSION]] // @description Improve Xbox Cloud Gaming (xCloud) experience // @author redphx // @license MIT @@ -9,7 +9,7 @@ // @match https://www.xbox.com/*/auth/msa?*loggedIn* // @run-at document-start // @grant none -// @updateURL https://raw.githubusercontent.com/redphx/better-xcloud/main/better-xcloud.meta.js +// @updateURL https://raw.githubusercontent.com/redphx/better-xcloud/typescript/dist/better-xcloud.meta.js // @downloadURL https://github.com/redphx/better-xcloud/releases/latest/download/better-xcloud.user.js // ==/UserScript== 'use strict';