From e66d267809dd80089274b75406fcd5ea5598b721 Mon Sep 17 00:00:00 2001 From: TheoryOfNekomata Date: Sun, 22 May 2022 11:08:34 +0800 Subject: [PATCH] Delete over-abstracted code Use methods from Node libraries directly. --- src/modules/git/Git.service.ts | 46 ++++++++++++++++------------------ src/utils/fs.ts | 19 -------------- src/utils/process.ts | 7 ------ 3 files changed, 21 insertions(+), 51 deletions(-) delete mode 100644 src/utils/fs.ts delete mode 100644 src/utils/process.ts diff --git a/src/modules/git/Git.service.ts b/src/modules/git/Git.service.ts index 65b032b..50628f7 100644 --- a/src/modules/git/Git.service.ts +++ b/src/modules/git/Git.service.ts @@ -1,14 +1,13 @@ -import {ChildProcess} from 'child_process'; -import {spawn} from '../../utils/process'; -import {mkdirp, unlink} from '../../utils/fs'; +import {ChildProcess, spawn} from 'child_process'; +import * as fs from 'fs/promises'; export interface GitService { - create(repoBasePath: string): Promise - delete(repoBasePath: string): Promise - advertiseReceivePackRefs(repoBasePath: string): Promise - advertiseUploadPackRefs(repoBasePath: string): Promise - receivePack(repoBasePath: string): Promise - uploadPack(repoBasePath: string): Promise + create(cwd: string): Promise + delete(cwd: string): Promise + advertiseReceivePackRefs(cwd: string): Promise + advertiseUploadPackRefs(cwd: string): Promise + receivePack(cwd: string): Promise + uploadPack(cwd: string): Promise } export class GitServiceImpl implements GitService { @@ -16,43 +15,40 @@ export class GitServiceImpl implements GitService { return /^win/.test(process.platform); } - async create(repoBasePath: string): Promise { - await mkdirp(repoBasePath); - return spawn( - repoBasePath, - 'git', ['init', '--bare'], - ); + async create(cwd: string): Promise { + await fs.mkdir(cwd, { recursive: true }); + return spawn('git', ['init', '--bare'], { cwd }); } - async delete(repoBasePath: string): Promise { - await unlink(repoBasePath); + async delete(cwd: string): Promise { + await fs.rm(cwd, { recursive: true, force: true, }); } - async advertiseReceivePackRefs(repoBasePath: string): Promise { + async advertiseReceivePackRefs(cwd: string): Promise { const command = GitServiceImpl.isWindows() ? 'git' : 'git-receive-pack'; const commonArgs = ['--stateless-rpc', '--advertise-refs', '.']; const args = GitServiceImpl.isWindows() ? ['receive-pack', ...commonArgs] : commonArgs; - return spawn(repoBasePath, command, args); + return spawn(command, args, { cwd }); } - async advertiseUploadPackRefs(repoBasePath: string): Promise { + async advertiseUploadPackRefs(cwd: string): Promise { const command = GitServiceImpl.isWindows() ? 'git' : 'git-upload-pack'; const commonArgs = ['--stateless-rpc', '--advertise-refs', '.']; const args = GitServiceImpl.isWindows() ? ['upload-pack', ...commonArgs] : commonArgs; - return spawn(repoBasePath, command, args); + return spawn(command, args, { cwd }); } - async receivePack(repoBasePath: string): Promise { + async receivePack(cwd: string): Promise { const command = GitServiceImpl.isWindows() ? 'git' : 'git-receive-pack'; const commonArgs = ['--stateless-rpc', '.']; const args = GitServiceImpl.isWindows() ? ['receive-pack', ...commonArgs] : commonArgs; - return spawn(repoBasePath, command, args); + return spawn(command, args, { cwd }); } - async uploadPack(repoBasePath: string): Promise { + async uploadPack(cwd: string): Promise { const command = GitServiceImpl.isWindows() ? 'git' : 'git-upload-pack'; const commonArgs = ['--stateless-rpc', '.']; const args = GitServiceImpl.isWindows() ? ['upload-pack', ...commonArgs] : commonArgs; - return spawn(repoBasePath, command, args); + return spawn(command, args, { cwd }); } } diff --git a/src/utils/fs.ts b/src/utils/fs.ts deleted file mode 100644 index 9f7126f..0000000 --- a/src/utils/fs.ts +++ /dev/null @@ -1,19 +0,0 @@ -import * as fs from 'fs/promises'; -import * as p from 'path'; - -export const mkdirp = (path: string) => { - const pathFragments = path.replaceAll('\\', '/').split('/'); - const directoriesToCheck = pathFragments.reduce( - (theDirectories, fragment, i) => [ - ...theDirectories, - theDirectories.length > 0 ? p.join(theDirectories[i - 1], fragment) : fragment, - ], - [] as string[], - ) - - return Promise.allSettled(directoriesToCheck.map((d) => fs.mkdir(d))); -} - -export const unlink = (path: string) => { - return fs.rm(path, { recursive: true, force: true, }) -} diff --git a/src/utils/process.ts b/src/utils/process.ts deleted file mode 100644 index f30def9..0000000 --- a/src/utils/process.ts +++ /dev/null @@ -1,7 +0,0 @@ -import * as childProcess from 'child_process'; - -export const spawn = ( - cwd: string, - command: string, - args: string[], -) => childProcess.spawn(command, args, { cwd });