UNPKG

changelog-guru

Version:
92 lines (91 loc) 4.64 kB
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) { if (kind === "m") throw new TypeError("Private method is not writable"); if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter"); if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it"); return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value; }; var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) { if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter"); if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it"); return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver); }; var _GitHubProvider_authors, _GitHubProvider_provider; import Provider from 'gh-gql'; import { Package } from 'package-json-helper'; import TaskTree from 'tasktree-cli'; import { GitServiceProvider } from '../Config.js'; import Author from '../entities/Author.js'; import Commit from '../entities/Commit.js'; import GitProvider from './GitProvider.js'; class GitHubProvider extends GitProvider { constructor(url, branch) { super({ type: GitServiceProvider.GitHub, url, branch }); _GitHubProvider_authors.set(this, new Map()); _GitHubProvider_provider.set(this, void 0); __classPrivateFieldSet(this, _GitHubProvider_provider, new Provider(), "f"); } async getCommits(since) { const commits = []; const list = await __classPrivateFieldGet(this, _GitHubProvider_provider, "f").query.commit.getList({ owner: this.owner, repository: this.repository, branch: this.branch.dev, since: since.toISOString(), }); commits.push(...list.map(commit => this.parseCommit(commit))); return commits; } async getCurrentPackage(since) { const pkg = await this.getPackage(this.branch.dev, since); return pkg; } async getLastChangeDate(dev) { const lastCommit = await __classPrivateFieldGet(this, _GitHubProvider_provider, "f").query.commit.getLastCommit({ owner: this.owner, repository: this.repository, branch: dev ? this.branch.dev : this.branch.main, }); return new Date(lastCommit?.committedDate ?? Date.now()); } async getPreviousPackage(since) { const pkg = await this.getPackage(this.branch.main, since); return pkg; } async getPackage(branch, since) { const { owner, repository, package: filePath } = this; const task = TaskTree.add(`Loading {bold package.json} from {bold ${branch}}...`); const id = await __classPrivateFieldGet(this, _GitHubProvider_provider, "f").query.file.getId({ until: since.toISOString(), branch, owner, repository, filePath, }); let data = {}; if (id) { const text = await __classPrivateFieldGet(this, _GitHubProvider_provider, "f").query.file.getContent({ filePath, owner, repository, oid: id }); if (text) { data = JSON.parse(text); task.complete(`File {bold package.json} is loaded for {bold ${branch}}`); } } else { task.skip(`Branch {bold ${branch}} did not contain {bold package.json}`); } return new Package(data); } parseAuthor({ avatarUrl, user: { login, url }, }) { const author = __classPrivateFieldGet(this, _GitHubProvider_authors, "f").get(login) ?? new Author({ login, url, avatar: avatarUrl }); if (!__classPrivateFieldGet(this, _GitHubProvider_authors, "f").has(login)) __classPrivateFieldGet(this, _GitHubProvider_authors, "f").set(login, author); return author; } parseCommit(commit) { const { url, oid: hash, messageHeadline: headline, messageBody: body } = commit; const timestamp = new Date(commit.committedDate).getTime(); const author = this.parseAuthor(commit.author); return new Commit({ hash, headline, body, author, timestamp, url }); } } _GitHubProvider_authors = new WeakMap(), _GitHubProvider_provider = new WeakMap(); export default GitHubProvider;