forked from bankless-academy/bankless-academy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
import-config.js
131 lines (122 loc) · 3.82 KB
/
import-config.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
/* eslint-disable no-console */
require('dotenv').config()
const axios = require('axios')
const FileSystem = require('fs')
const crc32 = require('js-crc').crc32
const fs = require('fs')
const stringifyObject = require('stringify-object')
const PROJECT_DIR = process.env.PROJECT_DIR || ''
// TODO: update
const DEFAULT_NOTION_ID = '623e965e4f10456094d17aa94ec37105'
const POTION_API = 'https://potion.banklessacademy.com'
const CONFIG_FILE = 'whitelabel.ts'
const args = process.argv
const NOTION_ID =
args[2] && args[2].length === 32
? args[2]
: process.env.DEFAULT_CONFIG_DB_ID || DEFAULT_NOTION_ID
const UMAMI_ID = process.env.UMAMI_ID || '62d1cf48-425d-4658-9b86-3eea78ac9714'
console.log('NOTION_ID', NOTION_ID)
const KEY_MATCHING = {
'Project name': 'project_name',
Domain: 'domain_prod',
'Default metadata description': 'default_metadata_description',
'Default metadata image': 'default_metadata_image',
Favicon: 'favicon',
'Homepage logo': 'logo',
// 'Homepage logo': 'logo_small',
'Homepage background image': 'homepage_background',
'MintKudos community admin': 'mintkudos_community_admin',
}
// TODO: move to lib file
const slugify = (text) =>
text
.toLowerCase()
.replace(/<[^>]*>?/gm, '') // remove tags
.replace(/[^a-z0-9 -]/g, '') // remove invalid chars
.replace(/\s+/g, '-') // collapse whitespace and replace by -
.replace(/-+/g, '-') // collapse dashes
const download_image = (url, image_path) =>
axios({
url,
responseType: 'stream',
}).then(function (response) {
response.data.pipe(fs.createWriteStream(image_path))
})
const get_img = (imageLink, slug, image_name) => {
const [file_name] = imageLink.split('?')
const file_extension = file_name
.match(/\.(png|svg|jpg|jpeg|webp|webm|mp4|gif)/)[1]
.replace('jpeg', 'jpg')
// console.log(file_extension)
// create "unique" hash based on Notion imageLink (different when re-uploaded)
const hash = crc32(file_name)
const image_dir = `/${PROJECT_DIR}${slug}`
const image_path = `${image_dir}${slugify(
image_name
)}-${hash}.${file_extension}`
// console.log('image_path', image_path)
const local_image_path = `public${image_path}`
if (!fs.existsSync(local_image_path)) {
download_image(imageLink, local_image_path)
console.log('downloading image: ', local_image_path)
}
return image_path
}
let config = {}
axios
.get(`${POTION_API}/table?id=${NOTION_ID}`)
.then((response) => {
response.data.map((notion) => {
config = {}
// console.log(notion)
config = Object.keys(KEY_MATCHING).reduce(
(obj, k) =>
Object.assign(obj, {
[KEY_MATCHING[k]]: notion.fields[k],
}),
{}
)
// console.log(notion.fields)
if (config.default_metadata_image) {
config.default_metadata_image = get_img(
config.default_metadata_image,
'image',
''
)
}
if (config.favicon) {
config.favicon = get_img(config.favicon, 'favicon', '')
}
if (config.logo) {
config.logo = get_img(config.logo, 'logo', '')
config.logo_small = config.logo
}
if (config.homepage_background) {
config.homepage_background = get_img(
config.homepage_background,
'homepage_background',
''
)
}
config.umami_prod = UMAMI_ID
})
console.log(config)
const FILE_CONTENT = `import { WhitelabelType } from 'entities/whitelabel'
export const WHITELABEL: WhitelabelType = ${stringifyObject(config, {
indent: ' ',
singleQuotes: true,
})}
`
FileSystem.writeFile(
`src/constants/${CONFIG_FILE}`,
FILE_CONTENT,
(error) => {
if (error) throw error
}
)
console.log(`export done -> check src/constants/${CONFIG_FILE}`)
})
.catch((error) => {
console.log(error)
})