diff --git a/npm/gen-root.ts b/npm/gen-root.ts index 18a5a82e776..1e79274d67a 100644 --- a/npm/gen-root.ts +++ b/npm/gen-root.ts @@ -63,11 +63,14 @@ async function genServerPackage(buildDefinitions: string[]) { await fs.mkdir(scriptsPath, { recursive: true }) await fs.mkdir(directoryPath, { recursive: true }) + const postInstallScript = await fs.readFile(resolve(__dirname, "./post-install.js"), "utf8") const preInstallScript = await fs.readFile(resolve(__dirname, "./pre-install.js"), "utf8") + const postInstallScriptContent = `const version = "${packageVersion}";\n${postInstallScript}` const preInstallScriptContent = `const optionalDependencies = ${JSON.stringify(optionalDependencies)};\n${preInstallScript}` + await fs.writeFile(resolve(scriptsPath, "post-install.js"), postInstallScriptContent, "utf8") await fs.writeFile(resolve(scriptsPath, "pre-install.js"), preInstallScriptContent, "utf8") await fs.writeFile(resolve(directoryPath, "./package.json"), JSON.stringify(tailcallPackage, null, 2), "utf8") diff --git a/npm/post-install.js b/npm/post-install.js new file mode 100644 index 00000000000..40f6471ed72 --- /dev/null +++ b/npm/post-install.js @@ -0,0 +1,27 @@ +// @ts-check +// @ts-ignore +import { familySync, GLIBC, MUSL } from "detect-libc" +import { exec } from 'child_process' +import util from 'util' + +const execa = util.promisify(exec) +const platform = process.platform +const arch = process.arch + +const libcFamily = familySync() +let libc +if (platform === "win32") { + libc = "-msvc" +} else { + libc = libcFamily === GLIBC ? "-gnu" : libcFamily === MUSL ? "-musl" : "" +} + +const pkg = `@tailcallhq/core-${platform}-${arch}${libc}` + +try { + // @ts-ignore + const { stdout, stderr } = await execa(`npm install ${pkg}@${version} --no-save`) + stderr ? console.log(stderr) : console.log(`Successfully installed optional dependency: ${pkg}`, stdout) +} catch (error) { + console.error(`Failed to install optional dependency: ${pkg}`, error.stderr) +} \ No newline at end of file diff --git a/npm/pre-install.js b/npm/pre-install.js index f3d242291f3..6d008b256b6 100644 --- a/npm/pre-install.js +++ b/npm/pre-install.js @@ -1,8 +1,7 @@ const os = process.platform const arch = process.arch -const libc = process.libc -const dependency = libc ? Object.keys(optionalDependencies).find((name) => name.includes(`${os}-${arch}-${libc}`)) : Object.keys(optionalDependencies).find((name) => name.includes(`${os}-${arch}`)) +const dependency = Object.keys(optionalDependencies).find((name) => name.includes(`${os}-${arch}`)) if (!dependency) { const redColor = "\x1b[31m" const resetColor = "\x1b[0m"