|
| 1 | +import * as fs from "fs"; |
| 2 | +import * as path from "path"; |
| 3 | +import * as p from "vscode-languageserver-protocol"; |
| 4 | + |
| 5 | +import { BuildSchema, ModuleFormat, ModuleFormatObject } from "./buildSchema"; |
| 6 | +import * as c from "./constants"; |
| 7 | + |
| 8 | +const getCompiledFolderName = (moduleFormat: ModuleFormat): string => { |
| 9 | + switch (moduleFormat) { |
| 10 | + case "es6": |
| 11 | + return "es6"; |
| 12 | + case "es6-global": |
| 13 | + return "es6_global"; |
| 14 | + case "commonjs": |
| 15 | + default: |
| 16 | + return "js"; |
| 17 | + } |
| 18 | +}; |
| 19 | + |
| 20 | +export const replaceFileExtension = (filePath: string, ext: string): string => { |
| 21 | + let name = path.basename(filePath, path.extname(filePath)); |
| 22 | + return path.format({ dir: path.dirname(filePath), name, ext }); |
| 23 | +}; |
| 24 | + |
| 25 | +// Check if filePartialPath exists at directory and return the joined path, |
| 26 | +// otherwise recursively check parent directories for it. |
| 27 | +export const findFilePathFromProjectRoot = ( |
| 28 | + directory: p.DocumentUri | null, // This must be a directory and not a file! |
| 29 | + filePartialPath: string |
| 30 | +): null | p.DocumentUri => { |
| 31 | + if (directory == null) { |
| 32 | + return null; |
| 33 | + } |
| 34 | + |
| 35 | + let filePath: p.DocumentUri = path.join(directory, filePartialPath); |
| 36 | + if (fs.existsSync(filePath)) { |
| 37 | + return filePath; |
| 38 | + } |
| 39 | + |
| 40 | + let parentDir: p.DocumentUri = path.dirname(directory); |
| 41 | + if (parentDir === directory) { |
| 42 | + // reached the top |
| 43 | + return null; |
| 44 | + } |
| 45 | + |
| 46 | + return findFilePathFromProjectRoot(parentDir, filePartialPath); |
| 47 | +}; |
| 48 | + |
| 49 | +export const readBsConfig = (projDir: p.DocumentUri): BuildSchema | null => { |
| 50 | + try { |
| 51 | + let bsconfigFile = fs.readFileSync( |
| 52 | + path.join(projDir, c.bsconfigPartialPath), |
| 53 | + { encoding: "utf-8" } |
| 54 | + ); |
| 55 | + |
| 56 | + let result: BuildSchema = JSON.parse(bsconfigFile); |
| 57 | + return result; |
| 58 | + } catch (e) { |
| 59 | + return null; |
| 60 | + } |
| 61 | +}; |
| 62 | + |
| 63 | +// Collect data from bsconfig to be able to find out the correct path of |
| 64 | +// the compiled JS artifacts. |
| 65 | +export const getSuffixAndPathFragmentFromBsconfig = (bsconfig: BuildSchema) => { |
| 66 | + let pkgSpecs = bsconfig["package-specs"]; |
| 67 | + let pathFragment = ""; |
| 68 | + let module = c.bsconfigModuleDefault; |
| 69 | + let moduleFormatObj: ModuleFormatObject = { module: module }; |
| 70 | + let suffix = c.bsconfigSuffixDefault; |
| 71 | + |
| 72 | + if (pkgSpecs) { |
| 73 | + if ( |
| 74 | + !Array.isArray(pkgSpecs) && |
| 75 | + typeof pkgSpecs !== "string" && |
| 76 | + pkgSpecs.module |
| 77 | + ) { |
| 78 | + moduleFormatObj = pkgSpecs; |
| 79 | + } else if (typeof pkgSpecs === "string") { |
| 80 | + module = pkgSpecs; |
| 81 | + } else if (Array.isArray(pkgSpecs) && pkgSpecs[0]) { |
| 82 | + if (typeof pkgSpecs[0] === "string") { |
| 83 | + module = pkgSpecs[0]; |
| 84 | + } else { |
| 85 | + moduleFormatObj = pkgSpecs[0]; |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + if (moduleFormatObj["module"]) { |
| 91 | + module = moduleFormatObj["module"]; |
| 92 | + } |
| 93 | + |
| 94 | + if (!moduleFormatObj["in-source"]) { |
| 95 | + pathFragment = "lib/" + getCompiledFolderName(module); |
| 96 | + } |
| 97 | + |
| 98 | + if (moduleFormatObj.suffix) { |
| 99 | + suffix = moduleFormatObj.suffix; |
| 100 | + } else if (bsconfig.suffix) { |
| 101 | + suffix = bsconfig.suffix; |
| 102 | + } |
| 103 | + |
| 104 | + return [suffix, pathFragment]; |
| 105 | +}; |
| 106 | + |
| 107 | +export const getFilenameFromBsconfig = ( |
| 108 | + projDir: string, |
| 109 | + partialFilePath: string |
| 110 | +): string | null => { |
| 111 | + let bsconfig = readBsConfig(projDir); |
| 112 | + |
| 113 | + if (!bsconfig) { |
| 114 | + return null; |
| 115 | + } |
| 116 | + |
| 117 | + let [suffix, pathFragment] = getSuffixAndPathFragmentFromBsconfig(bsconfig); |
| 118 | + |
| 119 | + let compiledPartialPath = replaceFileExtension(partialFilePath, suffix); |
| 120 | + |
| 121 | + return path.join(projDir, pathFragment, compiledPartialPath); |
| 122 | +}; |
| 123 | + |
| 124 | +// Monorepo helpers |
| 125 | +export const getFilenameFromRootBsconfig = ( |
| 126 | + projDir: string, |
| 127 | + partialFilePath: string |
| 128 | +): string | null => { |
| 129 | + let rootBsConfigPath = findFilePathFromProjectRoot( |
| 130 | + path.join("..", projDir), |
| 131 | + c.bsconfigPartialPath |
| 132 | + ); |
| 133 | + |
| 134 | + if (!rootBsConfigPath) { |
| 135 | + return null; |
| 136 | + } |
| 137 | + |
| 138 | + let rootBsconfig = readBsConfig(path.dirname(rootBsConfigPath)); |
| 139 | + |
| 140 | + if (!rootBsconfig) { |
| 141 | + return null; |
| 142 | + } |
| 143 | + |
| 144 | + let [suffix, pathFragment] = |
| 145 | + getSuffixAndPathFragmentFromBsconfig(rootBsconfig); |
| 146 | + |
| 147 | + let compiledPartialPath = replaceFileExtension(partialFilePath, suffix); |
| 148 | + |
| 149 | + return path.join(projDir, pathFragment, compiledPartialPath); |
| 150 | +}; |
0 commit comments