-
Notifications
You must be signed in to change notification settings - Fork 154
Expand file tree
/
Copy pathcheck-activation-status.js
More file actions
190 lines (170 loc) · 6.65 KB
/
Copy pathcheck-activation-status.js
File metadata and controls
190 lines (170 loc) · 6.65 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
#!/usr/bin/env node
// Checks whether a Power Pages site is already activated (provisioned) in the environment.
// Used by deploy-site and activate-site skills to avoid unnecessary activation prompts
// or redundant activation attempts.
//
// Usage:
// node check-activation-status.js --projectRoot "<path>"
//
// Output (JSON to stdout):
// { "activated": true, "siteName": "...", "websiteRecordId": "...", "websiteUrl": "..." }
// { "activated": false, "siteName": "...", "websiteRecordId": "..." }
// { "error": "..." } — when prerequisites are missing (PAC CLI, Azure CLI, config, etc.)
const fs = require('fs');
const path = require('path');
const { execSync } = require('child_process');
const { findPath, getPacAuthInfo, getAuthToken, makeRequest, CLOUD_TO_API } = require('./lib/validation-helpers');
const { readWebsiteYml } = require('./lib/detect-project-context');
function output(obj) {
process.stdout.write(JSON.stringify(obj));
process.exit(0);
}
// Resolve site identity (siteName + websiteRecordId) from the project root.
//
// Resolution order:
// 1. powerpages.config.json (code/SPA sites) — siteName + (optional) websiteRecordId.
// 2. .powerpages-site/website.yml (declarative sites — standard or
// enhanced data model — which have no powerpages.config.json) — `name` -> siteName,
// `id` -> websiteRecordId.
// 3. `pac pages list` — ONLY when the GUID is still unknown (e.g. a code site whose
// config omitted websiteRecordId). Declarative sites already have it from
// website.yml, so they skip this exec entirely (`usedPacList` reports whether it ran).
//
// Returns `{ siteName, websiteRecordId, source, usedPacList }` on success, or
// `{ error }` when no site identity can be resolved. Dependencies are injectable so
// the resolution (incl. the skip-exec optimization) is unit-testable without PAC CLI.
function resolveSiteIdentity(projectRoot, deps = {}) {
const _findPath = deps.findPath || findPath;
const _readWebsiteYml = deps.readWebsiteYml || readWebsiteYml;
const _execSync = deps.execSync || execSync;
const _readFileSync = deps.readFileSync || fs.readFileSync;
let siteName = null;
let websiteRecordId = null;
let source = null;
const configPath = _findPath(projectRoot, 'powerpages.config.json');
if (configPath) {
try {
const config = JSON.parse(_readFileSync(configPath, 'utf8'));
siteName = config.siteName;
websiteRecordId = config.websiteRecordId || null;
source = 'config';
} catch {
return { error: 'Failed to parse powerpages.config.json' };
}
} else {
const websiteYmlPath = _findPath(projectRoot, path.join('.powerpages-site', 'website.yml'));
const site = websiteYmlPath ? _readWebsiteYml(websiteYmlPath) : null;
if (site) {
siteName = site.name;
websiteRecordId = site.id || null;
source = 'website.yml';
}
}
if (!siteName) {
return { error: 'Site name not found — looked in powerpages.config.json and .powerpages-site/website.yml' };
}
// Only hit `pac pages list` when the GUID isn't already known. Declarative sites (and code
// sites whose config included websiteRecordId) skip this entirely.
let usedPacList = false;
if (!websiteRecordId) {
usedPacList = true;
try {
const pacOutput = _execSync('pac pages list', { encoding: 'utf8', timeout: 15000 });
// pac pages list outputs a table with columns. Find the row matching siteName.
// Column headers vary but Website Record ID is always a GUID column.
const lines = pacOutput.split(/\r?\n/).filter((l) => l.trim());
for (const line of lines) {
// Skip header/separator lines
if (line.includes('----') || line.toLowerCase().includes('website name')) continue;
// Check if this line contains our site name (case-insensitive)
if (line.toLowerCase().includes(siteName.toLowerCase())) {
// Extract GUID from the line
const guidMatch = line.match(/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/i);
if (guidMatch) {
websiteRecordId = guidMatch[0];
}
break;
}
}
} catch {
// pac pages list failed — continue without websiteRecordId
}
}
return { siteName, websiteRecordId, source, usedPacList };
}
async function getWebsites(ppApiBaseUrl, token, environmentId) {
try {
const result = await makeRequest({
url: `${ppApiBaseUrl}/powerpages/environments/${environmentId}/websites?api-version=2022-03-01-preview`,
headers: {
Authorization: `Bearer ${token}`,
Accept: 'application/json',
},
timeout: 15000,
});
if (result.error || result.statusCode !== 200) return null;
const parsed = JSON.parse(result.body);
const value = parsed.value;
if (!value) return [];
return Array.isArray(value) ? value : [value];
} catch {
return null;
}
}
async function main() {
// --- Parse --projectRoot argument ---
const args = process.argv.slice(2);
const rootIdx = args.indexOf('--projectRoot');
const projectRoot = rootIdx !== -1 ? args[rootIdx + 1] : process.cwd();
// --- Resolve site identity (config -> website.yml -> pac pages list) ---
const identity = resolveSiteIdentity(projectRoot);
if (identity.error) {
output({ error: identity.error });
}
const { siteName } = identity;
let { websiteRecordId } = identity;
// --- Get PAC auth info ---
const pacInfo = getPacAuthInfo();
if (!pacInfo) {
output({ error: 'PAC CLI not authenticated' });
}
const ppApiBaseUrl = CLOUD_TO_API[pacInfo.cloud] || CLOUD_TO_API['Public'];
// --- Get Azure CLI token ---
const token = getAuthToken(ppApiBaseUrl);
if (!token) {
output({ error: 'Azure CLI token not available' });
}
// --- Query websites API ---
const websites = await getWebsites(ppApiBaseUrl, token, pacInfo.environmentId);
if (websites === null) {
output({ error: 'Websites API call failed' });
}
// --- Match by websiteRecordId or siteName ---
const match = websites.find((w) => {
if (websiteRecordId && w.websiteRecordId && w.websiteRecordId.toLowerCase() === websiteRecordId.toLowerCase()) {
return true;
}
if (siteName && w.name && w.name.toLowerCase() === siteName.toLowerCase()) {
return true;
}
return false;
});
if (match) {
output({
activated: true,
siteName: match.name || siteName,
websiteRecordId: match.websiteRecordId || websiteRecordId,
websiteUrl: match.websiteUrl || null,
});
} else {
output({
activated: false,
siteName,
websiteRecordId,
});
}
}
if (require.main === module) {
main();
}
module.exports = { resolveSiteIdentity, getWebsites };