-
Notifications
You must be signed in to change notification settings - Fork 60
Fix open compiled command for monorepos #592
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
cristianoc
merged 7 commits into
rescript-lang:master
from
fhammerschmidt:fix-open-compiled-monorepo
Oct 11, 2022
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
2f76dff
Fix the open compiled command for monorepos where the package's gets …
fhammerschmidt 323b70f
Add build-schema interface
fhammerschmidt 2710b34
Make getSuffixAndPathFragmentFromBsconfig more typesafe
fhammerschmidt d76f8aa
Strip down build-schema to what the extension actually uses
fhammerschmidt b995fad
Add explanatory comment to getSuffixAndPathFragmentFromBsconfig
fhammerschmidt 6830b29
Refactor lookup methods from utils into own file
fhammerschmidt 3d8b4d5
Fix partialFilePath parameter
fhammerschmidt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// This file has been generated from https://raw.githubusercontent.com/rescript-lang/rescript-compiler/master/docs/docson/build-schema.json | ||
// with https://app.quicktype.io/ and stripped down to what we actually need for the extension. | ||
|
||
export interface BuildSchema { | ||
name: string; | ||
namespace?: boolean | string; | ||
"package-specs"?: | ||
| Array<ModuleFormat | ModuleFormatObject> | ||
| ModuleFormat | ||
| ModuleFormatObject; | ||
suffix?: SuffixSpec; | ||
} | ||
|
||
export enum ModuleFormat { | ||
Commonjs = "commonjs", | ||
Es6 = "es6", | ||
Es6Global = "es6-global", | ||
} | ||
|
||
export interface ModuleFormatObject { | ||
"in-source"?: boolean; | ||
module: ModuleFormat; | ||
suffix?: SuffixSpec; | ||
} | ||
|
||
export enum SuffixSpec { | ||
BsCjs = ".bs.cjs", | ||
BsJS = ".bs.js", | ||
BsMjs = ".bs.mjs", | ||
Cjs = ".cjs", | ||
JS = ".js", | ||
Mjs = ".mjs", | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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); | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.