-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathast.ts
More file actions
115 lines (100 loc) · 2.77 KB
/
ast.ts
File metadata and controls
115 lines (100 loc) · 2.77 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
import * as ts from 'typescript';
import { Dirent } from 'fs';
import { readdir, readFile } from 'fs/promises';
import { pipe, filter, toArray, map, curry, add, toAsync, flat, take } from '@fxts/core';
/*
const code = `
import test from 'app/test'
import react from 'react'
import { get, chain } from 'lodash';
import { Dialog } from '@mui/dialog';
function foo() { }
`;
const run = () => {
const sourceFile = ts.createSourceFile('x.ts', code, ts.ScriptTarget.ES2015, true);
parse(sourceFile);
};
run();
*/
// base dir
const PROJECT_DIR = '/Users/hamtori/kurly/kurlymall-nx';
const IGNORE_DIR = [
'.git',
'.github',
'.next',
'.storybook',
'.idea',
'.husky',
'@types',
'.vscode',
'__mocks__',
'documents',
'fixtures',
'libs',
'nginx',
'node_modules',
'public',
'styles',
'stories',
'util',
'scripts',
];
const ALLOW_FILE_PATTERN = /.+(ts|tsx)$/;
const checkIsFile = (file: Dirent) => file.isFile();
const checkIsDir = (file: Dirent) => file.isDirectory();
const checkAllowedFilePattern = (fileName: string) => fileName.match(ALLOW_FILE_PATTERN);
const getFileName = (file: Dirent): string => file.name;
const extractAllTypescriptFiles = async (dirPath: string): Promise<string[]> => {
const result: string[] = [];
const currentDir = await readdir(dirPath, { withFileTypes: true });
const concatPath = curry(add)(`${dirPath}/`);
const files = pipe(
currentDir,
filter(checkIsFile),
map(getFileName),
filter(checkAllowedFilePattern),
map((p) => add(`${dirPath}/`, p)),
toArray,
);
const dirs = pipe(
currentDir,
filter(checkIsDir),
map(getFileName),
filter((fileName) => fileName !== 'node_modules'),
map((p) => add(`${dirPath}/`, p)),
toArray,
);
const child = await pipe(dirs, toAsync, map(extractAllTypescriptFiles), flat, toArray);
return pipe([files, child], flat, toArray);
};
const parse = (node: ts.Node) => {
if (ts.isImportDeclaration(node)) {
const moduleName = node.moduleSpecifier.getText().replace(/['"]/g, '');
}
// TODO: extract import statements
ts.forEachChild(node, parse);
};
const run = async () => {
const allTypescriptFiles = await extractAllTypescriptFiles(PROJECT_DIR);
const data = await pipe(
allTypescriptFiles,
toAsync,
map(async (fileName) => {
const buffer = await readFile(fileName);
return [fileName, buffer.toString()];
}),
map((args) => {
const [fileName, fileContent] = args;
const sourceFile = ts.createSourceFile(fileName, fileContent, ts.ScriptTarget.ES2015, true);
return [fileName, sourceFile];
}),
map((args) => {
const [fileName, sourceFile] = args;
return parse(sourceFile as ts.SourceFile);
}),
take(1),
toArray,
);
console.log(data);
};
run();