-
Notifications
You must be signed in to change notification settings - Fork 99
/
index.mjs
executable file
·487 lines (432 loc) · 13.6 KB
/
index.mjs
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
#!/usr/bin/env node
import * as cheerio from 'cheerio'
import * as dotenv from 'dotenv'
import { expand } from 'dotenv-expand'
import * as fs from 'fs'
import https from 'https'
import * as _path from 'path'
import { exit } from 'process'
// polyfill matchAll for node versions < 12
import matchAll from 'string.prototype.matchall'
expand(dotenv.config())
matchAll.shim()
const { dirname, basename } = _path
export const kebab = (s) => s.toLowerCase().replace(/[^\w.]/g, '-')
export const camelCase = (s) => {
const matches = Array.from(s.matchAll(/[a-zA-Z0-9]+/g))
return (
matches[0][0].toLowerCase() +
matches
.slice(1)
.map(([item]) => item[0].toUpperCase() + item.substr(1).toLowerCase())
.join('')
)
}
export const cleanFilename = (filename) =>
filename
.toLowerCase()
.replace(/[^\w.]/g, '_')
.replace(/^_+|_+$/g, '')
export const ensureDirExists = (dir) => {
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, { recursive: true })
}
}
export function mergeDeep(target, source) {
const isObject = (obj) => obj && typeof obj === 'object'
if (!isObject(target) || !isObject(source)) {
return source
}
Object.keys(source).forEach((key) => {
const targetValue = target[key]
const sourceValue = source[key]
if (Array.isArray(targetValue) && Array.isArray(sourceValue)) {
target[key] = targetValue.concat(sourceValue)
} else if (isObject(targetValue) && isObject(sourceValue)) {
target[key] = mergeDeep(Object.assign({}, targetValue), sourceValue)
} else {
target[key] = sourceValue
}
})
return target
}
const rootUrl = 'https://tailwindui.com'
const output = process.env.OUTPUT || './output'
// list of languages to save (defaults to html)
const languages = (process.env.LANGUAGES || 'html').split(',')
// list of components to save (defaults to all)
const components = (process.env.COMPONENTS || 'all').split(',')
const retries = 3
let oldAssets = {}
let newAssets = {}
const regexEmail = new RegExp(process.env.EMAIL.replace(/[.@]/g, '\\$&'), 'g')
let cookies = {}
async function fetchHttps(url, options = {}, body = undefined) {
return new Promise((resolve, reject) => {
const uri = new URL(url)
options = {
hostname: uri.hostname,
port: uri.port || 443,
path: uri.pathname + uri.search,
method: 'GET',
...options,
}
let response
const req = https.request(options, (res) => {
response = res
response.body = Buffer.alloc(0)
response.status = res.statusCode
response.text = async () => response.body.toString()
response.json = async () => JSON.parse(await response.text())
response.arrayBuffer = async () => response.body.buffer
const setCookieHeaders = response.headers['set-cookie']
if (setCookieHeaders) {
const newCookies = parseSetCookieHeaders(setCookieHeaders)
cookies = { ...cookies, ...newCookies }
}
res.on('data', (d) => {
response.body = Buffer.concat([response.body, d])
})
res.on('end', () => {
resolve(response)
})
})
req.on('error', (error) => {
reject(error)
})
if (body) {
req.write(body)
}
req.end()
})
}
async function fetchWithRetry(url, retries, options = {}) {
let tries = 0
while (true) {
const start = new Date().getTime()
let response
let cookieHeader = getCookieHeader(cookies)
console.log(`🔍 Fetching ${url}`)
try {
response = await fetchHttps(url, {
...options,
headers: {
...options?.headers,
cookie: cookieHeader,
},
})
const elapsed = new Date().getTime() - start
console.log(`⏱ ${elapsed}ms (${response.status})`)
if (response.status === 302) {
return fetchWithRetry(response.headers.location, retries, options)
}
return response
} catch (err) {
console.error(err)
const elapsed = new Date().getTime() - start
tries++
const status = response ? response.status : 500
console.log(`🔄 ${elapsed}ms (${status}) Try #${tries} ${url}`)
if (tries === retries) {
console.log(`‼️ Error downloading ${url}.\n${err.message}`)
exit(1)
}
}
}
}
function parseSetCookieHeaders(setCookieHeaders) {
let cookies = {}
setCookieHeaders.forEach((header) => {
const [cookie] = header.split(';')
const [name, value] = cookie.split('=')
cookies[name] = decodeURIComponent(value)
})
return cookies
}
async function downloadPage(url) {
if (!url.startsWith(rootUrl)) url = rootUrl + url
const response = await fetchWithRetry(url, retries)
const html = await response.text()
return html.trim()
}
async function postData(url, data) {
if (!url.startsWith(rootUrl)) url = rootUrl + url
const body = JSON.stringify(data)
return fetchHttps(
url,
{
method: 'POST',
headers: {
'content-type': 'application/json',
'content-length': Buffer.byteLength(body),
cookie: getCookieHeader(cookies),
'x-inertia': 'true',
'x-xsrf-token': cookies['XSRF-TOKEN'],
},
},
body,
)
}
function getCookieHeader(cookies) {
return (
Object.entries(cookies)
//.map(([name, value]) => `${name}=${encodeURIComponent(value)}`)
.map(([name, value]) => `${name}=${value}`)
.join('; ')
)
}
async function processComponentPage(url) {
const html = await downloadPage(url)
if (!html.includes(process.env.EMAIL)) {
console.log(`🚫 Not logged in`)
process.exit()
}
const $ = cheerio.load(html)
// component data stored in #app data-page attribute
const json = $('#app').attr('data-page')
const data = JSON.parse(json)
const components = data.props.subcategory.components
console.log(
`🔍 Found ${components.length} component${
components.length === 1 ? '' : 's'
}`,
)
for (let i = 0; i < components.length; i++) {
await processComponent(url, components[i])
}
if (process.env.BUILDINDEX === '1') {
const preview = replaceTokens(html)
await savePageAndResources(url, preview, $)
}
}
function replaceTokens(html) {
// replace tokens in page with constant so it won't generate superfluous diffs
// also replace links to css/js assets to remove id querystring
const regexTokens = /name="(csrf-token|_token)"\s+(content|value)="(.+?)"/gm
const regexAssets = /(css|js)(\?id=[a-f0-9]+)/gm
return html
.replace(regexTokens, `name="$1" $2="CONSTANT_TOKEN"`)
.replace(regexAssets, '$1')
}
async function processComponent(url, component) {
const title = component.name
const filename = cleanFilename(title)
const path = `${url}/${filename}`
// output snippets by language
component.snippets.forEach((snippet) => {
const language = snippet.language.toLowerCase()
if (!languages.includes(language)) return
saveLanguageContent(path, language, snippet.snippet)
})
// save resources required by snippet preview
const html = component.iframeHtml
// if languages contains alpine, then save the preview as alpine
if (languages.includes('alpine')) {
const $body = cheerio.load(html)('body')
// default code to body
let code = $body.html().trim()
// strip empty wrapper divs if present
let $container = findFirstElementWithClass($body.children().first())
if ($container) {
code = $container.parent().html().trim()
}
const disclaimer = `<!--
This example requires Tailwind CSS v2.0+
The alpine.js code is *NOT* production ready and is included to preview
possible interactivity
-->
`
saveLanguageContent(path, 'alpine', `${disclaimer}${code}`)
}
await savePageAndResources(url, null, cheerio.load(html))
}
function findFirstElementWithClass($elem) {
// ignore empty class and elements with _style attribute
if (
$elem.attr('class') &&
$elem.attr('class').length > 0 &&
!$elem.attr('_style')
) {
return $elem
}
if ($elem.children().length === 0) return null
return findFirstElementWithClass($elem.children().first())
}
async function saveLanguageContent(path, language, code) {
const ext =
language === 'react' ? 'jsx' : language === 'alpine' ? 'html' : language
const dir = `${output}/${language}${dirname(path)}`
ensureDirExists(dir)
const filename = basename(path)
const filePath = `${dir}/${filename}.${ext}`
console.log(`📝 Writing ${language} ${filename}.${ext}`)
fs.writeFileSync(filePath, code)
}
async function savePageAndResources(url, html, $) {
// download referenced css and js inside <head>
const items = $('head>link,script,img')
for (let i = 0; i < items.length; i++) {
const $item = $(items[i])
const url = $item.attr('src') || $item.attr('href')
if (!url || !url.startsWith('/')) continue
// strip off querystring
const path = new URL(rootUrl + url).pathname
const dir = `${output}/preview${dirname(path)}`
const filePath = `${dir}/${basename(path)}`
// check assets to see if we've already downloaded this file
if (newAssets[filePath]) continue
ensureDirExists(dir)
let options = {}
if (oldAssets[filePath]) {
options = {
method: 'GET',
headers: {
'If-None-Match': oldAssets[filePath], // etag from previous GET
},
}
}
const response = await fetchWithRetry(rootUrl + url, retries, options)
// check etag
if (response.status === 304) {
continue
}
newAssets[filePath] = response.headers['etag']
const content = await response.arrayBuffer()
fs.writeFileSync(filePath, Buffer.from(content))
}
if (html) {
// write preview index page
const dir = `${output}/preview${url}`
ensureDirExists(dir)
html = html.replace(regexEmail, 'Licensed User')
fs.writeFileSync(`${dir}/index.html`, html)
console.log(`📝 Writing ${url}/index.html`)
}
}
async function login() {
await downloadPage('/login')
const response = await postData('/login', {
email: process.env.EMAIL,
password: process.env.PASSWORD,
remember: false,
})
return response.status === 409 || response.status === 302
}
async function saveTemplates() {
const html = await downloadPage('/templates')
const $ = cheerio.load(html)
const $templates = $('section[id^="product"]')
console.log(
`🔍 Found ${$templates.length} template${
$templates.length === 1 ? '' : 's'
}`,
)
for (let i = 0; i < $templates.length; i++) {
const $template = $($templates[i])
const $link = $template.find('h2>a')
const title = $link.text()
const url = $link.attr('href')
console.log(`🔍 Downloading template ${title}`)
const path = new URL(url).pathname
const dir = `${output}${dirname(path)}`
const filePath = `${dir}/${basename(path)}.zip`
ensureDirExists(dir)
let options = {}
if (oldAssets[filePath]) {
options = {
method: 'GET',
headers: {
'If-None-Match': oldAssets[filePath], // etag from previous GET
},
}
}
const response = await fetchWithRetry(url + '/download', retries, options)
// check etag
if (response.status === 304) {
continue
}
newAssets[filePath] = response.headers['etag']
const content = await response.arrayBuffer()
fs.writeFileSync(filePath, Buffer.from(content))
}
}
function debugLog(...args) {
if (process.env.DEBUG === '1') {
console.log(...args)
}
}
;(async function () {
const start = new Date().getTime()
try {
ensureDirExists(output)
// load old assets
if (fs.existsSync(`${output}/assets.json`)) {
oldAssets = JSON.parse(fs.readFileSync(`${output}/assets.json`))
newAssets = JSON.parse(JSON.stringify(oldAssets))
}
console.log('🔐 Logging into tailwindui.com...')
const success = await login()
if (!success) {
console.log('🚫 Invalid credentials')
return 1
}
console.log('✅ Success!\n')
console.log(`🗂 Output is ${output}`)
const html = await downloadPage('/components')
const $ = cheerio.load(html)
const library = {}
const links = $('.grid a')
let urls = []
debugLog(`📣 Found ${links.length} links`)
for (let i = 0; i < links.length; i++) {
const link = links[i]
const url = $(link).attr('href')
debugLog(`📣 ${i + 1}: ${url}`)
if (!url || !url.match(/\/components\//)) continue
// check if component is in list of components to save
const component = url.split('/')[2]
if (
component &&
components[0] !== 'all' &&
!components.includes(component)
)
continue
urls.push(url)
}
const count = process.env.COUNT || urls.length
for (let i = 0; i < count; i++) {
const url = urls[i]
console.log(`⏳ Processing #${i + 1}: ${url}...`)
const components = await processComponentPage(url)
mergeDeep(library, components)
console.log()
}
if (process.env.BUILDINDEX === '1') {
const preview = replaceTokens(html)
console.log('⏳ Saving preview page... this may take awhile')
await savePageAndResources('/components', preview, $)
fs.copyFileSync(
_path.join(process.cwd(), 'previewindex.html'),
`${output}/preview/index.html`,
)
console.log()
}
if (process.env.TEMPLATES === '1') {
console.log('⏳ Saving templates...')
ensureDirExists(`${output}/preview`)
await saveTemplates()
console.log()
}
// save assets file
fs.writeFileSync(
`${output}/assets.json`,
JSON.stringify(newAssets, null, 2),
)
} catch (err) {
console.error('‼️ ', err)
return 1
}
const elapsed = new Date().getTime() - start
console.log(`🏁 Done! ${elapsed / 1000} seconds`)
return 0
})()