@aaronhayes/hasura-cli
Version:
A package that automatically installs and wraps Hasura CLI binary in isolated manner
64 lines (63 loc) • 2.74 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getUrl = exports.download = void 0;
const axios_1 = __importDefault(require("axios"));
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
function download({ url, destDir, fileName, }) {
return __awaiter(this, void 0, void 0, function* () {
if (!fs_1.default.existsSync(destDir)) {
fs_1.default.mkdirSync(destDir);
}
const dest = path_1.default.resolve(destDir, fileName);
const writer = fs_1.default.createWriteStream(dest);
const res = yield axios_1.default.get(url, {
responseType: 'stream',
headers: {
Accept: 'application/octet-stream',
'User-Agent': 'hasura-cli',
},
});
res.data.pipe(writer);
return new Promise((resolve, reject) => {
writer.on('finish', () => {
fs_1.default.chmodSync(dest, '777');
resolve(dest);
});
writer.on('error', reject);
});
});
}
exports.download = download;
function getUrl(tag, platform = process.platform, arch = process.arch) {
return __awaiter(this, void 0, void 0, function* () {
const prefix = tag.slice(0, -1).endsWith('alpha0') && parseInt(tag.slice(-1), 10) < 5
? ''
: 'cli-';
if (platform === 'win32') {
platform = 'windows';
}
if (arch === 'arm64' && platform === 'windows') {
arch = 'amd64';
}
else if (arch !== 'arm64') {
arch = 'amd64';
}
const ext = platform === 'windows' ? '.exe' : '';
const asset = `${prefix}hasura-${platform}-${arch}${ext}`;
return `https://github.com/hasura/graphql-engine/releases/download/${tag}/${asset}`;
});
}
exports.getUrl = getUrl;