diff --git a/tools/tsp-client/CHANGELOG.md b/tools/tsp-client/CHANGELOG.md index b560c89d7..8fb9d201f 100644 --- a/tools/tsp-client/CHANGELOG.md +++ b/tools/tsp-client/CHANGELOG.md @@ -6,7 +6,8 @@ - Use `resolveCompilerOptions` to get emitter configurations from tspconfig.yaml. - Remove unused functions: `getEmitterOptions()`, `resolveCliOptions()`, `resolveImports()`. - Fix `additionalDirectories` property outputted by the `init` command. -- Fixed support for local specs using additional directories. +- Fixed support for local and remote specs using additional directories. +- Switched to use path helper functions from `@typespec/compiler`. ## 2023-12-8 - 0.2.0 diff --git a/tools/tsp-client/src/index.ts b/tools/tsp-client/src/index.ts index 0432f8aaf..30a8fb415 100644 --- a/tools/tsp-client/src/index.ts +++ b/tools/tsp-client/src/index.ts @@ -7,8 +7,8 @@ import { mkdir, writeFile, cp, readFile } from "node:fs/promises"; import { addSpecFiles, checkoutCommit, cloneRepo, getRepoRoot, sparseCheckout } from "./git.js"; import { fetch } from "./network.js"; import { parse as parseYaml } from "yaml"; -import { joinPaths, normalizeSlashes, resolvePath } from "@typespec/compiler"; -import { formatAdditionalDirectories } from "./utils.js"; +import { joinPaths, resolvePath } from "@typespec/compiler"; +import { formatAdditionalDirectories, getAdditionalDirectoryName } from "./utils.js"; async function sdkInit( @@ -116,16 +116,8 @@ async function syncTspFiles(outputDir: string, localSpecRepo?: string) { throw new Error("Could not find local spec repo root, please make sure the path is correct"); } for (const dir of tspLocation.additionalDirectories!) { - Logger.info(`Syncing additional directory: ${dir}`); - let normalizedDir = normalizeSlashes(dir); - if (normalizedDir.slice(-1) === "/") { - normalizedDir = normalizedDir.slice(0, -1); - } - const finalDirName = normalizedDir.split("/").pop(); - if (!finalDirName) { - throw new Error(`Could not find a final directory for the following value: ${normalizedDir}`); - } - await cp(joinPaths(localSpecRepoRoot, dir), joinPaths(tempRoot, finalDirName), { recursive: true, filter: filter }); + Logger.info(`Syncing additional directory: ${dir}`);; + await cp(joinPaths(localSpecRepoRoot, dir), joinPaths(tempRoot, getAdditionalDirectoryName(dir)), { recursive: true, filter: filter }); } } else { const cloneDir = joinPaths(repoRoot, "..", "sparse-spec"); @@ -142,10 +134,8 @@ async function syncTspFiles(outputDir: string, localSpecRepo?: string) { await checkoutCommit(cloneDir, tspLocation.commit); await cp(joinPaths(cloneDir, tspLocation.directory), srcDir, { recursive: true }); for (const dir of tspLocation.additionalDirectories!) { - const dirSplit = dir.split("/"); - let projectName = dirSplit[dirSplit.length - 1]; - const dirName = joinPaths(tempRoot, projectName!); - await cp(joinPaths(cloneDir, dir), dirName, { recursive: true }); + Logger.info(`Syncing additional directory: ${dir}`); + await cp(joinPaths(cloneDir, dir), joinPaths(tempRoot, getAdditionalDirectoryName(dir)), { recursive: true }); } Logger.debug(`Removing sparse-checkout directory ${cloneDir}`); await removeDirectory(cloneDir); diff --git a/tools/tsp-client/src/utils.ts b/tools/tsp-client/src/utils.ts index 3abf8dcfb..32d021c32 100644 --- a/tools/tsp-client/src/utils.ts +++ b/tools/tsp-client/src/utils.ts @@ -1,3 +1,5 @@ +import { normalizeSlashes } from "@typespec/compiler"; + export function formatAdditionalDirectories(additionalDirectories?: string[]): string { let additionalDirOutput = ""; for (const dir of additionalDirectories ?? []) { @@ -5,3 +7,15 @@ export function formatAdditionalDirectories(additionalDirectories?: string[]): s } return additionalDirOutput; } + +export function getAdditionalDirectoryName(dir: string): string { + let normalizedDir = normalizeSlashes(dir); + if (normalizedDir.slice(-1) === "/") { + normalizedDir = normalizedDir.slice(0, -1); + } + const finalDirName = normalizedDir.split("/").pop(); + if (!finalDirName) { + throw new Error(`Could not find a final directory for the following value: ${normalizedDir}`); + } + return finalDirName; +}