diff --git a/.github/workflows/nightly.yml b/.github/workflows/nightly.yml index db8bba3..304b822 100644 --- a/.github/workflows/nightly.yml +++ b/.github/workflows/nightly.yml @@ -207,3 +207,86 @@ jobs: run: | sleep 3 gh release edit "${{ needs.check.outputs.tag }}" --draft=false + + npm: + needs: [check, build] + runs-on: ubuntu-latest + permissions: + contents: read + id-token: write + steps: + - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2 + with: + fetch-depth: 1 + + - uses: actions/setup-node@53b83947a5a98c8d113130e565377fae1a50d02f # v6.3.0 + with: + node-version: "22" + registry-url: "https://registry.npmjs.org" + + - name: Download all artifacts + uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1 + with: + path: artifacts + merge-multiple: true + + - name: Generate and publish platform packages + env: + NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} + run: | + VERSION="${{ needs.check.outputs.version }}" + + declare -A PLATFORM_MAP=( + ["linux-amd64"]="linux-x64 linux x64" + ["linux-arm64"]="linux-arm64 linux arm64" + ["darwin-arm64"]="darwin-arm64 darwin arm64" + ["darwin-amd64"]="darwin-x64 darwin x64" + ["windows-amd64"]="win32-x64 win32 x64" + ) + + for key in "${!PLATFORM_MAP[@]}"; do + read -r suffix os cpu <<< "${PLATFORM_MAP[$key]}" + PKG_DIR="npm-staging/@tslua/cli-${suffix}" + mkdir -p "${PKG_DIR}/bin" + + # Copy binary + SRC="artifacts/tslua-${key}" + if [ "$os" = "win32" ]; then + SRC="${SRC}.exe" + cp "$SRC" "${PKG_DIR}/bin/tslua.exe" + else + cp "$SRC" "${PKG_DIR}/bin/tslua" + chmod +x "${PKG_DIR}/bin/tslua" + fi + + # Generate package.json + node -e " + const pkg = { name: '@tslua/cli-${suffix}', version: '${VERSION}', + description: 'tslua platform binary (${os}-${cpu})', license: 'MIT', + os: ['${os}'], cpu: ['${cpu}'], files: ['bin/'] }; + require('fs').writeFileSync('${PKG_DIR}/package.json', JSON.stringify(pkg, null, 2) + '\n'); + " + + echo "Publishing @tslua/cli-${suffix}@${VERSION}" + npm publish "${PKG_DIR}" --tag dev --access public --provenance + done + + - name: Publish main package + env: + NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} + run: | + VERSION="${{ needs.check.outputs.version }}" + PKG_DIR="npm/tslua" + + # Inject version into main package and its optionalDependencies + node -e " + const pkg = require('./${PKG_DIR}/package.json'); + pkg.version = '${VERSION}'; + for (const dep of Object.keys(pkg.optionalDependencies || {})) { + pkg.optionalDependencies[dep] = '${VERSION}'; + } + require('fs').writeFileSync('./${PKG_DIR}/package.json', JSON.stringify(pkg, null, 2) + '\n'); + " + + echo "Publishing @tslua/cli@${VERSION}" + npm publish "${PKG_DIR}" --tag dev --access public diff --git a/npm/package.json b/npm/package.json deleted file mode 100644 index 0cd7c24..0000000 --- a/npm/package.json +++ /dev/null @@ -1,7 +0,0 @@ -{ - "name": "@tslua/cli", - "version": "0.0.1", - "description": "TypeScript-to-Lua transpiler", - "author": "Cold Fry", - "license": "MIT" -} diff --git a/npm/tslua/bin/tslua.js b/npm/tslua/bin/tslua.js new file mode 100644 index 0000000..de47ad4 --- /dev/null +++ b/npm/tslua/bin/tslua.js @@ -0,0 +1,36 @@ +#!/usr/bin/env node + +const process = require("node:process"); +const child_process = require("node:child_process"); + +const PLATFORMS = { + darwin: { x64: "@tslua/cli-darwin-x64", arm64: "@tslua/cli-darwin-arm64" }, + linux: { x64: "@tslua/cli-linux-x64", arm64: "@tslua/cli-linux-arm64" }, + win32: { x64: "@tslua/cli-win32-x64" }, +}; + +const platformPkgs = PLATFORMS[process.platform]; +if (!platformPkgs) { + console.error(`Unsupported platform: ${process.platform}`); + process.exit(1); +} +const pkg = platformPkgs[process.arch]; +if (!pkg) { + console.error(`Unsupported architecture: ${process.platform}-${process.arch}`); + process.exit(1); +} + +const bin = process.platform === "win32" ? "tslua.exe" : "tslua"; +const exePath = require.resolve(`${pkg}/bin/${bin}`); + +try { + child_process.execFileSync(exePath, process.argv.slice(2), { + stdio: "inherit", + }); +} catch (e) { + if (e.status != null) { + process.exitCode = e.status; + } else { + throw e; + } +} diff --git a/npm/tslua/package.json b/npm/tslua/package.json new file mode 100644 index 0000000..8247aab --- /dev/null +++ b/npm/tslua/package.json @@ -0,0 +1,24 @@ +{ + "name": "@tslua/cli", + "version": "0.0.0", + "description": "TypeScript-to-Lua transpiler", + "author": "Cold Fry", + "license": "MIT", + "repository": { + "type": "git", + "url": "https://github.com/RealColdFry/tslua" + }, + "bin": { + "tslua": "bin/tslua.js" + }, + "files": [ + "bin/tslua.js" + ], + "optionalDependencies": { + "@tslua/cli-darwin-arm64": "0.0.0", + "@tslua/cli-darwin-x64": "0.0.0", + "@tslua/cli-linux-arm64": "0.0.0", + "@tslua/cli-linux-x64": "0.0.0", + "@tslua/cli-win32-x64": "0.0.0" + } +}