This repository has been archived by the owner on Mar 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
111 lines (107 loc) · 3.22 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
const recast = require("recast");
const fs = require("fs");
const mkdirp = require("mkdirp");
const nodePath = require("path");
const uuid = require("uuid");
function parser(options) {
return {
parse(code) {
return require("babylon").parse(code, {
sourceType: "module",
plugins: options
});
}
};
};
const DEFAULT_BABYLON_PLUGINS = ["jsx", "classProperties", "dynamicImport", "objectRestSpread"];
module.exports = function({ source , filePath, babylonPlugins = DEFAULT_BABYLON_PLUGINS }) {
const ast = recast.parse(source, { parser: parser(babylonPlugins) });
mkdirp.sync(nodePath.join(filePath));
const newImports = new Map();
const queriesWritten = new Map();
function extractQuery(node) {
const type = node && node.type;
if (type === "TaggedTemplateExpression") {
const tag = node.tag.name;
if (tag === "gql") {
const literal = node.quasi.quasis[0].value.raw;
const splitLiteral = literal.split(" ");
const nameStart = splitLiteral.find((_, index) => (
index !== 0 &&
(
splitLiteral[index -1].includes("query") ||
splitLiteral[index -1] .includes("mutation")
)
));
const name = nameStart ? nameStart.split("(")[0] : uuid.v4();
const queryFilePath = nodePath.join(filePath, `${name}.graphql`);
fs.writeFileSync(queryFilePath, literal, "utf8");
newImports.set(name, queryFilePath);
queriesWritten.set(name, literal);
return { name, queryFilePath };
}
}
return false;
}
recast.visit(ast, {
visitImportDeclaration(path) {
const moduleName = path.node.source.value;
if (moduleName === "graphql-tag") {
path.prune();
}
return false;
},
visitVariableDeclarator(path) {
const init = path.node && path.node.init;
const queryInfo = extractQuery(init);
if (queryInfo) {
if (path.parent.parent.value.type === "ExportNamedDeclaration") {
path.parent.parent.replace(
recast.parse(`export { ${queryInfo.name} } from "${queryInfo.queryFilePath}";\n`).program.body[0],
path.parent.parent.node
);
}
else {
path.prune();
}
}
return false;
},
visitExportDefaultDeclaration(path) {
const declaration = path.node.declaration;
const queryInfo = extractQuery(declaration);
if (queryInfo) {
path.replace(
recast.parse(`export default ${queryInfo.name};\n`).program.body[0],
path.node
);
}
return false;
}
});
newImports.forEach((filePath, name) => {
ast.program.body = recast.parse(`import ${name} from "${filePath}";\n`).program.body.concat(ast.program.body);
});
ast.program.body = ast.program.body.filter(node => {
if (
node.type === "ExportNamedDeclaration" &&
node.declaration &&
node.declaration.declarations &&
node.declaration.declarations[0] &&
node.declaration.declarations[0].init &&
node.declaration.declarations[0].init.type === "TaggedTemplateExpression" &&
node.declaration.declarations[0].init.tag.name === "gql"
) {
return false;
} else if (
node.type === "ExportDefaultDeclaration" &&
node.declaration &&
node.declaration.type === "TaggedTemplateExpression" &&
node.declaration.tag.name === 'gql'
) {
return false;
}
return true;
});
return { source: recast.print(ast).code, queriesWritten: queriesWritten };
};