-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Expand file tree
/
Copy patharm-lease-fetch-resource-providers.js
More file actions
249 lines (222 loc) · 8.15 KB
/
Copy patharm-lease-fetch-resource-providers.js
File metadata and controls
249 lines (222 loc) · 8.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
import { existsSync, readdirSync, statSync, writeFileSync } from "fs";
import { basename, dirname, join, relative, resolve } from "path";
import { fileURLToPath } from "url";
import { parseArgs } from "util";
/**
* Check if a directory is a service name directory (not stable, preview, common-types, or examples)
* @param {string} dirPath - Path to the directory
* @returns {boolean} True if it's a valid service name directory
*/
export function isServiceNameDirectory(dirPath) {
const excludeNames = new Set(["stable", "preview", "common-types", "examples"]);
try {
return !excludeNames.has(basename(dirPath)) && statSync(dirPath).isDirectory();
} catch {
return false;
}
}
/**
* Check if a resource provider path has version directories (stable or preview)
* @param {string} rpPath - Path to the resource provider
* @returns {boolean} True if it has version directories
*/
export function hasVersionDirectories(rpPath) {
return existsSync(join(rpPath, "stable")) || existsSync(join(rpPath, "preview"));
}
/**
* Check if a directory looks like the repository root
* @param {string} dir - Directory to check
* @returns {boolean} True if it looks like repo root
*/
function looksLikeRepoRoot(dir) {
return (
existsSync(join(dir, ".git")) ||
existsSync(join(dir, "specification")) ||
existsSync(join(dir, ".github"))
);
}
/**
* Find the repository root by looking for repo markers (.git, specification, .github)
* Supports sparse checkouts in CI by checking GITHUB_WORKSPACE first
* @param {string} [startPath] - Starting path to search from
* @returns {string} The repository root path
* @throws {Error} If repository root cannot be found
*/
export function findRepoRoot(startPath = process.cwd()) {
const workspace = process.env.GITHUB_WORKSPACE;
if (workspace && looksLikeRepoRoot(workspace)) {
return workspace;
}
let current = resolve(startPath);
for (let i = 0; i < 20; i++) {
if (looksLikeRepoRoot(current)) return current;
const parent = dirname(current);
if (parent === current) break;
current = parent;
}
throw new Error("Could not find repository root. Run from within azure-rest-api-specs.");
}
/**
* @typedef {Object} ResourceProvider
* @property {string} rpNamespace - Resource provider namespace (e.g., Microsoft.Storage)
* @property {string} path - Relative path to the resource provider
* @property {string} orgName - Organization name from specification folder
* @property {string[]} [serviceNames] - Service names if withServiceNames is true
*/
/**
* Find all resource providers in the specification directory
* @param {string} repoRoot - Repository root path
* @param {boolean} [withServiceNames=false] - Whether to include only RPs with service names
* @returns {ResourceProvider[]} Array of resource provider objects
*/
export function findResourceProviders(repoRoot, withServiceNames = false) {
const results = [];
const specDir = join(repoRoot, "specification");
if (!existsSync(specDir)) {
// In sparse checkouts (CI), specification/ may not exist. Return empty list.
return [];
}
for (const orgName of readdirSync(specDir)) {
const orgDir = join(specDir, orgName);
if (!statSync(orgDir).isDirectory()) continue;
const rmDir = join(orgDir, "resource-manager");
if (!existsSync(rmDir)) continue;
for (const rpNamespace of readdirSync(rmDir)) {
const rpPath = join(rmDir, rpNamespace);
if (!statSync(rpPath).isDirectory() || !rpNamespace.startsWith("Microsoft.")) {
continue;
}
const serviceNames = readdirSync(rpPath)
.filter((sn) => isServiceNameDirectory(join(rpPath, sn)))
.sort();
if (withServiceNames && serviceNames.length > 0) {
results.push({
rpNamespace: rpNamespace,
path: relative(repoRoot, rpPath),
orgName: orgName,
serviceNames: serviceNames,
});
} else if (!withServiceNames && serviceNames.length === 0 && hasVersionDirectories(rpPath)) {
results.push({
rpNamespace: rpNamespace,
path: relative(repoRoot, rpPath),
orgName: orgName,
});
}
}
}
return results.sort((a, b) => a.rpNamespace.localeCompare(b.rpNamespace));
}
/**
* Format resource providers for output
* @param {ResourceProvider[]} rps - Array of resource providers
* @param {"list"|"json"|"table"} fmt - Output format
* @param {boolean} withSN - Whether resource providers have service names
* @returns {string} Formatted output string
*/
export function formatOutput(rps, fmt, withSN) {
if (fmt === "json") return JSON.stringify(rps, null, 2);
if (rps.length === 0) {
return `No resource providers ${withSN ? "with" : "without"} serviceNames found.`;
}
if (fmt === "table") {
const maxOrg = Math.max(...rps.map((r) => r.orgName.length));
const maxRp = Math.max(...rps.map((r) => r.rpNamespace.length));
const header = withSN
? `${"orgName".padEnd(maxOrg)} ${"rpNamespace".padEnd(maxRp)} serviceNames`
: `${"orgName".padEnd(maxOrg)} ${"rpNamespace".padEnd(maxRp)} Path`;
const sep = `${"-".repeat(maxOrg)} ${"-".repeat(maxRp)} ${"-".repeat(60)}`;
const rows = withSN
? rps.map(
(r) =>
`${r.orgName.padEnd(maxOrg)} ${r.rpNamespace.padEnd(maxRp)} ${/** @type {string[]} */ (r.serviceNames).join(", ")}`,
)
: rps.map((r) => `${r.orgName.padEnd(maxOrg)} ${r.rpNamespace.padEnd(maxRp)} ${r.path}`);
return [header, sep, ...rows].join("\n");
}
return withSN
? rps
.map(
(r) =>
`${r.orgName}, ${r.rpNamespace}, [${/** @type {string[]} */ (r.serviceNames).join(", ")}]`,
)
.join("\n")
: rps.map((r) => `${r.orgName}, ${r.rpNamespace}`).join("\n");
}
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
function usage() {
console.log(`Usage:
npx arm-lease-fetch-resource-providers [options]
Options:
--with-service-groups Include only RPs with serviceNames (service groups)
--format <format> Output format: list, json, table (default: list)
--count Output only the count
--output <file> Write output to file instead of stdout
--repo-root <path> Repository root path (auto-detected if not provided)
--help Show this help message
Examples:
# RPs without service names
npx arm-lease-fetch-resource-providers
# RPs with service names
npx arm-lease-fetch-resource-providers --with-service-groups
# Output to file
npx arm-lease-fetch-resource-providers --output rps.txt
# JSON format
npx arm-lease-fetch-resource-providers --format json`);
}
// Only run CLI if this file is executed directly (not imported)
if (process.argv[1] === __filename) {
const {
values: {
"with-service-groups": withServiceGroups,
format,
count,
output,
"repo-root": repoRootArg,
help,
},
} = parseArgs({
options: {
"with-service-groups": { type: "boolean", default: false },
format: { type: "string", default: "list" },
count: { type: "boolean", default: false },
output: { type: "string", default: "" },
"repo-root": { type: "string", default: "" },
help: { type: "boolean", default: false },
},
allowPositionals: false,
});
if (help) {
usage();
process.exit(0);
}
try {
const repoRoot = repoRootArg
? resolve(repoRootArg)
: findRepoRoot(resolve(__dirname, "../../../"));
const rps = findResourceProviders(repoRoot, withServiceGroups);
let outputText;
if (count) {
outputText = String(rps.length);
} else {
outputText = formatOutput(
rps,
/** @type {"list"|"json"|"table"} */ (format),
withServiceGroups,
);
if (format !== "json") {
outputText += `\n\nTotal: ${rps.length} resource provider(s) ${withServiceGroups ? "with" : "without"} serviceNames`;
}
}
if (output) {
writeFileSync(output, outputText + "\n");
console.log(`Output written to ${output}`);
} else {
console.log(outputText);
}
} catch (error) {
console.error(`Error: ${/** @type {Error} */ (error).message}`);
process.exit(1);
}
}