This repository has been archived by the owner on Jul 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 117
/
Copy pathrendering-service.js
125 lines (107 loc) · 4.18 KB
/
rendering-service.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
const path = require('path')
const fs = require('fs-extra')
const glob = require('glob')
const config = require('./rs-config')
const StoryblokClient = require('storyblok-js-client')
const version = Date.now()
const RenderingServiceHelper = {
init: () => {
RenderingServiceHelper.generateRouter()
RenderingServiceHelper.generateLiquids()
RenderingServiceHelper.generateAssets()
},
generateRouter: () => {
let routes = []
glob(`${__dirname}/dist/**/*.html`, { ignore: '**/dist/*.html' },
(err, files) => {
files.forEach((file) => {
let requestRoute = path.dirname(file).replace(config.contentInputFolder, '')
let output = RenderingServiceHelper.generateLiquidOutputPaths(file)
let pathToLiquid = RenderingServiceHelper.replaceAll(output.folder.replace(config.contentOutputFolder, ''), '-', '_') + '/' + RenderingServiceHelper.replaceAll(output.originalFilename, '-', '_')
routes.push(`
{% when '/${requestRoute}' %}
{% include 'gnd/${pathToLiquid}' %}`)
})
let routerTemplate = `{% comment %} THIS FILE IS AUTOGENERATED - DO NOT CHANGE! {% endcomment %}
{%spaceless%}
{% case request.path %}${routes.join('')}
{% else %}
{% flow show_404 %}
{% endcase %}
{%endspaceless%}`
fs.ensureDirSync(path.dirname(config.routerFile))
fs.writeFile(config.routerFile, routerTemplate, (err) => {
if (err) throw err
})
}
)
},
generateLiquids: () => {
let Storyblok = new StoryblokClient({
token: process.env.THEME_TOKEN
})
let themeEnv = process.env.THEME_ENV == 'live' ? 'live' : 'dev'
glob(`${__dirname}/dist/**/*.html`, { ignore: '**/dist/*.html' },
(err, files) => {
files.forEach((input) => {
let output = RenderingServiceHelper.generateLiquidOutputPaths(input)
fs.ensureDirSync(output.folder)
fs.readFile(input, { encoding: 'utf8' }, async (err, data) => {
data = RenderingServiceHelper.resolveAssets(data)
data = RenderingServiceHelper.addVersionParam(data)
let renderingServiceFolder = output.folder.split('deploy/views/')[1]
let uploadPath = renderingServiceFolder.replace('-', '_') + '/' + output.filename
console.log(uploadPath)
await Storyblok.put('spaces/' + config.spaceId + '/templates/create_or_update?token=' + process.env.THEME_TOKEN, {
template: {
path: uploadPath,
tmpl_type: 'text',
body: data,
env: themeEnv
}
}).catch((e) => {
console.log(e.response.data)
})
})
})
}
)
},
generateAssets: () => {
glob(`${__dirname}/dist/_nuxt/*.js`, (err, files) => {
files.forEach((file) => {
let outputFolder = path.dirname(file).replace(config.assetInputFolder, config.assetOutputFolder)
let outputFilename = path.basename(file)
fs.ensureDirSync(outputFolder)
fs.readFile(file, { encoding: 'utf8' }, (err, data) => {
data = RenderingServiceHelper.resolveAssets(data)
data = RenderingServiceHelper.addVersionParam(data)
fs.writeFile(path.join(outputFolder, outputFilename), data, (err) => {
if (err) throw err
})
})
})
})
},
generateLiquidOutputPaths: (file) => {
let absoluteOutputFilepath = path.dirname(file).replace(config.contentInputFolder, config.contentOutputFolder)
let originalFilename = path.basename(absoluteOutputFilepath)
let filename = '_' + RenderingServiceHelper.replaceAll(originalFilename, '-', '_') + '.liquid'
let folder = path.dirname(absoluteOutputFilepath)
return {
folder,
originalFilename,
filename
}
},
addVersionParam: (data) => {
return RenderingServiceHelper.replaceAll(data, `.js"`, `.js?v=${version}"`)
},
resolveAssets: (data) => {
return RenderingServiceHelper.replaceAll(data, '/_nuxt/', 'https://storyblok-docs.netlify.app/_nuxt/')
},
replaceAll(target, search, replacement) {
return target.replace(new RegExp(search, 'g'), replacement);
}
}
RenderingServiceHelper.init()