-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgqlTestExtraction.js
executable file
·85 lines (79 loc) · 2.32 KB
/
gqlTestExtraction.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
const fs = require('fs');
const path = require('path');
const mkdir = require('mkdirp');
const gqlExtract = require("gql-extract");
const tmp = require("tmp");
const generateTest = require('./generateTest');
function errorExit(err) {
if (err) {
console.error(err.stack);
process.exit(1);
}
}
function writeFile(filename, query, queryName, schemaLocation, importLocation) {
fs.writeFileSync(
filename,
generateTest({ query, queryName, schemaLocation, importLocation })
);
console.log(`test generated for ${queryName}`);
}
function gqlTestExtraction({ entry, output, schemaLocation, overwriteFiles, importLocation }) {
tmp.dir({ unsafeCleanup: true}, (err, graphqlOutput, cleanup) => {
fs.readdir(entry, (err, files) => {
errorExit(err);
let finishedCount = 0;
files.forEach(file => {
fs.stat(path.join(entry, file), (err, stat) => {
errorExit(err);
if (stat.isFile() && file.includes(".js")) {
fs.readFile(path.join(entry, file), "utf8", (err, data) => {
errorExit(err);
const { queriesWritten } = gqlExtract({
source: data,
filePath: graphqlOutput
});
if (queriesWritten.size) {
queriesWritten.forEach((query, queryName) => {
errorExit(err);
mkdir(output, err => {
errorExit(err);
const filename = path.join(output, `${queryName}-test.js`);
if (overwriteFiles) {
writeFile(filename, query, queryName, schemaLocation, importLocation);
finishedCount++;
if (finishedCount === files.length) {
cleanup();
}
} else {
fs.stat(filename, function(err, stat) {
const fileDoesNotExist = err || !stat.isFile();
if (fileDoesNotExist) {
writeFile(filename, query, queryName, schemaLocation, importLocation);
finishedCount++;
}
if (finishedCount === files.length) {
cleanup();
}
});
}
});
});
}
});
}
else if (!stat.isFile()) {
gqlTestExtraction({
entry: path.join(entry, file),
output,
graphqlOutput,
schemaLocation,
overwriteFiles,
importLocation
});
}
});
});
});
});
}
module.exports = gqlTestExtraction;