-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
155 lines (136 loc) · 4.3 KB
/
index.js
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
const nodeFetch = require('node-fetch'),
h = require('highland'),
urlUtil = require('url'),
{ getComponentInstance, replaceVersion, getComponentVersion } = require('clayutils'),
CLAY_ACCESS_KEY = process.env.CLAY_ACCESS_KEY_PROD,
// AMPHORA_IP = '127.0.0.1'
AMPHORA_IP = '172.24.16.82'; // clay00
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
function streamFetch(url, options) {
const { hostname } = urlUtil.parse(url);
url = url.replace(hostname, AMPHORA_IP);
options = options || {};
options.headers = options.headers || {};
options.headers['x-forwarded-host'] = hostname;
return h(nodeFetch(url, options))
.flatMap(res => {
if (res.ok) {
return h.of(res);
}
return h(res.text())
.map(text => {
// console.log('(rest error)', JSON.stringify(arguments), res.status, text);
const err = new Error(`request to ${arguments[0]} failed; ${res.status}: ${text}`);
err.url = arguments[0];
err.status = res.status;
throw err;
})
});
}
function streamFetchJson() {
return streamFetch.apply(this, arguments)
.flatMap(res => h(res.json()));
}
function streamClayAssets(endpoint) {
const { protocol } = urlUtil.parse(endpoint);
return streamFetchJson(endpoint)
.sequence()
.flatMap(uri => h([uri, replaceVersion(uri, 'published')]))
.map(uri => {
const url = `${protocol}//${uri}`;
return streamFetchJson(url)
.map(data => ({url, uri, data}))
.errors((err, push) => {
// swallow 404s on published pages
if (err.status !== 404) throw err;
})
})
.parallel(10);
}
function replaceLayout(asset, fromLayout, toLayout) {
if (asset.data.layout) {
if (asset.data.layout === fromLayout) {
asset.data.layout = toLayout;
}
}
}
function commitAsset(asset) {
return streamFetch(asset.url, {
method: 'PUT',
headers: {
'content-type': 'application/json',
'authorization': `token ${CLAY_ACCESS_KEY}`
},
body: JSON.stringify(asset.data)
})
.flatMap(res => {
if (res.ok) {
return h.of(({status: 'success', url: asset.url}));
} else {
return h(res.text())
.map(text => ({
status: 'error',
url: asset.url,
error: `${res.status}: ${text}`
}));
}
})
}
/**
* Replaces all refs to a given layout to another layout in all pages of a
* given site.
* @param {string} site e.g. 'https://www.thecut.com'
* @param {string} replacements Mapping start layout to end layout
* (do not use versions)
* @return {Stream}
*/
function replaceLayoutInSite(site, replacements) {
return streamClayAssets(`${site}/_pages`)
.filter(asset => asset.data.layout)
.filter(asset => replacements[replaceVersion(asset.data.layout)])
.tap(asset => {
const layout = asset.data.layout,
baseLayout = replaceVersion(layout),
version = getComponentVersion(layout),
replacement = replacements[baseLayout];
if (replacement) {
asset.data.layout = replaceVersion(replacement, version);
}
})
// .tap(i => console.log(JSON.stringify(i)));
.map(commitAsset)
.parallel(10);
}
/**
* Lists all page assets from a site that have the specified layout.
* @param {string} site e.g.'www.thecut.com'
* @param {string} layout e.g. 'www.thecut.com/_layout/instances/foo'
*/
function report(site, layout) {
const publishedLayout = replaceVersion(layout, 'published');
assertUri(layout);
return streamClayAssets(`${site}/_pages`)
.filter(asset => {
return asset.data.layout === layout ||
asset.data.layout === publishedLayout;
})
.tap(asset => console.log(JSON.stringify(asset.url)));
}
function assertUri(uri) {
if (uri.startsWith('http') || !uri.match(/_pages|_components/)) {
throw new Error('Expected URI but got: ' + uri);
}
}
function logResult(result) {
if (result.error) {
result.error = result.error.stack;
}
console.log(JSON.stringify(result));
}
replaceLayoutInSite(
'http://www.grubstreet.com',
{
"www.grubstreet.com/_components/layout/instances/talk": "www.grubstreet.com/_components/layout/instances/article"
}
).tap(logResult).done(process.exit);
// report('http://www.grubstreet.com', 'www.grubstreet.com/_components/layout/instances/talk').done(process.exit);