Skip to content

Commit

Permalink
Refactor(accureleaser): 从面向对象改为组合式的扩展git工具对象
Browse files Browse the repository at this point in the history
  • Loading branch information
E0SelmY4V committed Apr 5, 2024
1 parent c705491 commit b3358df
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 23 deletions.
45 changes: 29 additions & 16 deletions packages/accureleaser/lib/git.js
Original file line number Diff line number Diff line change
@@ -1,53 +1,66 @@
import * as fsp from 'fs/promises';
import simpleGit from 'simple-git';

class ExtendedGit {
/**@param {import('simple-git').SimpleGit} git Git 操作对象 */
constructor(git) {
this.git = git;
}
/**
* @typedef {ReturnType<typeof getExtendedGit>} ExtendedGit
*/

/**@param {import('simple-git').SimpleGit} git Git 操作对象 */
function getExtendedGit(git) {
const instance = {};
/**
* 获取提交的差异索引
* @param {string} commit 提交
* @param {string} file 文件
*/
diffIndex = async (commit, file = '') => {
return await this.git.raw('diff-index', ...file ? [commit, file] : [commit]);
instance.diffIndex = async function (commit, file = '') {
return await git.raw('diff-index', ...file ? [commit, file] : [commit]);
};
/**
* 查看一个文件本次提交是否是新建或修改过
* @param {string} file 文件路径
*/
isChanged = async file => {
return await this.diffIndex('HEAD^', file) !== '';
instance.isChanged = async function (file) {
return await instance.diffIndex('HEAD^', file) !== '';
};
/**
* 查看一个文件本次提交是否是新建
* @param {string} file 文件路径
*/
isNewed = async file => {
return (await this.diffIndex('HEAD^', file)).slice(1, 7) === '000000';
instance.isNewed = async function (file) {
return (await instance.diffIndex('HEAD^', file)).slice(1, 7) === '000000';
};
/**
* 读取特定提交的文件
* @param {string} commit 提交
* @param {string} file 文件
*/
checkoutFile = async (commit, file) => {
await this.git.checkout(commit, ['--', file]);
instance.checkoutFile = async function (commit, file) {
await git.checkout(commit, ['--', file]);
const content = await fsp.readFile(file);
await this.git.checkout('HEAD', ['--', file]);
await git.checkout('HEAD', ['--', file]);
return content;
};

return instance;
}

/**
* 获得拓展过的 Git 操作对象
* @param {string} root Git 仓库地址
* @returns {ExtendedGit & import('simple-git').SimpleGit}
*/
export default function getGit(root) {
const git = simpleGit(root);
return Object.assign(git, new ExtendedGit(git));
const sgit = simpleGit(root);
const git = getExtendedGit(sgit);
for (const key in sgit) {
// @ts-ignore
if (typeof sgit[key] === 'function') {
// @ts-ignore
git[key] = (...args) => sgit[key](...args);
}
}
// @ts-ignore
return git;
}

14 changes: 7 additions & 7 deletions packages/accureleaser/test/git.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import getGit from '../lib/git.js';

// const releaser = new Releaser(dir);

const git = getGit(dir);
console.log(await git.isChanged('utilities/accurtype-releaser/test/init.js'));
console.log(await git.isChanged('utilities/accurtype-releaser/lib/index.js'));
console.log(await git.isChanged('utilities/accurtype-releaser/package.json'));
console.log(await git.isNewed('utilities/accurtype-releaser/test/init.js'));
console.log(await git.isNewed('utilities/accurtype-releaser/lib/index.js'));
console.log(await git.isNewed('utilities/accurtype-releaser/package.json'));
const { isChanged, isNewed } = getGit(dir);
console.log(await isChanged('packages/accureleaser/test/init.js'));
console.log(await isChanged('packages/accureleaser/lib/index.js'));
console.log(await isChanged('packages/accureleaser/package.json'));
console.log(await isNewed('packages/accureleaser/test/init.js'));
console.log(await isNewed('packages/accureleaser/lib/index.js'));
console.log(await isNewed('packages/accureleaser/package.json'));

0 comments on commit b3358df

Please sign in to comment.