@contentstack/cli-cm-bootstrap
Version:
Bootstrap contentstack apps
71 lines (70 loc) • 2.29 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const zlib = require("zlib");
const tar = require("tar");
const mkdirp = require('mkdirp');
const cli_utilities_1 = require("@contentstack/cli-utilities");
const github_error_1 = require("./github-error");
const messages_1 = require("../../messages");
const DEFAULT_BRANCH = 'cli-use';
class GitHubClient {
static parsePath(gitPath) {
const result = {
user: '',
name: '',
};
if (gitPath) {
const parts = gitPath.split('/');
result.user = parts[0];
if (parts.length === 2) {
result.name = parts[1];
}
}
return result;
}
constructor(repo, privateRepo = false, token) {
this.repo = repo;
this.private = privateRepo;
if (privateRepo) {
this.accessToken = token;
}
this.gitTarBallUrl = `https://api.github.com/repos/${repo.user}/${repo.name}/tarball/${repo.branch || DEFAULT_BRANCH}`;
}
async getLatest(destination) {
const releaseStream = await this.streamRelease(this.gitTarBallUrl);
await mkdirp(destination);
return this.extract(destination, releaseStream);
}
async streamRelease(url) {
const options = {
responseType: 'stream',
};
if (this.private) {
if (this.accessToken) {
options.headers = {
Authorization: `token ${this.accessToken}`,
};
}
else {
throw new github_error_1.default(messages_1.default.parse('CLI_BOOTSTRAP_GITHUB_ACCESS_NOT_FOUND'), 1);
}
}
const response = await cli_utilities_1.HttpClient.create().options(options).get(url);
return response.data;
}
async extract(destination, stream) {
return new Promise((resolve, reject) => {
stream
.pipe(zlib.createUnzip())
.pipe(tar.extract({
cwd: destination,
strip: 1,
}))
.on('end', () => {
resolve('done');
})
.on('error', reject);
});
}
}
exports.default = GitHubClient;