Skip to content

Commit

Permalink
feat: fix npm install
Browse files Browse the repository at this point in the history
  • Loading branch information
maxs-rose committed Feb 17, 2022
1 parent 78f6fed commit bd50117
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 17 deletions.
5 changes: 5 additions & 0 deletions npm-install/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ export const CONFIG = {
* @type {string}
*/
url: "https://github.com/railwayapp/cli/releases/download/v{{version}}/{{bin_name}}_{{version}}_{{platform}}_{{arch}}.tar.gz",

/**
* The URL for the latest release on GitHub
*/
releasesUrl: "https://api.github.com/repos/railwayapp/cli/releases/latest"
};

/**
Expand Down
25 changes: 8 additions & 17 deletions npm-install/postinstall.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,22 @@
import { createWriteStream } from "fs";
import * as fs from "fs/promises";
import { mkdir, rm } from "fs/promises";
import fetch from "node-fetch";
import { pipeline } from "stream/promises";
import tar from "tar";

import { ARCH_MAPPING, CONFIG, PLATFORM_MAPPING } from "./config.js";

async function install() {
const packageJson = await fs.readFile("package.json").then(JSON.parse);
let version = packageJson.version;
const latestRelease = await fetch(CONFIG.releasesUrl).then(res => res.json())
let version = latestRelease.tag_name?.replace("v", "");

if (typeof version !== "string") {
throw new Error("Missing version in package.json");
throw new Error("Missing version!");
}

if (version[0] === "v") version = version.slice(1);

// Fetch Static Config
let { name: binName, path: binPath, url } = CONFIG;

// Binary name on Windows has .exe suffix
if (process.platform === "win32") {
binName += ".exe";

url = url.replace(/{{win_ext}}/g, ".exe");
} else {
url = url.replace(/{{win_ext}}/g, "");
}

url = url.replace(/{{arch}}/g, ARCH_MAPPING[process.arch]);
url = url.replace(/{{platform}}/g, PLATFORM_MAPPING[process.platform]);
url = url.replace(/{{version}}/g, version);
Expand All @@ -40,10 +29,12 @@ async function install() {

const tarFile = "downloaded.tar.gz";

await fs.mkdir(binPath, { recursive: true });
await mkdir(binPath, { recursive: true });
await pipeline(response.body, createWriteStream(tarFile));
await tar.x({ file: tarFile, cwd: binPath });
await fs.rm(tarFile);
await rm(tarFile);

console.info(`Railway CLI v${version} installed`)
}

install()
Expand Down

0 comments on commit bd50117

Please sign in to comment.