UNPKG

@wocker/ws

Version:

Docker workspace for web projects

66 lines (65 loc) 2.28 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.GithubClient = void 0; const axios_1 = __importDefault(require("axios")); const core_1 = require("@wocker/core"); class GithubClient { constructor(owner, repository) { this.owner = owner; this.repository = repository; } get axios() { return axios_1.default.create({ headers: { "User-Agent": "Wocker" }, }); } async getInfo() { const response = await this.axios.get(`https://api.github.com/repos/${this.owner}/${this.repository}`, { headers: { "Accept": "application/vnd.github+json" } }); return response.data; } async getFile(branch, path) { const response = await this.axios.get(`https://raw.githubusercontent.com/${this.owner}/${this.repository}/${branch}/${path}`, { headers: { "Accept": "application/vnd.github+json" } }); return response.data; } async download(branch, dirPath) { const res = await this.axios.get(`https://github.com/${this.owner}/${this.repository}/archive/refs/heads/${branch}.zip`, { responseType: "stream" }); const fs = new core_1.FileSystem(dirPath); if (!fs.exists()) { fs.mkdir("", { recursive: true }); } return new Promise((resolve, reject) => { const { Parse } = require("unzipper"), pipe = res.data.pipe(Parse()); pipe.on("entry", (entry) => { const path = entry.path.replace(/^[^\/]+\//, ""); if (entry.type === "File") { entry.pipe(fs.createWriteStream(path)); } else if (entry.type === "Directory") { fs.mkdir(path, { recursive: true }); } }); pipe.on("end", () => resolve()); pipe.on("error", reject); }); } } exports.GithubClient = GithubClient;