in tools/tsp-client/src/commands.ts [33:168]
export async function initCommand(argv: any) {
let outputDir = argv["output-dir"];
let tspConfig = argv["tsp-config"];
const skipSyncAndGenerate = argv["skip-sync-and-generate"];
const commit = argv["commit"] ?? "<replace with your value>";
const repo = argv["repo"] ?? "<replace with your value>";
const repoRoot = await getRepoRoot(outputDir);
const emitterPackageOverride = resolveEmitterPathFromArgs(argv);
const emitter = await getEmitterFromRepoConfig(emitterPackageOverride ?? joinPaths(repoRoot, defaultRelativeEmitterPackageJsonPath));
if (!emitter) {
throw new Error("Couldn't find emitter-package.json in the repo");
}
let isUrl = true;
if (argv["local-spec-repo"]) {
const localSpecRepo = argv["local-spec-repo"];
if (!(await doesFileExist(localSpecRepo))) {
throw new Error(`Local spec repo not found: ${localSpecRepo}`);
}
isUrl = false;
tspConfig = localSpecRepo;
} else if (await doesFileExist(tspConfig)) {
isUrl = false;
}
if (isUrl) {
// URL scenario
const resolvedConfigUrl = resolveTspConfigUrl(tspConfig);
const cloneDir = await makeSparseSpecDir(repoRoot);
Logger.debug(`Created temporary sparse-checkout directory ${cloneDir}`);
Logger.debug(`Cloning repo to ${cloneDir}`);
await cloneRepo(outputDir, cloneDir, `https://github.com/${resolvedConfigUrl.repo}.git`);
await sparseCheckout(cloneDir);
const tspConfigPath = joinPaths(resolvedConfigUrl.path, "tspconfig.yaml");
await addSpecFiles(cloneDir, tspConfigPath);
await checkoutCommit(cloneDir, resolvedConfigUrl.commit);
let data;
try {
data = await readFile(joinPaths(cloneDir, tspConfigPath), "utf8");
} catch (err) {
throw new Error(`Could not read tspconfig.yaml at ${tspConfigPath}. Error: ${err}`);
}
if (!data) {
throw new Error(`tspconfig.yaml is empty at ${tspConfigPath}`);
}
const configYaml = parseYaml(data);
const serviceDir = getServiceDir(configYaml, emitter);
const packageDir: string | undefined = configYaml?.options?.[emitter]?.["package-dir"];
if (!packageDir) {
throw new Error(
`Missing package-dir in ${emitter} options of tspconfig.yaml. Please refer to https://github.com/Azure/azure-rest-api-specs/blob/main/specification/contosowidgetmanager/Contoso.WidgetManager/tspconfig.yaml for the right schema.`,
);
}
const newPackageDir = joinPaths(outputDir, serviceDir, packageDir!);
await mkdir(newPackageDir, { recursive: true });
const tspLocationData: TspLocation = {
directory: resolvedConfigUrl.path,
commit: resolvedConfigUrl.commit,
repo: resolvedConfigUrl.repo,
additionalDirectories:
configYaml?.options?.["@azure-tools/typespec-client-generator-cli"]?.[
"additionalDirectories"
] ?? [],
};
if (argv["emitter-package-json-path"]) {
tspLocationData.emitterPackageJsonPath = argv["emitter-package-json-path"];
}
await writeTspLocationYaml(tspLocationData, newPackageDir);
Logger.debug(`Removing sparse-checkout directory ${cloneDir}`);
await removeDirectory(cloneDir);
outputDir = newPackageDir;
} else {
// Local directory scenario
if (!tspConfig.endsWith("tspconfig.yaml")) {
tspConfig = joinPaths(tspConfig, "tspconfig.yaml");
}
let data;
try {
data = await readFile(tspConfig, "utf8");
} catch (err) {
throw new Error(`Could not read tspconfig.yaml at ${tspConfig}`);
}
if (!data) {
throw new Error(`tspconfig.yaml is empty at ${tspConfig}`);
}
const configYaml = parseYaml(data);
const serviceDir = getServiceDir(configYaml, emitter);
const packageDir = configYaml?.options?.[emitter]?.["package-dir"];
if (!packageDir) {
throw new Error(
`Missing package-dir in ${emitter} options of tspconfig.yaml. Please refer to https://github.com/Azure/azure-rest-api-specs/blob/main/specification/contosowidgetmanager/Contoso.WidgetManager/tspconfig.yaml for the right schema.`,
);
}
const newPackageDir = joinPaths(outputDir, serviceDir, packageDir);
await mkdir(newPackageDir, { recursive: true });
tspConfig = tspConfig.replaceAll("\\", "/");
const matchRes = tspConfig.match(".*/(?<path>specification/.*)/tspconfig.yaml$");
var directory = "";
if (matchRes) {
if (matchRes.groups) {
directory = matchRes.groups!["path"]!;
}
}
const tspLocationData: TspLocation = {
directory: directory,
commit: commit ?? "",
repo: repo ?? "",
additionalDirectories:
configYaml?.options?.["@azure-tools/typespec-client-generator-cli"]?.[
"additionalDirectories"
] ?? [],
};
const emitterPackageOverride = resolveEmitterPathFromArgs(argv);
if (emitterPackageOverride) {
// store relative path to repo root
tspLocationData.emitterPackageJsonPath = relative(repoRoot, emitterPackageOverride);
}
await writeTspLocationYaml(tspLocationData, newPackageDir);
outputDir = newPackageDir;
}
if (!skipSyncAndGenerate) {
// update argv in case anything changed and call into sync and generate
argv["output-dir"] = outputDir;
if (!isUrl) {
// If the local spec repo is provided, we need to update the local-spec-repo argument for syncing as well
argv["local-spec-repo"] = tspConfig;
}
await syncCommand(argv);
await generateCommand(argv);
}
return outputDir;
}