forked from neo4j-documentation/knowledge-base
-
Notifications
You must be signed in to change notification settings - Fork 0
/
imgur-sync-to-s3.js
71 lines (67 loc) · 2.58 KB
/
imgur-sync-to-s3.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
const axios = require('axios')
const process = require('process')
const path = require('path')
const fs = require('fs')
const execSync = require('child_process').execSync;
const imgurImageMacroRx = /image::?(https?:\/\/(:?i\.)?imgur.com\/([a-zA-Z0-9]+\.(png|jpe?g)))\[([^\]]*)]/g
const pagesDir = path.join(__dirname, 'articles', 'modules', 'ROOT', 'pages')
const buildDir = path.join(__dirname, 'build', 's3', 'images')
fs.mkdirSync(buildDir, { recursive: true })
// clean build/s3/images directory
const imageFiles = fs.readdirSync(buildDir)
for (const file of imageFiles) {
fs.unlinkSync(path.join(buildDir, file))
}
async function getImage(url) {
console.log(`\tGET ${url}`)
return axios({
method: 'get',
url,
responseType: 'stream'
})
}
;(async () => {
try {
const asciidocFiles = fs.readdirSync(pagesDir)
for (const file of asciidocFiles) {
if (file.endsWith('.adoc')) {
const content = fs.readFileSync(path.join(pagesDir, file), 'utf8')
const lines = content.split(/\r?\n/)
for (const line of lines) {
const matches = [...line.matchAll(imgurImageMacroRx)]
if (matches && matches.length > 0) {
console.log(`- ${file}`)
for (const match of matches) {
let imageUrl = match[1]
let imageName = match[3]
if (imageUrl.startsWith('http://')) {
imageUrl = `https://i.imgur.com/${imageName}`
}
const fileExtension = match[4]
const attributesList = match[5]
const description = attributesList.split(',')[0]
if (description && description !== 'image' && !description.includes('=')) {
imageName = description.toLowerCase().replace(/\s/g, '-') + '.' + fileExtension
} else {
imageName = file.replace(/\.adoc$/, '') + '-' + imageName
}
const response = await getImage(imageUrl)
const imagePath = path.join(buildDir, imageName)
console.log(`\twriting image to ${imagePath}`)
response.data.pipe(fs.createWriteStream(imagePath))
}
}
}
}
}
const profileOption = process.env.AWS_PROFILE ? ` --profile ${process.env.AWS_PROFILE}` : ''
const awsS3SyncCommand = `aws s3 sync . s3://dev.assets.neo4j.com/kb-content --acl public-read${profileOption}`;
console.log(awsS3SyncCommand)
const result = execSync(awsS3SyncCommand, {
cwd: buildDir
})
console.log(result.toString('utf8'))
} catch (err) {
console.error('Error', err)
}
})()