-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsourcemap.ts
More file actions
192 lines (179 loc) · 5.75 KB
/
Copy pathsourcemap.ts
File metadata and controls
192 lines (179 loc) · 5.75 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import { readdirSync, statSync } from "node:fs";
import { dirname, join } from "node:path";
import { chdir, exit } from "node:process";
import {
DEFAULT_EXCLUDE_DIRECTORIES,
LANG,
PARSER_TYPE,
PARSE_TYPE_NORMAL,
} from "./constant";
import logger from "./logger";
import type { Source } from "./protocol/map";
import { Dep, Dir, File } from "./sourcecode";
import { PARSER_VERSION, PROTOCOL_VERSION } from "./version";
const packageName = "package.json";
export class Project {
name: string;
path: string;
directory: string;
registry: Map<string, string>;
dirs: Map<string, Dir>;
files: Map<string, File>;
deps: Map<string, Dep>;
excludes: Set<string>;
allowDot: boolean;
constructor(
name: string,
path: string,
directory: string,
excludes: string,
) {
this.name = name;
this.path = path;
this.directory = directory;
this.registry = new Map();
this.dirs = new Map();
this.files = new Map();
this.deps = new Map();
this.excludes = new Set();
this.allowDot = true;
if (excludes === "") {
excludes = DEFAULT_EXCLUDE_DIRECTORIES;
}
const excludesList = excludes.split(",");
for (const excl of excludesList) {
const excl_ = excl.trim();
if (excl_ === ".*") {
this.allowDot = false;
} else {
this.excludes.add(join(excl_));
}
}
}
async scanRepository() {
// if errors happen, exit the process immediately
try {
chdir(this.path);
} catch (error) {
logger.error(
`fail to chdir to directory (${this.path}), error: ${error}`,
);
exit(1);
}
const projectPath = statSync(this.directory);
if (!projectPath.isDirectory()) return;
this.dirs.set(this.directory, new Dir(this.directory));
await this.walkDirectory(this.directory);
}
// walk the given directory
// find all the packages and files
async walkDirectory(dirPath: string) {
const entries = readdirSync(dirPath);
for (const entry of entries) {
const fullPath = join(dirPath, entry);
if (this.excludes.has(fullPath)) {
continue;
}
if (!this.allowDot && entry.startsWith(".")) {
continue;
}
const stats = statSync(fullPath);
if (stats.isDirectory()) {
// handle directories
this.dirs.set(fullPath, new Dir(fullPath));
await this.walkDirectory(fullPath);
} else {
// handle files
const dir = this.lookupDir(dirname(fullPath));
if (dir) {
if (entry.endsWith(packageName)) {
const name = await this.retrivePackageName(fullPath);
if (name) this.registry.set(name, dir.path);
}
const f = new File(
fullPath,
dir,
this.lookupDep.bind(this),
this.registry,
);
this.files.set(fullPath, f);
if (f.isSource()) {
dir.pkg = true;
this.deps.set(fullPath, new Dep(f));
if (f.name.startsWith("index.")) {
this.deps.set(dirPath, new Dep(f));
}
}
}
}
}
}
async retrivePackageName(filePath: string): Promise<string> {
const f = Bun.file(filePath);
const packageInfo = await f.json();
if (packageInfo?.name) {
return packageInfo.name;
}
return "";
}
// to parse all the files
parseAllFiles() {
for (const file of this.files.values()) {
if (file.ts || file.js) file.parse();
}
}
lookupDir(name: string): Dir | undefined {
return this.dirs.get(name);
}
lookupDep(name: string): Dep | undefined {
if (!name.endsWith(".ts") && !name.endsWith(".js")) {
const tsName = `${name}.ts`;
const jsName = `${name}.js`;
if (this.deps.has(tsName)) return this.deps.get(tsName);
if (this.deps.has(jsName)) return this.deps.get(jsName);
return undefined;
}
if (this.deps.has(name)) return this.deps.get(name);
return undefined;
}
dump(): Source {
const now = new Date();
const result: Source = {
name: this.name,
lang: LANG,
parser: `${PARSER_TYPE} ${PARSER_VERSION}`,
protocol: PROTOCOL_VERSION,
timestamp: now.toISOString(),
repository: "",
typ: PARSE_TYPE_NORMAL,
version: "",
pkgs: [],
files: [],
absts: [],
fns: [],
calls: [],
refs: [],
deps: [],
};
for (const d of this.dirs.values()) {
if (d.pkg) {
result.pkgs.push(d.dump());
}
}
for (const f of this.files.values()) {
if (f.ts || f.js || f.json) {
const fileDump = f.dump();
result.files.push(fileDump);
if (f.ts || f.js) {
for (const fn of f.fns.values()) {
result.fns.push(fn.dump());
}
}
}
}
for (const d of this.deps.values()) {
result.deps.push(d.dump());
}
return result;
}
}