-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
162 lines (138 loc) · 4.04 KB
/
Copy pathindex.js
File metadata and controls
162 lines (138 loc) · 4.04 KB
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
"use strict";
import htmlParser from "node-html-parser";
const { parse } = htmlParser;
import Set from "es6-set";
import { standardHtmlElements, angularElements } from "./html-elements.js";
import { readFileSync } from "fs";
import { dirname, resolve } from "path";
import printTree from "print-tree";
import detectiveTypescript from "detective-typescript";
import glob from "glob";
import { generateSummarySync } from "./ts-file-summary.js";
import ts from "typescript";
const { ScriptTarget, ModuleKind } = ts;
export function getDependenciesFromHtml(src) {
const root = parse(src);
const dependencies = [...new Set(flatten(root.childNodes).flat())].filter(
(el) => !standardHtmlElements.includes(el) && !angularElements.includes(el)
);
return dependencies;
}
function flatten(nodes) {
const flat = [];
nodes.forEach((item) => {
if (item.childNodes && item.childNodes.length) {
flat.push(...flatten(item.childNodes));
}
});
const filteredNodes = nodes.map((n) => n.rawTagName).filter((_) => _);
if (filteredNodes.length) {
flat.push(filteredNodes);
}
return flat;
}
function getFilename(filename, parent) {
if (parent) {
const selector = getSelector(filename, parent);
if (selector) {
return selector;
}
}
const parts = filename.split("/");
return parts[parts.length - 1];
}
function getSelector(name, parent) {
const dir = dirname(parent);
const fullPath = resolve(dir, `${name}.ts`);
const types = generateSummarySync([fullPath], {
target: ScriptTarget.ES5,
module: ModuleKind.CommonJS,
});
const component = types
.map((type) => {
return (
type.decorators &&
type.decorators.find((decorator) => decorator.name === "Component")
);
})
.filter((_) => _)[0];
if (!component || !component.param || !component.param.selector) return;
return component.param.selector.replace("'", "");
}
function buildTree(filename, deps, getChild = (name) => ({ name })) {
return {
name: getFilename(filename),
children: deps.map(getChild),
};
}
function getDepsFromModule(filename) {
const src = readFileSync(filename, "utf8");
const moduleDeps = detectiveTypescript(src);
return moduleDeps.filter((dep) => dep.startsWith("."));
}
function getDepsForComponent(parent, name) {
const dir = dirname(parent);
const fullPath = resolve(dir, `${name}.html`);
try {
const componentSrc = readFileSync(fullPath, "utf8");
const children = getDependenciesFromHtml(componentSrc);
return {
name: getFilename(name, parent),
children: children.map((name) => ({ name })),
};
} catch (e) {
console.warn(e.message);
// Couldn't load the html, so assume no children
return {
name: getFilename(name, parent),
children: [],
};
}
}
const _glob = function globp(pattern, options) {
return new Promise((res, rej) =>
glob(pattern, options, (error, files) => {
if (error) return rej(error);
res(files);
})
);
};
export { _glob as glob };
export function getModuleTree(moduleFilename) {
const moduleDeps = getDepsFromModule(moduleFilename);
const components = moduleDeps.filter((dep) => dep.endsWith(".component"));
const moduleComponentTree = buildTree(moduleFilename, components, (name) => {
return getDepsForComponent(moduleFilename, name);
});
printTree(
moduleComponentTree,
(node) => node.name,
(node) => node.children
);
return moduleComponentTree;
}
export function getFlatModuleDeps(moduleFilename) {
const moduleComponentTree = getModuleTree(moduleFilename);
console.log("✔ " + moduleFilename + " done!");
return flattenTree(moduleComponentTree);
}
function flattenTree(child) {
if (child.children && child.children.length) {
return [
{
name: child.name,
children: child.children.map((c) => c.name),
},
...child.children.map((c) => flattenTree(c)).flat(),
];
}
return [
{
name: child.name,
children: [],
},
];
}
export function showModuleTree(moduleFilename) {
return getModuleTree(moduleFilename);
}