Skip to content

Commit

Permalink
fix: npm server publish (#569)
Browse files Browse the repository at this point in the history
  • Loading branch information
amitksingh1490 authored Nov 4, 2023
1 parent 07b37f1 commit 39ca13d
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 12 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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 }}
Expand All @@ -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 }}
Expand Down
40 changes: 35 additions & 5 deletions npm/generate-root.js
Original file line number Diff line number Diff line change
@@ -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 = {};
Expand Down Expand Up @@ -30,24 +33,51 @@ 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;
}, {});

const tailcallPackage = {
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),
Expand Down
3 changes: 0 additions & 3 deletions npm/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 1 addition & 4 deletions npm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,5 @@
"dependencies": {
"yaml": "^2.3.3",
"yml": "^1.0.0"
},
"workspaces": [
"@tailcallhq/*"
]
}
}

0 comments on commit 39ca13d

Please sign in to comment.