forked from vaadin/cookbook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parseClientRoutes.ts
168 lines (152 loc) · 4.35 KB
/
parseClientRoutes.ts
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import * as ts from "typescript";
import RecipeInfo from "./frontend/generated/com/vaadin/recipes/data/RecipeInfo";
import { BaseRoute } from "@vaadin/router";
import Tag from "./frontend/generated/com/vaadin/recipes/recipe/Tag";
const debug = (...theArgs) => {
// console.log(theArgs);
};
debug("Running");
const glob = require("glob");
const path = require("path");
const fs = require("fs");
const args = process.argv.slice(2);
export const firstToLower = (text: string) => {
if (!text || text.length < 2) {
return text;
}
return text.substr(0, 1).toLowerCase() + text.substr(1);
};
export const recipeToRoute = (
recipeInfo: RecipeInfo
): RecipeRouteWithAction => {
const folder = "recipe/" + recipeInfo.url + "/";
const absoluteSourceFiles = [
recipeInfo.url + ".ts",
...(recipeInfo.sourceFiles || []),
].map((sourceFile) => {
if (sourceFile.includes(".java")) {
return sourceFile;
} else {
return folder + sourceFile;
}
});
const modifiedRecipeInfo = Object.assign(recipeInfo, {
sourceFiles: absoluteSourceFiles,
howDoI: firstToLower(recipeInfo.howDoI),
tags: ["Tag.TYPE_SCRIPT", ...(recipeInfo.tags || [])].map(
(tag) => "" + tag
),
});
return {
path: recipeInfo.url,
component: recipeInfo.url,
actionString: `./recipe/${recipeInfo.url}/${recipeInfo.url}`,
info: modifiedRecipeInfo,
};
};
const getSource = (node: ts.Node, source: ts.SourceFile): string => {
return source.getFullText().substring(node.pos, node.end);
};
const findRoute = (
node: ts.Node,
source: ts.SourceFile
): RecipeRouteWithAction | undefined => {
if (ts.isDecorator(node)) {
const decoratorNode = node;
if (
ts.isCallExpression(decoratorNode.expression) &&
ts.isIdentifier(decoratorNode.expression.expression) &&
decoratorNode.expression.expression.text == "recipeInfo"
) {
const src = getSource(decoratorNode.expression.arguments[0], source)
.replace(/\n/g, "")
.replace(/Tag.([A-Za-z]+)/, '"Tag.$1"');
const recipeInfo = eval("const a= " + src + ";a;");
const route = recipeToRoute(recipeInfo);
return route;
}
}
return undefined;
};
const visit = (
node: ts.Node,
source: ts.SourceFile,
routes: RecipeRouteWithAction[]
) => {
let route = findRoute(node, source);
if (route) {
return route;
}
node.forEachChild((child) => {
const r = visit(child, source, routes);
if (r) {
route = r;
}
});
return route;
};
interface RecipeRouteWithAction extends BaseRoute {
info: RecipeInfo;
actionString: string;
}
const writeIfChanged = (file, contents) => {
debug("Checking if write is needed for ", file);
debug("Contents:");
debug(contents);
if (fs.existsSync(file) && fs.readFileSync(file, "utf-8") == contents) {
debug("No write needed for ", file);
return;
}
debug("Writing ", file);
fs.writeFileSync(file, contents);
};
const tsFileGlobs = args[0];
const routes: RecipeRouteWithAction[] = [];
const g = path.resolve(__dirname, tsFileGlobs);
debug("Looking for " + g);
const files = glob.sync(g);
debug("Found files", files);
files.forEach((file) => {
debug("Scanning " + file);
const originalCode = fs.readFileSync(file, "utf8");
const source = ts.createSourceFile(
"my-view.ts",
originalCode,
ts.ScriptTarget.Latest
);
const r = visit(source, source, routes);
if (r) {
routes.push(r);
}
});
const frontend = path.resolve(__dirname, "frontend");
const recipeInfoJson = path.resolve(
__dirname,
"target",
"classes",
"ts-recipe-info.json"
);
const routesTsTemplateFile = path.resolve(__dirname, "ts-routes.ts.template");
const routesTsFile = path.resolve(frontend, "ts-routes.ts");
writeIfChanged(
recipeInfoJson,
JSON.stringify(
routes.map((route) => route.info),
null,
2
).replace(/"Tag.([A-Za-z_]*)"/g, '"$1"')
);
const routesJson = JSON.stringify(routes, null, 2);
const routesTS = routesJson
.replace(
/"actionString": "(.*)"/g,
'action: async() => { await import("$1");}'
)
.replace(/"Tag.([A-Za-z_]*)"/g, "Tag.$1")
.replace(/Tag.TYPE_SCRIPT/g, "Tag.TYPESCRIPT");
const tsRoutesTpl = fs.readFileSync(routesTsTemplateFile, "utf-8");
const tsRoutes = tsRoutesTpl.replace(
/export const tsRecipeRoutes: RecipeRoute\[\] = .*/s,
"export const tsRecipeRoutes: RecipeRoute[] = " + routesTS + ";"
);
writeIfChanged(routesTsFile, tsRoutes);