Skip to content

Commit de65381

Browse files
committed
fix: recursive dir creation in resolver command
if passing a path to non existing directory, it will be created
1 parent f4ac4b9 commit de65381

File tree

4 files changed

+61
-10
lines changed

4 files changed

+61
-10
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: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
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

5-
describe('Resolver command test', () => {
6+
//TODO - remove only
7+
describe.only('Resolver command test', () => {
68
let res: AbstractCommand
79
const pathToType = './test/commands/resolver/test-schema.ts'
810
const pathToActualResolver = './test/output/actual/test-resolver.ts'
11+
const pathToDirectoryActualResolver = './test/output/actual/non/existing/dir/test-resolver.ts'
12+
const resolverDirLocation = './test/output/actual/non'
913
const pathToExpectedResolver = './test/output/expected/commands/resolver/test-resolver.ts'
1014
const pathToNonOverridenFile =
1115
'./test/output/expected/commands/resolver/test-not-overriden-by-resolver.ts'
@@ -18,20 +22,39 @@ describe('Resolver command test', () => {
1822
if (fs.existsSync(pathToActualResolver)) {
1923
fs.unlinkSync(pathToActualResolver)
2024
}
25+
fs.stat(resolverDirLocation, (err, stats) => {
26+
if (!err) {
27+
if (stats.isDirectory()) {
28+
fse.remove(resolverDirLocation, err => {
29+
console.log(
30+
'Could not remove resolver dir. On windows desktops, remove it manually...',
31+
err
32+
)
33+
})
34+
}
35+
}
36+
})
2137
})
2238

2339
it('works if action returns a function', () => {
2440
const act = res.getAction()
2541
expect(act).toBeInstanceOf(Function)
2642
})
2743

28-
it('works if resolver file was generated', async () => {
44+
it.only('works if resolver file was generated', async () => {
2945
const actFunction = res.getAction()
3046
await actFunction(pathToType, pathToActualResolver)
3147
const resolverFileExist: boolean = fs.existsSync(pathToActualResolver)
3248
expect(resolverFileExist).toBeTruthy()
3349
})
3450

51+
it('works if resolver file was generated in directory structure', async () => {
52+
const actFunction = res.getAction()
53+
await actFunction(pathToType, pathToDirectoryActualResolver)
54+
const resolverFileExist: boolean = fs.existsSync(pathToDirectoryActualResolver)
55+
expect(resolverFileExist).toBeTruthy()
56+
})
57+
3558
it('works if resolver file is identical to expected file', async () => {
3659
const actFunction = res.getAction()
3760
await actFunction(pathToType, pathToActualResolver)
@@ -46,6 +69,20 @@ describe('Resolver command test', () => {
4669
expect(actualContent).toEqual(expectedContent)
4770
})
4871

72+
// it('works if resolver file is in dir and identical to expected', async () => {
73+
// const actFunction = res.getAction()
74+
// await actFunction(pathToType, pathToDirectoryActualResolver)
75+
// const expectedContent = fs
76+
// .readFileSync(pathToExpectedResolver)
77+
// .toString()
78+
// .replace(/\s/g, '')
79+
// const actualContent = fs
80+
// .readFileSync(pathToDirectoryActualResolver)
81+
// .toString()
82+
// .replace(/\s/g, '')
83+
// expect(actualContent).toEqual(expectedContent)
84+
// })
85+
4986
it('works if resolver command does not override existing file', async () => {
5087
const actFunction = res.getAction()
5188
await actFunction(pathToType, pathToNonOverridenFile)

0 commit comments

Comments
 (0)