UNPKG

@lcap/cli

Version:

utils for lcap

205 lines 7.42 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Gitlab = void 0; const axios_1 = __importDefault(require("axios")); const constant_1 = require("./constant"); const encoding = 'base64'; class Gitlab { constructor(id, token) { this.id = id; this.token = token; } /** 列出当前路径下的所有文件 */ async listFiles(basePath = '/', ref = 'master') { const files = []; const perPage = 40; const data = { id: this.id, ref, path: basePath, pagination: 'keyset', per_page: perPage, recursive: true, }; for (let stop = false; !stop;) { const { data: result, headers, ...rest } = await (0, axios_1.default)({ ...constant_1.Repositories.Tree(this.id), params: data, headers: { 'PRIVATE-TOKEN': this.token, }, }); files.push(...result .filter((item) => item.type === 'blob') .map((item) => item.path)); const nextPage = Number(headers['x-next-page']); if (Number.isNaN(nextPage) || nextPage === 0) { stop = true; } data.page = nextPage; } return files; } /** 获取文件 meta 信息 */ async getFileMetaData(file, ref = 'master') { const { headers } = await (0, axios_1.default)({ ...constant_1.Repositories.FileMeta(this.id)(file), params: { ref, }, headers: { 'PRIVATE-TOKEN': this.token, }, }); const data = { blob_id: headers['x-gitlab-blob-id'], content_sha256: headers['x-gitlab-content-sha256'], encoding: headers['x-gitlab-encoding'], ref: headers['x-gitlab-ref'], size: Number(headers['x-gitlab-size']), commit_id: headers['x-gitlab-commit-id'], last_commit_id: headers['x-gitlab-last-commit-id'], file_name: headers['x-gitlab-file-name'], file_path: headers['x-gitlab-file-path'], }; return data; } /** 获取文件信息 */ async getFileData(file, ref = 'master') { const { data } = await (0, axios_1.default)({ ...constant_1.Repositories.File(this.id)(file), params: { ref, }, headers: { 'PRIVATE-TOKEN': this.token, }, }); return data; } /** 对比远端和本地文件 */ async getFilesDiff(remoteFiles, filesInDisk) { // TODO: 暂不考虑移动的情况 // /** 文件名称相同但文件路径不同 */ // const filesWithSameName = remoteFiles // .map((remoteFile) => { // const baseName = path.basename(remoteFile); // const diskFileWithSameBaseName = filesInDisk.find((file) => { // return path.basename(file.diskPath) === baseName && file.remotePath !== remoteFile; // }); // if (diskFileWithSameBaseName) { // return { // remoteFile, // diskFile: diskFileWithSameBaseName, // baseName: baseName, // }; // } // }) // .filter(<T>(input: T): input is NonNullable<T> => Boolean(input)); // // 文件名称相同,判断是否只是移动 // for (const file of filesWithSameName) { // // .. // } /** 只在远端的文件 */ const filesOnlyInRemote = remoteFiles.filter((file) => !filesInDisk.find((remote) => remote.remotePath === file)); /** 只在硬盘里的文件 */ const filesOnlyInDisk = filesInDisk.filter(({ remotePath: remote }) => !remoteFiles.includes(remote)); /** 同时存在两端的文件 */ const filesBothIn = filesInDisk.filter(({ remotePath: remote }) => remoteFiles.includes(remote)); /** 更新操作数据 */ const actions = []; // 只存在于远端的文件要删除 await Promise.all(filesOnlyInRemote.map(async (file) => { const data = await this.getFileMetaData(file); actions.push({ action: 'delete', last_commit_id: data.last_commit_id, file_path: file, }); })); // 只存在于硬盘的文件要创建 for (const file of filesOnlyInDisk) { actions.push({ action: 'create', file_path: file.remotePath, content: file.content.toString('base64'), encoding, }); } // 同时存在于这两者的需要更新 await Promise.all(filesBothIn.map(async (file) => { const meta = await this.getFileMetaData(file.remotePath); // 长度不同需要更新 if (meta.size !== file.size) { actions.push({ action: 'update', encoding, content: file.content.toString('base64'), file_path: file.remotePath, last_commit_id: meta.last_commit_id, }); return; } const remoteData = await this.getFileData(file.remotePath); // 内容不同需要更新 // TODO: encoding 不一样怎么办 if (remoteData.content !== file.content.toString('base64')) { actions.push({ action: 'update', encoding, content: file.content.toString('base64'), file_path: file.remotePath, last_commit_id: meta.last_commit_id, }); } })); return actions; } /** 从文件创建新分支 */ async createCommitWithFiles(params) { const { data } = await (0, axios_1.default)({ ...constant_1.Commit.CreateWithAction(this.id), data: { id: encodeURIComponent(this.id), ...params, }, headers: { 'PRIVATE-TOKEN': this.token, }, }); return data; } /** 创建新分支 */ async createBranch(params) { const { data } = await (0, axios_1.default)({ ...constant_1.Branch.Create(this.id), data: { id: encodeURIComponent(this.id), ...params, }, headers: { 'PRIVATE-TOKEN': this.token, }, }); return data; } /** 从分支创建合并请求 */ async createMrWithBranch(params) { const { data } = await (0, axios_1.default)({ ...constant_1.MR.CreateWithBranch(this.id), data: { id: this.id, ...params, }, headers: { 'PRIVATE-TOKEN': this.token, }, }); return data; } } exports.Gitlab = Gitlab; //# sourceMappingURL=api.js.map