-
Notifications
You must be signed in to change notification settings - Fork 245
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
get entrypoint file #6200
base: main
Are you sure you want to change the base?
get entrypoint file #6200
Changes from all commits
b49c25c
23d536c
30dcc8e
14eaf48
807cfaa
98d72bc
e0065d9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
changeKind: internal | ||
packages: | ||
- typespec-vscode | ||
--- | ||
|
||
Develop strategies to obtain the entry point file. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,28 +11,9 @@ export async function getEntrypointTspFile(tspPath: string): Promise<string | un | |
let baseDir = isFilePath ? getDirectoryPath(tspPath) : tspPath; | ||
|
||
while (true) { | ||
const pkgPath = path.resolve(baseDir, "package.json"); | ||
if (await isFile(pkgPath)) { | ||
/* get the tspMain from package.json. */ | ||
try { | ||
const data = await readFile(pkgPath, { encoding: "utf-8" }); | ||
const packageJson = JSON.parse(data); | ||
const tspMain = packageJson.tspMain; | ||
if (typeof tspMain === "string") { | ||
const tspMainFile = path.resolve(baseDir, tspMain); | ||
if (await isFile(tspMainFile)) { | ||
logger.debug(`tspMain file ${tspMainFile} selected as entrypoint file.`); | ||
return tspMainFile; | ||
} | ||
} | ||
} catch (error) { | ||
logger.error(`An error occurred while reading the package.json file ${pkgPath}`, [error]); | ||
} | ||
} | ||
|
||
const mainTspFile = path.resolve(baseDir, StartFileName); | ||
if (await isFile(mainTspFile)) { | ||
return mainTspFile; | ||
const entrypointTspFile = await getEntrypointTspFileInFolder(baseDir); | ||
if (entrypointTspFile) { | ||
return entrypointTspFile; | ||
} | ||
const parentDir = getDirectoryPath(baseDir); | ||
if (parentDir === baseDir) { | ||
|
@@ -41,6 +22,9 @@ export async function getEntrypointTspFile(tspPath: string): Promise<string | un | |
baseDir = parentDir; | ||
} | ||
|
||
/* if there is no defined main entry point tsp file, return the selected tsp file. */ | ||
if (isFilePath) return tspPath; | ||
|
||
return undefined; | ||
} | ||
|
||
|
@@ -53,3 +37,56 @@ export async function TraverseMainTspFileInWorkspace() { | |
.map((uri) => normalizeSlashes(uri.fsPath)), | ||
); | ||
} | ||
|
||
export async function TraverseEntrypointTspFileInWorkspace(): Promise<string[]> { | ||
const targetFolders = await vscode.workspace | ||
.findFiles(`**/${StartFileName}`, "**/node_modules/**") | ||
.then((uris) => { | ||
const directories = new Set<string>(); | ||
uris | ||
.filter((uri) => uri.scheme === "file" && !uri.fsPath.includes("node_modules")) | ||
.map((uri) => directories.add(getDirectoryPath(normalizeSlashes(uri.fsPath)))); | ||
return Array.from(directories); | ||
}); | ||
const entrypointTspFiles: string[] = []; | ||
|
||
for (const folder of targetFolders) { | ||
while (true) { | ||
const entrypointTspFile = await getEntrypointTspFileInFolder(folder); | ||
if (entrypointTspFile) { | ||
entrypointTspFiles.push(entrypointTspFile); | ||
} | ||
} | ||
} | ||
|
||
return entrypointTspFiles; | ||
} | ||
|
||
async function getEntrypointTspFileInFolder(folder: string): Promise<string | undefined> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you shouldn't rewrite that this is not the actual logic, you are missing There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. +1 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agree. @timotheeguerin , maybe you can help to also expose the "resolveTypeSpecEntrypoint" too when you are separating the PR for init bundling if it's easy. thx. |
||
if (await isFile(folder)) return undefined; | ||
const pkgPath = path.resolve(folder, "package.json"); | ||
if (await isFile(pkgPath)) { | ||
/* get the tspMain from package.json. */ | ||
try { | ||
const data = await readFile(pkgPath, { encoding: "utf-8" }); | ||
const packageJson = JSON.parse(data); | ||
const tspMain = packageJson.tspMain; | ||
if (typeof tspMain === "string") { | ||
const tspMainFile = path.resolve(folder, tspMain); | ||
if (await isFile(tspMainFile)) { | ||
logger.debug(`tspMain file ${tspMainFile} selected as entrypoint file.`); | ||
return tspMainFile; | ||
} | ||
} | ||
} catch (error) { | ||
logger.error(`An error occurred while reading the package.json file ${pkgPath}`, [error]); | ||
} | ||
} | ||
|
||
const mainTspFile = path.resolve(folder, StartFileName); | ||
if (await isFile(mainTspFile)) { | ||
return mainTspFile; | ||
} | ||
|
||
return undefined; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you document what gets resolved here?
Does it now only look for a pacakge.json first and then a main.tsp or it just look up for the first it finds