/
lookup-closest-module.ts
60 lines (53 loc) · 2.05 KB
/
lookup-closest-module.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
import { readdirSync, readFileSync } from 'fs';
import { normalize, resolve } from 'path';
import { getSlashRegexpFromPlatform } from './slash';
const slashRegexp = getSlashRegexpFromPlatform();
/**
* Lookup for the closest module component from the destination path.
* @param fullDestinationPath
* @param isMain
*/
export function lookupClosestModule(fullDestinationPath: string, isMain = true): string | undefined {
let resolvedModulePath = undefined;
const isRootDir = !!readdirSync(fullDestinationPath, { withFileTypes: true }).some(
(component) => component.name === 'package.json'
);
if (isRootDir) {
return resolvedModulePath;
}
const components = readdirSync(fullDestinationPath, { withFileTypes: true });
const files = components.filter((component) => component.isFile());
for (const file of files) {
const componentFullDestinationPath = resolve(fullDestinationPath, file.name);
const componentContent = readFileSync(componentFullDestinationPath).toString();
const containsModuleDecorator = !!componentContent.match(/@Module\(/g);
if (containsModuleDecorator) {
resolvedModulePath = componentFullDestinationPath;
break;
}
}
const directories = components.filter((component) => component.isDirectory());
if (!resolvedModulePath) {
for (const directory of directories) {
const childFullDestinationPath = resolve(fullDestinationPath, directory.name);
resolvedModulePath = lookupClosestModule(childFullDestinationPath, false);
if (resolvedModulePath) {
break;
}
}
}
/*
* At this point, the module was not found in the current and children directories,
* so we will look into the parent directory until the root is reached.
*/
if (isMain && !resolvedModulePath) {
const parentFullDestinationPath = fullDestinationPath.split(slashRegexp).slice(0, -1).join('/');
if (parentFullDestinationPath) {
const modulePath = lookupClosestModule(parentFullDestinationPath);
if (modulePath) {
resolvedModulePath = modulePath;
}
}
}
return resolvedModulePath ? normalize(resolvedModulePath) : resolvedModulePath;
}