Skip to content

Commit

Permalink
[dev-tool] Update migrate-package for ARM packages and update snippets (
Browse files Browse the repository at this point in the history
#32550)

### Packages impacted by this PR

- @azure/dev-tool
- @azure/arm-voiceservices
- @azure/arm-webpubsub
- @azure/arm-workloads
- @azure/arm-workloadssapvirtualinstance

### Issues associated with this PR

- #32184
- #32416

### Describe the problem that is addressed by this PR

Updates the migration tool for ARM packages and updates snippets for the
above packages.

This adds the following for the migration tool.
- Fixes for `samples-dev` to add `Promise<void>` to all non-marked
methods
- Change `beforeEach` and `afterEach` to use arrow functions instead of
`function`
- Fix the `timeout` issue on `it` and `it.skip` to move it to an options
parameter.

### What are the possible designs available to address the problem? If
there are more than one possible design, why was the one in this PR
chosen?


### Are there test cases added in this PR? _(If not, why?)_


### Provide a list of related PRs _(if any)_


### Command used to generate this PR:**_(Applicable only to SDK release
request PRs)_

### Checklists
- [ ] Added impacted package name to the issue description
- [ ] Does this PR needs any fixes in the SDK Generator?** _(If so,
create an Issue in the
[Autorest/typescript](https://github.com/Azure/autorest.typescript)
repository and link it here)_
- [ ] Added a changelog (if necessary)
  • Loading branch information
mpodwysocki authored Jan 14, 2025
1 parent 150f1ad commit 706086f
Show file tree
Hide file tree
Showing 410 changed files with 2,483 additions and 2,521 deletions.
191 changes: 111 additions & 80 deletions common/config/rush/pnpm-lock.yaml

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,8 +1,68 @@
import { existsSync, lstatSync } from "node:fs";
import { resolve } from "node:path";
import { CallExpression, SourceFile, SyntaxKind } from "ts-morph";
import { CallExpression, FunctionExpression, SourceFile, SyntaxKind } from "ts-morph";

export default function fixSourceFile(sourceFile: SourceFile): void {
removeLegacyStatements(sourceFile);
fixVitestMethods(sourceFile);
fixLegacyStatements(sourceFile);
fixArrowFunctions(sourceFile);
fixFunctionReturnTypes(sourceFile);

// Iterate over all the import declarations
for (const importExportDeclaration of sourceFile.getImportDeclarations()) {
let moduleSpecifier = importExportDeclaration.getModuleSpecifierValue();
moduleSpecifier = fixDeclaration(sourceFile, moduleSpecifier);
importExportDeclaration.setModuleSpecifier(moduleSpecifier);
}

// iterate over all the export declarations
for (const exportDeclaration of sourceFile.getExportDeclarations()) {
let moduleSpecifier = exportDeclaration.getModuleSpecifierValue();
if (moduleSpecifier) {
moduleSpecifier = fixDeclaration(sourceFile, moduleSpecifier);
exportDeclaration.setModuleSpecifier(moduleSpecifier);
}
}
}

function fixFunctionReturnTypes(sourceFile: SourceFile): void {
const functionDeclarations = sourceFile.getDescendantsOfKind(SyntaxKind.FunctionDeclaration);

for (const funcDecl of functionDeclarations) {
if (
funcDecl.getModifiers().some((mod) => mod.getText() === "async") &&
!funcDecl.getReturnTypeNode()
) {
funcDecl.setReturnType("Promise<void>");
}
}
}

function fixLegacyStatements(sourceFile: SourceFile): void {
for (const statement of sourceFile.getStatements()) {
const patternsToReplace = [
{ pattern: /\(this: Suite\)/g, replace: "(ctx)" },
{ pattern: /\(this: Context\)/g, replace: "(ctx)" },
{ pattern: /\(this\.currentTest\)/g, replace: "(ctx)" },
{ pattern: /\(!this\.currentTest\?\.isPending\(\)\)/g, replace: "(!ctx.task.pending)" },
{ pattern: /this\.skip\(\);/g, replace: "ctx.skip();" },
{
pattern: /import\s+(?:\*\s+as\s+dotenv|dotenv)\s+from\s+"dotenv";/g,
replace: 'import "dotenv/config";',
},
];

// Replace the patterns in the source file
for (const { pattern, replace } of patternsToReplace) {
if (pattern.test(statement.getText())) {
statement.replaceWithText(statement.getText().replace(pattern, replace));
}
}
}
}

function removeLegacyStatements(sourceFile: SourceFile): void {
const sourceLinesToRemove = [
"const should = chai.should();",
"chai.use(chaiAsPromised);",
Expand All @@ -19,11 +79,16 @@ export default function fixSourceFile(sourceFile: SourceFile): void {
}
}
}
}

// Find all 'it' call expressions and check for 'timeout'
function fixVitestMethods(sourceFile: SourceFile): void {
// Find all 'it' and 'it.skip' call expressions and check for 'timeout'
const itCalls = sourceFile
.getDescendantsOfKind(SyntaxKind.CallExpression)
.filter((callExpr: CallExpression) => callExpr.getExpression().getText() === "it");
.filter((callExpr: CallExpression) => {
const exprText = callExpr.getExpression().getText();
return exprText === "it" || exprText === "it.skip";
});

for (const itCall of itCalls) {
const timeoutCall = itCall
Expand All @@ -40,43 +105,28 @@ export default function fixSourceFile(sourceFile: SourceFile): void {
itCall.replaceWithText(`it(${testName}, { timeout: ${timeoutValue} }, ${testFunction});`);
}
}
}

for (const statement of sourceFile.getStatements()) {
const patternsToReplace = [
{ pattern: /\(this: Suite\)/g, replace: "(ctx)" },
{ pattern: /\(this: Context\)/g, replace: "(ctx)" },
{ pattern: /\(this\.currentTest\)/g, replace: "(ctx)" },
{ pattern: /\(!this\.currentTest\?\.isPending\(\)\)/g, replace: "(!ctx.task.pending)" },
{ pattern: /this\.skip\(\);/g, replace: "ctx.skip();" },
{
pattern: /import\s+(?:\*\s+as\s+dotenv|dotenv)\s+from\s+"dotenv";/g,
replace: 'import "dotenv/config";',
},
];
function fixArrowFunctions(sourceFile: SourceFile): void {
// Replace 'function' with arrow functions in 'beforeEach' and 'afterEach' calls
const lifecycleMethods = ["beforeEach", "afterEach"];
for (const method of lifecycleMethods) {
const methodCalls = sourceFile
.getDescendantsOfKind(SyntaxKind.CallExpression)
.filter((callExpr: CallExpression) => callExpr.getExpression().getText() === method);

// Replace the patterns in the source file
for (const { pattern, replace } of patternsToReplace) {
if (pattern.test(statement.getText())) {
statement.replaceWithText(statement.getText().replace(pattern, replace));
for (const methodCall of methodCalls) {
const funcExpr = methodCall.getArguments()[0] as FunctionExpression;
if (funcExpr && funcExpr.getKind() === SyntaxKind.FunctionExpression) {
const params = funcExpr
.getParameters()
.map((param) => param.getText())
.join(", ");
const body = funcExpr.getBody().getText();
methodCall.getArguments()[0].replaceWithText(`async (${params}) => ${body}`);
}
}
}

// Iterate over all the import declarations
for (const importExportDeclaration of sourceFile.getImportDeclarations()) {
let moduleSpecifier = importExportDeclaration.getModuleSpecifierValue();
moduleSpecifier = fixDeclaration(sourceFile, moduleSpecifier);
importExportDeclaration.setModuleSpecifier(moduleSpecifier);
}

// iterate over all the export declarations
for (const exportDeclaration of sourceFile.getExportDeclarations()) {
let moduleSpecifier = exportDeclaration.getModuleSpecifierValue();
if (moduleSpecifier) {
moduleSpecifier = fixDeclaration(sourceFile, moduleSpecifier);
exportDeclaration.setModuleSpecifier(moduleSpecifier);
}
}
}

function fixDeclaration(sourceFile: SourceFile, moduleSpecifier: string): string {
Expand Down
33 changes: 21 additions & 12 deletions sdk/voiceservices/arm-voiceservices/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,20 +46,28 @@ Set the values of the client ID, tenant ID, and client secret of the AAD applica

For more information about how to create an Azure AD Application check out [this guide](https://learn.microsoft.com/azure/active-directory/develop/howto-create-service-principal-portal).

```javascript
const { MicrosoftVoiceServices } = require("@azure/arm-voiceservices");
const { DefaultAzureCredential } = require("@azure/identity");
// For client-side applications running in the browser, use InteractiveBrowserCredential instead of DefaultAzureCredential. See https://aka.ms/azsdk/js/identity/examples for more details.
Using Node.js and Node-like environments, you can use the `DefaultAzureCredential` class to authenticate the client.

```ts snippet:ReadmeSampleCreateClient_Node
import { MicrosoftVoiceServices } from "@azure/arm-voiceservices";
import { DefaultAzureCredential } from "@azure/identity";

const subscriptionId = "00000000-0000-0000-0000-000000000000";
const client = new MicrosoftVoiceServices(new DefaultAzureCredential(), subscriptionId);
```

For browser environments, use the `InteractiveBrowserCredential` from the `@azure/identity` package to authenticate.

// For client-side applications running in the browser, use this code instead:
// const credential = new InteractiveBrowserCredential({
// tenantId: "<YOUR_TENANT_ID>",
// clientId: "<YOUR_CLIENT_ID>"
// });
// const client = new MicrosoftVoiceServices(credential, subscriptionId);
```ts snippet:ReadmeSampleCreateClient_Browser
import { InteractiveBrowserCredential } from "@azure/identity";
import { MicrosoftVoiceServices } from "@azure/arm-voiceservices";

const subscriptionId = "00000000-0000-0000-0000-000000000000";
const credential = new InteractiveBrowserCredential({
tenantId: "<YOUR_TENANT_ID>",
clientId: "<YOUR_CLIENT_ID>",
});
const client = new MicrosoftVoiceServices(credential, subscriptionId);
```

### JavaScript Bundle
Expand All @@ -78,8 +86,9 @@ To use this client library in the browser, first you need to use a bundler. For

Enabling logging may help uncover useful information about failures. In order to see a log of HTTP requests and responses, set the `AZURE_LOG_LEVEL` environment variable to `info`. Alternatively, logging can be enabled at runtime by calling `setLogLevel` in the `@azure/logger`:

```javascript
const { setLogLevel } = require("@azure/logger");
```ts snippet:SetLogLevel
import { setLogLevel } from "@azure/logger";

setLogLevel("info");
```

Expand Down
6 changes: 3 additions & 3 deletions sdk/voiceservices/arm-voiceservices/api-extractor.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"$schema": "https://developer.microsoft.com/json-schemas/api-extractor/v7/api-extractor.schema.json",
"mainEntryPointFilePath": "./dist-esm/src/index.d.ts",
"mainEntryPointFilePath": "dist/esm/index.d.ts",
"docModel": {
"enabled": true
},
Expand All @@ -11,7 +11,7 @@
"dtsRollup": {
"enabled": true,
"untrimmedFilePath": "",
"publicTrimmedFilePath": "./types/arm-voiceservices.d.ts"
"publicTrimmedFilePath": "dist/arm-voiceservices.d.ts"
},
"messages": {
"tsdocMessageReporting": {
Expand All @@ -28,4 +28,4 @@
}
}
}
}
}
111 changes: 70 additions & 41 deletions sdk/voiceservices/arm-voiceservices/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,65 +8,54 @@
"node": ">=18.0.0"
},
"dependencies": {
"@azure/abort-controller": "^1.0.0",
"@azure/core-auth": "^1.3.0",
"@azure/core-client": "^1.7.0",
"@azure/core-lro": "^2.5.0",
"@azure/core-paging": "^1.2.0",
"@azure/core-rest-pipeline": "^1.8.0",
"tslib": "^2.2.0"
"@azure/abort-controller": "^2.1.2",
"@azure/core-auth": "^1.9.0",
"@azure/core-client": "^1.9.2",
"@azure/core-lro": "^2.7.2",
"@azure/core-paging": "^1.6.2",
"@azure/core-rest-pipeline": "^1.18.2",
"tslib": "^2.8.1"
},
"keywords": [
"node",
"azure",
"typescript",
"browser",
"isomorphic"
"isomorphic",
"cloud"
],
"license": "MIT",
"main": "./dist/index.js",
"module": "./dist-esm/src/index.js",
"types": "./types/arm-voiceservices.d.ts",
"main": "./dist/commonjs/index.js",
"module": "./dist/esm/index.js",
"types": "./dist/commonjs/index.d.ts",
"devDependencies": {
"@azure-tools/test-credential": "^1.0.0",
"@azure-tools/test-recorder": "^3.0.0",
"@azure-tools/test-credential": "^2.0.0",
"@azure-tools/test-recorder": "^4.1.0",
"@azure-tools/test-utils-vitest": "^1.0.0",
"@azure/dev-tool": "^1.0.0",
"@azure/identity": "^4.0.1",
"@types/chai": "^4.2.8",
"@types/mocha": "^10.0.0",
"@azure/identity": "^4.5.0",
"@azure/logger": "^1.1.4",
"@types/node": "^18.0.0",
"chai": "^4.2.0",
"@vitest/browser": "^2.1.8",
"@vitest/coverage-istanbul": "^2.1.8",
"dotenv": "^16.0.0",
"mocha": "^11.0.2",
"ts-node": "^10.0.0",
"typescript": "~5.7.2"
},
"repository": {
"type": "git",
"url": "https://github.com/Azure/azure-sdk-for-js.git"
"playwright": "^1.49.1",
"typescript": "~5.7.2",
"vitest": "^2.1.8"
},
"repository": "github:Azure/azure-sdk-for-js",
"bugs": {
"url": "https://github.com/Azure/azure-sdk-for-js/issues"
},
"files": [
"dist/**/*.js",
"dist/**/*.js.map",
"dist/**/*.d.ts",
"dist/**/*.d.ts.map",
"dist-esm/**/*.js",
"dist-esm/**/*.js.map",
"dist-esm/**/*.d.ts",
"dist-esm/**/*.d.ts.map",
"src/**/*.ts",
"dist/",
"README.md",
"LICENSE",
"tsconfig.json",
"review/*",
"CHANGELOG.md",
"types/*"
"review/",
"CHANGELOG.md"
],
"scripts": {
"build": "npm run clean && tsc && dev-tool run bundle && npm run minify && dev-tool run vendored mkdirp ./review && npm run extract-api",
"build": "npm run clean && dev-tool run build-package && dev-tool run vendored mkdirp ./review && dev-tool run extract-api",
"build:browser": "echo skipped",
"build:node": "echo skipped",
"build:samples": "echo skipped.",
Expand All @@ -78,7 +67,7 @@
"format": "echo skipped",
"integration-test": "npm run integration-test:node && npm run integration-test:browser",
"integration-test:browser": "echo skipped",
"integration-test:node": "dev-tool run test:node-ts-input -- --timeout 1200000 'test/*.ts'",
"integration-test:node": "dev-tool run test:vitest --esm",
"lint": "echo skipped",
"minify": "dev-tool run vendored uglifyjs -c -m --comments --source-map \"content='./dist/index.js.map'\" -o ./dist/index.min.js ./dist/index.js",
"pack": "npm pack 2>&1",
Expand All @@ -88,8 +77,8 @@
"test:node": "echo skipped",
"unit-test": "npm run unit-test:node && npm run unit-test:browser",
"unit-test:browser": "echo skipped",
"unit-test:node": "dev-tool run vendored cross-env TEST_MODE=playback npm run integration-test:node",
"update-snippets": "echo skipped"
"unit-test:node": "dev-tool run test:vitest",
"update-snippets": "dev-tool run update-snippets"
},
"sideEffects": false,
"//metadata": {
Expand All @@ -109,5 +98,45 @@
],
"disableDocsMs": true,
"apiRefLink": "https://learn.microsoft.com/javascript/api/@azure/arm-voiceservices?view=azure-node-preview"
},
"type": "module",
"tshy": {
"project": "./tsconfig.src.json",
"exports": {
"./package.json": "./package.json",
".": "./src/index.ts"
},
"dialects": [
"esm",
"commonjs"
],
"esmDialects": [
"browser",
"react-native"
],
"selfLink": false
},
"browser": "./dist/browser/index.js",
"react-native": "./dist/react-native/index.js",
"exports": {
"./package.json": "./package.json",
".": {
"browser": {
"types": "./dist/browser/index.d.ts",
"default": "./dist/browser/index.js"
},
"react-native": {
"types": "./dist/react-native/index.d.ts",
"default": "./dist/react-native/index.js"
},
"import": {
"types": "./dist/esm/index.d.ts",
"default": "./dist/esm/index.js"
},
"require": {
"types": "./dist/commonjs/index.d.ts",
"default": "./dist/commonjs/index.js"
}
}
}
}
Loading

0 comments on commit 706086f

Please sign in to comment.