microsoft/typespec
Publicmirrored from https://github.com/microsoft/typespecAvailable
packages/bundle-uploader/src/index.ts
87lines · modecode
| 1 | import { AzureCliCredential } from "@azure/identity"; |
| 2 | import { findWorkspacePackagesNoCheck } from "@pnpm/find-workspace-packages"; |
| 3 | import { createTypeSpecBundle } from "@typespec/bundler"; |
| 4 | import { resolve } from "path"; |
| 5 | import { join as joinUnix } from "path/posix"; |
| 6 | import pc from "picocolors"; |
| 7 | import { parse } from "semver"; |
| 8 | import { TypeSpecBundledPackageUploader } from "./upload-browser-package.js"; |
| 9 | |
| 10 | function logInfo(...args: any[]) { |
| 11 | // eslint-disable-next-line no-console |
| 12 | console.log(...args); |
| 13 | } |
| 14 | |
| 15 | function logSuccess(message: string) { |
| 16 | logInfo(pc.green(`✔ ${message}`)); |
| 17 | } |
| 18 | |
| 19 | export interface BundleAndUploadPackagesOptions { |
| 20 | repoRoot: string; |
| 21 | /** |
| 22 | * List of packages to bundle and upload. |
| 23 | */ |
| 24 | packages: string[]; |
| 25 | |
| 26 | /** |
| 27 | * Name of the index for those packages. |
| 28 | */ |
| 29 | indexName: string; |
| 30 | |
| 31 | /** |
| 32 | * Version for the index |
| 33 | */ |
| 34 | indexVersion: string; |
| 35 | } |
| 36 | |
| 37 | /** Return the version of the package in major.minor.x format */ |
| 38 | export async function getPackageVersion(repoRoot: string, pkgName: string) { |
| 39 | const projects = await findWorkspacePackagesNoCheck(repoRoot); |
| 40 | |
| 41 | const project = projects.find((x) => x.manifest.name === pkgName); |
| 42 | if (project === undefined) { |
| 43 | throw new Error( |
| 44 | `Cannot get version for package: "${pkgName}", pnpm couldn't find a package with that name in the workspace` |
| 45 | ); |
| 46 | } |
| 47 | const version = parse(project.manifest.version); |
| 48 | if (version === null) { |
| 49 | throw new Error(`Couldn't resolve version from "${pkgName}": "${project.manifest.version}"`); |
| 50 | } |
| 51 | return `${version.major}.${version.minor}.x`; |
| 52 | } |
| 53 | |
| 54 | export async function bundleAndUploadPackages({ |
| 55 | repoRoot, |
| 56 | packages, |
| 57 | indexName, |
| 58 | indexVersion, |
| 59 | }: BundleAndUploadPackagesOptions) { |
| 60 | const allProjects = await findWorkspacePackagesNoCheck(repoRoot); |
| 61 | const projects = allProjects.filter((x) => packages.includes(x.manifest.name!)); |
| 62 | logInfo("Current index version:", indexVersion); |
| 63 | |
| 64 | const uploader = new TypeSpecBundledPackageUploader(new AzureCliCredential()); |
| 65 | await uploader.createIfNotExists(); |
| 66 | |
| 67 | const importMap: Record<string, string> = {}; |
| 68 | for (const project of projects) { |
| 69 | const bundle = await createTypeSpecBundle(resolve(repoRoot, project.dir)); |
| 70 | const manifest = bundle.manifest; |
| 71 | const result = await uploader.upload(bundle); |
| 72 | if (result.status === "uploaded") { |
| 73 | logSuccess(`Bundle for package ${manifest.name}@${manifest.version} uploaded.`); |
| 74 | } else { |
| 75 | logInfo(`Bundle for package ${manifest.name} already exist for version ${manifest.version}.`); |
| 76 | } |
| 77 | for (const [key, value] of Object.entries(result.imports)) { |
| 78 | importMap[joinUnix(project.manifest.name!, key)] = value; |
| 79 | } |
| 80 | } |
| 81 | logInfo(`Import map for ${indexVersion}:`, importMap); |
| 82 | await uploader.uploadIndex(indexName, { |
| 83 | version: indexVersion, |
| 84 | imports: importMap, |
| 85 | }); |
| 86 | logSuccess(`Updated index for version ${indexVersion}.`); |
| 87 | } |
| 88 | |