Skip to content

Commit 807e632

Browse files
committed
add new test, validator and fixed bugs read file
1 parent c8cf8e5 commit 807e632

File tree

8 files changed

+113
-34
lines changed

8 files changed

+113
-34
lines changed

.eslintrc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
"@typescript-eslint/no-this-alias": 1,
2323
"jest/valid-expect-in-promise": 1,
2424
"jest/no-conditional-expect": 1,
25-
"no-mixed-spaces-and-tabs": 0
25+
"no-mixed-spaces-and-tabs": 0,
26+
"no-unneeded-ternary": 0
2627
},
2728
"env": {
2829
"node": true,

__test__/index.test.ts

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,32 @@ import path from 'path'
44

55
describe('GTL Testing Group', function () {
66
beforeAll(function () {
7-
fs.writeFileSync(path.join(process.cwd(), 'schema.graphql'), 'hello wordl', { encoding: 'utf-8' })
7+
fs.writeFileSync(path.resolve(process.cwd(), 'src', 'isf.graphql'), 'hello wordl', { encoding: 'utf-8' })
8+
fs.writeFileSync('osf.graphql', 'hello wordl', { encoding: 'utf-8' })
89
})
910

1011
afterAll(function () {
11-
fs.unlinkSync(path.join(process.cwd(), 'schema.graphql'))
12+
fs.unlinkSync(path.resolve(process.cwd(), 'src', 'isf.graphql'))
13+
fs.unlinkSync('osf.graphql')
1214
})
1315

14-
it('Should be gtl function is defined', function () {
15-
const graphqlLoader = gtl({ fileName: 'schema.graphql' })
16-
expect(graphqlLoader).toBeDefined()
16+
it('Should be gtl read file inside multiple directory success', function () {
17+
const graphqlLoader = gtl({ directory: 'src', pattern: '**/*', fileName: 'isf.graphql' })
18+
expect(graphqlLoader).toBeInstanceOf(Array)
19+
})
20+
21+
it('Should be gtl read file outside multiple directory success', function () {
22+
const graphqlLoader = gtl({ pattern: '**/*', fileName: 'osf.graphql' })
23+
expect(graphqlLoader).toBeInstanceOf(Array)
24+
})
25+
26+
it('Should be gtl read file inside single directory success', function () {
27+
const graphqlLoader = gtl({ directory: 'src', fileName: 'isf.graphql' })
28+
expect(graphqlLoader).toBeInstanceOf(Array)
1729
})
1830

19-
it('Should be gtl read file success', function () {
20-
const graphqlLoader = gtl({ fileName: 'schema.graphql' })
31+
it('Should be gtl read file outside directory success', function () {
32+
const graphqlLoader = gtl({ fileName: 'osf.graphql' })
2133
expect(graphqlLoader).toBeInstanceOf(Array)
2234
})
2335
})

package-lock.json

Lines changed: 7 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "gtl-node",
3-
"version": "0.0.1",
4-
"description": "GTL is multiple loader for graphql schema",
3+
"version": "0.0.2",
4+
"description": "gtl-node is multiple loader for graphql schema file",
55
"main": "./dist/index.js",
66
"files": [
77
"/dist/**"
@@ -49,7 +49,8 @@
4949
],
5050
"dependencies": {
5151
"fast-glob": "^3.2.7",
52-
"fs-extra": "^10.0.0"
52+
"fs-extra": "^10.0.0",
53+
"is-any-type": "0.0.3"
5354
},
5455
"devDependencies": {
5556
"@types/fs-extra": "^9.0.12",

src/index.ts

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import path from 'path'
2-
import glob from 'fast-glob'
32
import fs from 'fs-extra'
3+
import { isType } from 'is-any-type'
4+
import { validatorCheck } from './utils/validatorCheck'
5+
import { fileCheck } from './utils/fileCheck'
46

57
export interface IGraphqTypeDefsLoader {
68
directory?: string
@@ -12,27 +14,23 @@ export interface IGraphqTypeDefsLoader {
1214
export function gtl(options: IGraphqTypeDefsLoader): string[] | Promise<string> {
1315
let readFile: string[]
1416
let typeDefsLoader: string
17+
let validator = validatorCheck(options)
1518

16-
if (options.extension !== undefined || options.pattern !== undefined || options.directory !== undefined) {
17-
readFile = glob.sync(path.join(process.cwd(), `${options.directory}/${options.pattern}.${options.extension}`))
18-
} else if (options.directory !== undefined) {
19-
readFile = glob.sync(path.join(process.cwd(), `${options.directory}/${options.fileName}`))
19+
if (isType(validator) !== 'boolean') {
20+
validatorCheck(options)
2021
} else {
21-
readFile = glob.sync(path.join(process.cwd(), `${options.fileName}`))
22+
readFile = fileCheck(options)
23+
if (readFile.length > 0) {
24+
return readFile.map((v: string): string => {
25+
let data = v.replace(/.*[/]/gi, '')
26+
if (options.directory !== undefined) {
27+
typeDefsLoader = fs.readFileSync(path.join(process.cwd(), `${options.directory}/${data.trim()}`)).toString('utf-8')
28+
} else {
29+
typeDefsLoader = fs.readFileSync(path.join(process.cwd(), `${data.trim()}`)).toString('utf-8')
30+
}
31+
return typeDefsLoader
32+
})
33+
}
34+
return Promise.reject(new Error('.graphql file not exist in directory'))
2235
}
23-
24-
if (readFile.length > 0) {
25-
return readFile.map((v: string): string => {
26-
let data = v.replace(/.*[/]/gi, '')
27-
28-
if (options.directory !== undefined) {
29-
typeDefsLoader = fs.readFileSync(path.join(process.cwd(), `${options.directory}/${data}`)).toString('utf-8')
30-
} else {
31-
typeDefsLoader = fs.readFileSync(path.join(process.cwd(), `${data}`)).toString('utf-8')
32-
}
33-
34-
return typeDefsLoader
35-
})
36-
}
37-
return Promise.reject(new Error('.graphql not exist in directory'))
3836
}

src/utils/fileCheck.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import glob from 'fast-glob'
2+
import path from 'path'
3+
import { isType } from 'is-any-type'
4+
5+
export const fileCheck = (options: Record<string, any>): string[] | any => {
6+
if (isType(options) !== 'object' && !(options instanceof Object)) {
7+
return Promise.reject(new Error('options is required'))
8+
}
9+
10+
let readFile: string[]
11+
let directory: string = options.directory
12+
let fileName: string = options.fileName
13+
let pattern: string = options.pattern
14+
let extension: string = options.extension
15+
16+
if (fileName === undefined) {
17+
readFile = glob.sync(path.resolve(directory, `${pattern}.${extension}`))
18+
} else if (pattern !== undefined && extension !== undefined) {
19+
readFile = glob.sync(path.resolve(directory, `${pattern}.${extension}`))
20+
} else if (directory !== undefined) {
21+
readFile = glob.sync(path.resolve(directory, `${fileName}`))
22+
} else {
23+
readFile = glob.sync(path.resolve(`${fileName}`))
24+
}
25+
26+
return readFile
27+
}

src/utils/matchProperty.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export const matchProperty = (data: Record<string, any>, compare: Record<string, any>): boolean => {
2+
const compareIn = Object.keys(compare)
3+
const newInData = compareIn.map((v) => `${v}` in data)
4+
return newInData.includes(false) ? false : true
5+
}

src/utils/validatorCheck.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { isType } from 'is-any-type'
2+
import { matchProperty } from './matchProperty'
3+
4+
export const validatorCheck = (options: Record<string, any>): any => {
5+
let defaultProperty: Record<string, any> = { directory: null, pattern: null, fileName: null, extension: null }
6+
let inOptions: boolean = matchProperty(defaultProperty, options)
7+
8+
if (isType(options) !== 'object' && !(options instanceof Object)) {
9+
return Promise.reject(new Error('options is required'))
10+
} else if (inOptions !== true) {
11+
return Promise.reject(new Error('options property not exist'))
12+
} else {
13+
let fileName: string = options.fileName
14+
let extension: string = options.extension
15+
let pathExt: string
16+
17+
if (fileName !== undefined) {
18+
pathExt = fileName.split('.')[1]
19+
} else {
20+
pathExt = extension
21+
}
22+
23+
let extPattern: boolean = /[graphql]/gi.test(pathExt)
24+
if (extPattern !== true) {
25+
return Promise.reject(new Error('extension file not supported'))
26+
} else {
27+
return true
28+
}
29+
}
30+
}

0 commit comments

Comments
 (0)