From 39ca13d71a367e8a530e1ec942cd499d07258774 Mon Sep 17 00:00:00 2001 From: Amit Singh Date: Sat, 4 Nov 2023 12:31:31 +0530 Subject: [PATCH] fix: npm server publish (#569) --- .github/workflows/ci.yml | 5 +++++ npm/generate-root.js | 40 +++++++++++++++++++++++++++++++++++----- npm/package-lock.json | 3 --- npm/package.json | 5 +---- 4 files changed, 41 insertions(+), 12 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 143973c18b..2d7fac9854 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -103,6 +103,7 @@ jobs: - id: create_release uses: release-drafter/release-drafter@v5 + if: github.event_name == 'push' && github.ref == 'refs/heads/main' env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} with: @@ -233,11 +234,13 @@ jobs: access: public - name: Rename Binary with Target Name + if: github.event_name == 'push' && github.ref == 'refs/heads/main' run: | pwd cp target/${{ matrix.target }}/release/tailcall target/${{ matrix.target }}/release/tailcall-${{ matrix.target }} - name: Upload ${{ matrix.target }} Binary + if: github.event_name == 'push' && github.ref == 'refs/heads/main' uses: xresloader/upload-to-github-release@v1 with: release_id: ${{ needs.draft_release.outputs.create_release_id }} @@ -261,6 +264,8 @@ jobs: cd npm npm install - name: Run generate-root.js script + env: + APP_VERSION: ${{ (github.event_name == 'release' && github.event.action == 'published') && github.event.release.tag_name}} run: | cd npm npm run gen-root -- --version ${{ env.APP_VERSION }} diff --git a/npm/generate-root.js b/npm/generate-root.js index f199071954..e0d20bb659 100644 --- a/npm/generate-root.js +++ b/npm/generate-root.js @@ -1,8 +1,11 @@ import * as fs from "fs/promises"; import { resolve, dirname } from "path"; import * as yml from "yaml"; +import { fileURLToPath } from 'url'; +import { exec } from 'child_process'; -const __dirname = dirname(new URL(import.meta.url).pathname); +// This snippet makes sure __dirname is defined in an ES module context +const __dirname = dirname(fileURLToPath(import.meta.url)); function getArguments() { const args = {}; @@ -30,7 +33,7 @@ async function genServerPackage(buildDefinitions) { // Construct the optionalDependencies object with the provided version const optionalDependencies = buildDefinitions.reduce((deps, buildDef) => { - deps["@tailcallhq/core-" + buildDef] = "*"; + deps["@tailcallhq/core-" + buildDef] = packageVersion; return deps; }, {}); @@ -38,16 +41,43 @@ async function genServerPackage(buildDefinitions) { name: "@tailcallhq/server", version: packageVersion, description: "Tailcall Server", - optionalDependencies, // Now it's an object with versions set from CLI + optionalDependencies, + scripts: { + postinstall: "node ./scripts/installOptionalDeps.js" + } }; // Define the directory path where the package.json should be created const directoryPath = resolve(__dirname, "@tailcallhq/server"); + const scriptsPath = resolve(directoryPath, "./scripts"); - // Ensure the directory exists + await fs.mkdir(scriptsPath, { recursive: true }); + + const installScriptContent = ` +const exec = require('child_process').exec; +const optionalDependencies = ${JSON.stringify(optionalDependencies)}; + +Object.entries(optionalDependencies).forEach(([pkg, version]) => { + exec(\`npm install \${pkg}@\${version} --no-save\`, (error, stdout, stderr) => { + if (error) { + console.error(\`Failed to install optional dependency: \${pkg}\`, stderr); + } else { + console.log(\`Successfully installed optional dependency: \${pkg}\`, stdout); + } + }); +}); + `.trim(); + +// Ensure the directory exists await fs.mkdir(directoryPath, { recursive: true }); - // Write the package.json file with pretty JSON formatting + await fs.writeFile( + resolve(scriptsPath, "installOptionalDeps.js"), + installScriptContent, + "utf8" + ); + +// Write the package.json file with pretty JSON formatting await fs.writeFile( resolve(directoryPath, "./package.json"), JSON.stringify(tailcallPackage, null, 2), diff --git a/npm/package-lock.json b/npm/package-lock.json index c38989da07..e007ba886f 100644 --- a/npm/package-lock.json +++ b/npm/package-lock.json @@ -5,9 +5,6 @@ "packages": { "": { "name": "tailcall-npm-generator", - "workspaces": [ - "@tailcallhq/*" - ], "dependencies": { "yaml": "^2.3.3", "yml": "^1.0.0" diff --git a/npm/package.json b/npm/package.json index 79728a1ee8..960a177cfb 100644 --- a/npm/package.json +++ b/npm/package.json @@ -9,8 +9,5 @@ "dependencies": { "yaml": "^2.3.3", "yml": "^1.0.0" - }, - "workspaces": [ - "@tailcallhq/*" - ] + } }