[tsp-client] Fix additional directory support for remote project scenario (#7483)

* fix additional directory support for url scenario

* changelog

* clean up

* newline
This commit is contained in:
catalinaperalta 2023-12-21 15:43:21 -08:00 коммит произвёл GitHub
Родитель 0c65b7cf7b
Коммит de40f27177
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
3 изменённых файлов: 22 добавлений и 17 удалений

Просмотреть файл

@ -6,7 +6,8 @@
- Use `resolveCompilerOptions` to get emitter configurations from tspconfig.yaml. - Use `resolveCompilerOptions` to get emitter configurations from tspconfig.yaml.
- Remove unused functions: `getEmitterOptions()`, `resolveCliOptions()`, `resolveImports()`. - Remove unused functions: `getEmitterOptions()`, `resolveCliOptions()`, `resolveImports()`.
- Fix `additionalDirectories` property outputted by the `init` command. - 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 ## 2023-12-8 - 0.2.0

Просмотреть файл

@ -7,8 +7,8 @@ import { mkdir, writeFile, cp, readFile } from "node:fs/promises";
import { addSpecFiles, checkoutCommit, cloneRepo, getRepoRoot, sparseCheckout } from "./git.js"; import { addSpecFiles, checkoutCommit, cloneRepo, getRepoRoot, sparseCheckout } from "./git.js";
import { fetch } from "./network.js"; import { fetch } from "./network.js";
import { parse as parseYaml } from "yaml"; import { parse as parseYaml } from "yaml";
import { joinPaths, normalizeSlashes, resolvePath } from "@typespec/compiler"; import { joinPaths, resolvePath } from "@typespec/compiler";
import { formatAdditionalDirectories } from "./utils.js"; import { formatAdditionalDirectories, getAdditionalDirectoryName } from "./utils.js";
async function sdkInit( 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"); throw new Error("Could not find local spec repo root, please make sure the path is correct");
} }
for (const dir of tspLocation.additionalDirectories!) { for (const dir of tspLocation.additionalDirectories!) {
Logger.info(`Syncing additional directory: ${dir}`); Logger.info(`Syncing additional directory: ${dir}`);;
let normalizedDir = normalizeSlashes(dir); await cp(joinPaths(localSpecRepoRoot, dir), joinPaths(tempRoot, getAdditionalDirectoryName(dir)), { recursive: true, filter: filter });
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 });
} }
} else { } else {
const cloneDir = joinPaths(repoRoot, "..", "sparse-spec"); const cloneDir = joinPaths(repoRoot, "..", "sparse-spec");
@ -142,10 +134,8 @@ async function syncTspFiles(outputDir: string, localSpecRepo?: string) {
await checkoutCommit(cloneDir, tspLocation.commit); await checkoutCommit(cloneDir, tspLocation.commit);
await cp(joinPaths(cloneDir, tspLocation.directory), srcDir, { recursive: true }); await cp(joinPaths(cloneDir, tspLocation.directory), srcDir, { recursive: true });
for (const dir of tspLocation.additionalDirectories!) { for (const dir of tspLocation.additionalDirectories!) {
const dirSplit = dir.split("/"); Logger.info(`Syncing additional directory: ${dir}`);
let projectName = dirSplit[dirSplit.length - 1]; await cp(joinPaths(cloneDir, dir), joinPaths(tempRoot, getAdditionalDirectoryName(dir)), { recursive: true });
const dirName = joinPaths(tempRoot, projectName!);
await cp(joinPaths(cloneDir, dir), dirName, { recursive: true });
} }
Logger.debug(`Removing sparse-checkout directory ${cloneDir}`); Logger.debug(`Removing sparse-checkout directory ${cloneDir}`);
await removeDirectory(cloneDir); await removeDirectory(cloneDir);

Просмотреть файл

@ -1,3 +1,5 @@
import { normalizeSlashes } from "@typespec/compiler";
export function formatAdditionalDirectories(additionalDirectories?: string[]): string { export function formatAdditionalDirectories(additionalDirectories?: string[]): string {
let additionalDirOutput = ""; let additionalDirOutput = "";
for (const dir of additionalDirectories ?? []) { for (const dir of additionalDirectories ?? []) {
@ -5,3 +7,15 @@ export function formatAdditionalDirectories(additionalDirectories?: string[]): s
} }
return additionalDirOutput; 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;
}