1 | import {
|
2 | Declaration,
|
3 | importDeclaration,
|
4 | Identifier,
|
5 | identifier,
|
6 | importDefaultSpecifier,
|
7 | stringLiteral,
|
8 | variableDeclaration,
|
9 | variableDeclarator,
|
10 | callExpression,
|
11 | memberExpression,
|
12 | arrayExpression
|
13 | } from "@babel/types";
|
14 | import { stringIdentifier, exportDeclaration } from "./types";
|
15 | import { camelCase } from "lodash";
|
16 | import { relativePath } from "./paths";
|
17 | import compact from "./compact";
|
18 |
|
19 | const rawStringIdentifier = (fragmentOrOperationName: string): Identifier =>
|
20 | identifier(camelCase(fragmentOrOperationName + "RawString"));
|
21 |
|
22 | const importDeclarationWithIdentifier = (
|
23 | identifier: Identifier,
|
24 | source: string
|
25 | ): Declaration =>
|
26 | importDeclaration(
|
27 | [importDefaultSpecifier(identifier)],
|
28 | stringLiteral(source)
|
29 | );
|
30 |
|
31 | const rawStringImportDeclaration = (
|
32 | fragmentOrOperationName: string,
|
33 | source: string
|
34 | ): Declaration =>
|
35 | importDeclarationWithIdentifier(
|
36 | rawStringIdentifier(fragmentOrOperationName),
|
37 | source
|
38 | );
|
39 |
|
40 | const stringImportDeclaration = (
|
41 | fragmentOrOperationName: string,
|
42 | source: string
|
43 | ): Declaration =>
|
44 | importDeclarationWithIdentifier(
|
45 | stringIdentifier(fragmentOrOperationName),
|
46 | source
|
47 | );
|
48 |
|
49 | const joinedStringDeclaration = (
|
50 | fragmentOrOperationName: string,
|
51 | fragmentDependencies: string[]
|
52 | ): Declaration =>
|
53 | variableDeclaration("const", [
|
54 | variableDeclarator(
|
55 | stringIdentifier(fragmentOrOperationName),
|
56 | callExpression(
|
57 | memberExpression(
|
58 | arrayExpression([
|
59 | rawStringIdentifier(fragmentOrOperationName),
|
60 | ...fragmentDependencies.map(fragment => stringIdentifier(fragment))
|
61 | ]),
|
62 | identifier("join")
|
63 | ),
|
64 | [stringLiteral("\n\n")]
|
65 | )
|
66 | )
|
67 | ]);
|
68 |
|
69 | const exportedJoinedStringDeclaration = (
|
70 | fragmentOrOperationName: string,
|
71 | fragmentDependencies: string[]
|
72 | ): Declaration =>
|
73 | exportDeclaration(
|
74 | joinedStringDeclaration(fragmentOrOperationName, fragmentDependencies)
|
75 | );
|
76 |
|
77 | const exportedStringDeclaration = (
|
78 | fragmentOrOperationName: string
|
79 | ): Declaration =>
|
80 | exportDeclaration(
|
81 | variableDeclaration("const", [
|
82 | variableDeclarator(
|
83 | stringIdentifier(fragmentOrOperationName),
|
84 | rawStringIdentifier(fragmentOrOperationName)
|
85 | )
|
86 | ])
|
87 | );
|
88 |
|
89 | export default (
|
90 | fragmentOrOperationName: string,
|
91 | filePath: string,
|
92 | outputPath: string,
|
93 | fragmentDependencies: string[],
|
94 | exportStringDeclaration: boolean = false
|
95 | ): Declaration[] =>
|
96 | compact(
|
97 | (fragmentDependencies.length > 0 || exportStringDeclaration
|
98 | ? rawStringImportDeclaration
|
99 | : stringImportDeclaration)(
|
100 | fragmentOrOperationName,
|
101 | relativePath(outputPath, filePath)
|
102 | ),
|
103 | fragmentDependencies.length > 0
|
104 | ? (exportStringDeclaration
|
105 | ? exportedJoinedStringDeclaration
|
106 | : joinedStringDeclaration)(
|
107 | fragmentOrOperationName,
|
108 | fragmentDependencies
|
109 | )
|
110 | : exportStringDeclaration &&
|
111 | exportedStringDeclaration(fragmentOrOperationName)
|
112 | );
|