forked from bankless-academy/bankless-academy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
import-modules.js
136 lines (126 loc) · 3.95 KB
/
import-modules.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
/* 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 || ''
const IS_WHITELABEL = PROJECT_DIR !== ''
// TODO: update
const DEFAULT_NOTION_ID = '623e965e4f10456094d17aa94ec37105'
const POTION_API = 'https://potion.banklessacademy.com'
const MODULES_FILE = IS_WHITELABEL ? 'whitelabel_modules.ts' : 'modules.ts'
const args = process.argv
const NOTION_ID =
args[2] && args[2].length === 32
? args[2]
: process.env.DEFAULT_MODULE_DB_ID || DEFAULT_NOTION_ID
console.log('NOTION_ID', NOTION_ID)
const KEY_MATCHING = {
'Module name': 'name',
'Module image': 'moduleImageLink',
'Social image': 'socialImageLink',
Description: 'description',
// 'Parent Module': 'parentModule',
// Submodules: 'subModules',
}
// 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}module/${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
}
const modules = []
axios
.get(`${POTION_API}/table?id=${NOTION_ID}`)
.then((response) => {
if (IS_WHITELABEL) {
// create image directory dynamically in case it doesn't exist yet
if (!fs.existsSync(`public/${PROJECT_DIR}`)) {
fs.mkdirSync(`public/${PROJECT_DIR}`)
}
if (!fs.existsSync(`public/${PROJECT_DIR}module`)) {
fs.mkdirSync(`public/${PROJECT_DIR}module`)
}
}
response.data.map((notion) => {
// console.log(notion)
const module = Object.keys(KEY_MATCHING).reduce(
(obj, k) =>
Object.assign(obj, {
[KEY_MATCHING[k]]: notion.fields[k],
}),
{}
)
// console.log(notion.fields)
module.slug = slugify(module.name)
module.moduleId = notion.id.replace(/-/g, '')
if (module.moduleImageLink) {
module.moduleImageLink = get_img(
module.moduleImageLink,
module.slug,
''
)
}
if (module.socialImageLink) {
module.socialImageLink = get_img(
module.socialImageLink,
module.slug,
'-social'
)
}
module.parentModule = notion.fields['Parent module'][0] || null
module.subModules = notion.fields['Submodules']
// console.log(module)
modules.push(module)
})
console.log(modules)
const FILE_CONTENT = `import { ModuleType } from 'entities/module'
const MODULES: ModuleType[] = ${stringifyObject(modules, {
indent: ' ',
singleQuotes: true,
})}
export default MODULES
`
FileSystem.writeFile(
`src/constants/${MODULES_FILE}`,
FILE_CONTENT,
(error) => {
if (error) throw error
}
)
console.log(`export done -> check src/constants/${MODULES_FILE}`)
})
.catch((error) => {
console.log(error)
})