Skip to content

Commit 0e3e80e

Browse files
committed
make pages deployable
1 parent 42bd707 commit 0e3e80e

File tree

4 files changed

+140
-37
lines changed

4 files changed

+140
-37
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
以下の書籍が入荷しました
2+
3+
4+
5+
6+
7+
* [Language Implementation Patterns: Create Your Own Domain-Specific and General Programming Languages](https://k9bookshelf.com/products/language-implementation-patterns-create-your-own-domain-specific-and-general-programming-languages)
8+
* [Node.js Design Patterns - Third edition: Design and implement production-grade Node.js applications using proven patterns and techniques](https://k9bookshelf.com/products/node-js-design-patterns-third-edition-design-and-implement-production-grade-node-js-applications-using-proven-patterns-and-techniques)
9+
* [The Maker's Guide to the Zombie Apocalypse: Defend Your Base with Simple Circuits, Arduino, and Raspberry Pi](https://k9bookshelf.com/products/the-makers-guide-to-the-zombie-apocalypse-defend-your-base-with-simple-circuits-arduino-and-raspberry-pi)
10+
* [Programming Webassembly with Rust: Unified Development for Web, Mobile, and Embedded Applications (1ST ed.)](https://k9bookshelf.com/products/programming-webassembly-with-rust)
11+

contents/blogs/news/新着入荷.md

-6
This file was deleted.

contents/pages/contact.md

-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
11
k9bookshelfに関するお問い合わせはこちらからお願いいたします。
2-

syncdata/deploy.go

+129-30
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@ import (
44
"fmt"
55
"io/ioutil"
66
"k9bookshelf/generated"
7+
"os"
78
"path"
89
"path/filepath"
910
"sync"
1011

12+
shopify "github.com/bold-commerce/go-shopify"
1113
"github.com/gomarkdown/markdown"
1214
"github.com/vbauerster/mpb"
1315
"github.com/vbauerster/mpb/decor"
@@ -61,51 +63,148 @@ func deployProducts(contents []Content, bar *mpb.Bar) error {
6163
return err
6264
}
6365

64-
// Deploy uploads contents to store
65-
func Deploy(input string) error {
66-
rawProducts, err := ioutil.ReadDir(path.Join(input, "products"))
67-
if err != nil {
68-
return err
66+
func deployPages(contents []Content, bar *mpb.Bar) error {
67+
var err error
68+
adminClient := establishRestClient()
69+
wg := sync.WaitGroup{}
70+
c := make(chan error)
71+
for _, content := range contents {
72+
wg.Add(1)
73+
74+
go func(handle, html string) {
75+
defer wg.Done()
76+
defer bar.Increment()
77+
78+
pages, err := adminClient.Page.List(nil)
79+
if err != nil {
80+
c <- err
81+
return
82+
}
83+
var page shopify.Page
84+
for _, p := range pages {
85+
if p.Handle == handle {
86+
page = p
87+
break
88+
}
89+
}
90+
91+
// NOTE: Because Page struct isn't tagged by `omitempty` and Metafields are initialized with nil,
92+
metafields := []shopify.Metafield{}
93+
if page.Metafields != nil {
94+
metafields = page.Metafields
95+
}
96+
_, err = adminClient.Page.Update(shopify.Page{
97+
ID: page.ID,
98+
Author: page.Author,
99+
Handle: handle,
100+
Title: page.Title,
101+
CreatedAt: page.CreatedAt,
102+
UpdatedAt: page.UpdatedAt,
103+
BodyHTML: html,
104+
TemplateSuffix: page.TemplateSuffix,
105+
PublishedAt: page.PublishedAt,
106+
ShopID: page.ShopID,
107+
Metafields: metafields,
108+
})
109+
110+
if err != nil {
111+
c <- err
112+
return
113+
}
114+
}(content.handle, content.html)
69115
}
70-
products := []Content{}
71-
for _, file := range rawProducts {
116+
go func() {
117+
wg.Wait()
118+
c <- nil
119+
}()
120+
121+
err = <-c
122+
return err
123+
}
124+
125+
func filesToContents(inputDir string, files []os.FileInfo) ([]Content, error) {
126+
contents := []Content{}
127+
for _, file := range files {
72128
filename := file.Name()
73129
handle := filename[0 : len(filename)-len(filepath.Ext(filename))]
74-
md, err := ioutil.ReadFile(path.Join(input, "products", filename))
130+
md, err := ioutil.ReadFile(path.Join(inputDir, filename))
75131
if err != nil {
76-
return err
132+
return nil, err
77133
}
78134
html := string(markdown.ToHTML(md, nil, nil))
79-
products = append(products, Content{
135+
contents = append(contents, Content{
80136
handle: handle,
81137
html: html,
82138
})
83139
}
140+
return contents, nil
141+
}
84142

85-
// pages, err := ioutil.ReadDir(path.Join(input, "pages"))
86-
// if err != nil {
87-
// return err
88-
// }
143+
type tmpIterable struct {
144+
f func(contents []Content, bar *mpb.Bar) error
145+
contents []Content
146+
}
89147

90-
wg := sync.WaitGroup{}
91-
p := mpb.New(mpb.WithWaitGroup(&wg))
92-
bar := p.AddBar(int64(len(products)),
93-
mpb.PrependDecorators(
94-
decor.Name(path.Join(input, "products")),
95-
decor.Percentage(decor.WCSyncSpace),
96-
),
97-
mpb.AppendDecorators(
98-
decor.OnComplete(
99-
decor.EwmaETA(decor.ET_STYLE_GO, 60), "done",
100-
),
101-
),
102-
)
148+
// Deploy uploads contents to store
149+
func Deploy(input string) error {
150+
rawProducts, err := ioutil.ReadDir(path.Join(input, "products"))
151+
if err != nil {
152+
return err
153+
}
154+
products, err := filesToContents(path.Join(input, "products"), rawProducts)
155+
if err != nil {
156+
return err
157+
}
103158

104-
err = deployProducts(products, bar)
159+
rawPages, err := ioutil.ReadDir(path.Join(input, "pages"))
105160
if err != nil {
106161
return err
107162
}
163+
pages, err := filesToContents(path.Join(input, "pages"), rawPages)
108164

109-
p.Wait()
110-
return nil
165+
wg := sync.WaitGroup{}
166+
p := mpb.New(mpb.WithWaitGroup(&wg))
167+
168+
c := make(chan error)
169+
for name, _f := range map[string]tmpIterable{
170+
"products": {
171+
f: func(contents []Content, bar *mpb.Bar) error {
172+
return deployProducts(products, bar)
173+
},
174+
contents: products,
175+
},
176+
"pages": {
177+
f: func(contents []Content, bar *mpb.Bar) error {
178+
return deployPages(pages, bar)
179+
},
180+
contents: pages,
181+
},
182+
} {
183+
wg.Add(1)
184+
bar := p.AddBar(int64(len(_f.contents)),
185+
mpb.PrependDecorators(
186+
decor.Name(path.Join(input, name)),
187+
decor.Percentage(decor.WCSyncSpace),
188+
),
189+
mpb.AppendDecorators(
190+
decor.OnComplete(
191+
decor.EwmaETA(decor.ET_STYLE_GO, 60), "done",
192+
),
193+
),
194+
)
195+
go func(f func(contents []Content, bar *mpb.Bar) error) {
196+
defer wg.Done()
197+
if err = f(pages, bar); err != nil {
198+
c <- err
199+
return
200+
}
201+
}(_f.f)
202+
}
203+
go func() {
204+
p.Wait()
205+
c <- nil
206+
}()
207+
208+
err = <-c
209+
return err
111210
}

0 commit comments

Comments
 (0)