UNPKG

@wocker/ws

Version:

Docker workspace for web projects

80 lines (79 loc) 2.76 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 getBranches() { const response = await this.axios .get(`https://api.github.com/repos/${this.owner}/${this.repository}/branches`); return response.data; } async getTags() { const response = await this.axios .get(`https://api.github.com/repos/${this.owner}/${this.repository}/tags`); return response.data; } async getFile(ref, path) { const res = await this.axios .get(`https://raw.githubusercontent.com/${this.owner}/${this.repository}/${ref}/${path}`, { headers: { "Accept": "application/vnd.github+json" } }); return res.data; } async downloadZipByUrl(url, dirPath) { const res = await this.axios.get(url, { 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); }); } async download(branch, dirPath) { return this.downloadZipByUrl(`https://api.github.com/repos/${this.owner}/${this.repository}/zipball/${branch}`, dirPath); } } exports.GithubClient = GithubClient;