@nodesecure/github
Version:
Download repository from github
42 lines • 1.54 kB
JavaScript
// Import Node.js Dependencies
import * as path from "node:path";
import { createWriteStream } from "node:fs";
// Import Third-party Dependencies
import * as httpie from "@openally/httpie";
// CONSTANTS
const kGithubURL = new URL("https://github.com/");
const kDefaultBranch = "main";
const agent = new httpie.Agent().compose(httpie.interceptors.redirect({ maxRedirections: 1 }));
/**
* @example
* const { location } = await github.download("NodeSecure.utils", {
* dest: __dirname
* });
* console.log(location);
*/
export async function download(repository, options = Object.create(null)) {
if (typeof repository !== "string") {
throw new TypeError("repository must be a string!");
}
const { branch = kDefaultBranch, dest = process.cwd(), token = process.env.GITHUB_TOKEN } = options;
// Create URL!
const [organization, repo] = repository.split(".");
const repositoryURL = new URL(`${organization}/${repo}/archive/${branch}.tar.gz`, kGithubURL);
const location = path.join(dest, `${repo}-${branch}.tar.gz`);
const writableCallback = httpie.stream("GET", repositoryURL, {
headers: {
"User-Agent": "NodeSecure",
"Accept-Encoding": "gzip, deflate",
Authorization: typeof token === "string" ? `token ${token}` : void 0
},
agent
});
await writableCallback(() => createWriteStream(location));
return {
location,
organization,
repository: repo,
branch
};
}
//# sourceMappingURL=download.js.map