From 3080b67c9316dd7e7d25c95aaf8f10f4c7dc7e7f Mon Sep 17 00:00:00 2001 From: June Rhodes Date: Mon, 6 Apr 2026 13:28:07 +1000 Subject: [PATCH] Build and publish packages to GitHub --- .github/workflows/build.yml | 67 +++++++++++++++++++++++++++++++++++ .gitignore | 2 +- .vscode/settings.json | 3 ++ packages/framework/compile.js | 44 +++++++++++++++++++++++ 4 files changed, 115 insertions(+), 1 deletion(-) create mode 100644 .github/workflows/build.yml create mode 100644 .vscode/settings.json diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml new file mode 100644 index 00000000..b3a9b627 --- /dev/null +++ b/.github/workflows/build.yml @@ -0,0 +1,67 @@ +name: Build and Publish + +on: + push: + branches: ["main"] + pull_request: + branches: ["main"] + +jobs: + build-and-publish: + name: "Build and Publish" + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd + with: + ref: ${{ github.head_ref }} + fetch-depth: 0 + - name: Install Dependencies + run: | + corepack enable + pnpm i --frozen-lockfile + - name: Build + run: | + pnpm compile + for PACKAGE in packages/client-component-transforms packages/server-function-transforms packages/framework; do + pushd $PACKAGE + pnpm lint + popd + done + - name: Push to GitHub + if: github.event_name != 'pull_request' + env: + NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }} + run: | + export PACKAGE_NAMESPACE=$GITHUB_REPOSITORY_OWNER + + # set repository + for PACKAGE_JSON in packages/*/package.json; do jq ".repository |= \"https://github.com/$GITHUB_REPOSITORY\"" $PACKAGE_JSON > $PACKAGE_JSON.tmp; mv $PACKAGE_JSON.tmp $PACKAGE_JSON; done + + # rename package versions + export PACKAGE_VERSION_SUFFIX="dev.$(date +%Y.%m.%d.%H.%M.%S)+$(git rev-parse --short HEAD)" + for PACKAGE_JSON in packages/*/package.json; do jq ".version |= (match(\"^([0-9.]+)\").captures[0].string + \"-$PACKAGE_VERSION_SUFFIX\")" $PACKAGE_JSON > $PACKAGE_JSON.tmp; mv $PACKAGE_JSON.tmp $PACKAGE_JSON; done + + # rename package names + for PACKAGE_JSON in packages/*/package.json; do jq ".name |= sub(\"@twofold\"; \"@$PACKAGE_NAMESPACE\")" $PACKAGE_JSON > $PACKAGE_JSON.tmp; mv $PACKAGE_JSON.tmp $PACKAGE_JSON; done + + # rename package references + for PACKAGE_JSON in packages/*/package.json; do jq ".dependencies |= (to_entries | map({key: (.key | sub(\"@twofold/\"; \"@$PACKAGE_NAMESPACE/\")), value: .value}) | from_entries)" $PACKAGE_JSON > $PACKAGE_JSON.tmp; mv $PACKAGE_JSON.tmp $PACKAGE_JSON; done + + # compile and package again + # PACKAGE_NAMESPACE is read by compile.js and rewrites imports + # in built code once compiled + pnpm compile + pnpm -r exec pnpm pack + + # set npmrc to auth with GitHub + cat >.npmrc < { + const options = build.initialOptions; + const absWorkingDir = options.absWorkingDir ?? process.cwd(); + outDir = options.outdir + ? path.resolve(absWorkingDir, options.outdir) + : options.outfile + ? path.dirname(path.resolve(absWorkingDir, options.outfile)) + : null; + }); + build.onEnd(async (args) => { + if (outDir) { + for (const file of readdirSync(outDir, { + recursive: true, + withFileTypes: true, + })) { + if (file.isFile() && file.name.endsWith(".js")) { + const originalContents = readFileSync( + path.join(file.parentPath, file.name), + "utf-8", + ); + const newContents = originalContents.replaceAll( + /"@twofold\/([a-z-]+)"/g, + `"@${process.env.PACKAGE_NAMESPACE}/$1"`, + ); + if (originalContents != newContents) { + writeFileSync(path.join(file.parentPath, file.name), newContents); + } + } + } + } + }); + }, +}; async function main() { let dir = new URL("./dist/", import.meta.url); @@ -13,6 +53,10 @@ async function main() { outdir: "./dist/backend", packages: "external", platform: "node", + plugins: + process.env.PACKAGE_NAMESPACE !== undefined + ? [rewriteImports] + : undefined, }); }