-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathlookup.ts
150 lines (123 loc) · 3.86 KB
/
lookup.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
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
import * as fs from "fs";
import * as path from "path";
import * as p from "vscode-languageserver-protocol";
import { BuildSchema, ModuleFormat, ModuleFormatObject } from "./buildSchema";
import * as c from "./constants";
const getCompiledFolderName = (moduleFormat: ModuleFormat): string => {
switch (moduleFormat) {
case "es6":
return "es6";
case "es6-global":
return "es6_global";
case "commonjs":
default:
return "js";
}
};
export const replaceFileExtension = (filePath: string, ext: string): string => {
let name = path.basename(filePath, path.extname(filePath));
return path.format({ dir: path.dirname(filePath), name, ext });
};
// Check if filePartialPath exists at directory and return the joined path,
// otherwise recursively check parent directories for it.
export const findFilePathFromProjectRoot = (
directory: p.DocumentUri | null, // This must be a directory and not a file!
filePartialPath: string
): null | p.DocumentUri => {
if (directory == null) {
return null;
}
let filePath: p.DocumentUri = path.join(directory, filePartialPath);
if (fs.existsSync(filePath)) {
return filePath;
}
let parentDir: p.DocumentUri = path.dirname(directory);
if (parentDir === directory) {
// reached the top
return null;
}
return findFilePathFromProjectRoot(parentDir, filePartialPath);
};
export const readBsConfig = (projDir: p.DocumentUri): BuildSchema | null => {
try {
let bsconfigFile = fs.readFileSync(
path.join(projDir, c.bsconfigPartialPath),
{ encoding: "utf-8" }
);
let result: BuildSchema = JSON.parse(bsconfigFile);
return result;
} catch (e) {
return null;
}
};
// Collect data from bsconfig to be able to find out the correct path of
// the compiled JS artifacts.
export const getSuffixAndPathFragmentFromBsconfig = (bsconfig: BuildSchema) => {
let pkgSpecs = bsconfig["package-specs"];
let pathFragment = "";
let module = c.bsconfigModuleDefault;
let moduleFormatObj: ModuleFormatObject = { module: module };
let suffix = c.bsconfigSuffixDefault;
if (pkgSpecs) {
if (
!Array.isArray(pkgSpecs) &&
typeof pkgSpecs !== "string" &&
pkgSpecs.module
) {
moduleFormatObj = pkgSpecs;
} else if (typeof pkgSpecs === "string") {
module = pkgSpecs;
} else if (Array.isArray(pkgSpecs) && pkgSpecs[0]) {
if (typeof pkgSpecs[0] === "string") {
module = pkgSpecs[0];
} else {
moduleFormatObj = pkgSpecs[0];
}
}
}
if (moduleFormatObj["module"]) {
module = moduleFormatObj["module"];
}
if (!moduleFormatObj["in-source"]) {
pathFragment = "lib/" + getCompiledFolderName(module);
}
if (moduleFormatObj.suffix) {
suffix = moduleFormatObj.suffix;
} else if (bsconfig.suffix) {
suffix = bsconfig.suffix;
}
return [suffix, pathFragment];
};
export const getFilenameFromBsconfig = (
projDir: string,
partialFilePath: string
): string | null => {
let bsconfig = readBsConfig(projDir);
if (!bsconfig) {
return null;
}
let [suffix, pathFragment] = getSuffixAndPathFragmentFromBsconfig(bsconfig);
let compiledPartialPath = replaceFileExtension(partialFilePath, suffix);
return path.join(projDir, pathFragment, compiledPartialPath);
};
// Monorepo helpers
export const getFilenameFromRootBsconfig = (
projDir: string,
partialFilePath: string
): string | null => {
let rootBsConfigPath = findFilePathFromProjectRoot(
path.join("..", projDir),
c.bsconfigPartialPath
);
if (!rootBsConfigPath) {
return null;
}
let rootBsconfig = readBsConfig(path.dirname(rootBsConfigPath));
if (!rootBsconfig) {
return null;
}
let [suffix, pathFragment] =
getSuffixAndPathFragmentFromBsconfig(rootBsconfig);
let compiledPartialPath = replaceFileExtension(partialFilePath, suffix);
return path.join(projDir, pathFragment, compiledPartialPath);
};