Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion common/tools/dev-tool/src/util/samples/generation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,31 @@ function jsonify(value: unknown) {
return output;
}

/**
* Rewrites relative `.ts` import/export specifiers in TypeScript sample source
* text to use `.js` extensions.
*
* Samples authored under `samples-dev/` use `.ts` extensions on relative imports
* so that Node's native TypeScript loader can resolve sibling sample files when
* developers run them directly. Published TypeScript samples, however, are
* compiled with `tsc` into `dist/` and must reference the emitted `.js` files
* to be runnable. This helper performs that rewrite on the copied sample text.
*
* Matches the following forms:
* - `import ... from "./foo.ts"`
* - `import "./foo.ts"`
* - `import("./foo.ts")` (dynamic import)
* - `export ... from "./foo.ts"`
*
* @param content - the raw module source text
*/
function rewriteRelativeTsImports(content: string): string {
return content.replace(
/(\b(?:import|export|from)\s*\(?\s*)(['"])(\.\.?\/[^'"]*?)\.ts(['"])/g,
"$1$2$3.js$4",
);
Comment on lines +347 to +351
}
Comment on lines +329 to +352

/**
* Checks if a file exists.
* @param filePath - The path to the file
Expand Down Expand Up @@ -368,6 +393,7 @@ export async function createTsconfig(projectInfo: ProjectInfo): Promise<string>
delete tsconfig.compilerOptions.inlineSources;
delete tsconfig.compilerOptions.sourceMap;
delete tsconfig.compilerOptions.verbatimModuleSyntax;
delete tsconfig.compilerOptions.allowImportingTsExtensions;
tsconfig.include = ["./src"];
tsconfig.compilerOptions.outDir = "./dist";
tsconfig.compilerOptions.rootDir = "./src";
Expand Down Expand Up @@ -429,7 +455,7 @@ export async function makeSamplesFactory(
function postProcess(moduleText: string | Buffer): string {
const content = Buffer.isBuffer(moduleText) ? moduleText.toString("utf8") : moduleText;
return (
content
rewriteRelativeTsImports(content)
.replace(new RegExp(`^\\s*\\*\\s*@${AZSDK_META_TAG_PREFIX}.*\n`, "gm"), "")
// We also need to clean up extra blank lines that might be left behind by
// removing azsdk tags. These regular expressions are extremely frustrating
Expand Down
1 change: 1 addition & 0 deletions common/tools/dev-tool/src/util/samples/processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ export async function processSources(
result.summary === undefined &&
!importedRelativeModules.has(result.relativeSourcePath.replace(/\.ts$/, "")) &&
!importedRelativeModules.has(result.relativeSourcePath.replace(/\.ts$/, ".js")) &&
!importedRelativeModules.has(result.relativeSourcePath) &&
!result.azSdkTags.util
) {
fail(
Expand Down
27 changes: 26 additions & 1 deletion common/tools/dev-tool/src/util/samples/transforms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,11 @@ export function importDeclarationToCommonJs(
factory.createCallExpression(
factory.createIdentifier("require"),
/* typeArguments: */ undefined,
[factory.createStringLiteral((decl.moduleSpecifier as ts.StringLiteral).text)],
[
factory.createStringLiteral(
convertTsExtensionToJs((decl.moduleSpecifier as ts.StringLiteral).text),
),
],
);

if (!decl.importClause) {
Expand Down Expand Up @@ -241,6 +245,27 @@ export function isNodeBuiltin(moduleSpecifier: string): boolean {
*/
export const isRelativePath = (input: string): boolean => /^\.\.?[/\\]/.test(input);

/**
* Rewrites a relative module specifier ending in `.ts` to use `.js` instead.
*
* Sample sources under `samples-dev/` use `.ts` extensions on relative imports
* so that Node's native TypeScript loader can resolve sibling sample files
* directly. When publishing the camera-ready samples, both the TypeScript and
* JavaScript outputs are emitted as `.js`, so the import specifiers must be
* rewritten to match.
*
* Non-relative specifiers (package imports, node builtins) and specifiers that
* are not `.ts` are returned unchanged. `.d.ts` specifiers are also left alone.
*
* @param specifier - the module specifier to normalize
*/
export function convertTsExtensionToJs(specifier: string): string {
if (isRelativePath(specifier) && specifier.endsWith(".ts") && !specifier.endsWith(".d.ts")) {
return specifier.slice(0, -".ts".length) + ".js";
}
return specifier;
}

/**
* Determines whether a module specifier is a package dependency.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import {
loadScreenshotAssets,
handleComputerActionAndTakeScreenshot,
printFinalOutput,
} from "./computerUseUtil.js";
} from "./computerUseUtil.ts";

const projectEndpoint = process.env["FOUNDRY_PROJECT_ENDPOINT"] || "<project endpoint>";
const deploymentName =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import { AppConfigurationClient } from "@azure/app-configuration";
import type { EventGridEvent } from "@azure/eventgrid";
import { isSystemEvent, EventGridDeserializer } from "@azure/eventgrid";
import { appConfigTestEvent } from "./testData.js";
import { appConfigTestEvent } from "./testData.ts";

// Load the .env file if it exists
import * as dotenv from "dotenv";
Expand Down
4 changes: 2 additions & 2 deletions sdk/attestation/attestation/samples-dev/attestEnclaves.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
*/
import { AttestationClient } from "@azure/attestation";
import { DefaultAzureCredential } from "@azure/identity";
import { writeBanner } from "./utils/helpers.js";
import { decodeString } from "./utils/base64url.js";
import { writeBanner } from "./utils/helpers.ts";
import { decodeString } from "./utils/base64url.ts";
// Load environment from a .env file if it exists.
import "dotenv/config";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import { AttestationClient } from "@azure/attestation";
import { DefaultAzureCredential } from "@azure/identity";
import { X509 } from "jsrsasign";
import { writeBanner } from "./utils/helpers.js";
import { writeBanner } from "./utils/helpers.ts";
// Load environment from a .env file if it exists.
import "dotenv/config";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

import { AttestationAdministrationClient, KnownAttestationType } from "@azure/attestation";
import { DefaultAzureCredential } from "@azure/identity";
import { writeBanner } from "./utils/helpers.js";
import { writeBanner } from "./utils/helpers.ts";
// Load environment from a .env file if it exists.
import "dotenv/config";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import { AttestationAdministrationClient } from "@azure/attestation";
import { DefaultAzureCredential } from "@azure/identity";
import { X509 } from "jsrsasign";
import { writeBanner } from "./utils/helpers.js";
import { writeBanner } from "./utils/helpers.ts";
// Load environment from a .env file if it exists.
import "dotenv/config";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@

import { AttestationAdministrationClient } from "@azure/attestation";
import { DefaultAzureCredential } from "@azure/identity";
import { createRSAKey, createX509Certificate, generateSha1Hash } from "./utils/cryptoUtils.js";
import { createRSAKey, createX509Certificate, generateSha1Hash } from "./utils/cryptoUtils.ts";
import { X509 } from "jsrsasign";
import { writeBanner } from "./utils/helpers.js";
import { writeBanner } from "./utils/helpers.ts";
// Load environment from a .env file if it exists.
import "dotenv/config";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ import {
KnownAttestationType,
} from "@azure/attestation";
import { DefaultAzureCredential } from "@azure/identity";
import { writeBanner } from "./utils/helpers.js";
import { createRSAKey, createX509Certificate, generateSha256Hash } from "./utils/cryptoUtils.js";
import { writeBanner } from "./utils/helpers.ts";
import { createRSAKey, createX509Certificate, generateSha256Hash } from "./utils/cryptoUtils.ts";
import { X509 } from "jsrsasign";
// Load environment from a .env file if it exists.
import "dotenv/config";
Expand Down
2 changes: 1 addition & 1 deletion sdk/cosmosdb/cosmos/samples-dev/AlterQueryThroughput.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/

import "dotenv/config";
import { finish, handleError, logStep, logSampleHeader } from "./Shared/handleError.js";
import { finish, handleError, logStep, logSampleHeader } from "./Shared/handleError.ts";
import type {
OfferDefinition,
Resource,
Expand Down
2 changes: 1 addition & 1 deletion sdk/cosmosdb/cosmos/samples-dev/Bulk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/

import "dotenv/config";
import { handleError, finish, logStep } from "./Shared/handleError.js";
import { handleError, finish, logStep } from "./Shared/handleError.ts";
import type { OperationInput } from "@azure/cosmos";
import { BulkOperationType, CosmosClient, PatchOperationType } from "@azure/cosmos";

Expand Down
2 changes: 1 addition & 1 deletion sdk/cosmosdb/cosmos/samples-dev/BulkUpdateWithSproc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/

import "dotenv/config";
import { logSampleHeader, handleError, finish, logStep } from "./Shared/handleError.js";
import { logSampleHeader, handleError, finish, logStep } from "./Shared/handleError.ts";
import { CosmosClient } from "@azure/cosmos";
import { randomUUID } from "@azure/core-util";

Expand Down
2 changes: 1 addition & 1 deletion sdk/cosmosdb/cosmos/samples-dev/ChangeFeed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/

import "dotenv/config";
import { finish, handleError, logSampleHeader } from "./Shared/handleError.js";
import { finish, handleError, logSampleHeader } from "./Shared/handleError.ts";
import { CosmosClient } from "@azure/cosmos";

const key = process.env.COSMOS_KEY || "<cosmos key>";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/

import "dotenv/config";
import { finish, handleError, logSampleHeader } from "../Shared/handleError.js";
import { finish, handleError, logSampleHeader } from "../Shared/handleError.ts";
import type { Container, ChangeFeedIteratorOptions } from "@azure/cosmos";
import {
CosmosClient,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/

import "dotenv/config";
import { finish, handleError, logSampleHeader, logStep } from "../Shared/handleError.js";
import { finish, handleError, logSampleHeader, logStep } from "../Shared/handleError.ts";
import type {
Container,
ChangeFeedIteratorOptions,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/

import "dotenv/config";
import { finish, handleError, logSampleHeader, logStep } from "../Shared/handleError.js";
import { finish, handleError, logSampleHeader, logStep } from "../Shared/handleError.ts";
import type {
Container,
ChangeFeedIteratorOptions,
Expand Down
2 changes: 1 addition & 1 deletion sdk/cosmosdb/cosmos/samples-dev/ClientSideEncryption.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
EncryptionType,
EncryptionQueryBuilder,
} from "@azure/cosmos";
import { finish, handleError, logStep } from "./Shared/handleError.js";
import { finish, handleError, logStep } from "./Shared/handleError.ts";

const key = process.env.COSMOS_KEY || "<cosmos key>";
const endpoint = process.env.COSMOS_ENDPOINT || "<cosmos endpoint>";
Expand Down
2 changes: 1 addition & 1 deletion sdk/cosmosdb/cosmos/samples-dev/ContainerManagement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/

import "dotenv/config";
import { finish, handleError, logStep, logSampleHeader } from "./Shared/handleError.js";
import { finish, handleError, logStep, logSampleHeader } from "./Shared/handleError.ts";
import type { ContainerDefinition, IndexingPolicy, SpatialIndex } from "@azure/cosmos";
import {
CosmosClient,
Expand Down
2 changes: 1 addition & 1 deletion sdk/cosmosdb/cosmos/samples-dev/DatabaseManagement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/

import "dotenv/config";
import { handleError, logStep, logSampleHeader, finish } from "./Shared/handleError.js";
import { handleError, logStep, logSampleHeader, finish } from "./Shared/handleError.ts";
import { CosmosClient } from "@azure/cosmos";
import assert from "node:assert";

Expand Down
2 changes: 1 addition & 1 deletion sdk/cosmosdb/cosmos/samples-dev/Diagnostics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/

import "dotenv/config";
import { handleError, logSampleHeader, finish } from "./Shared/handleError.js";
import { handleError, logSampleHeader, finish } from "./Shared/handleError.ts";
import type { OperationInput, Container, GatewayStatistics, Database } from "@azure/cosmos";
import { CosmosClient, BulkOperationType, PatchOperationType } from "@azure/cosmos";

Expand Down
2 changes: 1 addition & 1 deletion sdk/cosmosdb/cosmos/samples-dev/EntraAuth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { DefaultAzureCredential } from "@azure/identity";
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
import { CosmosClient } from "@azure/cosmos";
import { handleError, finish, logStep } from "./Shared/handleError.js";
import { handleError, finish, logStep } from "./Shared/handleError.ts";

const key = process.env.COSMOS_KEY || "<cosmos key>";
const endpoint = process.env.COSMOS_ENDPOINT || "<cosmos endpoint>";
Expand Down
2 changes: 1 addition & 1 deletion sdk/cosmosdb/cosmos/samples-dev/ExcludedLocations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import "dotenv/config";
import type { ChangeFeedIteratorOptions } from "@azure/cosmos";
import { CosmosClient, BulkOperationType, ChangeFeedStartFrom } from "@azure/cosmos";
import { handleError, logStep } from "./Shared/handleError.js";
import { handleError, logStep } from "./Shared/handleError.ts";

const endpoint = process.env.COSMOS_ENDPOINT || "<cosmos endpoint>";
const key = process.env.COSMOS_KEY || "<cosmos key>";
Expand Down
2 changes: 1 addition & 1 deletion sdk/cosmosdb/cosmos/samples-dev/ExecuteBulkOperations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/

import "dotenv/config";
import { handleError, finish, logStep } from "./Shared/handleError.js";
import { handleError, finish, logStep } from "./Shared/handleError.ts";
import type { OperationInput } from "@azure/cosmos";
import { BulkOperationType, CosmosClient, PatchOperationType } from "@azure/cosmos";
import { DefaultAzureCredential } from "@azure/identity";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/

import "dotenv/config";
import { handleError, logSampleHeader, finish } from "./Shared/handleError.js";
import { handleError, logSampleHeader, finish } from "./Shared/handleError.ts";
import type { PatchOperation, OperationInput } from "@azure/cosmos";
import {
CosmosClient,
Expand Down
2 changes: 1 addition & 1 deletion sdk/cosmosdb/cosmos/samples-dev/IndexManagement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/

import "dotenv/config";
import { logSampleHeader, handleError, finish, logStep } from "./Shared/handleError.js";
import { logSampleHeader, handleError, finish, logStep } from "./Shared/handleError.ts";
import type { ContainerDefinition } from "@azure/cosmos";
import { CosmosClient, IndexKind, DataType, IndexingMode } from "@azure/cosmos";

Expand Down
2 changes: 1 addition & 1 deletion sdk/cosmosdb/cosmos/samples-dev/ItemManagement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/

import "dotenv/config";
import { logSampleHeader, handleError, finish, logStep } from "./Shared/handleError.js";
import { logSampleHeader, handleError, finish, logStep } from "./Shared/handleError.ts";
import type { PatchOperation } from "@azure/cosmos";
import { CosmosClient, PriorityLevel } from "@azure/cosmos";
import FamiliesJSON from "./Data/Families.json" with { type: "json" };
Expand Down
2 changes: 1 addition & 1 deletion sdk/cosmosdb/cosmos/samples-dev/PriorityLevel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/

import "dotenv/config";
import { finish, handleError, logSampleHeader, logStep } from "./Shared/handleError.js";
import { finish, handleError, logSampleHeader, logStep } from "./Shared/handleError.ts";
import type {
Container,
ChangeFeedIteratorOptions,
Expand Down
2 changes: 1 addition & 1 deletion sdk/cosmosdb/cosmos/samples-dev/Query/FullTextSearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/

import "dotenv/config";
import { finish, handleError, logSampleHeader } from "./../Shared/handleError.js";
import { finish, handleError, logSampleHeader } from "./../Shared/handleError.ts";
import type { IndexingPolicy } from "@azure/cosmos";
import { CosmosClient } from "@azure/cosmos";

Expand Down
2 changes: 1 addition & 1 deletion sdk/cosmosdb/cosmos/samples-dev/SasTokenAuth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import "dotenv/config";
import type { SasTokenProperties } from "@azure/cosmos";
import { CosmosClient, createAuthorizationSasToken, SasTokenPermissionKind } from "@azure/cosmos";
import { handleError, finish, logStep } from "./Shared/handleError.js";
import { handleError, finish, logStep } from "./Shared/handleError.ts";
const masterKey = process.env.COSMOS_KEY || "<cosmos key>";
const endpoint = process.env.COSMOS_ENDPOINT || "<cosmos endpoint>";
const sasToken = "your-sas-token";
Expand Down
2 changes: 1 addition & 1 deletion sdk/cosmosdb/cosmos/samples-dev/ServerSideScripts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/

import "dotenv/config";
import { logSampleHeader, logStep, finish, handleError } from "./Shared/handleError.js";
import { logSampleHeader, logStep, finish, handleError } from "./Shared/handleError.ts";
import type { ErrorResponse, FeedOptions, Item, Resource } from "@azure/cosmos";
import { CosmosClient } from "@azure/cosmos";

Expand Down
2 changes: 1 addition & 1 deletion sdk/cosmosdb/cosmos/samples-dev/ThroughputBucket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/
import "dotenv/config";
import { ChangeFeedStartFrom, CosmosClient, type Container } from "@azure/cosmos";
import { logSampleHeader, handleError, logStep } from "./Shared/handleError.js";
import { logSampleHeader, handleError, logStep } from "./Shared/handleError.ts";
import { randomUUID } from "@azure/core-util";

const endpoint = process.env.COSMOS_ENDPOINT || "<cosmos endpoint>";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { DefaultAzureCredential } from "@azure/identity";
import { EventHubConsumerClient } from "@azure/event-hubs";
import { BlobCheckpointStore } from "@azure/eventhubs-checkpointstore-blob";
import { BlobServiceClient } from "@azure/storage-blob";
import { createCustomPipeline } from "./createCustomPipeline.js";
import { createCustomPipeline } from "./createCustomPipeline.ts";

import "dotenv/config";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import { DocumentAnalysisClient } from "@azure/ai-form-recognizer";
import { DefaultAzureCredential } from "@azure/identity";
import { PrebuiltBusinessCardModel } from "./prebuilt/prebuilt-businessCard.js";
import { PrebuiltBusinessCardModel } from "./prebuilt/prebuilt-businessCard.ts";
import "dotenv/config";

async function main(): Promise<void> {
Expand Down
Loading
Loading