Skip to content

Commit 6e4d35d

Browse files
authored
Merge pull request #3 from tomyitav/resolver-create-dir-recursive
Resolver create dir recursive
2 parents f4ac4b9 + 2e9d3bf commit 6e4d35d

File tree

4 files changed

+44
-8
lines changed

4 files changed

+44
-8
lines changed

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/commands/resolver/resolver.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
11
import { AbstractCommand } from '../abstract-command'
2-
import * as path from 'path'
32
import * as transform_ from 'graphql-json-schema'
43
const transform = transform_
54
import { Operation, Schema } from './resolver-types'
65
import {
76
functionOperationPrefix,
8-
gqlMethodSuffix,
97
gqlMethodSignature,
8+
gqlMethodSuffix,
109
keysToGenerate,
10+
operationFuntionsSuffix,
1111
operationPrefix,
1212
operationSuffix,
1313
resolverPrefix,
1414
resolverSuffix,
15-
subscriptionSubscribeDefinition,
16-
operationFuntionsSuffix
15+
subscriptionSubscribeDefinition
1716
} from './resolver-constants'
1817
import { fileExists, readFileContent, writeToFile } from '../../utils/file-operations'
1918

src/utils/file-operations.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,28 @@
11
import * as fs from 'fs'
2+
import * as path from 'path'
3+
import * as fse from 'fs-extra'
4+
5+
const allowedFileExtensions = ['.ts']
26

37
export function writeToFile(pathToFile: string, content: string): Promise<void> {
48
return new Promise((resolve, reject) => {
5-
fs.writeFile(pathToFile, content, err => {
9+
const pathProps = path.parse(pathToFile)
10+
if (!allowedFileExtensions.includes(pathProps.ext)) {
11+
reject('Illegal file extension.. only ' + allowedFileExtensions + ' extensions are allowed')
12+
return
13+
}
14+
fse.ensureDir(pathProps.dir, err => {
615
if (err) {
716
reject(err)
8-
} else {
9-
resolve()
17+
return
1018
}
19+
fs.writeFile(pathToFile, content, err => {
20+
if (err) {
21+
reject(err)
22+
} else {
23+
resolve()
24+
}
25+
})
1126
})
1227
})
1328
}

test/commands/resolver/resolver.test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
import { AbstractCommand } from '../../../src/commands/abstract-command'
22
import { Resolver } from '../../../src/commands/resolver/resolver'
33
import * as fs from 'fs'
4+
import * as fse from 'fs-extra'
45

56
describe('Resolver command test', () => {
67
let res: AbstractCommand
78
const pathToType = './test/commands/resolver/test-schema.ts'
89
const pathToActualResolver = './test/output/actual/test-resolver.ts'
10+
const pathToDirectoryActualResolver = './test/output/actual/non/existing/dir/test-resolver.ts'
11+
const resolverDirLocation = './test/output/actual/non'
912
const pathToExpectedResolver = './test/output/expected/commands/resolver/test-resolver.ts'
1013
const pathToNonOverridenFile =
1114
'./test/output/expected/commands/resolver/test-not-overriden-by-resolver.ts'
@@ -18,6 +21,18 @@ describe('Resolver command test', () => {
1821
if (fs.existsSync(pathToActualResolver)) {
1922
fs.unlinkSync(pathToActualResolver)
2023
}
24+
fs.stat(resolverDirLocation, (err, stats) => {
25+
if (!err) {
26+
if (stats.isDirectory()) {
27+
fse.remove(resolverDirLocation, err => {
28+
console.log(
29+
'Could not remove resolver dir. On windows desktops, remove it manually...',
30+
err
31+
)
32+
})
33+
}
34+
}
35+
})
2136
})
2237

2338
it('works if action returns a function', () => {
@@ -32,6 +47,13 @@ describe('Resolver command test', () => {
3247
expect(resolverFileExist).toBeTruthy()
3348
})
3449

50+
it('works if resolver file was generated in directory structure', async () => {
51+
const actFunction = res.getAction()
52+
await actFunction(pathToType, pathToDirectoryActualResolver)
53+
const resolverFileExist: boolean = fs.existsSync(pathToDirectoryActualResolver)
54+
expect(resolverFileExist).toBeTruthy()
55+
})
56+
3557
it('works if resolver file is identical to expected file', async () => {
3658
const actFunction = res.getAction()
3759
await actFunction(pathToType, pathToActualResolver)

0 commit comments

Comments
 (0)