From cff721880b96c450d7c76a23c2e30da92d15e276 Mon Sep 17 00:00:00 2001 From: Erica Marigold Date: Wed, 19 Jun 2024 18:27:12 +0530 Subject: [PATCH] fix(js): use script dir not running cwd --- package/js/.gitignore | 4 +++- package/js/README.md | 10 +++++++--- package/js/bin/lune.ts | 15 ++++++++++----- package/js/build.ts | 4 ++-- package/js/consts.ts | 7 ++++++- package/js/deno.json | 2 +- package/js/deno.lock | 1 + package/js/install.ts | 30 ++++++++++++++++-------------- 8 files changed, 46 insertions(+), 27 deletions(-) diff --git a/package/js/.gitignore b/package/js/.gitignore index f896133..42b5963 100644 --- a/package/js/.gitignore +++ b/package/js/.gitignore @@ -1 +1,3 @@ -npm/ \ No newline at end of file +npm/ +lune +lune.exe \ No newline at end of file diff --git a/package/js/README.md b/package/js/README.md index 53acf8c..51a6166 100644 --- a/package/js/README.md +++ b/package/js/README.md @@ -1,8 +1,10 @@ # Deno/Node Packages -This package exports [lune](https://github.com/lune-org/lune), the standalone luau runtime as a JS package to be used as a dependency in a JS project. +This package exports [lune](https://github.com/lune-org/lune), the standalone +luau runtime as a JS package to be used as a dependency in a JS project. -To install the package, run one of the following in your project, substituting `{VERSION}` with the version of lune you want to install: +To install the package, run one of the following in your project, substituting +`{VERSION}` with the version of lune you want to install: ```sh # Install from NPM for node @@ -12,4 +14,6 @@ npm install lune@{VERSION} deno install https://raw.githubusercontent.com/CompeyDev/lune-packaging/v{VERSION}/package/js/bin/lune.ts ``` -> **Note**: Only lune versions starting from lune v0.8.5 are supported for NPM. Pinned deno versions are currently not supported, so you would need to omit the version portion from the script link. +> **Note**: Only lune versions starting from lune v0.8.5 are supported for NPM. +> Pinned deno versions are currently not supported, so you would need to replace +> the version portion from the script link with main. diff --git a/package/js/bin/lune.ts b/package/js/bin/lune.ts index 8eed709..3560bf8 100644 --- a/package/js/bin/lune.ts +++ b/package/js/bin/lune.ts @@ -1,3 +1,5 @@ +import consts, { BASE_PATH } from "../consts.ts"; +import * as path from "jsr:@std/path"; import { checkAndInstallLune, EXE_EXTENSION } from "../install.ts"; // Install lune, if unavailable @@ -8,7 +10,7 @@ await checkAndInstallLune(); dnt does not support Deno.Command yet, but once they do, this would like so: - new Deno.Command(Deno.cwd() + "/lune" + EXE_EXTENSION, { + new Deno.Command(path.join(BASE_PATH, "lune" + EXE_EXTENSION), { args: Deno.args, stdout: "inherit", stderr: "inherit", @@ -17,10 +19,13 @@ await checkAndInstallLune(); */ // deno-lint-ignore no-deprecated-deno-api const luneStatus = await Deno.run({ - cmd: [Deno.cwd() + "/lune" + EXE_EXTENSION, ...Deno.args], + cmd: [ + path.join(BASE_PATH, consts.version, "lune" + EXE_EXTENSION), + ...Deno.args, + ], stdout: "inherit", stderr: "inherit", - stdin: "inherit" -}).status() + stdin: "inherit", +}).status(); -Deno.exit(luneStatus.signal) \ No newline at end of file +Deno.exit(luneStatus.signal); diff --git a/package/js/build.ts b/package/js/build.ts index 9e7e802..8a65f6d 100644 --- a/package/js/build.ts +++ b/package/js/build.ts @@ -14,7 +14,7 @@ await build({ outDir: "./npm", shims: { deno: true, - // undici: true, + undici: true, }, package: { name: consts.name, @@ -34,5 +34,5 @@ await build({ Deno.copyFileSync("README.md", "npm/README.md"); Deno.copyFileSync("consts.toml", "npm/consts.toml"); }, - typeCheck: false // FIXME: This is a problem on dnt's end while importing things + typeCheck: false, // FIXME: This is a problem on dnt's end while importing things }); diff --git a/package/js/consts.ts b/package/js/consts.ts index 18a694b..edfc9f0 100644 --- a/package/js/consts.ts +++ b/package/js/consts.ts @@ -1,6 +1,11 @@ import { parse as parseToml } from "jsr:@std/toml"; +import * as path from "jsr:@std/path"; -export default parseToml(Deno.readTextFileSync("consts.toml")) as { +export const BASE_PATH = path.dirname(path.fromFileUrl(import.meta.url)); + +export default parseToml( + Deno.readTextFileSync(path.join(BASE_PATH, "consts.toml")), +) as { name: string; api_url: string; version: string; diff --git a/package/js/deno.json b/package/js/deno.json index b3826eb..002e0a2 100644 --- a/package/js/deno.json +++ b/package/js/deno.json @@ -1,6 +1,6 @@ { "tasks": { - "lune": "deno run --allow-all lune.ts", + "lune": "deno run --allow-all bin/lune.ts", "build": "deno run --allow-all build.ts" } } diff --git a/package/js/deno.lock b/package/js/deno.lock index 90f145b..2da3bd8 100644 --- a/package/js/deno.lock +++ b/package/js/deno.lock @@ -13,6 +13,7 @@ "jsr:@std/fs@0.218.2": "jsr:@std/fs@0.218.2", "jsr:@std/fs@^0.218.2": "jsr:@std/fs@0.218.2", "jsr:@std/io@^0.218.2": "jsr:@std/io@0.218.2", + "jsr:@std/path": "jsr:@std/path@0.218.2", "jsr:@std/path@0.218.2": "jsr:@std/path@0.218.2", "jsr:@std/path@^0.218.2": "jsr:@std/path@0.218.2", "jsr:@std/toml": "jsr:@std/toml@0.224.0", diff --git a/package/js/install.ts b/package/js/install.ts index 9c1e98e..6b8590f 100644 --- a/package/js/install.ts +++ b/package/js/install.ts @@ -1,16 +1,16 @@ -// import { decompress as unzip } from "jsr:@fakoua/zip-ts@^1.3.1"; +import * as path from "jsr:@std/path"; import { unzip } from "https://deno.land/x/nzip@v1.2.1/mod.ts"; import { fetchLuneReleases } from "./github.ts"; -import consts from "./consts.ts"; +import consts, { BASE_PATH } from "./consts.ts"; export const LUNE_VERSION = consts.version; export const EXE_EXTENSION = Deno.build.os == "windows" ? ".exe" : ""; async function installLune() { - let ghAuthToken; + let ghAuthToken: string | undefined; try { - /* + /* Ideally, this would look like this: new TextDecoder().decode( @@ -19,14 +19,16 @@ async function installLune() { stdout: "piped", }).output()).stdout, ); - + However, dnt is yet to support Deno.command */ - // deno-lint-ignore no-deprecated-deno-api - ghAuthToken = new TextDecoder().decode(await Deno.run({ - cmd: ["gh", "auth", "token"], - stdout: "piped" - }).output()) + ghAuthToken = new TextDecoder().decode( + // deno-lint-ignore no-deprecated-deno-api + await Deno.run({ + cmd: ["gh", "auth", "token"], + stdout: "piped", + }).output(), + ); } catch (_) { // Don't use an auth token, be subjected to GitHub ratelimit } @@ -52,7 +54,7 @@ async function installLune() { } const zipFile = await Deno.makeTempFile({ suffix: ".zip" }); - const finalDest = Deno.cwd(); + const finalDestDir = path.join(BASE_PATH, consts.version); const binaryBlob = resp.body!; await binaryBlob.pipeTo( @@ -62,15 +64,15 @@ async function installLune() { )).writable, ); - const binaryPaths = await unzip(zipFile, finalDest, { + const binaryPaths = await unzip(zipFile, finalDestDir, { useWebWorkers: true, }); - if (Deno.build.os !== "windows") await Deno.chmod(binaryPaths[0], 0o777) + if (Deno.build.os !== "windows") await Deno.chmod(binaryPaths[0], 0o777); } export async function checkAndInstallLune() { - const luneExePath = Deno.cwd() + "/lune" + EXE_EXTENSION; + const luneExePath = path.join(BASE_PATH, consts.version, "lune" + EXE_EXTENSION); const luneExists = await Deno.lstat(luneExePath).then( (stat) => {