-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmodule-parsing-utils.ts
More file actions
167 lines (150 loc) · 6.55 KB
/
module-parsing-utils.ts
File metadata and controls
167 lines (150 loc) · 6.55 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
163
164
165
166
167
import { appendFileSync, promises, readFileSync, writeFileSync } from "fs";
import * as path from "path";
import * as ts from "typescript";
import { Statement } from "typescript";
const IGNORE_DIRS: string[] = ["src/stories"];
const IGNORE_FILES: string[] = ["src/index.ts", "src/utils.ts", "src/constants.ts"];
const IGNORE_FILE_NAMES: string[] = [];
export type TExport = {
path: string;
exportName: string;
};
export async function getFiles(pathUrl: string, rootFolderPath: string) {
const entries = await promises.readdir(pathUrl, { withFileTypes: true });
// Get files within the current directory and add a path key to the file objects
const files = entries
.filter(file => {
// console.log("file", file);
// console.log("file.name", file.name);
const filePath = path.join(pathUrl, file.name);
const isIgnoredFile = IGNORE_FILES.some(ignoredFile => {
// console.log("ignoredPath", path.join(rootFolderPath, ignoredPath));
// console.log("filePath", filePath);
return path.join(rootFolderPath, ignoredFile) === filePath;
});
// console.log("isIgnoredFile", isIgnoredFile);
const isIgnoredDir = IGNORE_DIRS.some(ignoredDir => {
// console.log("CHILD", filePath);
// console.log("PARENT", path.join(rootFolderPath, ignoredDir));
return isChildOf(filePath, path.join(rootFolderPath, ignoredDir));
});
// console.log("isIgnoredDir", isIgnoredDir);
const isIgnoredFileName = IGNORE_FILE_NAMES.some(ignoredFileName => ignoredFileName === file.name);
// console.log("isIgnoredFileName", isIgnoredFileName);
const fileExtension = path.extname(file.name).toLowerCase();
const isExtensionTs = fileExtension === ".ts" || fileExtension === ".tsx";
// console.log("isExtensionTs", file.name, isExtensionTs);
// console.log("file.isDirectory()", file.isDirectory());
return !isIgnoredFile && !isIgnoredDir && !isIgnoredFileName && isExtensionTs && !file.isDirectory();
})
.map(file => path.join(pathUrl, file.name));
// Get folders within the current directory
const folders = entries.filter(folder => folder.isDirectory());
// Add the found files within the subdirectory to the files array
for (const folder of folders) {
files.push(...(await getFiles(path.join(pathUrl, folder.name), rootFolderPath)));
}
return files;
}
export function generateTypedExportsFile(allExports: TExport[], fileName: string) {
// console.log(fileName);
const sourceFile: ts.SourceFile = ts.createSourceFile(
fileName,
readFileSync(fileName).toString(),
ts.ScriptTarget.ES2015,
/*setParentNodes */ true
);
sourceFile.statements.forEach(statement => {
// console.log("statement.kind", statement.kind);
let name = "";
if (ts.isFunctionDeclaration(statement)) {
// console.log("isFunctionDeclaration");
name = statement.name!.text;
} else if (ts.isVariableStatement(statement)) {
// console.log("isVariableStatement");
name = statement!.declarationList.declarations[0].name.getText(sourceFile);
} else if (ts.isClassDeclaration(statement)) {
// console.log("isClassDeclaration");
name = statement.name!.text;
} else if (ts.isEnumDeclaration(statement)) {
// console.log("isEnumDeclaration");
name = statement.name.text;
} else if (ts.isInterfaceDeclaration(statement)) {
// console.log("isInterfaceDeclaration");
name = statement.name.text;
} else if (ts.isTypeAliasDeclaration(statement)) {
// console.log("isTypeAliasDeclaration");
name = statement.name.text;
}
const comment = statement.getFullText().slice(0, statement.getLeadingTriviaWidth());
// console.log(fileName, "comment", comment);
const hasIgnoreComment = comment.toLowerCase().includes("@ignore");
// console.log("hasIgnoreComment", hasIgnoreComment);
const hasExport = testHasExport(statement);
const isNotAbstract = testNotAbstract(statement);
// console.log("hasExport", hasExport);
if (name && hasExport && !hasIgnoreComment) {
allExports.push({
exportName: name,
path: fileName
});
}
});
}
function testHasExport(statement: Statement) {
let hasExport = false;
ts.forEachChild(statement, childNode => {
if (childNode.kind === ts.SyntaxKind.ExportKeyword) {
hasExport = true;
}
});
return hasExport;
}
function testNotAbstract(statement: Statement) {
let isAbstract = false;
ts.forEachChild(statement, childNode => {
if (childNode.kind === ts.SyntaxKind.AbstractKeyword) {
isAbstract = true;
}
});
return !isAbstract;
}
function isChildOf(child: string, parent: string) {
if (child === parent) {
return true;
}
const parentTokens = parent.split(path.sep).filter(i => i.length);
const childTokens = child.split(path.sep).filter(i => i.length);
// console.log("parent", parent);
// console.log("parentTokens", parentTokens[0], parentTokens[1]);
// console.log("child", child);
// console.log("childTokens", childTokens[0], childTokens[1]);
return parentTokens.every((t, i) => {
return childTokens[i] === t;
});
}
export function writeAllAsNamedExports(
allExports: TExport[],
outputFile: string,
prefixLength: number,
relativePrefix: string
) {
writeFileSync(
outputFile,
"// This file is generated by generate-exports-file.ts script, do not edit it manually!\n"
);
allExports.forEach(el => {
writeImport(el.exportName, el.path);
});
function writeImport(elementName: string, importPath: string) {
const fileExtension = path.extname(importPath).toLowerCase();
const lengthWithoutPrefixAndExtension = importPath.length - prefixLength - fileExtension.length;
let relativePath = `${relativePrefix}${importPath.substr(prefixLength, lengthWithoutPrefixAndExtension)}`;
const isWindows = process.platform === "win32";
if (isWindows) {
relativePath = relativePath.split(path.sep).join(path.posix.sep);
}
const textToWrite = `export { ${elementName} } from "${relativePath}";\n`;
appendFileSync(outputFile, textToWrite);
}
}