Browse Source

Delete over-abstracted code

Use methods from Node libraries directly.
master
TheoryOfNekomata 2 years ago
parent
commit
e66d267809
3 changed files with 21 additions and 51 deletions
  1. +21
    -25
      src/modules/git/Git.service.ts
  2. +0
    -19
      src/utils/fs.ts
  3. +0
    -7
      src/utils/process.ts

+ 21
- 25
src/modules/git/Git.service.ts View File

@@ -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<ChildProcess>
delete(repoBasePath: string): Promise<void>
advertiseReceivePackRefs(repoBasePath: string): Promise<ChildProcess>
advertiseUploadPackRefs(repoBasePath: string): Promise<ChildProcess>
receivePack(repoBasePath: string): Promise<ChildProcess>
uploadPack(repoBasePath: string): Promise<ChildProcess>
create(cwd: string): Promise<ChildProcess>
delete(cwd: string): Promise<void>
advertiseReceivePackRefs(cwd: string): Promise<ChildProcess>
advertiseUploadPackRefs(cwd: string): Promise<ChildProcess>
receivePack(cwd: string): Promise<ChildProcess>
uploadPack(cwd: string): Promise<ChildProcess>
}

export class GitServiceImpl implements GitService {
@@ -16,43 +15,40 @@ export class GitServiceImpl implements GitService {
return /^win/.test(process.platform);
}

async create(repoBasePath: string): Promise<ChildProcess> {
await mkdirp(repoBasePath);
return spawn(
repoBasePath,
'git', ['init', '--bare'],
);
async create(cwd: string): Promise<ChildProcess> {
await fs.mkdir(cwd, { recursive: true });
return spawn('git', ['init', '--bare'], { cwd });
}

async delete(repoBasePath: string): Promise<void> {
await unlink(repoBasePath);
async delete(cwd: string): Promise<void> {
await fs.rm(cwd, { recursive: true, force: true, });
}

async advertiseReceivePackRefs(repoBasePath: string): Promise<ChildProcess> {
async advertiseReceivePackRefs(cwd: string): Promise<ChildProcess> {
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<ChildProcess> {
async advertiseUploadPackRefs(cwd: string): Promise<ChildProcess> {
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<ChildProcess> {
async receivePack(cwd: string): Promise<ChildProcess> {
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<ChildProcess> {
async uploadPack(cwd: string): Promise<ChildProcess> {
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 });
}
}

+ 0
- 19
src/utils/fs.ts View File

@@ -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, })
}

+ 0
- 7
src/utils/process.ts View File

@@ -1,7 +0,0 @@
import * as childProcess from 'child_process';

export const spawn = (
cwd: string,
command: string,
args: string[],
) => childProcess.spawn(command, args, { cwd });

Loading…
Cancel
Save