This repository has been archived by the owner on Jul 23, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·115 lines (100 loc) · 2.56 KB
/
index.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#!/usr/bin/env node
/*jshint esversion: 6 */
const { transpileSchema } = require("graphql-s2s").graphqls2s;
//const { makeExecutableSchema } = require('graphql-tools');
const minimist = require("minimist");
const meow = require("meow");
const fs = require("fs");
const path = require("path");
const args = meow(`
Usage
$ graphqls2s-transpiler
Options
--file, -f Input file to transpile
--output, -o Output file for transpiled code optional if not given will print output to console
`, {
flags: {
file: {
type: "string",
alias: "f"
},
output: {
type: "string",
alias: "o"
}
}
});
const checkForError = (err)=>{
if(err){
console.error(err);
process.exit();
}
};
const outputSchema = (data)=>{
outputFile = args.flags.output;
data = transpileSchema(data);
if(!args.flags.output){
console.log(data);
return;
}
// writing the output to a file
fs.writeFile(outputFile, data, function(err) {
checkForError(err);
});
};
inputFile = args.flags.file;
if(!inputFile || inputFile.length === 0){
console.log("Please check the help to provide right parameters");
console.log(args.help);
process.exit();
}
if(!Array.isArray(inputFile)){
inputFile = [inputFile];
}
let inputFiles = [];
let inputPromises = [];
function checkForDirectories(){
// Iterating over inputFiles to check if any of them are directories
inputFile.forEach((file_path)=>{
if(!fs.statSync(file_path).isDirectory()){
inputFiles.push(file_path);
return;
}
inputPromises.push(new Promise((resolve, reject)=>{
fs.readdir(file_path, (error, files)=>{
checkForError(error);
files.forEach((file_name)=>{
if(!file_name.endsWith(".graphql")){
return;
}
file_name = path.join(file_path, file_name);
console.log(typeof file_name);
inputFiles.push(file_name);
resolve();
});
});
}));
});
}
checkForDirectories();
Promise.all(inputPromises).then(()=>{
if(!inputFiles){
console.log("Please check the help to provide right parameters");
console.log(args.help);
return;
}
let promisses = [];
inputFiles.forEach((file)=>{
// Using promises so that we can wait for all the files being read
promisses.push(new Promise((resolve, reject)=>{
fs.readFile(file, "utf8", function(err, contents){
checkForError(err);
resolve(contents);
});
}));
});
// waiting for all the files being read
Promise.all(promisses).then((...args)=>{
outputSchema(args[0].join("\n"));
});
});