Skip to content

Commit

Permalink
Migrate keyvault-secrets to TypeSpec
Browse files Browse the repository at this point in the history
  • Loading branch information
maorleger committed Dec 10, 2024
1 parent ec6a153 commit 8c01e6f
Show file tree
Hide file tree
Showing 31 changed files with 1,903 additions and 1,942 deletions.
86 changes: 86 additions & 0 deletions sdk/keyvault/generate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
#!/usr/bin/env node

const { execSync } = require("child_process");
const fs = require("fs");
const path = require("path");

// Helper to execute shell commands and log output
function execCommand(command) {
try {
execSync(command, { stdio: "inherit" });
} catch (error) {
console.error(`Command failed: ${command}`);
process.exit(1);
}
}

console.log("Setting up the environment...");

// Workaround for src-folder support in emitter:
// End state: src/generated/* contains generated code (instead of src/generated/src/*)

// Step 1: Remove all files in src/generated/*
execCommand("rm -rf src/generated/*");

// Step 2: Copy tsp-location.yaml to src/generated
execCommand("cp tsp-location.yaml src/generated");

// Step 3: Run tsp-client command
// emitter-option as a workaround for https://github.com/Azure/azure-rest-api-specs/issues/31610
execCommand(`tsp-client update -d -o src/generated --emitter-options generateMetadata=false`);
// execCommand(
// "tsp-client update -d -o src/generated --tsp-config ~/workspace/azure-rest-api-specs/specification/keyvault/Security.KeyVault.Keys/tspconfig.yaml --local-spec-repo ~/workspace/azure-rest-api-specs/specification/keyvault/Security.KeyVault.Keys --repo ~/workspace/azure-rest-api-specs --commit 9561bad7d2eed94cc91aa6164d3721b8aa8699fe --emitter-options generateMetadata=false"
// );

// Step 4: Move generated/src/* files to generated until src-folder is supported
execCommand("mv src/generated/src/* src/generated/");

// Step 5: Remove generated/src
execCommand("rm -rf src/generated/src");

// Step 6: Remove tsp-location.yaml from generated folder
execCommand("rm src/generated/tsp-location.yaml");

// Step 7: Read and update package.json
console.log("Updating package.json dependencies...");
const packageJsonPath = path.resolve("./package.json");
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, "utf8"));

// Remove dependency on @azure/core-client and add @azure-rest/core-client
delete packageJson.dependencies["@azure/core-client"];
packageJson.dependencies["@azure-rest/core-client"] = "^2.0.0";

// Write updated package.json back to disk
fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2), "utf8");

// Generated code changes
// Workaround for https://github.com/Azure/autorest.typescript/pull/2135/files
const modelsPath = path.resolve("./src/generated/models/models.ts");
let modelsContent = fs.readFileSync(modelsPath, "utf8");
modelsContent = modelsContent
.replace(
/created: !item\["created"\] \? item\["created"\] : new Date\(item\["created"\]\),/g,
'created: !item["created"] ? item["created"] : new Date(item["created"] * 1000),'
)
.replace(
/updated: !item\["updated"\] \? item\["updated"\] : new Date\(item\["updated"\]\),/g,
'updated: !item["updated"] ? item["updated"] : new Date(item["updated"] * 1000),'
)
.replace(
/notBefore: !item\["nbf"\] \? item\["nbf"\] : new Date\(item\["nbf"\]\),/g,
'notBefore: !item["nbf"] ? item["nbf"] : new Date(item["nbf"] * 1000),'
)
.replace(
/expires: !item\["exp"\] \? item\["exp"\] : new Date\(item\["exp"\]\),/g,
'expires: !item["exp"] ? item["exp"] : new Date(item["exp"] * 1000),'
)
.replace(
/nbf: !item\["notBefore"\] \? item\["notBefore"\] : item\["notBefore"\].getTime\(\),/g,
'nbf: !item["notBefore"] ? item["notBefore"] : item["notBefore"].getTime() / 1000,'
)
.replace(
/exp: !item\["expires"\] \? item\["expires"\] : item\["expires"\].getTime\(\),/g,
'exp: !item["expires"] ? item["expires"] : item["expires"].getTime() / 1000,'
);

fs.writeFileSync(modelsPath, modelsContent, "utf8");
2 changes: 1 addition & 1 deletion sdk/keyvault/keyvault-secrets/assets.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
"AssetsRepo": "Azure/azure-sdk-assets",
"AssetsRepoPrefixPath": "js",
"TagPrefix": "js/keyvault/keyvault-secrets",
"Tag": "js/keyvault/keyvault-secrets_41e131fba8"
"Tag": "js/keyvault/keyvault-secrets_946a3d52fe"
}
4 changes: 2 additions & 2 deletions sdk/keyvault/keyvault-secrets/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
"test:node": "npm run clean && npm run build:test && npm run unit-test:node",
"unit-test": "npm run unit-test:node && npm run unit-test:browser",
"unit-test:browser": "echo skipped",
"unit-test:node": "dev-tool run test:vitest",
"unit-test:node": "dev-tool run test:vitest -- --test-timeout 250000",
"update-snippets": "echo skipped"
},
"sideEffects": false,
Expand Down Expand Up @@ -92,9 +92,9 @@
}
},
"dependencies": {
"@azure-rest/core-client": "^2.0.0",
"@azure/abort-controller": "^2.0.0",
"@azure/core-auth": "^1.3.0",
"@azure/core-client": "^1.5.0",
"@azure/core-http-compat": "^2.0.1",
"@azure/core-lro": "^2.2.0",
"@azure/core-paging": "^1.1.1",
Expand Down
4 changes: 2 additions & 2 deletions sdk/keyvault/keyvault-secrets/review/keyvault-secrets.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
```ts

import { AzureLogger } from '@azure/logger';
import type * as coreClient from '@azure/core-client';
import type * as coreClient from '@azure-rest/core-client';
import type { ExtendedCommonClientOptions } from '@azure/core-http-compat';
import { PagedAsyncIterableIterator } from '@azure/core-paging';
import { PageSettings } from '@azure/core-paging';
import { PollerLike } from '@azure/core-lro';
import { PollOperationState } from '@azure/core-lro';
import type { PollOperationState } from '@azure/core-lro';
import type { TokenCredential } from '@azure/core-auth';

// @public
Expand Down
36 changes: 36 additions & 0 deletions sdk/keyvault/keyvault-secrets/src/generated/api/index.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

66 changes: 66 additions & 0 deletions sdk/keyvault/keyvault-secrets/src/generated/api/keyVaultContext.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 8c01e6f

Please sign in to comment.