@bookshop/hugo-engine 3.14.0 → 3.15.0-alpha.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,219 +0,0 @@
1
- // Copyright 2015 The Hugo Authors. All rights reserved.
2
- //
3
- // Licensed under the Apache License, Version 2.0 (the "License");
4
- // you may not use this file except in compliance with the License.
5
- // You may obtain a copy of the License at
6
- // http://www.apache.org/licenses/LICENSE-2.0
7
- //
8
- // Unless required by applicable law or agreed to in writing, software
9
- // distributed under the License is distributed on an "AS IS" BASIS,
10
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
- // See the License for the specific language governing permissions and
12
- // limitations under the License.
13
-
14
- package main
15
-
16
- import (
17
- "encoding/json"
18
- "fmt"
19
- "io/ioutil"
20
- "log"
21
- "path/filepath"
22
- "syscall/js"
23
-
24
- "github.com/fsnotify/fsnotify"
25
- "github.com/gohugoio/hugo/config"
26
- "github.com/gohugoio/hugo/config/allconfig"
27
- "github.com/gohugoio/hugo/deps"
28
- "github.com/gohugoio/hugo/hugofs"
29
- "github.com/gohugoio/hugo/hugolib"
30
- "github.com/spf13/afero"
31
- )
32
-
33
- type bookshopSiteBuilder struct {
34
- Cfg *allconfig.Configs
35
- Afs afero.Fs
36
- Fs *hugofs.Fs
37
- Sites *hugolib.HugoSites
38
- changedFiles []string
39
- }
40
-
41
- // Load whatever source config file we have previously written to disk
42
- func (builder *bookshopSiteBuilder) LoadConfig() error {
43
- cfg, err := allconfig.LoadConfig(allconfig.ConfigSourceDescriptor{
44
- Fs: builder.Afs,
45
- Flags: config.New(),
46
- Filename: "config.toml",
47
- })
48
- if err != nil {
49
- return err
50
- }
51
-
52
- // Bookshop runs "rebuilds" rather than fresh builds to improve re-render speed
53
- cfg.Base.WorkingDir = ""
54
- cfg.Base.Internal.Running = true
55
- cfg.Base.Internal.Watch = true
56
- builder.Cfg = cfg
57
- builder.Fs = hugofs.NewFrom(builder.Afs, cfg.GetFirstLanguageConfig().BaseConfig())
58
-
59
- return nil
60
- }
61
-
62
- // Create our Hugo sites
63
- // TODO: I don't know how the lang aspects of Hugo
64
- // will interact with Hugo.
65
- func (builder *bookshopSiteBuilder) CreateSites() error {
66
- if err := builder.LoadConfig(); err != nil {
67
- fmt.Println(fmt.Sprintf("failed to load config: %s", err))
68
- return fmt.Errorf("failed to load config: %w", err)
69
- }
70
-
71
- builder.Fs.PublishDir = hugofs.NewCreateCountingFs(builder.Fs.PublishDir)
72
-
73
- sites, err := hugolib.NewHugoSites(deps.DepsCfg{
74
- Fs: builder.Fs,
75
- Configs: builder.Cfg,
76
- })
77
- if err != nil {
78
- fmt.Println(fmt.Sprintf("failed to create sites: %s", err))
79
- return fmt.Errorf("failed to create sites: %w", err)
80
- }
81
- builder.Sites = sites
82
-
83
- return nil
84
- }
85
-
86
- // Runs a standard Hugo build into our in-memory "public" directory
87
- func (builder *bookshopSiteBuilder) build(cfg hugolib.BuildCfg) error {
88
- if builder.Sites == nil {
89
- builder.CreateSites()
90
- }
91
-
92
- err := builder.Sites.Build(cfg, builder.changeEvents()...)
93
-
94
- if err == nil {
95
- logErrorCount := builder.Sites.NumLogErrors()
96
- if logErrorCount > 0 {
97
- err = fmt.Errorf("logged %d errors", logErrorCount)
98
- }
99
- }
100
- if err != nil {
101
- fmt.Errorf("Build failed: %s", err)
102
- fmt.Println(fmt.Sprintf("Build failed: %s", err))
103
- return err
104
- }
105
- return nil
106
- }
107
-
108
- // Writes content to the given file path in the in-memory filesystem
109
- func (builder *bookshopSiteBuilder) writeFile(filename, content string) {
110
- if err := afero.WriteFile(builder.Afs, filepath.FromSlash(filename), []byte(content), 0755); err != nil {
111
- fmt.Println(fmt.Sprintf("Failed to write file: %s", err))
112
- }
113
-
114
- builder.changedFiles = append(builder.changedFiles, filename)
115
- }
116
-
117
- // Reads content from the given file path in the in-memory filesystem
118
- func (builder *bookshopSiteBuilder) readFile(filename string) string {
119
- b, err := afero.ReadFile(builder.Afs, filepath.Clean(filename))
120
- if err != nil {
121
- fmt.Println(fmt.Sprintf("Failed to read file: %s", err))
122
- }
123
- return string(b)
124
- }
125
-
126
- // Helper method to list out any files in our virtual fs
127
- func (builder *bookshopSiteBuilder) debugDir(dir string) {
128
- files, err := afero.ReadDir(builder.Afs, dir)
129
- if err == nil {
130
- for _, file := range files {
131
- fmt.Println(fmt.Sprintf("%s > %+v", dir, file.Name()))
132
- }
133
- } else {
134
- fmt.Println(fmt.Sprintf("Error debugging directory: %+v", err))
135
- }
136
- }
137
-
138
- func (builder *bookshopSiteBuilder) changeEvents() []fsnotify.Event {
139
- var events []fsnotify.Event
140
-
141
- for _, v := range builder.changedFiles {
142
- events = append(events, fsnotify.Event{
143
- Name: v,
144
- Op: fsnotify.Write,
145
- })
146
- }
147
-
148
- return events
149
- }
150
-
151
- var builder bookshopSiteBuilder
152
- var build_config hugolib.BuildCfg
153
-
154
- func main() {
155
- afs := afero.NewMemMapFs()
156
- builder = bookshopSiteBuilder{Afs: afs}
157
- builder.LoadConfig()
158
- build_config = hugolib.BuildCfg{
159
- NoBuildLock: true,
160
- }
161
-
162
- log.SetOutput(ioutil.Discard)
163
-
164
- c := make(chan struct{}, 0)
165
- js.Global().Set("initHugoConfig", js.FuncOf(initHugoConfig))
166
- js.Global().Set("writeHugoFiles", js.FuncOf(writeHugoFiles))
167
- js.Global().Set("readHugoFiles", js.FuncOf(readHugoFiles))
168
- js.Global().Set("buildHugo", js.FuncOf(buildHugo))
169
- <-c
170
- }
171
-
172
- func initHugoConfig(this js.Value, args []js.Value) interface{} {
173
- if err := builder.LoadConfig(); err != nil {
174
- fmt.Println(fmt.Sprintf("failed to load config: %s", err))
175
- return fmt.Errorf("failed to load config: %w", err)
176
- }
177
- if err := builder.CreateSites(); err != nil {
178
- fmt.Println(fmt.Sprintf("failed to create sites: %s", err))
179
- return fmt.Errorf("failed to create sites: %w", err)
180
- }
181
- return nil
182
- }
183
-
184
- func writeHugoFiles(this js.Value, args []js.Value) interface{} {
185
- var writeFiles map[string]string
186
- err := json.Unmarshal([]byte(args[0].String()), &writeFiles)
187
- if err != nil {
188
- fmt.Println(fmt.Sprintf("Bad json: %+v", err))
189
- return nil
190
- }
191
-
192
- for file_name, file_contents := range writeFiles {
193
- builder.writeFile(file_name, file_contents)
194
- }
195
- return nil
196
- }
197
-
198
- func readHugoFiles(this js.Value, args []js.Value) interface{} {
199
- var readFiles []string
200
- err := json.Unmarshal([]byte(args[0].String()), &readFiles)
201
- if err != nil {
202
- fmt.Println(fmt.Sprintf("Bad json: %+v", err))
203
- return nil
204
- }
205
-
206
- fileContents := make(map[string]interface{})
207
- for _, file_name := range readFiles {
208
- fileContents[file_name] = builder.readFile(file_name)
209
- }
210
-
211
- return js.ValueOf(fileContents)
212
- }
213
-
214
- func buildHugo(this js.Value, args []js.Value) interface{} {
215
- if err := builder.build(build_config); err != nil {
216
- return fmt.Sprintf("Build error: %+v", err)
217
- }
218
- return nil
219
- }