This repository has been archived by the owner on Nov 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
script.js
80 lines (62 loc) · 2.17 KB
/
script.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
const path = require("path");
const fs = require("fs");
const glob = require("glob");
const prettier = require("prettier");
const dir = path.resolve(__dirname, process.argv[2]);
glob(`${dir}/**/*.graphql`, (error, files) => {
console.log(`Processing ${files.length} .graphql files`);
for (const file of files) {
const oldFileName = file.split("/").at(-1);
const newFileName = oldFileName.replace("graphql", "js");
const className = oldFileName.replace(".graphql", "");
const oldContents = fs.readFileSync(file).toString();
const lines = oldContents.split("\n");
const imports = lines
.filter((line) => line.includes("#import"))
.map((line) => {
const importSegments = line
.replace(/#import "([^"]+)"/gi, "$1")
.split("/");
const name = importSegments.pop().replace(".graphql", "");
const path = importSegments.concat([name]).join("/");
return { name, path };
});
const headImports = imports
.map(({ name, path }) => `import ${name} from "${path}";`)
.join("\n");
const gqlImports = imports.map(({ name }) => `\${${name}}`).join("\n");
const rawGql = lines
.filter((line) => line && !line.includes("#import"))
.map((line) => ` ${line}`)
.join("\n");
const newContents = prettier.format(
`
import gql from "graphql-tag";
${headImports}
const ${className} = gql\`
${gqlImports}
${rawGql}
\`;
export default ${className};
`,
{ parser: "babel" }
);
console.log(` ${file.replace(dir, "")}`);
fs.writeFileSync(file.replace(oldFileName, newFileName), newContents);
fs.unlinkSync(file);
}
});
glob(`${dir}/**/*.{js,jsx,ts,tsx}`, (error, files) => {
console.log(`Processing ${files.length} .{js,jsx,ts,tsx} files`);
for (const file of files) {
const contents = fs.readFileSync(file).toString();
if (!contents.includes(".graphql")) {
continue;
}
const lines = contents.split("\n").map((line) => {
return line.replace(".graphql", "");
});
console.log(` ${file.replace(dir, "")}`);
fs.writeFileSync(file, lines.join("\n"));
}
});