From 1d8b99fe8efae522b75ff763bea5ddbeff845cd6 Mon Sep 17 00:00:00 2001 From: Ivan Date: Thu, 25 Feb 2021 18:15:12 +0800 Subject: [PATCH] =?UTF-8?q?feat(repos):=20=E6=A3=80=E5=87=BA=E8=B7=AF?= =?UTF-8?q?=E5=BE=84=E6=B7=BB=E5=8A=A0=E5=88=86=E6=94=AF=E5=90=8D=E7=A7=B0?= =?UTF-8?q?=E6=88=96Commit=20Number=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package-lock.json | 26 ++++++++++++++++++-------- src/repos/repos.service.spec.ts | 10 +++++----- src/repos/repos.service.ts | 13 +++++++------ 3 files changed, 30 insertions(+), 19 deletions(-) diff --git a/package-lock.json b/package-lock.json index f9204cf..9917d98 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9087,14 +9087,6 @@ } } }, - "mkdirp": { - "version": "0.5.5", - "resolved": "https://registry.npmjs.org/mkdirp/-/mkdirp-0.5.5.tgz", - "integrity": "sha512-NKmAlESf6jMGym1++R0Ra7wvhV+wFW63FaSOFPwRahvea0gMUcGUhVeAg/0BC0wiv9ih5NYPB1Wn1UEI1/L+xQ==", - "requires": { - "minimist": "^1.2.5" - } - }, "moment": { "version": "2.29.1", "resolved": "https://registry.npmjs.org/moment/-/moment-2.29.1.tgz", @@ -9126,6 +9118,16 @@ "on-finished": "^2.3.0", "type-is": "^1.6.4", "xtend": "^4.0.0" + }, + "dependencies": { + "mkdirp": { + "version": "0.5.5", + "resolved": "https://registry.npmjs.org/mkdirp/-/mkdirp-0.5.5.tgz", + "integrity": "sha512-NKmAlESf6jMGym1++R0Ra7wvhV+wFW63FaSOFPwRahvea0gMUcGUhVeAg/0BC0wiv9ih5NYPB1Wn1UEI1/L+xQ==", + "requires": { + "minimist": "^1.2.5" + } + } } }, "multimatch": { @@ -9489,6 +9491,14 @@ "minipass": "^2.9.0" } }, + "mkdirp": { + "version": "0.5.5", + "resolved": "https://registry.npmjs.org/mkdirp/-/mkdirp-0.5.5.tgz", + "integrity": "sha512-NKmAlESf6jMGym1++R0Ra7wvhV+wFW63FaSOFPwRahvea0gMUcGUhVeAg/0BC0wiv9ih5NYPB1Wn1UEI1/L+xQ==", + "requires": { + "minimist": "^1.2.5" + } + }, "rimraf": { "version": "2.7.1", "resolved": "https://registry.npmjs.org/rimraf/-/rimraf-2.7.1.tgz", diff --git a/src/repos/repos.service.spec.ts b/src/repos/repos.service.spec.ts index aadb965..fccac1f 100644 --- a/src/repos/repos.service.spec.ts +++ b/src/repos/repos.service.spec.ts @@ -75,7 +75,7 @@ describe('ReposService', () => { it('should be checkout', async () => { await service.checkoutBranch(getTest1Project(), 'master'); const filePath = join( - service.getWorkspaceRoot(getTest1Project()), + service.getWorkspaceRoot(getTest1Project(), 'master'), 'README.md', ); const text = await readFile(filePath, { encoding: 'utf-8' }); @@ -86,7 +86,7 @@ describe('ReposService', () => { await service.checkoutBranch(getTest1Project(), 'branch-a'); await service.checkoutBranch(getTest1Project(), 'branch-b'); const filePath = join( - service.getWorkspaceRoot(getTest1Project()), + service.getWorkspaceRoot(getTest1Project(), 'branch-b'), 'branch-b.md', ); const text = await readFile(filePath, { encoding: 'utf-8' }); @@ -100,7 +100,7 @@ describe('ReposService', () => { it('checkout the specified version', async () => { await service.checkoutBranch(getTest1Project(), 'master'); const filePath = join( - service.getWorkspaceRoot(getTest1Project()), + service.getWorkspaceRoot(getTest1Project(), 'master'), 'README.md', ); const text = await readFile(filePath, { encoding: 'utf-8' }); @@ -112,7 +112,7 @@ describe('ReposService', () => { it('should be checkout', async () => { await service.checkoutCommit(getTest1Project(), '498c782685'); const filePath = join( - service.getWorkspaceRoot(getTest1Project()), + service.getWorkspaceRoot(getTest1Project(), '498c782685'), 'README.md', ); const text = await readFile(filePath, { encoding: 'utf-8' }); @@ -121,7 +121,7 @@ describe('ReposService', () => { it('should be checkout right commit', async () => { await service.checkoutCommit(getTest1Project(), '7f7123fe5b'); const filePath = join( - service.getWorkspaceRoot(getTest1Project()), + service.getWorkspaceRoot(getTest1Project(), '7f7123fe5b'), 'README.md', ); const text = await readFile(filePath, { encoding: 'utf-8' }); diff --git a/src/repos/repos.service.ts b/src/repos/repos.service.ts index 04e4aa9..4696f72 100644 --- a/src/repos/repos.service.ts +++ b/src/repos/repos.service.ts @@ -19,10 +19,11 @@ export class ReposService { private readonly configService: ConfigService, ) {} - getWorkspaceRoot(project: Project): string { + getWorkspaceRoot(project: Project, subDir = ''): string { return join( this.configService.get('workspaces.root'), project.name, + encodeURIComponent(subDir), ); } @@ -30,14 +31,14 @@ export class ReposService { // TODO: 获取锁,失败抛错。 } - async getGit(project: Project) { - const workspaceRoot = this.getWorkspaceRoot(project); + async getGit(project: Project, subDir?: string) { + const workspaceRoot = this.getWorkspaceRoot(project, subDir); await this.lockWorkspace(workspaceRoot); const firstInit = await access(workspaceRoot, F_OK) .then(() => false) .catch(async () => { - await mkdir(workspaceRoot); + await mkdir(workspaceRoot, { recursive: true }); return true; }); const git = gitP(workspaceRoot); @@ -69,7 +70,7 @@ export class ReposService { } async checkoutBranch(project: Project, branch: string) { - const git = await this.getGit(project); + const git = await this.getGit(project, branch); try { await git.fetch(DEFAULT_REMOTE_NAME, branch); } catch (err) { @@ -87,7 +88,7 @@ export class ReposService { } async checkoutCommit(project: Project, commitNumber: string) { - const git = await this.getGit(project); + const git = await this.getGit(project, commitNumber); try { await git.fetch(DEFAULT_REMOTE_NAME); } catch (err) {