Skip to content

Commit a39e308

Browse files
authored
Merge pull request #20 from SecJS/feat/len-get-branch
feat: Add getBranch function and constructor to Blacklist
2 parents 058b21d + e0a33fe commit a39e308

File tree

6 files changed

+57
-2
lines changed

6 files changed

+57
-2
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,16 @@ console.log(clean.cleanArraysInObject(object2)) // { number2: [{ number1: "numbe
249249

250250
## Functions Usage
251251

252+
### getBranch
253+
254+
> Get the actual git branch that the project is running or not a repository.
255+
256+
```js
257+
import { getBranch } from '@secjs/utils'
258+
259+
await getBranch() // master || Not a repository
260+
```
261+
252262
### pathPattern
253263

254264
> Transform all route paths to the same pattern.

index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export * from './src/Functions/kmRadius'
1818
export * from './src/Functions/paginate'
1919
export * from './src/Functions/getFiles'
2020
export * from './src/Functions/scheduler'
21+
export * from './src/Functions/getBranch'
2122
export * from './src/Functions/getFolders'
2223
export * from './src/Functions/fileExists'
2324
export * from './src/Functions/pathPattern'

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@secjs/utils",
3-
"version": "1.3.6",
3+
"version": "1.3.7",
44
"description": "",
55
"license": "MIT",
66
"author": "João Lenon",

src/Classes/Blacklist.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,22 @@ import { createInterface } from 'readline'
22
import { createWriteStream, createReadStream } from 'fs'
33

44
export class Blacklist {
5+
private readonly filePath?: string
6+
7+
constructor(filePath?: string) {
8+
this.filePath = filePath
9+
}
10+
511
/**
612
* Add value to a file path
713
*
814
* @param value The value to be add
915
* @param filePath The file path to the blacklist
1016
* @return void
1117
*/
12-
async add(value: string, filePath: string): Promise<void> {
18+
async add(value: string, filePath?: string): Promise<void> {
19+
filePath = filePath || this.filePath
20+
1321
return new Promise((resolve, reject) => {
1422
const stream = createWriteStream(filePath, { flags: 'a' })
1523

@@ -31,6 +39,8 @@ export class Blacklist {
3139
* @return string|null The value found or null
3240
*/
3341
async find(value: string, filePath: string): Promise<string | null> {
42+
filePath = filePath || this.filePath
43+
3444
const stream = createReadStream(filePath)
3545

3646
const lines = createInterface({
@@ -57,6 +67,8 @@ export class Blacklist {
5767
* @return void The value found or null
5868
*/
5969
async remove(value: string, filePath: string): Promise<void> {
70+
filePath = filePath || this.filePath
71+
6072
return new Promise((resolve, reject) => {
6173
const readStream = createReadStream(filePath)
6274

src/Functions/getBranch.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { promisify } from 'util'
2+
3+
// eslint-disable-next-line @typescript-eslint/no-var-requires
4+
const exec = promisify(require('child_process').exec)
5+
6+
/**
7+
* Get the actual branch HEAD from the repository
8+
*
9+
* @return the branch HEAD name or not a repository
10+
*/
11+
export async function getBranch(): Promise<string> {
12+
let branch: string
13+
14+
try {
15+
branch = (await exec('git branch --show-current')).stdout.replace('\n', '')
16+
17+
if (!branch || branch === '') branch = 'Not a repository'
18+
} catch (error) {
19+
branch = 'Not a repository'
20+
}
21+
22+
return branch
23+
}

tests/Functions/getBranch.spec.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { getBranch } from '../../src/Functions/getBranch'
2+
3+
describe('\n getBranch Function', () => {
4+
it('should return the branch from @secjs/utils', async () => {
5+
const branch = await getBranch()
6+
7+
expect(branch).toBeTruthy()
8+
})
9+
})

0 commit comments

Comments
 (0)