Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
webbdays committed Sep 10, 2024
1 parent b4c7665 commit 48a0829
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 117 deletions.
23 changes: 20 additions & 3 deletions npm/gen-root.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import * as fs from "fs/promises"
import {resolve, dirname} from "path"
import * as yml from "yaml"
import {fileURLToPath} from "url"
import {parse} from "ts-command-line-args"
import {PackageJson as IPackageJSON} from "type-fest"
import YML from "yaml"

const __dirname = dirname(fileURLToPath(import.meta.url))

Expand All @@ -17,6 +17,23 @@ const options = parse<ICLI>({
name: {alias: "n", type: String},
})

async function get_build_matrix() {
const ciYMLPath = resolve(__dirname, "../.github/workflows/build_matrix.yml")
const ciYML = await fs.readFile(ciYMLPath, "utf8").then(YML.parse)
const steps = ciYML.jobs["setup-matrix"].steps

for (const step of steps) {
const matrix = step?.with?.matrix

if (matrix) {
// Parse yaml again since matrix is defined as string inside setup-matrix
return YML.parse(matrix)
}
}

throw new Error("Cannot find matrix definition in workflow file")
}

async function genServerPackage() {
const packageVersion = options.version || "0.1.0"
const name = options.name || "@tailcallhq/tailcall"
Expand Down Expand Up @@ -67,14 +84,14 @@ async function genServerPackage() {
const postInstallScript = await fs.readFile(resolve(__dirname, "./post-install.js"), "utf8")
const preInstallScript = await fs.readFile(resolve(__dirname, "./pre-install.js"), "utf8")
const utilsScript = await fs.readFile(resolve(__dirname, "./utils.js"), "utf8")
const buildMatrix = await fs.readFile(resolve(__dirname, "../.github/build-matrix.yaml"), "utf8")
const stringified_yaml = YML.stringify(await get_build_matrix())

const postInstallScriptContent = `const version = "${packageVersion}";\n${postInstallScript}`

await fs.writeFile(resolve(scriptsPath, "post-install.js"), postInstallScriptContent, "utf8")
await fs.writeFile(resolve(scriptsPath, "pre-install.js"), preInstallScript, "utf8")
await fs.writeFile(resolve(scriptsPath, "utils.js"), utilsScript, "utf8")
await fs.writeFile(resolve(directoryPath, "./build-matrix.yaml"), buildMatrix, "utf8")
await fs.writeFile(resolve(directoryPath, "./build-matrix.yaml"), stringified_yaml, "utf8")

await fs.writeFile(resolve(directoryPath, "./package.json"), JSON.stringify(tailcallPackage, null, 2), "utf8")

Expand Down
94 changes: 1 addition & 93 deletions npm/package-lock.json

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

46 changes: 25 additions & 21 deletions npm/post-install.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,34 +33,38 @@ if (matched_platform != null) {
const specific_url = `v${version}/tailcall-${targetPlatform.get("target")}${targetPlatformExt}`
const full_url = pkg_download_base_url + specific_url

console.log(`Downloading Tailcall for ${targetPlatform.get("target")}${targetPlatformExt} ...`)
console.log(`Downloading Tailcall for ${targetPlatform.get("target")}${targetPlatformExt} ,\nUrl - ${full_url} ...`)

const output_path = `bin/tailcall-${targetPlatform.get("target")}${targetPlatformExt}`
download_binary(full_url, output_path)
await download_binary(full_url, output_path)
}

async function download_binary(full_url, output_path) {
const file = fs.createWriteStream(output_path)
axios({
url: full_url,
method: "GET",
responseType: "stream",
})
.then((response) => {
response.data.pipe(file)
file.on("finish", async () => {
const __dirname = dirname(fileURLToPath(import.meta.url))
try {
const file = fs.createWriteStream(output_path)
console.log("bin path -", output_path)
const response = await axios({
url: full_url,
method: "GET",
responseType: "stream",
})

const directoryPath = resolve(__dirname, "../")
const packageJsonString = fs.readFileSync(resolve(directoryPath, "./package.json"), "utf8")
const packageJson = JSON.parse(packageJsonString)
packageJson.bin = {tailcall: output_path}
fs.writeFileSync(resolve(directoryPath, "./package.json"), JSON.stringify(packageJson, null, 2), "utf8")
response.data.pipe(file)
response.data.on("error", (error) => {
console.error("Error with resp data - ", error)
})

console.log("Tailcall binary downloaded successfully")
})
file.on("close", async () => {
const packageJsonString = await fs.promises.readFile("package.json", "utf8")
const packageJson = JSON.parse(packageJsonString)
packageJson.bin = {tailcall: output_path}
await fs.promises.writeFile("package.json", JSON.stringify(packageJson, null, 2), "utf8")
console.log("Tailcall binary downloaded successfully")
})
.catch((error) => {
console.error("Error downloading", error.message)
file.on("error", (error) => {
console.error("Error while writing to a file - ", error)
})
} catch (error) {
console.error("Error downloading", error.message)
}
}

0 comments on commit 48a0829

Please sign in to comment.