-
Notifications
You must be signed in to change notification settings - Fork 153
Expand file tree
/
Copy pathclear-site-cache.js
More file actions
122 lines (104 loc) · 4.32 KB
/
Copy pathclear-site-cache.js
File metadata and controls
122 lines (104 loc) · 4.32 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
#!/usr/bin/env node
// Clears the runtime cache of an activated Power Pages site by restarting it
// via the Power Platform API.
//
// Usage:
// node clear-site-cache.js --projectRoot "<path>"
//
// Output (JSON to stdout):
// { "success": true, "websiteUrl": "..." }
// { "success": false, "error": "..." }
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');
function output(obj) {
process.stdout.write(JSON.stringify(obj));
process.exit(obj.success ? 0 : 1);
}
// --- Parse --projectRoot argument ---
const args = process.argv.slice(2);
const rootIdx = args.indexOf('--projectRoot');
const projectRoot = rootIdx !== -1 ? args[rootIdx + 1] : process.cwd();
// --- Read siteName from powerpages.config.json ---
const configPath = findPath(projectRoot, 'powerpages.config.json');
if (!configPath) {
output({ success: false, error: 'powerpages.config.json not found' });
}
let siteName;
try {
const config = JSON.parse(fs.readFileSync(configPath, 'utf8'));
siteName = config.siteName;
} catch {
output({ success: false, error: 'Failed to parse powerpages.config.json' });
}
if (!siteName) {
output({ success: false, error: 'siteName not found in powerpages.config.json' });
}
// --- Get PAC auth info ---
const pacInfo = getPacAuthInfo();
if (!pacInfo) {
output({ success: false, error: 'PAC CLI not authenticated' });
}
const ppApiBaseUrl = CLOUD_TO_API[pacInfo.cloud] || CLOUD_TO_API['Public'];
// --- Get Power Platform API token ---
const token = getAuthToken(ppApiBaseUrl);
if (!token) {
output({ success: false, error: 'Failed to get Azure CLI access token. Ensure you are logged in with: az login --allow-no-subscriptions' });
}
// --- Find the website and restart it to clear cache ---
(async () => {
// Get websites for this environment
const listResult = await makeRequest({
url: `${ppApiBaseUrl}/powerpages/environments/${pacInfo.environmentId}/websites?api-version=2022-03-01-preview`,
headers: { Authorization: `Bearer ${token}`, Accept: 'application/json' },
timeout: 15000,
});
if (listResult.error || listResult.statusCode !== 200) {
output({ success: false, error: `Failed to list websites: ${listResult.error || `HTTP ${listResult.statusCode}`}` });
}
let websites;
try {
const parsed = JSON.parse(listResult.body);
websites = Array.isArray(parsed.value) ? parsed.value : [];
} catch {
output({ success: false, error: 'Failed to parse websites API response' });
}
// Match by websiteRecordId (from pac pages list) or by name
let websiteRecordId = null;
try {
const pacOutput = execSync('pac pages list', { encoding: 'utf8', timeout: 15000 });
const lines = pacOutput.split(/\r?\n/).filter((l) => l.trim());
for (const line of lines) {
if (line.includes('----') || line.toLowerCase().includes('website name')) continue;
if (line.toLowerCase().includes(siteName.toLowerCase())) {
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 {}
const match = websites.find((w) => {
if (websiteRecordId && w.websiteRecordId && w.websiteRecordId.toLowerCase() === websiteRecordId.toLowerCase()) return true;
if (w.name && w.name.toLowerCase() === siteName.toLowerCase()) return true;
return false;
});
if (!match || !match.id) {
output({ success: false, error: `Website "${siteName}" not found in environment` });
}
// Restart the site to clear its runtime cache
const restartResult = await makeRequest({
url: `${ppApiBaseUrl}/powerpages/environments/${pacInfo.environmentId}/websites/${match.id}/restart?api-version=2022-03-01-preview`,
method: 'POST',
headers: { Authorization: `Bearer ${token}`, Accept: 'application/json' },
timeout: 30000,
});
if (restartResult.error) {
output({ success: false, error: `Restart request failed: ${restartResult.error}` });
}
if (restartResult.statusCode >= 200 && restartResult.statusCode < 300) {
output({ success: true, websiteUrl: match.websiteUrl || null });
} else {
output({ success: false, error: `Restart returned HTTP ${restartResult.statusCode}: ${restartResult.body}` });
}
})();